minor edit for widget updating

This commit is contained in:
tibbi
2016-02-05 18:54:58 +01:00
parent 8928509e62
commit ccead6340b

View File

@ -12,7 +12,6 @@ import android.view.View;
import android.widget.RemoteViews; import android.widget.RemoteViews;
public class MyWidgetProvider extends AppWidgetProvider implements Calculator { public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
private static int[] widgetIds;
private static RemoteViews remoteViews; private static RemoteViews remoteViews;
private static CalculatorImpl calc; private static CalculatorImpl calc;
private static AppWidgetManager widgetManager; private static AppWidgetManager widgetManager;
@ -49,7 +48,8 @@ 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);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); updateWidget(context);
super.onUpdate(context, appWidgetManager, appWidgetIds);
} }
private void setupIntent(String action, int id) { private void setupIntent(String action, int id) {
@ -60,7 +60,6 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
private void initVariables(Context context) { private void initVariables(Context context) {
prefs = initPrefs(context); prefs = initPrefs(context);
final ComponentName component = new ComponentName(context, MyWidgetProvider.class);
final int defaultColor = context.getResources().getColor(R.color.dark_grey); final int defaultColor = context.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);
@ -71,12 +70,16 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
updateTextColors(newTextColor); updateTextColors(newTextColor);
widgetManager = AppWidgetManager.getInstance(context); widgetManager = AppWidgetManager.getInstance(context);
widgetIds = widgetManager.getAppWidgetIds(component);
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) {
final ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
AppWidgetManager.getInstance(context).updateAppWidget(thisWidget, remoteViews);
}
private SharedPreferences initPrefs(Context context) { private SharedPreferences initPrefs(Context context) {
return context.getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE); return context.getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
} }
@ -94,6 +97,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
cxt = context;
final String action = intent.getAction(); final String action = intent.getAction();
switch (action) { switch (action) {
case Constants.DECIMAL: case Constants.DECIMAL:
@ -125,8 +129,9 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
} }
private void myAction(String action, Context context) { private void myAction(String action, Context context) {
if (calc == null || remoteViews == null || widgetManager == null || widgetIds == null || prefs == null) if (calc == null || remoteViews == null || widgetManager == null || prefs == null) {
initVariables(context); initVariables(context);
}
switch (action) { switch (action) {
case Constants.DECIMAL: case Constants.DECIMAL:
@ -189,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);
widgetManager.updateAppWidget(widgetIds, remoteViews); updateWidget(cxt);
prefs.edit().putString(Constants.CALC_VALUE, value).apply(); prefs.edit().putString(Constants.CALC_VALUE, value).apply();
} }
@ -201,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);
widgetManager.updateAppWidget(widgetIds, remoteViews); updateWidget(cxt);
} }
@Override @Override