WeatherInformation: widget configure save to permanent storage
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 20 Oct 2014 01:01:52 +0000 (03:01 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 20 Oct 2014 01:01:52 +0000 (03:01 +0200)
app/src/main/AndroidManifest.xml
app/src/main/java/de/example/exampletdd/WidgetIntentService.java
app/src/main/java/de/example/exampletdd/widget/WidgetConfigure.java
app/src/main/java/de/example/exampletdd/widget/WidgetPreferences.java

index 55771d1..342ce75 100644 (file)
@@ -80,7 +80,8 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
-        <activity android:name="de.example.exampletdd.widget.WidgetConfigure">
+        <activity android:name="de.example.exampletdd.widget.WidgetConfigure"
+            android:excludeFromRecents="true">
             <intent-filter>
                 <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
             </intent-filter>
index ed38545..fde23b6 100644 (file)
@@ -244,21 +244,28 @@ public class WidgetIntentService extends IntentService {
                final Intent resultIntent =  new Intent(this.getApplicationContext(), WidgetConfigure.class);
                resultIntent.putExtra("actionFromUser", true);
                resultIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
+        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK );
                // 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)
-               stackBuilder.addParentStack(WidgetConfigure.class);
-               // Adds the Intent that starts the Activity to the top of the stack
-               stackBuilder.addNextIntent(resultIntent);
-               final PendingIntent resultPendingIntent =
-                               stackBuilder.getPendingIntent(
-                                               0,
-                                               PendingIntent.FLAG_UPDATE_CURRENT
-                                               );
-               remoteView.setOnClickPendingIntent(R.id.weather_appwidget, resultPendingIntent);
+//             final TaskStackBuilder stackBuilder = TaskStackBuilder.create(this.getApplicationContext());
+//             // Adds the back stack for the Intent (but not the Intent itself)
+//             stackBuilder.addParentStack(WidgetConfigure.class);
+//             // Adds the Intent that starts the Activity to the top of the stack
+//             stackBuilder.addNextIntent(resultIntent);
+//             final PendingIntent resultPendingIntent =
+//                             stackBuilder.getPendingIntent(
+//                                             0,
+//                                             PendingIntent.FLAG_UPDATE_CURRENT
+//                                             );
+//             remoteView.setOnClickPendingIntent(R.id.weather_appwidget, resultPendingIntent);
+        final PendingIntent resultPendingIntent = PendingIntent.getActivity(
+                this.getApplicationContext(),
+                0,
+                resultIntent,
+                PendingIntent.FLAG_UPDATE_CURRENT);
+        remoteView.setOnClickPendingIntent(R.id.weather_appwidget_error, resultPendingIntent);
                
                return remoteView;
        }
@@ -270,20 +277,26 @@ public class WidgetIntentService extends IntentService {
                final Intent resultIntent =  new Intent(this.getApplicationContext(), WidgetConfigure.class);
                resultIntent.putExtra("actionFromUser", true);
                resultIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
+        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK );
                // 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)
-               stackBuilder.addParentStack(WidgetConfigure.class);
-               // Adds the Intent that starts the Activity to the top of the stack
-               stackBuilder.addNextIntent(resultIntent);
-               final PendingIntent resultPendingIntent =
-                               stackBuilder.getPendingIntent(
-                                               0,
-                                               PendingIntent.FLAG_UPDATE_CURRENT
-                                               );
+//             final TaskStackBuilder stackBuilder = TaskStackBuilder.create(this.getApplicationContext());
+//             // Adds the back stack for the Intent (but not the Intent itself)
+//             stackBuilder.addParentStack(WidgetConfigure.class);
+//             // Adds the Intent that starts the Activity to the top of the stack
+//             stackBuilder.addNextIntent(resultIntent);
+//             final PendingIntent resultPendingIntent =
+//                             stackBuilder.getPendingIntent(
+//                                             0,
+//                                             PendingIntent.FLAG_UPDATE_CURRENT
+//                                             );
+        final PendingIntent resultPendingIntent = PendingIntent.getActivity(
+                this.getApplicationContext(),
+                0,
+                resultIntent,
+                PendingIntent.FLAG_UPDATE_CURRENT);
                remoteView.setOnClickPendingIntent(R.id.weather_appwidget_error, resultPendingIntent);
 
                return remoteView;
