Skip to content

Commit

Permalink
Widget breaks #7 - solves the problem with update of widget when the …
Browse files Browse the repository at this point in the history
…screen is rotated
  • Loading branch information
thuryn authored and thuryn committed Jan 19, 2018
1 parent fed58b4 commit f842542
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 22 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -41,6 +42,7 @@ public void onEnabled(Context context) {
new AppWidgetProviderAlarm(context, getWidgetClass());
appWidgetProviderAlarm.cancelAlarm();
appWidgetProviderAlarm.setAlarm();
appendLog(context, TAG, "onEnabled:end");
}


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit f842542

Please sign in to comment.