WeatherInformation: widget configuration no using PreferenceFragment
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 26 Oct 2014 23:21:26 +0000 (00:21 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 26 Oct 2014 23:21:26 +0000 (00:21 +0100)
app/src/main/java/de/example/exampletdd/WidgetIntentService.java
app/src/main/java/de/example/exampletdd/fragment/current/CurrentFragment.java
app/src/main/java/de/example/exampletdd/widget/WidgetConfigure.java
app/src/main/java/de/example/exampletdd/widget/WidgetPreferences.java [deleted file]
app/src/main/java/de/example/exampletdd/widget/WidgetProvider.java
app/src/main/res/layout/appwidget_configure.xml
app/src/main/res/values/strings.xml
app/src/main/res/xml/appwidget_preferences.xml

index 32228fe..386f08e 100644 (file)
@@ -25,6 +25,7 @@ import java.net.URL;
 import java.text.DecimalFormat;
 import java.text.NumberFormat;
 import java.util.Calendar;
+import java.util.Date;
 import java.util.Locale;
 
 import de.example.exampletdd.httpclient.CustomHTTPClient;
@@ -39,7 +40,7 @@ import de.example.exampletdd.widget.WidgetConfigure;
 
 public class WidgetIntentService extends IntentService {
        private static final String TAG = "WidgetIntentService";
-
+    private static final long UPDATE_TIME_RATE = 86400000L;
 
        public WidgetIntentService() {
                super("WIS-Thread");
@@ -49,7 +50,7 @@ public class WidgetIntentService extends IntentService {
        protected void onHandleIntent(final Intent intent) {
                Log.i(TAG, "onHandleIntent");
                final int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
-               final boolean isUpdateByApp = intent.getBooleanExtra("updateByApp", false);
+               final boolean isForceRefreshAppWidget = intent.getBooleanExtra("forceRefreshAppWidget", false);
 
                if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
                        // Nothing to do. Something went wrong. Show error.
@@ -66,42 +67,35 @@ public class WidgetIntentService extends IntentService {
                        this.updateWidget(view, appWidgetId);
                        return;
                }
-               
-               if (isUpdateByApp) {            
-                       this.updateByApp(weatherLocation, appWidgetId);
-               } else {
-                       this.updateByTimeout(weatherLocation, appWidgetId);
-               }
-
-       }
-
-       private void updateByApp(final WeatherLocation weatherLocation, final int appWidgetId) {
-               final PermanentStorage store = new PermanentStorage(this.getApplicationContext());
-        final Current current = store.getCurrent();
-               
-               this.updateWidget(current, weatherLocation, appWidgetId);
-       }
-       
-       private void updateByTimeout(final WeatherLocation weatherLocation, final int appWidgetId) {
-
-               final Current current = this.getRemoteCurrent(weatherLocation);
-
-               this.updateWidget(current, weatherLocation, appWidgetId);
-       }
-       
-       private void updateWidget(final Current current, final WeatherLocation weatherLocation, final int appWidgetId) {
 
-               if (current != null) {
-                       final RemoteViews view = this.makeView(current, weatherLocation, appWidgetId);
-                       this.updateWidget(view, appWidgetId);
+        // TODO: improve this code. Too tired right now...
+               if (!isForceRefreshAppWidget && this.isDataFresh(weatherLocation.getLastCurrentUIUpdate())) {
+            RemoteViews view;
+
+            final PermanentStorage store = new PermanentStorage(this.getApplicationContext());
+            final Current current = store.getCurrent();
+            if (current != null) {
+                // Update UI.
+                view = this.makeView(current, weatherLocation, appWidgetId);
+            } else {
+                // Show error.
+                view = this.makeErrorView(appWidgetId);
+            }
+            this.updateWidget(view, appWidgetId);
                } else {
-                       // Show error.
-                       final RemoteViews view = this.makeErrorView(appWidgetId);
-                       this.updateWidget(view, appWidgetId);
+            RemoteViews view;
+
+            final Current current = this.getRemoteCurrent(weatherLocation);
+            if (current != null) {
+                // Update UI.
+                view = this.makeView(current, weatherLocation, appWidgetId);
+            } else {
+                // Show error.
+                view = this.makeErrorView(appWidgetId);
+            }
+            this.updateWidget(view, appWidgetId);
                }
        }
-
-
        
        private Current getRemoteCurrent(final WeatherLocation weatherLocation) {
 
@@ -159,16 +153,14 @@ public class WidgetIntentService extends IntentService {
 
                // TODO: repeating the same code in Overview, Specific and Current!!!
                // 1. Update units of measurement.
-        String tempSymbol;
+
         UnitsConversor tempUnitsConversor;
-        String keyPreference = this.getApplicationContext().getString(R.string.widget_preferences_temperature_key);
+        String keyPreference = this.getApplicationContext().getString(R.string.widget_preferences_temperature_units_key);
         String realKeyPreference = keyPreference + "_" + appWidgetId;
         // What was saved to permanent storage (or default values if it is the first time)
-        final String tempValue = this.getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE)
-                .getString(realKeyPreference, this.getString(R.string.weather_preferences_temperature_celsius));
-        final String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
-        if (tempValue.equals(values[0])) {
-               tempSymbol = values[0];
+        final int tempValue = this.getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE).getInt(realKeyPreference, 0);
+        final String tempSymbol = this.getResources().getStringArray(R.array.weather_preferences_temperature)[tempValue];
+        if (tempValue == 0) {
                tempUnitsConversor = new UnitsConversor(){
 
                                @Override
@@ -177,8 +169,7 @@ public class WidgetIntentService extends IntentService {
                                }
 
                };
-        } else if (tempValue.equals(values[1])) {
-               tempSymbol = values[1];
+        } else if (tempValue == 1) {
                tempUnitsConversor = new UnitsConversor(){
 
                                @Override
@@ -188,7 +179,6 @@ public class WidgetIntentService extends IntentService {
 
                };
         } else {
-               tempSymbol = values[2];
                tempUnitsConversor = new UnitsConversor(){
 
                                @Override
@@ -324,6 +314,19 @@ public class WidgetIntentService extends IntentService {
                final AppWidgetManager manager = AppWidgetManager.getInstance(this.getApplicationContext());
                manager.updateAppWidget(appWidgetId, remoteView);
        }
+
+    private boolean isDataFresh(final Date lastUpdate) {
+        if (lastUpdate == null) {
+            return false;
+        }
+
+        final Date currentTime = new Date();
+        if (((currentTime.getTime() - lastUpdate.getTime())) < UPDATE_TIME_RATE) {
+            return true;
+        }
+
+        return false;
+    }
        
 //     private void updateWidgets(final RemoteViews remoteView) {
 //             
index e0423d5..5245658 100644 (file)
@@ -120,15 +120,7 @@ public class CurrentFragment extends Fragment {
 
                             // 4. If is new data (new location) update widgets.
                             if (weatherLocation.getIsNew()) {
-                                final ComponentName widgets = new ComponentName(context.getApplicationContext(), WidgetProvider.class);
-                                final AppWidgetManager manager = AppWidgetManager.getInstance(context.getApplicationContext());
-                                final int[] appWidgetIds = manager.getAppWidgetIds(widgets);
-                                for (final int appWidgetId : appWidgetIds) {
-                                    final Intent intentWidget = new Intent(context.getApplicationContext(), WidgetIntentService.class);
-                                    intentWidget.putExtra("updateByApp", true);
-                                    intentWidget.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
-                                    context.getApplicationContext().startService(intentWidget);
-                                }
+                                WidgetProvider.updateAllAppWidgets(context);
                             }
 
                             // 5. Update location data.
index 711adf1..410a847 100644 (file)
@@ -3,18 +3,23 @@ package de.example.exampletdd.widget;
 import android.app.ActionBar;
 import android.app.Activity;
 import android.appwidget.AppWidgetManager;
+import android.content.Context;
 import android.content.Intent;
+import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.view.View;
+import android.widget.CompoundButton;
+import android.widget.Spinner;
+import android.widget.Switch;
+
 import de.example.exampletdd.R;
 
 public class WidgetConfigure extends Activity {
        private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
-    private View.OnClickListener mOnClickListener;
 
     @Override
-    public void onCreate(final Bundle icicle) {
-        super.onCreate(icicle);
+    public void onCreate(final Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
 
         // Find the widget id from the intent. 
         final Intent intent = getIntent();
@@ -42,31 +47,60 @@ public class WidgetConfigure extends Activity {
         // Set the view layout resource to use.
         this.setContentView(R.layout.appwidget_configure);
 
+        /******************* Show/hide country field *******************/
+        String keyPreference = this.getApplicationContext().getString(
+                R.string.widget_preferences_country_switch_key);
+        String realKeyPreference = keyPreference + "_" + mAppWidgetId;
+
+        // What was saved to permanent storage (or default values if it is the first time)
+        final boolean isShowCountry = this.getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE)
+                .getBoolean(realKeyPreference, false);
+
+        // What is shown on the screen
+        final Switch countrySwitch = (Switch) this.findViewById(R.id.weather_appwidget_configure_country);
+        countrySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
+            @Override
+            public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
+                if (isChecked) {
+                    buttonView.setText(WidgetConfigure.this.getString(R.string.widget_preferences_country_switch_on_summary));
+                } else {
+                    buttonView.setText(WidgetConfigure.this.getString(R.string.widget_preferences_country_switch_off_summary));
+                }
+            }
+        });
+        if (isShowCountry) {
+            countrySwitch.setChecked(true);
+            countrySwitch.setText(this.getString(R.string.widget_preferences_country_switch_on_summary));
+        } else {
+            countrySwitch.setChecked(false);
+            countrySwitch.setText(this.getString(R.string.widget_preferences_country_switch_off_summary));
+        }
 
-        mOnClickListener = new View.OnClickListener() {
-            public void onClick(View v) {
+        /********************* Temperature units  **********************/
+        keyPreference = this.getApplicationContext().getString(
+                R.string.widget_preferences_temperature_units_key);
+        realKeyPreference = keyPreference + "_" + mAppWidgetId;
 
-                // Save to permanent storage
+        // What was saved to permanent storage (or default values if it is the first time)
+        final int tempValue = this.getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE).getInt(realKeyPreference, 0);
 
-                // Push widget update to surface with newly set prefix
-                final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(
-                        WidgetConfigure.this.getApplicationContext());
-                WidgetProvider.updateAppWidget(
-                        WidgetConfigure.this.getApplicationContext(),
-                        appWidgetManager,
-                        mAppWidgetId);
+        // What is shown on the screen
+        final Spinner tempUnits = (Spinner) this.findViewById(R.id.weather_appwidget_configure_temperature_units);
+        tempUnits.setSelection(tempValue);
 
-                // Make sure we pass back the original appWidgetId
-                final Intent resultValue = new Intent();
-                resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
-                WidgetConfigure.this.setResult(RESULT_OK, resultValue);
-                finish();
-            }
-        };
-        // Bind the action for the save button.
-        this.findViewById(R.id.weather_appwidget_configure_save_button).setOnClickListener(mOnClickListener);
+        /**
+         * android:saveEnabled
+         * Controls whether the saving of this view's state is enabled (that is, whether its onSaveInstanceState() method will be called).
+         *
+         * After onStart the onSaveInstanceState method will be called for every widget, so
+         * I do not need to do anything else to retrieve the UI's state after changing orientation.
+         *
+         * I do not know if this is a good pattern, it does not look like that. I guess, I should use
+         * on Resume instead of onCreate/onStart and implement my own onSaveInstanceState method.
+         * But I am tired...
+         */
     }
-    
+
     @Override
     public void onResume() {
         super.onResume();
@@ -74,4 +108,65 @@ public class WidgetConfigure extends Activity {
         final ActionBar actionBar = this.getActionBar();
         actionBar.setTitle(this.getString(R.string.widget_preferences_action_settings));
     }
+
+
+    public void onClickRefresh(final View view) {
+        // Push widget update to surface
+        WidgetProvider.forceRefreshAppWidget(this.getApplicationContext(), mAppWidgetId);
+    }
+
+    public void onClickOk(final View view) {
+        // Save to permanent storage
+        final SharedPreferences.Editor prefs = this.getSharedPreferences(
+                                        "WIDGET_PREFERENCES",
+                                        Context.MODE_PRIVATE).edit();
+
+        /******************* Show/hide country field *******************/
+        // What is shown on the screen
+        final Switch countrySwitch = (Switch) this.findViewById(R.id.weather_appwidget_configure_country);
+        String keyPreference = this.getApplicationContext().getString(
+                R.string.widget_preferences_country_switch_key);
+        String realKeyPreference = keyPreference + "_" + mAppWidgetId;
+        prefs.putBoolean(realKeyPreference, countrySwitch.isChecked());
+
+        /********************* Temperature units  **********************/
+        // What is shown on the screen
+        final Spinner tempUnits = (Spinner) this.findViewById(R.id.weather_appwidget_configure_temperature_units);
+        keyPreference = this.getApplicationContext().getString(
+                R.string.widget_preferences_temperature_units_key);
+        realKeyPreference = keyPreference + "_" + mAppWidgetId;
+        prefs.putInt(realKeyPreference, tempUnits.getSelectedItemPosition());
+
+        /****************** Saving to permanent storage  ***************/
+        prefs.commit();
+
+        // Push widget update to surface with newly set prefix
+        WidgetProvider.updateAppWidget(this.getApplicationContext(), mAppWidgetId);
+
+        // Make sure we pass back the original appWidgetId
+        final Intent resultValue = new Intent();
+        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
+        this.setResult(RESULT_OK, resultValue);
+        finish();
+    }
+
+    public static void deletePreference(final Context context, final int appWidgetId) {
+        final SharedPreferences.Editor prefs = context.getApplicationContext()
+                .getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE).edit();
+
+        /******************* Show/hide country field *******************/
+        String keyPreference = context.getApplicationContext().getString(
+                R.string.widget_preferences_country_switch_key);
+        String realKeyPreference = keyPreference + "_" + appWidgetId;
+        prefs.remove(realKeyPreference);
+
+        /********************* Temperature units  **********************/
+        keyPreference = context.getApplicationContext().getString(
+                R.string.widget_preferences_temperature_units_key);
+        realKeyPreference = keyPreference + "_" + appWidgetId;
+        prefs.remove(realKeyPreference);
+
+        /****************** Updating permanent storage  ***************/
+        prefs.commit();
+    }
 }
diff --git a/app/src/main/java/de/example/exampletdd/widget/WidgetPreferences.java b/app/src/main/java/de/example/exampletdd/widget/WidgetPreferences.java
deleted file mode 100644 (file)
index 9907b4d..0000000
+++ /dev/null
@@ -1,182 +0,0 @@
-package de.example.exampletdd.widget;
-
-import android.appwidget.AppWidgetManager;
-import android.content.Context;
-import android.content.SharedPreferences;
-import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
-import android.os.Bundle;
-import android.preference.ListPreference;
-import android.preference.PreferenceFragment;
-import android.preference.SwitchPreference;
-import de.example.exampletdd.R;
-
-/**
- * TODO:
- * IT DOES NOT WORK IF USER IS WORKING WITH TWO OR MORE WIDGET PREFERENCE WINDOWS AT THE SAME TIME
- * (hopefully nobody will realize...)
- * How to implement custom preference activities (no extending from PreferenceActivity or PreferenceFragment)
- * without pain?
- */
-public class WidgetPreferences extends PreferenceFragment implements OnSharedPreferenceChangeListener {
-    private int mAppWidgetId;
-    private boolean mIsCountry;
-    private String mTempUnits;
-       
-    @Override
-    public void onCreate(final Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-
-        // Do not retain this fragment across configuration changes because I am tired for following
-        // the fragment lifecycle (I am going to loose the instance field values but I DON'T CARE!!!)
-        this.setRetainInstance(false);
-       
-       final Bundle bundle = this.getArguments();
-       mAppWidgetId = bundle.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
-       
-        // Load the preferences from an XML resource
-        this.addPreferencesFromResource(R.xml.appwidget_preferences);
-
-
-        /******************* Show/hide country field *******************/
-        String keyPreference = this.getActivity().getApplicationContext().getString(
-                R.string.widget_preferences_country_switch_key);
-        String realKeyPreference = keyPreference + "_" + mAppWidgetId;
-        
-        // What was saved to permanent storage (or default values if it is the first time)
-        final boolean countryValue = this.getActivity().getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE)
-                       .getBoolean(realKeyPreference, false);
-
-        // Update temporal value
-        mIsCountry = countryValue;
-
-        // What is shown on the screen
-        final SwitchPreference countryPref = (SwitchPreference) this.findPreference(keyPreference);
-        countryPref.setChecked(countryValue);
-
-        /********************* Temperature units  **********************/
-        final String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
-        final String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_temperature_human_value);
-
-        keyPreference = this.getActivity().getApplicationContext().getString(
-                R.string.widget_preferences_temperature_key);
-        realKeyPreference = keyPreference + "_" + mAppWidgetId;
-
-
-        // What was saved to permanent storage (or default values if it is the first time)
-        final String tempValue = this.getActivity().getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE)
-                       .getString(realKeyPreference, this.getString(R.string.weather_preferences_temperature_celsius));
-        String humanValue = this.getString(R.string.weather_preferences_temperature_celsius_human_value);
-        int index = 0;
-        if (tempValue.equals(values[0])) {
-               index = 0;
-            humanValue = humanValues[0];
-        } else if (tempValue.equals(values[1])) {
-               index = 1;
-            humanValue = humanValues[1];
-        } else if (tempValue.equals(values[2])) {
-               index = 2;
-            humanValue = humanValues[2];
-        }
-
-        // Update temporal value
-        mTempUnits = tempValue;
-
-        // What is shown on the screen
-        final ListPreference listPref = (ListPreference) this.findPreference(keyPreference);
-        listPref.setSummary(humanValue);
-        listPref.setValueIndex(index);
-        listPref.setValue(tempValue);
-    }
-
-    @Override
-    public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
-
-        /******************* Show/hide country field *******************/
-        String keyPreference = this.getActivity().getApplicationContext().getString(
-                R.string.widget_preferences_country_switch_key);
-        if (key.equals(keyPreference)) {
-            // What is shown on the screen
-            final SwitchPreference preference = (SwitchPreference) this.findPreference(key);
-            // Update temporal value
-            mIsCountry = preference.isChecked();
-
-            return;
-        }
-
-        /********************* Temperature units  **********************/
-        keyPreference = this.getActivity().getApplicationContext().getString(
-                R.string.widget_preferences_temperature_key);
-        if (key.equals(keyPreference)) {
-            final String[] values = this.getResources().getStringArray(
-                    R.array.weather_preferences_temperature);
-            final String[] humanValues = this.getResources().getStringArray(
-                    R.array.weather_preferences_temperature_human_value);
-
-            // What is shown on the screen
-            final ListPreference listPref = (ListPreference) this.findPreference(key);
-            final String value = listPref.getValue();
-            String humanValue = "";
-            if (value.equals(values[0])) {
-                humanValue = humanValues[0];
-            } else if (value.equals(values[1])) {
-                humanValue = humanValues[1];
-            } else if (value.equals(values[2])) {
-                humanValue = humanValues[2];
-            }
-            // Update data on screen
-            listPref.setSummary(humanValue);
-
-            // Update temporal value
-            mTempUnits = value;
-
-            return;
-        }
-    }
-
-    @Override
-    public void onResume() {
-        super.onResume();
-        this.getPreferenceManager().getSharedPreferences()
-        .registerOnSharedPreferenceChangeListener(this);
-    }
-
-    @Override
-    public void onPause() {
-        super.onPause();
-        this.getPreferenceManager().getSharedPreferences()
-        .unregisterOnSharedPreferenceChangeListener(this);
-    }
-
-    public void onSavePreferences() {
-        final SharedPreferences.Editor prefs =
-                this.getActivity().getSharedPreferences(
-                        "WIDGET_PREFERENCES",
-                        Context.MODE_PRIVATE).edit();
-
-        /******************* Show/hide country field *******************/
-        String keyPreference = this.getActivity().getApplicationContext().getString(
-                R.string.widget_preferences_country_switch_key);
-        String realKeyPreference = keyPreference + "_" + mAppWidgetId;
-        prefs.putBoolean(realKeyPreference, mIsCountry);
-
-
-        /********************* Temperature units  **********************/
-        keyPreference = this.getActivity().getApplicationContext().getString(
-                R.string.widget_preferences_temperature_key);
-        realKeyPreference = keyPreference + "_" + mAppWidgetId;
-        prefs.putString(realKeyPreference, mTempUnits);
-
-        // Saving to permanent storage.
-        prefs.commit();
-    }
-
-    public static void deletePreference(final Context context, final int appWidgetId) {
-       final String keyPreference = context.getApplicationContext().getString(
-                R.string.widget_preferences_temperature_key);
-        final String realKeyPreference = keyPreference + "_" + appWidgetId;
-
-       final SharedPreferences.Editor prefs = context.getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE).edit();
-       prefs.remove(realKeyPreference);
-       prefs.commit();
-    }
-}
index 91263f2..06970e7 100644 (file)
@@ -4,6 +4,7 @@ package de.example.exampletdd.widget;
 import android.appwidget.AppWidgetManager;
 import android.appwidget.AppWidgetProvider;
 import android.appwidget.AppWidgetProviderInfo;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
@@ -23,7 +24,7 @@ public class WidgetProvider extends AppWidgetProvider {
             // To prevent any ANR timeouts, we perform the update in a service
                final Intent intent = new Intent(context.getApplicationContext(), WidgetIntentService.class);
                intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
-            intent.putExtra("updateByApp", false);
+            intent.putExtra("forceRefreshAppWidget", true);
             context.startService(intent);
         }
     }
@@ -33,11 +34,54 @@ public class WidgetProvider extends AppWidgetProvider {
         // When the user deletes the widget, delete the preference associated with it.
         final int N = appWidgetIds.length;
         for (int i=0; i<N; i++) {
-               WidgetPreferences.deletePreference(context, appWidgetIds[i]);
+               WidgetConfigure.deletePreference(context, appWidgetIds[i]);
         }
     }
-    
-    static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId) {
+
+    static void updateAppWidget(final Context context, final int appWidgetId) {
+        final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());
+
+        updateAppWidget(context, appWidgetManager, appWidgetId);
+    }
+
+    static void forceRefreshAppWidget(final Context context, final int appWidgetId) {
+        final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());
+
+        int widgetId;
+        Bundle myOptions = appWidgetManager.getAppWidgetOptions(appWidgetId);
+
+        // Get the value of OPTION_APPWIDGET_HOST_CATEGORY
+        int category = myOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY, -1);
+
+        // If the value is WIDGET_CATEGORY_KEYGUARD, it's a lockscreen widget
+        boolean isKeyguard = category == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD;
+
+        // Once you know the widget's category, you can optionally load a different base layout, set different
+        // properties, and so on. For example:
+        //int baseLayout = isKeyguard ? R.layout.keyguard_widget_layout : R.layout.widget_layout;
+
+        // Construct the RemoteViews object.  It takes the package name (in our case, it's our
+        // package, but it needs this because on the other side it's the widget host inflating
+        // the layout from our package).
+        //final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
+
+        final Intent intent = new Intent(context.getApplicationContext(), WidgetIntentService.class);
+        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
+        intent.putExtra("forceRefreshAppWidget", true);
+        context.startService(intent);
+    }
+
+    public static void updateAllAppWidgets(final Context context) {
+        final ComponentName widgets = new ComponentName(context.getApplicationContext(), WidgetProvider.class);
+        final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());
+
+        final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(widgets);
+        for (final int appWidgetId : appWidgetIds) {
+            updateAppWidget(context.getApplicationContext(), appWidgetManager, appWidgetId);
+        }
+    }
+
+    private static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId) {
 
         int widgetId;
         Bundle myOptions = appWidgetManager.getAppWidgetOptions(appWidgetId);
@@ -47,19 +91,19 @@ public class WidgetProvider extends AppWidgetProvider {
 
         // If the value is WIDGET_CATEGORY_KEYGUARD, it's a lockscreen widget
         boolean isKeyguard = category == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD;
-        
-        // Once you know the widget's category, you can optionally load a different base layout, set different 
+
+        // Once you know the widget's category, you can optionally load a different base layout, set different
         // properties, and so on. For example:
         //int baseLayout = isKeyguard ? R.layout.keyguard_widget_layout : R.layout.widget_layout;
-        
+
         // Construct the RemoteViews object.  It takes the package name (in our case, it's our
         // package, but it needs this because on the other side it's the widget host inflating
         // the layout from our package).
         //final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
-        
+
         final Intent intent = new Intent(context.getApplicationContext(), WidgetIntentService.class);
-       intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
-        intent.putExtra("updateByApp", true);
+        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
+        intent.putExtra("forceRefreshAppWidget", false);
         context.startService(intent);
     }
 }