index bb5d8ca..878c1d3 100644 (file)
@@ -11,28 +11,8 @@ import de.example.exampletdd.R;
 
 public class WidgetConfigure extends Activity {
        private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
-       
-    final View.OnClickListener mOnClickListener = new View.OnClickListener() {
-        public void onClick(View v) {
-
-               
-            // When the button is clicked, save the string in our prefs and return that they
-            // clicked OK.
-            // Push widget update to surface with newly set prefix
-            final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(
-                       WidgetConfigure.this.getApplicationContext());
-            WidgetProvider.updateAppWidget(
-                       WidgetConfigure.this.getApplicationContext(),
-                       appWidgetManager,
-                    mAppWidgetId);
-
-            // 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();
-        }
-    };
+    private WidgetPreferences mPreferences;
+    private View.OnClickListener mOnClickListener;
 
     @Override
     public void onCreate(final Bundle icicle) {
@@ -46,7 +26,7 @@ public class WidgetConfigure extends Activity {
         if (extras != null) {
             mAppWidgetId = extras.getInt(
                     AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
-            
+
             isActionFromUser = extras.getBoolean("actionFromUser", false);
         }
         
@@ -54,7 +34,7 @@ public class WidgetConfigure extends Activity {
        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
                this.finish();
        }
-       
+
         if (!isActionFromUser) {
             // Set the result to CANCELED.  This will cause the widget host to cancel
             // out of the widget placement if they press the back button.
@@ -66,14 +46,37 @@ public class WidgetConfigure extends Activity {
         
        final Bundle args = new Bundle();
        args.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
-       final Fragment preferences = new WidgetPreferences();
-        preferences.setRetainInstance(true);
-       preferences.setArguments(args);
+        mPreferences = new WidgetPreferences();
+        // 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!!!)
+        mPreferences.setRetainInstance(false);
+        mPreferences.setArguments(args);
         this.getFragmentManager()
         .beginTransaction()
-        .replace(R.id.weather_appwidget_configure_preferences, preferences)
+        .replace(R.id.weather_appwidget_configure_preferences, mPreferences)
         .commit();
-        
+
+        mOnClickListener = new View.OnClickListener() {
+            public void onClick(View v) {
+
+                // Save to permanent storage
+                mPreferences.onSavePreferences();
+
+                // Push widget update to surface with newly set prefix
+                final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(
+                        WidgetConfigure.this.getApplicationContext());
+                WidgetProvider.updateAppWidget(
+                        WidgetConfigure.this.getApplicationContext(),
+                        appWidgetManager,
+                        mAppWidgetId);
+
+                // 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);
     }
index 7744ea8..29b6ed9 100644 (file)
@@ -18,17 +18,20 @@ import de.example.exampletdd.R;
  * without pain?
  */
 public class WidgetPreferences extends PreferenceFragment implements OnSharedPreferenceChangeListener {
-       private int appWidgetId;
+    private int mAppWidgetId;
+    private boolean mIsCountry;
+    private String mTempUnits;
        
     @Override
     public void onCreate(final Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
-        // Retain this fragment across configuration changes.
-       this.setRetainInstance(true);
+        // 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();
-       appWidgetId = bundle.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
+       mAppWidgetId = bundle.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        
         // Load the preferences from an XML resource
         this.addPreferencesFromResource(R.xml.appwidget_preferences);
@@ -37,7 +40,7 @@ public class WidgetPreferences extends PreferenceFragment implements OnSharedPre
         /******************* Show/hide country field *******************/
         String keyPreference = this.getActivity().getApplicationContext().getString(
                 R.string.widget_preferences_country_switch_key);
-        String realKeyPreference = keyPreference + "_" + appWidgetId;
+        String realKeyPreference = keyPreference + "_" + mAppWidgetId;
         
         // 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)
@@ -53,7 +56,7 @@ public class WidgetPreferences extends PreferenceFragment implements OnSharedPre
 
         keyPreference = this.getActivity().getApplicationContext().getString(
                 R.string.widget_preferences_temperature_key);
-        realKeyPreference = keyPreference + "_" + appWidgetId;
+        realKeyPreference = keyPreference + "_" + mAppWidgetId;
 
 
         // What was saved to permanent storage (or default values if it is the first time)
@@ -81,70 +84,49 @@ public class WidgetPreferences extends PreferenceFragment implements OnSharedPre
     }
 
     @Override
-       public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
+    public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
 
-       /******************* Show/hide country field *******************/
+        /******************* Show/hide country field *******************/
         String keyPreference = this.getActivity().getApplicationContext().getString(
-                       R.string.widget_preferences_country_switch_key);
+                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();
+            // What is shown on the screen
+            final SwitchPreference preference = (SwitchPreference) this.findPreference(key);
+            // Update temporal value
+            mIsCountry = preference.isChecked();
+
+            return;
         }
 
-       /********************* Temperature units  **********************/
+        /********************* 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);
-               
-               
-               // Saving to permanent storage.
-            final String realKeyPreference = keyPreference + "_" + appWidgetId;
-            
-               final SharedPreferences.Editor prefs =
-                               this.getActivity().getSharedPreferences(
-                                               "WIDGET_PREFERENCES", 
-                                               Context.MODE_PRIVATE).edit();
-            prefs.putString(realKeyPreference, value);
-            prefs.commit();
-               return;
+            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() {
@@ -159,7 +141,30 @@ public class WidgetPreferences extends PreferenceFragment implements OnSharedPre
         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);