Reconfigurable count widget

pull/204/head
M66B 4 years ago
parent 5b4fcad9d3
commit 9188bcb84a

@ -76,6 +76,12 @@ public class ActivityWidget extends ActivityBase {
appWidgetId = extras.getInt( appWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ActivityWidget.this);
long account = prefs.getLong("widget." + appWidgetId + ".account", -1L);
boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true);
int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT);
int layout = prefs.getInt("widget." + appWidgetId + ".layout", 1 /* new */);
getSupportActionBar().setSubtitle(R.string.title_widget_title_count); getSupportActionBar().setSubtitle(R.string.title_widget_title_count);
setContentView(R.layout.activity_widget); setContentView(R.layout.activity_widget);
@ -154,7 +160,6 @@ public class ActivityWidget extends ActivityBase {
public void onClick(View view) { public void onClick(View view) {
EntityAccount account = (EntityAccount) spAccount.getSelectedItem(); EntityAccount account = (EntityAccount) spAccount.getSelectedItem();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ActivityWidget.this);
SharedPreferences.Editor editor = prefs.edit(); SharedPreferences.Editor editor = prefs.edit();
if (account != null && account.id > 0) if (account != null && account.id > 0)
editor.putString("widget." + appWidgetId + ".name", account.name); editor.putString("widget." + appWidgetId + ".name", account.name);
@ -178,10 +183,14 @@ public class ActivityWidget extends ActivityBase {
adapterAccount.setDropDownViewResource(R.layout.spinner_item1_dropdown); adapterAccount.setDropDownViewResource(R.layout.spinner_item1_dropdown);
spAccount.setAdapter(adapterAccount); spAccount.setAdapter(adapterAccount);
// Initialize
((TextView) inOld.findViewById(R.id.tvCount)).setText("12"); ((TextView) inOld.findViewById(R.id.tvCount)).setText("12");
((TextView) inNew.findViewById(R.id.tvCount)).setText("12"); ((TextView) inNew.findViewById(R.id.tvCount)).setText("12");
btnColor.setColor(Color.TRANSPARENT); cbSemiTransparent.setChecked(semi);
btnColor.setColor(background);
rbOld.setChecked(layout != 1);
rbNew.setChecked(layout == 1);
setBackground(); setBackground();
grpReady.setVisibility(View.GONE); grpReady.setVisibility(View.GONE);
@ -189,6 +198,8 @@ public class ActivityWidget extends ActivityBase {
setResult(RESULT_CANCELED, resultValue); setResult(RESULT_CANCELED, resultValue);
Bundle args = new Bundle();
new SimpleTask<List<EntityAccount>>() { new SimpleTask<List<EntityAccount>>() {
@Override @Override
protected List<EntityAccount> onExecute(Context context, Bundle args) { protected List<EntityAccount> onExecute(Context context, Bundle args) {
@ -210,6 +221,12 @@ public class ActivityWidget extends ActivityBase {
adapterAccount.addAll(accounts); adapterAccount.addAll(accounts);
for (int i = 0; i < accounts.size(); i++)
if (accounts.get(i).id.equals(account)) {
spAccount.setSelection(i);
break;
}
grpReady.setVisibility(View.VISIBLE); grpReady.setVisibility(View.VISIBLE);
pbWait.setVisibility(View.GONE); pbWait.setVisibility(View.GONE);
} }
@ -218,7 +235,7 @@ public class ActivityWidget extends ActivityBase {
protected void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getSupportFragmentManager(), ex); Log.unexpectedError(getSupportFragmentManager(), ex);
} }
}.execute(this, new Bundle(), "widget:accounts"); }.execute(this, args, "widget:accounts");
} }
private void setBackground() { private void setBackground() {

@ -27,6 +27,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Color; import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.View; import android.view.View;
import android.widget.RemoteViews; import android.widget.RemoteViews;
@ -62,6 +63,9 @@ public class Widget extends AppWidgetProvider {
int layout = prefs.getInt("widget." + appWidgetId + ".layout", 0); int layout = prefs.getInt("widget." + appWidgetId + ".layout", 0);
int version = prefs.getInt("widget." + appWidgetId + ".version", 0); int version = prefs.getInt("widget." + appWidgetId + ".version", 0);
if (version <= 1550)
semi = true; // Legacy
List<EntityFolder> folders = db.folder().getNotifyingFolders(account); List<EntityFolder> folders = db.folder().getNotifyingFolders(account);
if (folders == null) if (folders == null)
folders = new ArrayList<>(); folders = new ArrayList<>();
@ -126,8 +130,15 @@ public class Widget extends AppWidgetProvider {
} }
if (background == Color.TRANSPARENT) { if (background == Color.TRANSPARENT) {
if (!semi && version > 1550) if (semi)
views.setInt(R.id.widget, "setBackgroundResource", R.drawable.widget_background);
else
views.setInt(R.id.widget, "setBackgroundColor", background); views.setInt(R.id.widget, "setBackgroundColor", background);
int colorWidgetForeground = context.getResources().getColor(R.color.colorWidgetForeground);
views.setInt(R.id.ivMessage, "setColorFilter", colorWidgetForeground);
views.setTextColor(R.id.tvCount, colorWidgetForeground);
views.setTextColor(R.id.tvAccount, colorWidgetForeground);
} else { } else {
float lum = (float) ColorUtils.calculateLuminance(background); float lum = (float) ColorUtils.calculateLuminance(background);
@ -143,6 +154,9 @@ public class Widget extends AppWidgetProvider {
} }
} }
int pad = Helper.dp2pixels(context, layout == 0 ? 3 : 6);
views.setViewPadding(R.id.widget, pad, pad, pad, pad);
appWidgetManager.updateAppWidget(appWidgetId, views); appWidgetManager.updateAppWidget(appWidgetId, views);
} }
} }

@ -7,4 +7,5 @@
android:previewImage="@drawable/widget_preview_new" android:previewImage="@drawable/widget_preview_new"
android:resizeMode="horizontal|vertical" android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="0" android:updatePeriodMillis="0"
android:widgetCategory="home_screen" /> android:widgetCategory="home_screen"
android:widgetFeatures="reconfigurable" />

Loading…
Cancel
Save