index 489c010..9ac96a1 100644 (file)
@@ -15,6 +15,7 @@
             android:layout_height="wrap_content"
             android:layout_gravity="center"
             android:textAlignment="center"
+            android:onClick="onClickOk"
             android:text="@android:string/ok" />
     </LinearLayout>
 
@@ -32,7 +33,7 @@
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:textAppearance="?android:attr/textAppearanceMedium"
-                android:text="Country"
+                android:text="@string/widget_preferences_country"
                 android:padding="5dp"
                 android:layout_gravity="start" />
 
                 <Switch android:id="@+id/weather_appwidget_configure_country"
                     android:layout_width="fill_parent"
                     android:layout_height="wrap_content"
-                    android:text="Country field"
-                    android:textOff="HIDE"
-                    android:textOn="SHOW" />
+                    android:checked="false"
+                    android:text="@string/widget_preferences_country_switch_off_summary"
+                    android:textOff="@string/widget_preferences_country_switch_off"
+                    android:textOn="@string/widget_preferences_country_switch_on" />
             </LinearLayout>
 
             <TextView
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:textAppearance="?android:attr/textAppearanceMedium"
-                android:text="Temperature units"
+                android:text="@string/widget_preferences_temperature_units"
                 android:padding="5dp"
                 android:layout_gravity="start" />
 
