one more change to widget updating

This commit is contained in:
tibbi 2016-02-05 19:03:47 +01:00
parent ccead6340b
commit a23391ff56

View file

@ -21,8 +21,8 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
@Override @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
initVariables(context);
cxt = context; cxt = context;
initVariables();
intent = new Intent(context, MyWidgetProvider.class); intent = new Intent(context, MyWidgetProvider.class);
setupIntent(Constants.DECIMAL, R.id.btn_decimal); setupIntent(Constants.DECIMAL, R.id.btn_decimal);
@ -48,7 +48,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
setupIntent(Constants.CLEAR, R.id.btn_clear); setupIntent(Constants.CLEAR, R.id.btn_clear);
setupIntent(Constants.RESET, R.id.btn_reset); setupIntent(Constants.RESET, R.id.btn_reset);
updateWidget(context); updateWidget();
super.onUpdate(context, appWidgetManager, appWidgetIds); super.onUpdate(context, appWidgetManager, appWidgetIds);
} }
@ -58,26 +58,26 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
remoteViews.setOnClickPendingIntent(id, pendingIntent); remoteViews.setOnClickPendingIntent(id, pendingIntent);
} }
private void initVariables(Context context) { private void initVariables() {
prefs = initPrefs(context); prefs = initPrefs(cxt);
final int defaultColor = context.getResources().getColor(R.color.dark_grey); final int defaultColor = cxt.getResources().getColor(R.color.dark_grey);
final int newBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, defaultColor); final int newBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, defaultColor);
final int newTextColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, Color.WHITE); final int newTextColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, Color.WHITE);
remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main); remoteViews = new RemoteViews(cxt.getPackageName(), R.layout.activity_main);
remoteViews.setViewVisibility(R.id.btn_reset, View.VISIBLE); remoteViews.setViewVisibility(R.id.btn_reset, View.VISIBLE);
remoteViews.setInt(R.id.calculator_holder, "setBackgroundColor", newBgColor); remoteViews.setInt(R.id.calculator_holder, "setBackgroundColor", newBgColor);
updateTextColors(newTextColor); updateTextColors(newTextColor);
widgetManager = AppWidgetManager.getInstance(context); widgetManager = AppWidgetManager.getInstance(cxt);
final String displayValue = prefs.getString(Constants.CALC_VALUE, "0"); final String displayValue = prefs.getString(Constants.CALC_VALUE, "0");
calc = new CalculatorImpl(this, displayValue); calc = new CalculatorImpl(this, displayValue);
} }
private void updateWidget(Context context) { private void updateWidget() {
final ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class); final ComponentName thisWidget = new ComponentName(cxt, MyWidgetProvider.class);
AppWidgetManager.getInstance(context).updateAppWidget(thisWidget, remoteViews); AppWidgetManager.getInstance(cxt).updateAppWidget(thisWidget, remoteViews);
} }
private SharedPreferences initPrefs(Context context) { private SharedPreferences initPrefs(Context context) {
@ -121,16 +121,16 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
case Constants.MODULO: case Constants.MODULO:
case Constants.POWER: case Constants.POWER:
case Constants.ROOT: case Constants.ROOT:
myAction(action, context); myAction(action);
break; break;
default: default:
super.onReceive(context, intent); super.onReceive(context, intent);
} }
} }
private void myAction(String action, Context context) { private void myAction(String action) {
if (calc == null || remoteViews == null || widgetManager == null || prefs == null) { if (calc == null || remoteViews == null || widgetManager == null || prefs == null) {
initVariables(context); initVariables();
} }
switch (action) { switch (action) {
@ -174,7 +174,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
calc.handleClear(); calc.handleClear();
break; break;
case Constants.RESET: case Constants.RESET:
resetSavedValue(context); resetSavedValue(cxt);
calc.handleReset(); calc.handleReset();
break; break;
case Constants.PLUS: case Constants.PLUS:
@ -194,7 +194,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
@Override @Override
public void setValue(String value) { public void setValue(String value) {
remoteViews.setTextViewText(R.id.result, value); remoteViews.setTextViewText(R.id.result, value);
updateWidget(cxt); updateWidget();
prefs.edit().putString(Constants.CALC_VALUE, value).apply(); prefs.edit().putString(Constants.CALC_VALUE, value).apply();
} }
@ -206,7 +206,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
@Override @Override
public void setFormula(String value) { public void setFormula(String value) {
remoteViews.setTextViewText(R.id.formula, value); remoteViews.setTextViewText(R.id.formula, value);
updateWidget(cxt); updateWidget();
} }
@Override @Override