WeatherInformation: multiple widget configuration WIP
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Fri, 17 Oct 2014 19:51:07 +0000 (21:51 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Fri, 17 Oct 2014 19:51:07 +0000 (21:51 +0200)
Android/WeatherInformation/res/layout/appwidget_configure.xml
Android/WeatherInformation/res/values/strings.xml
Android/WeatherInformation/src/de/example/exampletdd/WidgetIntentService.java
Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentFragment.java
Android/WeatherInformation/src/de/example/exampletdd/widget/WidgetConfigure.java
Android/WeatherInformation/src/de/example/exampletdd/widget/WidgetPreferences.java
Android/WeatherInformation/src/de/example/exampletdd/widget/WidgetProvider.java

index 66463eb..f5df9a0 100644 (file)
     <LinearLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
+        android:layout_below="@+id/weather_appwidget_configure_preferences"
+        android:layout_centerHorizontal="true"
+        android:layout_gravity="center"
+        android:gravity="center"
+        android:baselineAligned="false"
+        android:orientation="horizontal" >
+
+        <Button
+            android:id="@+id/weather_appwidget_configure_refresh_button"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_gravity="center"
+            android:textAlignment="center"
+            android:text="@string/widget_preferences_button_refresh" />
+    </LinearLayout>
+
+    <LinearLayout
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
         android:layout_alignParentBottom="true"
         android:layout_centerHorizontal="true"
         android:layout_gravity="center"
index f21b278..dea5420 100644 (file)
 
     <!-- Widget Preferences Activity -->
     <string name="widget_preferences_action_settings">Settings</string>
+    <string name="widget_preferences_button_refresh">Refresh</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_ccountry_switch_off">OFF</string>
+    <string name="widget_preferences_country_switch_off">OFF</string>
     <string name="widget_preferences_country_switch_on">ON</string>
 
 
index c7f6438..ed38545 100644 (file)
@@ -14,13 +14,12 @@ import org.apache.http.client.ClientProtocolException;
 import android.app.IntentService;
 import android.app.PendingIntent;
 import android.appwidget.AppWidgetManager;
-import android.content.ComponentName;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
+import android.net.Uri;
 import android.net.http.AndroidHttpClient;
-import android.os.Bundle;
 import android.preference.PreferenceManager;
 import android.support.v4.app.TaskStackBuilder;
 import android.util.Log;
@@ -37,7 +36,6 @@ import de.example.exampletdd.service.IconsList;
 import de.example.exampletdd.service.PermanentStorage;
 import de.example.exampletdd.service.ServiceParser;
 import de.example.exampletdd.widget.WidgetConfigure;
-import de.example.exampletdd.widget.WidgetProvider;
 
 public class WidgetIntentService extends IntentService {
        private static final String TAG = "WidgetIntentService";
@@ -50,16 +48,22 @@ public class WidgetIntentService extends IntentService {
        @Override
        protected void onHandleIntent(final Intent intent) {
                Log.i(TAG, "onHandleIntent");
-               final int appWidgetId = intent.getIntExtra("appWidgetId", AppWidgetManager.INVALID_APPWIDGET_ID);
+               final int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
                final boolean isUpdateByApp = intent.getBooleanExtra("updateByApp", false);
 
+               if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
+                       // Nothing to do. Something went wrong. Show error.
+                       return;
+               }
+
+
                final DatabaseQueries query = new DatabaseQueries(this.getApplicationContext());
                final WeatherLocation weatherLocation = query.queryDataBase();
                
                if (weatherLocation == null) {
                        // Nothing to do. Show error.
                        final RemoteViews view = this.makeErrorView(appWidgetId);
-                       this.updateWidgets(view);
+                       this.updateWidget(view, appWidgetId);
                        return;
                }
                
@@ -74,29 +78,30 @@ public class WidgetIntentService extends IntentService {
        private void updateByApp(final WeatherLocation weatherLocation, final int appWidgetId) {
                final PermanentStorage store = new PermanentStorage(this.getApplicationContext());
         final Current current = store.getCurrent();
-               final RemoteViews view = this.makeView(current, weatherLocation, appWidgetId);
                
-               this.updateWidgets(view);
+               this.updateWidget(current, weatherLocation, appWidgetId);
        }
        
        private void updateByTimeout(final WeatherLocation weatherLocation, final int appWidgetId) {
-               if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
-                       // Nothing to do. Something went wrong. Show error.
-                       final RemoteViews view = this.makeErrorView(appWidgetId);
-                       this.updateWidgets(view);
-                       return;
-               }
-               
+
                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);
                } else {
                        // Show error.
                        final RemoteViews view = this.makeErrorView(appWidgetId);
-                       this.updateWidgets(view);
+                       this.updateWidget(view, appWidgetId);
                }
        }
+
+
        
        private Current getRemoteCurrent(final WeatherLocation weatherLocation) {
 
@@ -238,7 +243,10 @@ public class WidgetIntentService extends IntentService {
                // 5. Activity launcher.
                final Intent resultIntent =  new Intent(this.getApplicationContext(), WidgetConfigure.class);
                resultIntent.putExtra("actionFromUser", true);
-               resultIntent.putExtra("appWidgetId", appWidgetId);
+               resultIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
+               // From: http://stackoverflow.com/questions/4011178/multiple-instances-of-widget-only-updating-last-widget
+               final Uri data = Uri.withAppendedPath(Uri.parse("PAIN" + "://widget/id/") ,String.valueOf(appWidgetId));
+               resultIntent.setData(data);
 
                final TaskStackBuilder stackBuilder = TaskStackBuilder.create(this.getApplicationContext());
                // Adds the back stack for the Intent (but not the Intent itself)
@@ -261,7 +269,10 @@ public class WidgetIntentService extends IntentService {
                // 5. Activity launcher.
                final Intent resultIntent =  new Intent(this.getApplicationContext(), WidgetConfigure.class);
                resultIntent.putExtra("actionFromUser", true);
-               resultIntent.putExtra("appWidgetId", appWidgetId);
+               resultIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
+               // From: http://stackoverflow.com/questions/4011178/multiple-instances-of-widget-only-updating-last-widget
+               final Uri data = Uri.withAppendedPath(Uri.parse("PAIN" + "://widget/id/") ,String.valueOf(appWidgetId));
+               resultIntent.setData(data);
 
                final TaskStackBuilder stackBuilder = TaskStackBuilder.create(this.getApplicationContext());
                // Adds the back stack for the Intent (but not the Intent itself)
@@ -284,10 +295,10 @@ public class WidgetIntentService extends IntentService {
                manager.updateAppWidget(appWidgetId, remoteView);
        }
        
-       private void updateWidgets(final RemoteViews remoteView) {
-               
-               final ComponentName widgets = new ComponentName(this.getApplicationContext(), WidgetProvider.class);
-               final AppWidgetManager manager = AppWidgetManager.getInstance(this.getApplicationContext());
-               manager.updateAppWidget(widgets, remoteView);
-       }
+//     private void updateWidgets(final RemoteViews remoteView) {
+//             
+//             final ComponentName widgets = new ComponentName(this.getApplicationContext(), WidgetProvider.class);
+//             final AppWidgetManager manager = AppWidgetManager.getInstance(this.getApplicationContext());
+//             manager.updateAppWidget(widgets, remoteView);
+//     }
 }
index d199fef..13f9a77 100644 (file)
@@ -116,13 +116,6 @@ public class CurrentFragment extends Fragment {
                                                        store.saveCurrent(currentRemote);
                                            weatherLocation.setLastCurrentUIUpdate(new Date());
                                            query.updateDataBase(weatherLocation);
-
-                                           // 4. Update Widget's UI.
-                                           // TODO: MEJOR PONER UN REFRESH EN LA ACTIVITY DE CONFIGURACION DEL WIDGET!!!!
-                                           // PARA DE ESA FORMA NO PERDER EL WIDGETID
-                                           final Intent intentWidget = new Intent(context.getApplicationContext(), WidgetIntentService.class);
-                                           intentWidget.putExtra("updateByApp", true);
-                                           context.getApplicationContext().startService(intentWidget);
                                    }
 
                                        } else {
index 5f73926..bb5d8ca 100644 (file)
@@ -42,6 +42,7 @@ public class WidgetConfigure extends Activity {
         final Intent intent = getIntent();
         final Bundle extras = intent.getExtras();
         boolean isActionFromUser = false;
+
         if (extras != null) {
             mAppWidgetId = extras.getInt(
                     AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
@@ -49,12 +50,12 @@ public class WidgetConfigure extends Activity {
             isActionFromUser = extras.getBoolean("actionFromUser", false);
         }
         
+        // If they gave us an intent without the widget id, just bail.
+       if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
+               this.finish();
+       }
+       
         if (!isActionFromUser) {
-               // If they gave us an intent without the widget id, just bail.
-               if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
-                       this.finish();
-               }
-
             // Set the result to CANCELED.  This will cause the widget host to cancel
             // out of the widget placement if they press the back button.
             this.setResult(RESULT_CANCELED);
@@ -64,7 +65,7 @@ public class WidgetConfigure extends Activity {
         this.setContentView(R.layout.appwidget_configure);
         
        final Bundle args = new Bundle();
-       args.putInt("appWidgetId", mAppWidgetId);
+       args.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
        final Fragment preferences = new WidgetPreferences();
         preferences.setRetainInstance(true);
        preferences.setArguments(args);
index b4aecf2..7744ea8 100644 (file)
@@ -1,5 +1,6 @@
 package de.example.exampletdd.widget;
 
+import android.appwidget.AppWidgetManager;
 import android.content.Context;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
@@ -27,35 +28,46 @@ public class WidgetPreferences extends PreferenceFragment implements OnSharedPre
        this.setRetainInstance(true);
        
        final Bundle bundle = this.getArguments();
-       appWidgetId = bundle.getInt("appWidgetId");
+       appWidgetId = 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 + "_" + appWidgetId;
+        
+        // What was saved to permanent storage (or default values if it is the first time)
+        boolean countryValue = this.getActivity().getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE)
+                       .getBoolean(realKeyPreference, false);
+        
+        // What is shown on the screen
+        final SwitchPreference countryPref = (SwitchPreference) this.findPreference(keyPreference);
+        countryPref.setChecked(countryValue);
 
-        /******************* Temperature units  *******************/
-        String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
-        String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_temperature_human_value);
+        /********************* 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);
 
-        String keyPreference = this.getActivity().getApplicationContext().getString(
+        keyPreference = this.getActivity().getApplicationContext().getString(
                 R.string.widget_preferences_temperature_key);
-        String realKeyPreference = keyPreference + "_" + appWidgetId;
+        realKeyPreference = keyPreference + "_" + appWidgetId;
 
 
         // What was saved to permanent storage (or default values if it is the first time)
-        String value = this.getActivity().getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE)
+        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 (value.equals(values[0])) {
+        if (tempValue.equals(values[0])) {
                index = 0;
             humanValue = humanValues[0];
-        } else if (value.equals(values[1])) {
+        } else if (tempValue.equals(values[1])) {
                index = 1;
             humanValue = humanValues[1];
-        } else if (value.equals(values[2])) {
+        } else if (tempValue.equals(values[2])) {
                index = 2;
             humanValue = humanValues[2];
         }
@@ -65,25 +77,47 @@ public class WidgetPreferences extends PreferenceFragment implements OnSharedPre
         final ListPreference listPref = (ListPreference) this.findPreference(keyPreference);
         listPref.setSummary(humanValue);
         listPref.setValueIndex(index);
-        listPref.setValue(value);
+        listPref.setValue(tempValue);
     }
 
     @Override
        public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
-               
-       // Temperature units
-        String keyValue = this.getActivity().getApplicationContext().getString(
+
+       /******************* Show/hide country field *******************/
+        String keyPreference = this.getActivity().getApplicationContext().getString(
+                       R.string.widget_preferences_country_switch_key);
+        if (key.equals(keyPreference)) {
+               final String realKeyPreference = keyPreference + "_" + appWidgetId;
+               // Saving to permanent storage.
+               final SharedPreferences.Editor prefs =
+                               this.getActivity().getSharedPreferences(
+                                               "WIDGET_PREFERENCES", 
+                                               Context.MODE_PRIVATE).edit();
+               // What is shown on the screen
+               final SwitchPreference preference = (SwitchPreference) this.findPreference(key);
+               if (preference.isChecked())
+               {
+                       // Saving to permanent storage.
+                       prefs.putBoolean(realKeyPreference, true); 
+               } else {
+                       // Saving to permanent storage.
+                       prefs.putBoolean(realKeyPreference, false); 
+               }
+               prefs.commit();
+        }
+
+       /********************* Temperature units  **********************/
+        keyPreference = this.getActivity().getApplicationContext().getString(
                 R.string.widget_preferences_temperature_key);
-        if (key.equals(keyValue)) {
+        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();
-               // What was saved to permanent storage
-            //final String value = sharedPreferences.getString(key, "");
                String humanValue = "";
                if (value.equals(values[0])) {
                        humanValue = humanValues[0];
@@ -97,8 +131,6 @@ public class WidgetPreferences extends PreferenceFragment implements OnSharedPre
                
                
                // Saving to permanent storage.
-               final String keyPreference = this.getActivity().getApplicationContext().getString(
-                    R.string.widget_preferences_temperature_key);
             final String realKeyPreference = keyPreference + "_" + appWidgetId;
             
                final SharedPreferences.Editor prefs =
@@ -111,21 +143,7 @@ public class WidgetPreferences extends PreferenceFragment implements OnSharedPre
         }
         
         
-        // Notification switch
-        keyValue = this.getActivity().getApplicationContext().getString(
-                       R.string.widget_preferences_country_switch_key);
-        if (key.equals(keyValue)) {
-               final SwitchPreference preference = (SwitchPreference) this.findPreference(key);
-               if (preference.isChecked())
-               {
-                       keyValue = this.getActivity().getApplicationContext().getString(
-                               R.string.weather_preferences_update_time_rate_key);
-                       final String value = sharedPreferences.getString(keyValue, "");
-                       this.updateNotification(value);
-               } else {
-                       this.cancelNotification();
-               }
-        }
+
        }
 
     @Override
index 6cd1d74..b5126f6 100644 (file)
@@ -22,7 +22,7 @@ public class WidgetProvider extends AppWidgetProvider {
             int appWidgetId = appWidgetIds[i];
             // To prevent any ANR timeouts, we perform the update in a service
                final Intent intent = new Intent(context.getApplicationContext(), WidgetIntentService.class);
-               intent.putExtra("appWidgetId", appWidgetId);
+               intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                intent.putExtra("updateByApp", false);
             context.startService(intent);
         }
@@ -58,7 +58,7 @@ public class WidgetProvider extends AppWidgetProvider {
         //final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
         
         final Intent intent = new Intent(context.getApplicationContext(), WidgetIntentService.class);
-       intent.putExtra("appWidgetId", appWidgetId);
+       intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
         context.startService(intent);
     }
 }