@@ -81,7 +83,7 @@
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:textAppearance="?android:attr/textAppearanceMedium"
-                android:text="Refresh"
+                android:text="@string/widget_preferences_button_refresh"
                 android:padding="5dp"
                 android:layout_gravity="start" />
 
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:textAppearance="?android:attr/textAppearanceMedium"
-                    android:text="Refresh data"
+                    android:text="@string/widget_configure_button_refresh_summary"
                     android:padding="5dp"
                     android:layout_gravity="start" />
 
                     android:layout_height="wrap_content"
                     android:layout_gravity="end"
                     android:textAlignment="center"
-                    android:text="Refresh"/>
+                    android:onClick="onClickRefresh"
+                    android:text="@string/widget_preferences_button_refresh"/>
 
             </GridLayout>
 
index ec32311..746757d 100644 (file)
     <!-- Widget Preferences Activity -->
     <string name="widget_preferences_action_settings">Settings</string>
     <string name="widget_preferences_button_refresh">Refresh</string>
+    <string name="widget_configure_button_refresh_summary">Refresh values</string>
+    <string name="widget_preferences_temperature_units">Temperature units</string>
+    <string name="widget_preferences_temperature_units_key">widget_preferences_temperature</string>
     <string name="widget_preferences_country">Country</string>
