Refactored widget unified

master
M66B 9 months ago
parent 1f28396079
commit 93214faea4

@ -548,6 +548,7 @@ dependencies {
def startup_version = "1.2.0" def startup_version = "1.2.0"
def annotation_version_experimental = "1.5.0" def annotation_version_experimental = "1.5.0"
def core_version = "1.17.0" def core_version = "1.17.0"
def core_remoteviews_version = "1.1.0"
def appcompat_version = "1.7.1" def appcompat_version = "1.7.1"
def emoji_version = "1.6.0" def emoji_version = "1.6.0"
def flatbuffers_version = "2.0.0" def flatbuffers_version = "2.0.0"
@ -625,6 +626,9 @@ dependencies {
// https://mvnrepository.com/artifact/androidx.core/core // https://mvnrepository.com/artifact/androidx.core/core
implementation "androidx.core:core:$core_version" implementation "androidx.core:core:$core_version"
// https://mvnrepository.com/artifact/androidx.core/core-remoteviews
implementation "androidx.core:core-remoteviews:$core_remoteviews_version"
// https://mvnrepository.com/artifact/androidx.appcompat/appcompat // https://mvnrepository.com/artifact/androidx.appcompat/appcompat
// https://mvnrepository.com/artifact/androidx.activity/activity // https://mvnrepository.com/artifact/androidx.activity/activity
// https://mvnrepository.com/artifact/androidx.fragment/fragment // https://mvnrepository.com/artifact/androidx.fragment/fragment

@ -34,179 +34,201 @@ import android.view.View;
import android.widget.RemoteViews; import android.widget.RemoteViews;
import androidx.core.graphics.ColorUtils; import androidx.core.graphics.ColorUtils;
import androidx.core.widget.RemoteViewsCompat;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import java.util.concurrent.ExecutorService;
public class WidgetUnified extends AppWidgetProvider { public class WidgetUnified extends AppWidgetProvider {
@Override private static final ExecutorService executor = Helper.getBackgroundExecutor(1, "widget");
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
private static RemoteViews getRemoteViews(Context context, int appWidgetId, WidgetUnifiedRemoteViewsFactory factory) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int colorWidgetForeground = context.getResources().getColor(R.color.colorWidgetForeground); int colorWidgetForeground = context.getResources().getColor(R.color.colorWidgetForeground);
int lightColorSeparator = context.getResources().getColor(R.color.lightColorSeparator); int lightColorSeparator = context.getResources().getColor(R.color.lightColorSeparator);
int darkColorSeparator = context.getResources().getColor(R.color.darkColorSeparator); int darkColorSeparator = context.getResources().getColor(R.color.darkColorSeparator);
for (int appWidgetId : appWidgetIds) { String name = prefs.getString("widget." + appWidgetId + ".name", null);
String name = prefs.getString("widget." + appWidgetId + ".name", null); long account = prefs.getLong("widget." + appWidgetId + ".account", -1L);
long account = prefs.getLong("widget." + appWidgetId + ".account", -1L); long folder = prefs.getLong("widget." + appWidgetId + ".folder", -1L);
long folder = prefs.getLong("widget." + appWidgetId + ".folder", -1L); String type = prefs.getString("widget." + appWidgetId + ".type", null);
String type = prefs.getString("widget." + appWidgetId + ".type", null); boolean daynight = prefs.getBoolean("widget." + appWidgetId + ".daynight", false);
boolean daynight = prefs.getBoolean("widget." + appWidgetId + ".daynight", false); boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true);
boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true); int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT);
int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT); boolean separators = prefs.getBoolean("widget." + appWidgetId + ".separators", true);
boolean separators = prefs.getBoolean("widget." + appWidgetId + ".separators", true); int font = prefs.getInt("widget." + appWidgetId + ".font", 0);
int font = prefs.getInt("widget." + appWidgetId + ".font", 0); int padding = prefs.getInt("widget." + appWidgetId + ".padding", 0);
int padding = prefs.getInt("widget." + appWidgetId + ".padding", 0); boolean caption = prefs.getBoolean("widget." + appWidgetId + ".caption", true);
boolean caption = prefs.getBoolean("widget." + appWidgetId + ".caption", true); boolean refresh = prefs.getBoolean("widget." + appWidgetId + ".refresh", false);
boolean refresh = prefs.getBoolean("widget." + appWidgetId + ".refresh", false); boolean compose = prefs.getBoolean("widget." + appWidgetId + ".compose", false);
boolean compose = prefs.getBoolean("widget." + appWidgetId + ".compose", false); boolean standalone = prefs.getBoolean("widget." + appWidgetId + ".standalone", false);
boolean standalone = prefs.getBoolean("widget." + appWidgetId + ".standalone", false); int version = prefs.getInt("widget." + appWidgetId + ".version", 0);
int version = prefs.getInt("widget." + appWidgetId + ".version", 0);
if (version <= 1550)
if (version <= 1550) semi = true; // Legacy
semi = true; // Legacy if (font == 0)
if (font == 0) font = 2; // Default medium
font = 2; // Default medium if (padding == 0)
if (padding == 0) padding = 2; // Default medium
padding = 2; // Default medium
Intent view = new Intent(context, ActivityView.class);
Intent view = new Intent(context, ActivityView.class); if (account < 0 && folder < 0 && type == null)
if (account < 0 && folder < 0 && type == null) view.setAction("unified");
view.setAction("unified"); else {
else { view.setAction("folder:" + folder);
view.setAction("folder:" + folder); view.putExtra("account", account);
view.putExtra("account", account); view.putExtra("type", type);
view.putExtra("type", type); }
} view.putExtra("standalone", standalone);
view.putExtra("standalone", standalone); view.putExtra("refresh", true);
view.putExtra("refresh", true); view.putExtra("version", version);
view.putExtra("version", version); view.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
view.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pi = PendingIntentCompat.getActivity(
PendingIntent pi = PendingIntentCompat.getActivity( context, appWidgetId, view, PendingIntent.FLAG_UPDATE_CURRENT);
context, appWidgetId, view, PendingIntent.FLAG_UPDATE_CURRENT);
Intent sync = new Intent(context, ServiceUI.class);
Intent sync = new Intent(context, ServiceUI.class); sync.setAction("widget:" + appWidgetId);
sync.setAction("widget:" + appWidgetId); sync.putExtra("account", account);
sync.putExtra("account", account); sync.putExtra("folder", folder);
sync.putExtra("folder", folder); PendingIntent piSync = PendingIntentCompat.getService(
PendingIntent piSync = PendingIntentCompat.getService( context, appWidgetId, sync, PendingIntent.FLAG_UPDATE_CURRENT);
context, appWidgetId, sync, PendingIntent.FLAG_UPDATE_CURRENT);
Intent edit = new Intent(context, ActivityCompose.class);
Intent edit = new Intent(context, ActivityCompose.class); edit.setAction("widget:" + appWidgetId);
edit.setAction("widget:" + appWidgetId); edit.putExtra("action", "new");
edit.putExtra("action", "new"); edit.putExtra("account", account);
edit.putExtra("account", account); edit.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
edit.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent piCompose = PendingIntentCompat.getActivity(
PendingIntent piCompose = PendingIntentCompat.getActivity( context, appWidgetId, edit, PendingIntent.FLAG_UPDATE_CURRENT);
context, appWidgetId, edit, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_unified);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_unified);
views.setTextViewTextSize(R.id.title, TypedValue.COMPLEX_UNIT_SP, getFontSizeSp(font));
views.setTextViewTextSize(R.id.title, TypedValue.COMPLEX_UNIT_SP, getFontSizeSp(font));
int px = getPaddingPx(padding, context);
int px = getPaddingPx(padding, context); views.setViewPadding(R.id.title, px, px, px, px);
views.setViewPadding(R.id.title, px, px, px, px);
if (caption) {
if (caption) { if (name == null)
if (name == null) views.setTextViewText(R.id.title, context.getString(R.string.title_folder_unified));
views.setTextViewText(R.id.title, context.getString(R.string.title_folder_unified)); else
else views.setTextViewText(R.id.title, name);
views.setTextViewText(R.id.title, name); } else
} else views.setTextViewText(R.id.title, null);
views.setTextViewText(R.id.title, null); views.setViewVisibility(R.id.title, caption || refresh || compose ? View.VISIBLE : View.GONE);
views.setViewVisibility(R.id.title, caption || refresh || compose ? View.VISIBLE : View.GONE); views.setViewVisibility(R.id.padding, caption || refresh || compose ? View.GONE : View.VISIBLE);
views.setViewVisibility(R.id.padding, caption || refresh || compose ? View.GONE : View.VISIBLE);
views.setOnClickPendingIntent(R.id.title, pi);
views.setOnClickPendingIntent(R.id.title, pi);
views.setViewVisibility(R.id.refresh, refresh ? View.VISIBLE : View.GONE);
views.setViewVisibility(R.id.refresh, refresh ? View.VISIBLE : View.GONE); views.setViewPadding(R.id.refresh, px, px, px, px);
views.setViewPadding(R.id.refresh, px, px, px, px); views.setOnClickPendingIntent(R.id.refresh, piSync);
views.setOnClickPendingIntent(R.id.refresh, piSync);
views.setViewVisibility(R.id.compose, compose ? View.VISIBLE : View.GONE);
views.setViewVisibility(R.id.compose, compose ? View.VISIBLE : View.GONE); views.setViewPadding(R.id.compose, px, px, px, px);
views.setViewPadding(R.id.compose, px, px, px, px); views.setOnClickPendingIntent(R.id.compose, piCompose);
views.setOnClickPendingIntent(R.id.compose, piCompose);
// https://developer.android.com/reference/kotlin/androidx/core/widget/RemoteViewsCompat.RemoteCollectionItems.Builder
Intent service = new Intent(context, WidgetUnifiedService.class); RemoteViewsCompat.RemoteCollectionItems.Builder builder =
service.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); new RemoteViewsCompat.RemoteCollectionItems.Builder()
service.setData(Uri.parse(service.toUri(Intent.URI_INTENT_SCHEME))); .setHasStableIds(factory.hasStableIds())
.setViewTypeCount(factory.getViewTypeCount());
views.setRemoteAdapter(R.id.lv, service); for (int i = 0; i < factory.getCount(); i++)
builder.addItem(factory.getItemId(i), factory.getViewAt(i));
Intent thread = new Intent(context, ActivityView.class);
thread.setPackage(BuildConfig.APPLICATION_ID); // https://developer.android.com/reference/kotlin/androidx/core/widget/RemoteViewsCompat#setRemoteAdapter(android.content.Context,android.widget.RemoteViews,kotlin.Int,kotlin.Int,androidx.core.widget.RemoteViewsCompat.RemoteCollectionItems)
thread.setAction("widget:" + appWidgetId); RemoteViewsCompat.setRemoteAdapter(context, views, appWidgetId, R.id.lv, builder.build());
thread.putExtra("widget_account", account);
thread.putExtra("widget_folder", folder); Intent thread = new Intent(context, ActivityView.class);
thread.putExtra("widget_type", type); thread.setPackage(BuildConfig.APPLICATION_ID);
thread.putExtra("filter_archive", !EntityFolder.ARCHIVE.equals(type)); thread.setAction("widget:" + appWidgetId);
thread.putExtra("standalone", standalone); thread.putExtra("widget_account", account);
thread.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); thread.putExtra("widget_folder", folder);
PendingIntent piItem = PendingIntentCompat.getActivity( thread.putExtra("widget_type", type);
context, ActivityView.PI_WIDGET, thread, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE); thread.putExtra("filter_archive", !EntityFolder.ARCHIVE.equals(type));
thread.putExtra("standalone", standalone);
views.setPendingIntentTemplate(R.id.lv, piItem); thread.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent piItem = PendingIntentCompat.getActivity(
boolean syncing = prefs.getBoolean("widget." + appWidgetId + ".syncing", false); context, ActivityView.PI_WIDGET, thread, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
views.setImageViewResource(R.id.refresh, syncing
? R.drawable.twotone_compare_arrows_24 views.setPendingIntentTemplate(R.id.lv, piItem);
: R.drawable.twotone_sync_24);
boolean syncing = prefs.getBoolean("widget." + appWidgetId + ".syncing", false);
// https://developer.android.com/guide/topics/ui/look-and-feel/darktheme views.setImageViewResource(R.id.refresh, syncing
if (!daynight && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { ? R.drawable.twotone_compare_arrows_24
views.setColorStateListAttr(R.id.background, "setBackgroundTintList", 0); : R.drawable.twotone_sync_24);
views.setColorStateListAttr(R.id.separator, "setBackgroundTintList", 0);
} // https://developer.android.com/guide/topics/ui/look-and-feel/darktheme
if (!daynight && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (daynight && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { views.setColorStateListAttr(R.id.background, "setBackgroundTintList", 0);
views.setInt(R.id.background, "setBackgroundColor", Color.WHITE); views.setColorStateListAttr(R.id.separator, "setBackgroundTintList", 0);
views.setColorStateListAttr(R.id.background, "setBackgroundTintList", android.R.attr.colorBackground); }
views.setColorStateListAttr(R.id.title, "setTextColor", android.R.attr.textColorPrimary);
views.setInt(R.id.separator, "setBackgroundColor", Color.WHITE);
views.setColorStateListAttr(R.id.separator, "setBackgroundTintList", android.R.attr.colorControlNormal);
views.setColorAttr(R.id.refresh, "setColorFilter", android.R.attr.textColorPrimary);
views.setColorAttr(R.id.compose, "setColorFilter", android.R.attr.textColorPrimary);
} else if (background == Color.TRANSPARENT) {
if (semi)
views.setInt(R.id.background, "setBackgroundResource", R.drawable.widget_background);
else
views.setInt(R.id.background, "setBackgroundColor", background);
views.setTextColor(R.id.title, colorWidgetForeground);
views.setInt(R.id.separator, "setBackgroundColor", lightColorSeparator);
views.setInt(R.id.refresh, "setColorFilter", colorWidgetForeground);
views.setInt(R.id.compose, "setColorFilter", colorWidgetForeground);
} else {
float lum = (float) ColorUtils.calculateLuminance(background);
if (semi)
background = ColorUtils.setAlphaComponent(background, 127);
if (daynight && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
views.setInt(R.id.background, "setBackgroundColor", Color.WHITE);
views.setColorStateListAttr(R.id.background, "setBackgroundTintList", android.R.attr.colorBackground);
views.setColorStateListAttr(R.id.title, "setTextColor", android.R.attr.textColorPrimary);
views.setInt(R.id.separator, "setBackgroundColor", Color.WHITE);
views.setColorStateListAttr(R.id.separator, "setBackgroundTintList", android.R.attr.colorControlNormal);
views.setColorAttr(R.id.refresh, "setColorFilter", android.R.attr.textColorPrimary);
views.setColorAttr(R.id.compose, "setColorFilter", android.R.attr.textColorPrimary);
} else if (background == Color.TRANSPARENT) {
if (semi)
views.setInt(R.id.background, "setBackgroundResource", R.drawable.widget_background);
else
views.setInt(R.id.background, "setBackgroundColor", background); views.setInt(R.id.background, "setBackgroundColor", background);
int fg = (lum > 0.7f ? Color.BLACK : colorWidgetForeground); views.setTextColor(R.id.title, colorWidgetForeground);
views.setTextColor(R.id.title, fg); views.setInt(R.id.separator, "setBackgroundColor", lightColorSeparator);
views.setInt(R.id.separator, "setBackgroundColor", views.setInt(R.id.refresh, "setColorFilter", colorWidgetForeground);
lum > 0.7f ? darkColorSeparator : lightColorSeparator); views.setInt(R.id.compose, "setColorFilter", colorWidgetForeground);
views.setInt(R.id.refresh, "setColorFilter", fg); } else {
views.setInt(R.id.compose, "setColorFilter", fg); float lum = (float) ColorUtils.calculateLuminance(background);
}
views.setViewVisibility(R.id.separator, caption && separators ? View.VISIBLE : View.GONE); if (semi)
background = ColorUtils.setAlphaComponent(background, 127);
int dp6 = Helper.dp2pixels(context, 6); views.setInt(R.id.background, "setBackgroundColor", background);
views.setViewPadding(R.id.content, dp6, 0, dp6, 0);
appWidgetManager.updateAppWidget(appWidgetId, views); int fg = (lum > 0.7f ? Color.BLACK : colorWidgetForeground);
views.setTextColor(R.id.title, fg);
views.setInt(R.id.separator, "setBackgroundColor",
lum > 0.7f ? darkColorSeparator : lightColorSeparator);
views.setInt(R.id.refresh, "setColorFilter", fg);
views.setInt(R.id.compose, "setColorFilter", fg);
}
views.setViewVisibility(R.id.separator, caption && separators ? View.VISIBLE : View.GONE);
int dp6 = Helper.dp2pixels(context, 6);
views.setViewPadding(R.id.content, dp6, 0, dp6, 0);
return views;
}
RunnableEx update = new RunnableEx("widget") { @Override
@Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
protected void delegate() { executor.submit(new RunnableEx("widget") {
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.lv); @Override
protected void delegate() {
for (int appWidgetId : appWidgetIds) {
Intent service = new Intent(context, WidgetUnifiedService.class);
service.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
service.setData(Uri.parse(service.toUri(Intent.URI_INTENT_SCHEME)));
WidgetUnifiedRemoteViewsFactory factory = new WidgetUnifiedRemoteViewsFactory(context.getApplicationContext(), service);
factory.onDataSetChanged();
ApplicationEx.getMainHandler().post(new RunnableEx("widget:" + appWidgetId) {
@Override
protected void delegate() {
RemoteViews views = getRemoteViews(context, appWidgetId, factory);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
});
} }
}; }
ApplicationEx.getMainHandler().removeCallbacks(update); });
ApplicationEx.getMainHandler().postDelayed(update, 1000L);
}
} }
@Override @Override
@ -278,7 +300,8 @@ public class WidgetUnified extends AppWidgetProvider {
try { try {
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetUnified.class)); int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetUnified.class));
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.lv); for (int appWidgetId : appWidgetIds)
init(context, appWidgetId);
EntityLog.log(context, "Updated widget data count=" + appWidgetIds.length); EntityLog.log(context, "Updated widget data count=" + appWidgetIds.length);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);

Loading…
Cancel
Save