From f8425426fad43f67fca2ddbd55692e1baeb6710c Mon Sep 17 00:00:00 2001 From: thuryn Date: Fri, 19 Jan 2018 23:07:59 +0100 Subject: [PATCH] Widget breaks #7 - solves the problem with update of widget when the screen is rotated --- app/build.gradle | 4 +- .../widget/AbstractWidgetProvider.java | 39 ++++++++++++------- .../widget/ExtLocationWidgetProvider.java | 9 ++++- .../widget/ExtLocationWidgetService.java | 4 ++ .../widget/LessWidgetProvider.java | 3 +- .../widget/LessWidgetService.java | 4 ++ .../widget/MoreWidgetProvider.java | 3 +- .../widget/MoreWidgetService.java | 4 ++ 8 files changed, 48 insertions(+), 22 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 71fde1a56..94ec560cb 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,8 +6,8 @@ android { applicationId "org.thosp.yourlocalweather" minSdkVersion 14 targetSdkVersion 25 - versionCode 10 - versionName "1.7.1" + versionCode 11 + versionName "1.7.2" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true } diff --git a/app/src/main/java/org/thosp/yourlocalweather/widget/AbstractWidgetProvider.java b/app/src/main/java/org/thosp/yourlocalweather/widget/AbstractWidgetProvider.java index 974e1773d..4a6b079e7 100644 --- a/app/src/main/java/org/thosp/yourlocalweather/widget/AbstractWidgetProvider.java +++ b/app/src/main/java/org/thosp/yourlocalweather/widget/AbstractWidgetProvider.java @@ -31,6 +31,7 @@ public abstract class AbstractWidgetProvider extends AppWidgetProvider { @Override public void onEnabled(Context context) { + appendLog(context, TAG, "onEnabled:start"); super.onEnabled(context); if (PermissionUtil.areAllPermissionsGranted(context)) { Toast.makeText(context, @@ -41,6 +42,7 @@ public void onEnabled(Context context) { new AppWidgetProviderAlarm(context, getWidgetClass()); appWidgetProviderAlarm.cancelAlarm(); appWidgetProviderAlarm.setAlarm(); + appendLog(context, TAG, "onEnabled:end"); } @@ -96,28 +98,26 @@ public void onReceive(Context context, Intent intent) { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + appendLog(context, TAG, "onUpdate:start"); super.onUpdate(context, appWidgetManager, appWidgetIds); for (int appWidgetId : appWidgetIds) { RemoteViews remoteViews = new RemoteViews(context.getPackageName(), getWidgetLayout()); - setWidgetTheme(context, remoteViews); + if (ExtLocationWidgetProvider.class.equals(getWidgetClass())) { + ExtLocationWidgetProvider.setWidgetTheme(context, remoteViews); + } else if (MoreWidgetProvider.class.equals(getWidgetClass())) { + MoreWidgetProvider.setWidgetTheme(context, remoteViews); + } else if (LessWidgetProvider.class.equals(getWidgetClass())) { + LessWidgetProvider.setWidgetTheme(context, remoteViews); + } + setWidgetIntents(context, remoteViews, getWidgetClass()); preLoadWeather(context, remoteViews); - Intent intentRefreshService = new Intent(context, getWidgetClass()); - intentRefreshService.setAction(Constants.ACTION_FORCED_APPWIDGET_UPDATE); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, - intentRefreshService, 0); - remoteViews.setOnClickPendingIntent(R.id.widget_button_refresh, pendingIntent); - - Intent intentStartActivity = new Intent(context, MainActivity.class); - PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, - intentStartActivity, 0); - remoteViews.setOnClickPendingIntent(R.id.widget_root, pendingIntent2); - appWidgetManager.updateAppWidget(appWidgetId, remoteViews); } + appendLog(context, TAG, "onUpdate:end"); } @Override @@ -145,9 +145,20 @@ private void updateWather(Context context) { } } - protected abstract void preLoadWeather(Context context, RemoteViews remoteViews); + public static void setWidgetIntents(Context context, RemoteViews remoteViews, Class widgetClass) { + Intent intentRefreshService = new Intent(context, widgetClass); + intentRefreshService.setAction(Constants.ACTION_FORCED_APPWIDGET_UPDATE); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, + intentRefreshService, 0); + remoteViews.setOnClickPendingIntent(R.id.widget_button_refresh, pendingIntent); + + Intent intentStartActivity = new Intent(context, MainActivity.class); + PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, + intentStartActivity, 0); + remoteViews.setOnClickPendingIntent(R.id.widget_root, pendingIntent2); + } - protected abstract void setWidgetTheme(Context context, RemoteViews remoteViews); + protected abstract void preLoadWeather(Context context, RemoteViews remoteViews); protected abstract Class getWidgetClass(); diff --git a/app/src/main/java/org/thosp/yourlocalweather/widget/ExtLocationWidgetProvider.java b/app/src/main/java/org/thosp/yourlocalweather/widget/ExtLocationWidgetProvider.java index 808a5fe29..bb7fe9f50 100644 --- a/app/src/main/java/org/thosp/yourlocalweather/widget/ExtLocationWidgetProvider.java +++ b/app/src/main/java/org/thosp/yourlocalweather/widget/ExtLocationWidgetProvider.java @@ -15,6 +15,8 @@ import java.util.Calendar; import java.util.Locale; +import static org.thosp.yourlocalweather.utils.LogToFile.appendLog; + public class ExtLocationWidgetProvider extends AbstractWidgetProvider { private static final String TAG = "WidgetExtLocInfo"; @@ -25,6 +27,7 @@ public class ExtLocationWidgetProvider extends AbstractWidgetProvider { @Override protected void preLoadWeather(Context context, RemoteViews remoteViews) { + appendLog(context, TAG, "preLoadWeather:start"); SharedPreferences weatherPref = context.getSharedPreferences(Constants.PREF_WEATHER_NAME, Context.MODE_PRIVATE); String temperatureScale = Utils.getTemperatureScale(context); @@ -48,10 +51,11 @@ protected void preLoadWeather(Context context, RemoteViews remoteViews) { Utils.setWeatherIcon(remoteViews, context); remoteViews.setTextViewText(R.id.widget_last_update, lastUpdate); + appendLog(context, TAG, "preLoadWeather:end"); } - @Override - protected void setWidgetTheme(Context context, RemoteViews remoteViews) { + public static void setWidgetTheme(Context context, RemoteViews remoteViews) { + appendLog(context, TAG, "setWidgetTheme:start"); int textColorId = AppPreference.getTextColor(context); int backgroundColorId = AppPreference.getBackgroundColor(context); int windowHeaderBackgroundColorId = AppPreference.getWindowHeaderBackgroundColorId(context); @@ -65,6 +69,7 @@ protected void setWidgetTheme(Context context, RemoteViews remoteViews) { remoteViews.setTextColor(R.id.widget_sunrise, textColorId); remoteViews.setTextColor(R.id.widget_sunset, textColorId); remoteViews.setInt(R.id.header_layout, "setBackgroundColor", windowHeaderBackgroundColorId); + appendLog(context, TAG, "setWidgetTheme:end"); } @Override diff --git a/app/src/main/java/org/thosp/yourlocalweather/widget/ExtLocationWidgetService.java b/app/src/main/java/org/thosp/yourlocalweather/widget/ExtLocationWidgetService.java index 9497df3e5..0ce4e33ef 100644 --- a/app/src/main/java/org/thosp/yourlocalweather/widget/ExtLocationWidgetService.java +++ b/app/src/main/java/org/thosp/yourlocalweather/widget/ExtLocationWidgetService.java @@ -49,6 +49,10 @@ protected void onHandleIntent(Intent intent) { RemoteViews remoteViews = new RemoteViews(this.getPackageName(), R.layout.widget_ext_loc_3x3); + + ExtLocationWidgetProvider.setWidgetTheme(this, remoteViews); + ExtLocationWidgetProvider.setWidgetIntents(this, remoteViews, ExtLocationWidgetProvider.class); + remoteViews.setTextViewText(R.id.widget_city, Utils.getCityAndCountry(this)); remoteViews.setTextViewText(R.id.widget_temperature, temperature + temperatureScale); remoteViews.setTextViewText(R.id.widget_description, Utils.getWeatherDescription(this, weather)); diff --git a/app/src/main/java/org/thosp/yourlocalweather/widget/LessWidgetProvider.java b/app/src/main/java/org/thosp/yourlocalweather/widget/LessWidgetProvider.java index 878319e9b..c20cd2481 100644 --- a/app/src/main/java/org/thosp/yourlocalweather/widget/LessWidgetProvider.java +++ b/app/src/main/java/org/thosp/yourlocalweather/widget/LessWidgetProvider.java @@ -36,8 +36,7 @@ protected void preLoadWeather(Context context, RemoteViews remoteViews) { remoteViews.setTextViewText(R.id.widget_last_update, lastUpdate); } - @Override - protected void setWidgetTheme(Context context, RemoteViews remoteViews) { + public static void setWidgetTheme(Context context, RemoteViews remoteViews) { int textColorId = AppPreference.getTextColor(context); int backgroundColorId = AppPreference.getBackgroundColor(context); diff --git a/app/src/main/java/org/thosp/yourlocalweather/widget/LessWidgetService.java b/app/src/main/java/org/thosp/yourlocalweather/widget/LessWidgetService.java index 1311a0061..93b1bb4a6 100644 --- a/app/src/main/java/org/thosp/yourlocalweather/widget/LessWidgetService.java +++ b/app/src/main/java/org/thosp/yourlocalweather/widget/LessWidgetService.java @@ -34,6 +34,10 @@ protected void onHandleIntent(Intent intent) { for (int appWidgetId : widgetIds) { RemoteViews remoteViews = new RemoteViews(this.getPackageName(), R.layout.widget_less_3x1); + + LessWidgetProvider.setWidgetTheme(this, remoteViews); + LessWidgetProvider.setWidgetIntents(this, remoteViews, LessWidgetProvider.class); + String lastUpdate = Utils.setLastUpdateTime(this, AppPreference .getLastUpdateTimeMillis(this)); String temperatureScale = Utils.getTemperatureScale(this); diff --git a/app/src/main/java/org/thosp/yourlocalweather/widget/MoreWidgetProvider.java b/app/src/main/java/org/thosp/yourlocalweather/widget/MoreWidgetProvider.java index cb7c4ed92..8bb24b540 100644 --- a/app/src/main/java/org/thosp/yourlocalweather/widget/MoreWidgetProvider.java +++ b/app/src/main/java/org/thosp/yourlocalweather/widget/MoreWidgetProvider.java @@ -43,8 +43,7 @@ protected void preLoadWeather(Context context, RemoteViews remoteViews) { remoteViews.setTextViewText(R.id.widget_last_update, lastUpdate); } - @Override - protected void setWidgetTheme(Context context, RemoteViews remoteViews) { + public static void setWidgetTheme(Context context, RemoteViews remoteViews) { int textColorId = AppPreference.getTextColor(context); int backgroundColorId = AppPreference.getBackgroundColor(context); int windowHeaderBackgroundColorId = AppPreference.getWindowHeaderBackgroundColorId(context); diff --git a/app/src/main/java/org/thosp/yourlocalweather/widget/MoreWidgetService.java b/app/src/main/java/org/thosp/yourlocalweather/widget/MoreWidgetService.java index 8322468bc..4db91cfaa 100644 --- a/app/src/main/java/org/thosp/yourlocalweather/widget/MoreWidgetService.java +++ b/app/src/main/java/org/thosp/yourlocalweather/widget/MoreWidgetService.java @@ -45,6 +45,10 @@ protected void onHandleIntent(Intent intent) { RemoteViews remoteViews = new RemoteViews(this.getPackageName(), R.layout.widget_more_3x3); + + MoreWidgetProvider.setWidgetTheme(this, remoteViews); + MoreWidgetProvider.setWidgetIntents(this, remoteViews, MoreWidgetProvider.class); + remoteViews.setTextViewText(R.id.widget_city, Utils.getCityAndCountry(this)); remoteViews.setTextViewText(R.id.widget_temperature, temperature + temperatureScale); if(!AppPreference.hideDescription(this))