WeatherInformation: widget preferences WIP
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Thu, 9 Oct 2014 19:35:22 +0000 (21:35 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Thu, 9 Oct 2014 19:35:22 +0000 (21:35 +0200)
AndroidManifest.xml
res/layout/appwidget_configure.xml
res/values/strings.xml
res/xml/appwidget_preferences.xml [new file with mode: 0644]
res/xml/appwidget_provider.xml
src/de/example/exampletdd/WidgetIntentService.java
src/de/example/exampletdd/widget/WeatherInformationWidgetConfigure.java [deleted file]
src/de/example/exampletdd/widget/WeatherInformationWidgetProvider.java [deleted file]
src/de/example/exampletdd/widget/WidgetConfigure.java [new file with mode: 0644]
src/de/example/exampletdd/widget/WidgetPreferences.java [new file with mode: 0644]
src/de/example/exampletdd/widget/WidgetProvider.java [new file with mode: 0644]

index e49f556..a626e9b 100644 (file)
@@ -77,7 +77,7 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
-        <activity android:name="de.example.exampletdd.widget.WeatherInformationWidgetConfigure">
+        <activity android:name="de.example.exampletdd.widget.WidgetConfigure">
             <intent-filter>
                 <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
             </intent-filter>
@@ -89,7 +89,7 @@
                 <action android:name="android.intent.action.BOOT_COMPLETED"></action>
             </intent-filter>
         </receiver>
-        <receiver android:name="de.example.exampletdd.widget.WeatherInformationWidgetProvider" >
+        <receiver android:name="de.example.exampletdd.widget.WidgetProvider" >
             <intent-filter>
                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
             </intent-filter>
index 7a0764b..66463eb 100644 (file)
@@ -1,19 +1,36 @@
 <?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
-    android:layout_height="wrap_content"
-    android:orientation="vertical">
+    android:layout_height="match_parent"
+    android:layout_gravity="fill_vertical|fill_horizontal" >
 
-    <TextView
+    <LinearLayout android:id="@+id/weather_appwidget_configure_preferences"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:text="Acitivity configure, WIP."/>
+        android:layout_alignParentTop="true"
+        android:layout_gravity="center"
+        android:orientation="horizontal" >
 
-    <Button
-        android:id="@+id/weather_appwidget_configure_save_button"
+    </LinearLayout>
+
+    <LinearLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:text="@android:string/ok"/>
+        android:layout_alignParentBottom="true"
+        android:layout_centerHorizontal="true"
+        android:layout_gravity="center"
+        android:gravity="center"
+        android:baselineAligned="false"
+        android:orientation="horizontal" >
+        
+        <Button
+            android:id="@+id/weather_appwidget_configure_save_button"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_gravity="center"
+            android:textAlignment="center"
+            android:text="@android:string/ok" />
+    </LinearLayout>
 
-</LinearLayout>
+</RelativeLayout>
 
index 4214cd3..76158fa 100644 (file)
     <string name="weather_preferences_wind_key">weather_preferences_wind</string>
     <string name="weather_preferences_wind">Wind</string>
     <string name="weather_preferences_wind_summary">meter per second</string>
+    <string name="widget_preferences_units">Units</string>
+    <string name="widget_preferences_units_key">widget_preferences_units</string>
+    <string name="widget_preferences_refresh_interval_key">widget_preferences_refresh_interval</string>
+    <string name="widget_preferences_refresh_interval">Refresh interval</string>
     <string name="city_not_found">city not found</string>
     <string name="country_not_found">country not found</string>
     <string name="progress_dialog_generic_message">Please wait&#8230;</string>
diff --git a/res/xml/appwidget_preferences.xml b/res/xml/appwidget_preferences.xml
new file mode 100644 (file)
index 0000000..1445bae
--- /dev/null
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
+        <ListPreference android:key="@string/widget_preferences_refresh_interval_key"
+        android:title="@string/widget_preferences_refresh_interval"
+        android:entries="@array/weather_preferences_refresh_interval_human_value"
+        android:entryValues="@array/weather_preferences_refresh_interval"
+        android:selectable="true"
+        android:persistent="true"
+        android:defaultValue="300000"
+        android:summary="five minutes" />
+    <PreferenceCategory android:title="@string/widget_preferences_units">
+        <ListPreference android:key="@string/widget_preferences_units_key"
+            android:title="@string/weather_preferences_temperature_units"
+            android:summary="celsius"
+            android:entries="@array/weather_preferences_units_human_value"
+            android:entryValues="@array/weather_preferences_units_value"
+            android:selectable="true"
+            android:persistent="true"
+            android:defaultValue="ÂșC" />
+    </PreferenceCategory>
+
+</PreferenceScreen>
index c710a99..155242e 100644 (file)
@@ -7,7 +7,7 @@
     android:resizeMode="none"
     android:previewImage="@drawable/ic_launcher"
     android:initialLayout="@layout/appwidget_error"
-    android:configure="de.example.exampletdd.widget.WeatherInformationWidgetConfigure" 
+    android:configure="de.example.exampletdd.widget.WidgetConfigure" 
     android:widgetCategory="home_screen|keyguard"
     android:updatePeriodMillis="3600000">
 </appwidget-provider>
index ae164ba..75ed893 100644 (file)
@@ -35,7 +35,7 @@ import de.example.exampletdd.parser.JPOSWeatherParser;
 import de.example.exampletdd.service.IconsList;
 import de.example.exampletdd.service.PermanentStorage;
 import de.example.exampletdd.service.ServiceParser;
-import de.example.exampletdd.widget.WeatherInformationWidgetProvider;
+import de.example.exampletdd.widget.WidgetProvider;
 
 public class WidgetIntentService extends IntentService {
        private static final String TAG = "WidgetIntentService";
@@ -290,7 +290,7 @@ public class WidgetIntentService extends IntentService {
        
        private void updateWidgets(final RemoteViews remoteView) {
                
-               final ComponentName widgets = new ComponentName(this.getApplicationContext(), WeatherInformationWidgetProvider.class);
+               final ComponentName widgets = new ComponentName(this.getApplicationContext(), WidgetProvider.class);
                final AppWidgetManager manager = AppWidgetManager.getInstance(this.getApplicationContext());
                manager.updateAppWidget(widgets, remoteView);
        }
diff --git a/src/de/example/exampletdd/widget/WeatherInformationWidgetConfigure.java b/src/de/example/exampletdd/widget/WeatherInformationWidgetConfigure.java
deleted file mode 100644 (file)
index a38c2bf..0000000
+++ /dev/null
@@ -1,61 +0,0 @@
-package de.example.exampletdd.widget;
-
-import android.app.Activity;
-import android.appwidget.AppWidgetManager;
-import android.content.Intent;
-import android.os.Bundle;
-import android.view.View;
-import de.example.exampletdd.R;
-
-public class WeatherInformationWidgetConfigure 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(
-                       WeatherInformationWidgetConfigure.this.getApplicationContext());
-            WeatherInformationWidgetProvider.updateAppWidget(
-                       WeatherInformationWidgetConfigure.this.getApplicationContext(),
-                       appWidgetManager,
-                    mAppWidgetId);
-
-            // Make sure we pass back the original appWidgetId
-            final Intent resultValue = new Intent();
-            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
-            WeatherInformationWidgetConfigure.this.setResult(RESULT_OK, resultValue);
-            finish();
-        }
-    };
-    @Override
-    public void onCreate(final Bundle icicle) {
-        super.onCreate(icicle);
-     
-        // 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);
-
-        // Set the view layout resource to use.
-        this.setContentView(R.layout.appwidget_configure);
-        
-        // Bind the action for the save button.
-        this.findViewById(R.id.weather_appwidget_configure_save_button).setOnClickListener(mOnClickListener);
-        
-        // Find the widget id from the intent. 
-        final Intent intent = getIntent();
-        final Bundle extras = intent.getExtras();
-        if (extras != null) {
-            mAppWidgetId = extras.getInt(
-                    AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
-        }
-        
-        // If they gave us an intent without the widget id, just bail.
-        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
-               this.finish();
-        }
-    }
-}
diff --git a/src/de/example/exampletdd/widget/WeatherInformationWidgetProvider.java b/src/de/example/exampletdd/widget/WeatherInformationWidgetProvider.java
deleted file mode 100644 (file)
index 9f9ef7d..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-package de.example.exampletdd.widget;
-
-
-import android.appwidget.AppWidgetManager;
-import android.appwidget.AppWidgetProvider;
-import android.appwidget.AppWidgetProviderInfo;
-import android.content.Context;
-import android.content.Intent;
-import android.os.Bundle;
-import de.example.exampletdd.WidgetIntentService;
-
-public class WeatherInformationWidgetProvider extends AppWidgetProvider {
-
-    @Override
-    public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
-        // For each widget that needs an update, get the text that we should display:
-        //   - Create a RemoteViews object for it
-        //   - Set the text in the RemoteViews object
-        //   - Tell the AppWidgetManager to show that views object for the widget.
-        final int N = appWidgetIds.length;
-        for (int i=0; i<N; i++) {
-            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("updateByApp", false);
-            context.startService(intent);
-        }
-    }
-    
-    @Override
-    public void onDeleted(Context context, int[] appWidgetIds) {
-        // When the user deletes the widget, delete the preference associated with it.
-        final int N = appWidgetIds.length;
-        for (int i=0; i<N; i++) {
-            //ExampleAppWidgetConfigure.deleteTitlePref(context, appWidgetIds[i]);
-        }
-    }
-    
-    static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId) {
-
-        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("appWidgetId", appWidgetId);
-        context.startService(intent);
-    }
-}
diff --git a/src/de/example/exampletdd/widget/WidgetConfigure.java b/src/de/example/exampletdd/widget/WidgetConfigure.java
new file mode 100644 (file)
index 0000000..25ee849
--- /dev/null
@@ -0,0 +1,84 @@
+package de.example.exampletdd.widget;
+
+import android.app.ActionBar;
+import android.app.Activity;
+import android.app.Fragment;
+import android.appwidget.AppWidgetManager;
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.View;
+import de.example.exampletdd.R;
+import de.example.exampletdd.fragment.map.MapProgressFragment;
+
+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();
+        }
+    };
+
+    @Override
+    public void onCreate(final Bundle icicle) {
+        super.onCreate(icicle);
+     
+        // 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);
+
+        // Find the widget id from the intent. 
+        final Intent intent = getIntent();
+        final Bundle extras = intent.getExtras();
+        if (extras != null) {
+            mAppWidgetId = extras.getInt(
+                    AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
+        }
+        
+        // Set the view layout resource to use.
+        this.setContentView(R.layout.appwidget_configure);
+        
+       final Bundle args = new Bundle();
+       args.putInt("appWidgetId", mAppWidgetId);
+       final Fragment preferences = new WidgetPreferences();
+        preferences.setRetainInstance(true);
+       preferences.setArguments(args);
+        this.getFragmentManager()
+        .beginTransaction()
+        .replace(R.id.weather_appwidget_configure_preferences, preferences)
+        .commit();
+        
+        // Bind the action for the save button.
+        this.findViewById(R.id.weather_appwidget_configure_save_button).setOnClickListener(mOnClickListener);
+
+   
+        // If they gave us an intent without the widget id, just bail.
+        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
+               this.finish();
+        }
+    }
+    
+    @Override
+    public void onResume() {
+        super.onResume();
+
+        final ActionBar actionBar = this.getActionBar();
+        actionBar.setTitle(this.getString(R.string.weather_preferences_actionbar_title));
+    }
+}
diff --git a/src/de/example/exampletdd/widget/WidgetPreferences.java b/src/de/example/exampletdd/widget/WidgetPreferences.java
new file mode 100644 (file)
index 0000000..1fc7202
--- /dev/null
@@ -0,0 +1,121 @@
+package de.example.exampletdd.widget;
+
+import de.example.exampletdd.R;
+import android.content.SharedPreferences;
+import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
+import android.os.Bundle;
+import android.preference.Preference;
+import android.preference.PreferenceFragment;
+
+public class WidgetPreferences extends PreferenceFragment {
+
+    @Override
+    public void onCreate(final Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+     // Retain this fragment across configuration changes.
+       this.setRetainInstance(true);
+       
+       final Bundle bundle = this.getArguments();
+       int appWidgetId = bundle.getInt("appWidgetId");
+       
+        // Load the preferences from an XML resource
+        this.addPreferencesFromResource(R.xml.appwidget_preferences);
+        
+        // Temperature units
+        String[] values = this.getResources().getStringArray(R.array.weather_preferences_units_value);
+        String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_units_human_value);
+        String keyPreference = this.getActivity().getApplicationContext().getString(
+                R.string.weather_preferences_units_key);
+        Preference connectionPref = this.findPreference(keyPreference);
+        String value = this.getPreferenceManager().getSharedPreferences()
+                .getString(keyPreference, "");
+        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];
+        }
+        connectionPref.setSummary(humanValue);
+        
+        
+        // Refresh interval
+        values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval);
+        humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value);
+        keyPreference = this.getActivity().getApplicationContext().getString(
+                R.string.weather_preferences_refresh_interval_key);
+        connectionPref = this.findPreference(keyPreference);
+        value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, "");
+        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];
+        } else if (value.equals(values[3])) {
+            humanValue = humanValues[3];
+        } else if (value.equals(values[4])) {
+            humanValue = humanValues[4];
+        } else if (value.equals(values[5])) {
+            humanValue = humanValues[5];
+        } else if (value.equals(values[6])) {
+            humanValue = humanValues[6];
+        }
+        connectionPref.setSummary(humanValue);
+    }
+
+       public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
+               
+       // Temperature units
+       String[] values = this.getResources().getStringArray(R.array.weather_preferences_units_value);
+       String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_units_human_value);
+        String keyValue = this.getActivity().getApplicationContext().getString(
+                R.string.weather_preferences_units_key);
+        if (key.equals(keyValue)) {
+               final Preference connectionPref = this.findPreference(key);
+            final String value = sharedPreferences.getString(key, "");
+               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];
+               }
+
+               connectionPref.setSummary(humanValue);
+               return;
+        }
+               
+        // Refresh interval
+        values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval);
+        humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value);
+        keyValue = this.getActivity().getApplicationContext().getString(
+                R.string.weather_preferences_refresh_interval_key);
+        if (key.equals(keyValue)) {
+               final Preference connectionPref = this.findPreference(key);
+            final String value = sharedPreferences.getString(key, "");
+            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];
+            } else if (value.equals(values[3])) {
+                humanValue = humanValues[3];
+            } else if (value.equals(values[4])) {
+                humanValue = humanValues[4];
+            } else if (value.equals(values[5])) {
+                humanValue = humanValues[5];
+            } else if (value.equals(values[6])) {
+                humanValue = humanValues[6];
+            }
+            connectionPref.setSummary(humanValue);
+            return;
+        }
+       }
+}
diff --git a/src/de/example/exampletdd/widget/WidgetProvider.java b/src/de/example/exampletdd/widget/WidgetProvider.java
new file mode 100644 (file)
index 0000000..ca92d7b
--- /dev/null
@@ -0,0 +1,64 @@
+package de.example.exampletdd.widget;
+
+
+import android.appwidget.AppWidgetManager;
+import android.appwidget.AppWidgetProvider;
+import android.appwidget.AppWidgetProviderInfo;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import de.example.exampletdd.WidgetIntentService;
+
+public class WidgetProvider extends AppWidgetProvider {
+
+    @Override
+    public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
+        // For each widget that needs an update, get the text that we should display:
+        //   - Create a RemoteViews object for it
+        //   - Set the text in the RemoteViews object
+        //   - Tell the AppWidgetManager to show that views object for the widget.
+        final int N = appWidgetIds.length;
+        for (int i=0; i<N; i++) {
+            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("updateByApp", false);
+            context.startService(intent);
+        }
+    }
+    
+    @Override
+    public void onDeleted(Context context, int[] appWidgetIds) {
+        // When the user deletes the widget, delete the preference associated with it.
+        final int N = appWidgetIds.length;
+        for (int i=0; i<N; i++) {
+            //ExampleAppWidgetConfigure.deleteTitlePref(context, appWidgetIds[i]);
+        }
+    }
+    
+    static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId) {
+
+        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("appWidgetId", appWidgetId);
+        context.startService(intent);
+    }
+}