Added option refresh button / messages widget

pull/205/head
M66B 3 years ago
parent f094ca4c41
commit 386fb3361d

@ -60,6 +60,7 @@ public class ActivityWidgetUnified extends ActivityBase {
private ViewButtonColor btnColor;
private Spinner spFontSize;
private Spinner spPadding;
private CheckBox cbRefresh;
private CheckBox cbCompose;
private Button btnSave;
private ContentLoadingProgressBar pbWait;
@ -92,6 +93,7 @@ public class ActivityWidgetUnified extends ActivityBase {
int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT);
int font = prefs.getInt("widget." + appWidgetId + ".font", 0);
int padding = prefs.getInt("widget." + appWidgetId + ".padding", 0);
boolean refresh = prefs.getBoolean("widget." + appWidgetId + ".refresh", false);
boolean compose = prefs.getBoolean("widget." + appWidgetId + ".compose", false);
getSupportActionBar().setSubtitle(R.string.title_widget_title_list);
@ -105,6 +107,7 @@ public class ActivityWidgetUnified extends ActivityBase {
btnColor = findViewById(R.id.btnColor);
spFontSize = findViewById(R.id.spFontSize);
spPadding = findViewById(R.id.spPadding);
cbRefresh = findViewById(R.id.cbRefresh);
cbCompose = findViewById(R.id.cbCompose);
btnSave = findViewById(R.id.btnSave);
pbWait = findViewById(R.id.pbWait);
@ -173,6 +176,7 @@ public class ActivityWidgetUnified extends ActivityBase {
editor.putInt("widget." + appWidgetId + ".background", btnColor.getColor());
editor.putInt("widget." + appWidgetId + ".font", tinyOut(font));
editor.putInt("widget." + appWidgetId + ".padding", tinyOut(padding));
editor.putBoolean("widget." + appWidgetId + ".refresh", cbRefresh.isChecked());
editor.putBoolean("widget." + appWidgetId + ".compose", cbCompose.isChecked());
editor.putInt("widget." + appWidgetId + ".version", BuildConfig.VERSION_CODE);
@ -291,6 +295,7 @@ public class ActivityWidgetUnified extends ActivityBase {
btnColor.setColor(background);
spFontSize.setSelection(tinyIn(font));
spPadding.setSelection(tinyIn(padding));
cbRefresh.setChecked(refresh);
cbCompose.setChecked(compose);
grpReady.setVisibility(View.GONE);

@ -25,6 +25,8 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.core.app.RemoteInput;
@ -35,6 +37,7 @@ import org.json.JSONException;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
@ -58,6 +61,8 @@ public class ServiceUI extends IntentService {
static final int PI_IGNORED = 11;
static final int PI_THREAD = 12;
private static final long WIDGET_SYNC_DURATION = 1500L;
public ServiceUI() {
this(ServiceUI.class.getName());
}
@ -158,7 +163,11 @@ public class ServiceUI extends IntentService {
break;
case "sync":
onSync(id);
onSync(id, -1L);
break;
case "widget":
onWidget(intent, (int) id);
break;
case "exists":
@ -456,14 +465,18 @@ public class ServiceUI extends IntentService {
}
}
private void onSync(long aid) {
private void onSync(long aid, long fid) {
DB db = DB.getInstance(this);
try {
db.beginTransaction();
List<EntityAccount> accounts = db.account().getPollAccounts(aid < 0 ? null : aid);
for (EntityAccount account : accounts) {
List<EntityFolder> folders = db.folder().getSynchronizingFolders(account.id);
List<EntityFolder> folders;
if (fid < 0)
folders = db.folder().getSynchronizingFolders(account.id);
else
folders = Arrays.asList(db.folder().getFolder(fid));
if (folders.size() > 0)
Collections.sort(folders, folders.get(0).getComparator(this));
for (EntityFolder folder : folders)
@ -476,6 +489,29 @@ public class ServiceUI extends IntentService {
}
}
private void onWidget(Intent intent, int appWidgetId) {
long aid = intent.getLongExtra("account", -1L);
long fid = intent.getLongExtra("folder", -1L);
onSync(aid, fid);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String key = "widget." + appWidgetId + ".sync";
prefs.edit().putLong(key, new Date().getTime() + WIDGET_SYNC_DURATION).apply();
WidgetUnified.init(this, appWidgetId);
ApplicationEx.getMainHandler().postDelayed(new Runnable() {
@Override
public void run() {
try {
prefs.edit().remove(key).apply();
WidgetUnified.init(ServiceUI.this, appWidgetId);
} catch (Throwable ex) {
Log.e(ex);
}
}
}, WIDGET_SYNC_DURATION);
}
static void sync(Context context, Long account) {
try {
Intent sync = new Intent(context, ServiceUI.class)

@ -35,6 +35,8 @@ import android.widget.RemoteViews;
import androidx.core.graphics.ColorUtils;
import androidx.preference.PreferenceManager;
import java.util.Date;
public class WidgetUnified extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
@ -49,6 +51,7 @@ public class WidgetUnified extends AppWidgetProvider {
int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT);
int font = prefs.getInt("widget." + appWidgetId + ".font", 0);
int padding = prefs.getInt("widget." + appWidgetId + ".padding", 0);
boolean refresh = prefs.getBoolean("widget." + appWidgetId + ".refresh", false);
boolean compose = prefs.getBoolean("widget." + appWidgetId + ".compose", false);
int version = prefs.getInt("widget." + appWidgetId + ".version", 0);
@ -69,6 +72,13 @@ public class WidgetUnified extends AppWidgetProvider {
PendingIntent pi = PendingIntentCompat.getActivity(
context, appWidgetId, view, PendingIntent.FLAG_UPDATE_CURRENT);
Intent sync = new Intent(context, ServiceUI.class);
sync.setAction("widget:" + appWidgetId);
sync.putExtra("account", account);
sync.putExtra("folder", folder);
PendingIntent piSync = PendingIntentCompat.getService(
context, appWidgetId, sync, PendingIntent.FLAG_UPDATE_CURRENT);
Intent edit = new Intent(context, ActivityCompose.class);
edit.setAction("widget:" + appWidgetId);
edit.putExtra("action", "new");
@ -91,6 +101,15 @@ public class WidgetUnified extends AppWidgetProvider {
views.setOnClickPendingIntent(R.id.title, pi);
views.setViewVisibility(R.id.refresh, refresh ? View.VISIBLE : View.GONE);
views.setViewPadding(R.id.refresh, px, px, px, px);
views.setOnClickPendingIntent(R.id.refresh, piSync);
long refreshing = prefs.getLong("widget." + appWidgetId + ".sync", 0L);
views.setImageViewResource(R.id.refresh,
refreshing < new Date().getTime()
? R.drawable.twotone_sync_24 : R.drawable.twotone_compare_arrows_24);
views.setViewVisibility(R.id.compose, compose ? View.VISIBLE : View.GONE);
views.setViewPadding(R.id.compose, px, px, px, px);
views.setOnClickPendingIntent(R.id.compose, piCompose);

@ -121,6 +121,16 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvPadding" />
<CheckBox
android:id="@+id/cbRefresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:checked="true"
android:text="@string/title_widget_refresh"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/spPadding" />
<CheckBox
android:id="@+id/cbCompose"
android:layout_width="wrap_content"
@ -129,7 +139,7 @@
android:checked="true"
android:text="@string/title_widget_compose"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/spPadding" />
app:layout_constraintTop_toBottomOf="@id/cbRefresh" />
<Button
android:id="@+id/btnSave"

@ -26,6 +26,16 @@
android:textColor="@color/colorWidgetForeground"
android:textStyle="bold" />
<ImageView
android:id="@+id/refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:contentDescription="@string/tile_synchronize"
android:padding="6dp"
android:src="@drawable/twotone_sync_24"
app:tint="@color/colorWidgetForeground" />
<ImageView
android:id="@+id/compose"
android:layout_width="wrap_content"

@ -1696,6 +1696,7 @@
<string name="title_widget_font_size">Text size</string>
<string name="title_widget_padding">Padding size</string>
<string name="title_widget_semi_transparent">Semi transparent background</string>
<string name="title_widget_refresh">Show refresh messages button</string>
<string name="title_widget_compose">Show compose new message button</string>
<string name="title_widget_background">Background color</string>

Loading…
Cancel
Save