-    <string name="widget_preferences_units">Units</string>
-    <string name="widget_preferences_temperature_key">widget_preferences_temperature</string>
-    <string name="widget_preferences_country_switch_key">widget_preferences_countries_switch</string>
-    <string name="widget_preferences_country_switch_off_summary">Hide</string>
-    <string name="widget_preferences_country_switch_on_summary">Show</string>
+    <string name="widget_preferences_country_switch_key">widget_preferences_country_switch</string>
+    <string name="widget_preferences_country_switch_off_summary">Hide country</string>
+    <string name="widget_preferences_country_switch_on_summary">Show country</string>
     <string name="widget_preferences_country_switch_off">OFF</string>
     <string name="widget_preferences_country_switch_on">ON</string>
 
index db489da..85320c2 100644 (file)
@@ -12,8 +12,8 @@
             android:disableDependentsState="false"
             android:persistent="true"/>
     </PreferenceCategory>
-    <PreferenceCategory android:title="@string/widget_preferences_units">
-        <ListPreference android:key="@string/widget_preferences_temperature_key"
+    <PreferenceCategory android:title="@string/widget_preferences_temperature_units">
+        <ListPreference android:key="@string/widget_preferences_temperature_units_key"
             android:title="@string/weather_preferences_temperature"
             android:summary="@string/weather_preferences_temperature_celsius_human_value"
             android:entries="@array/weather_preferences_temperature_human_value"