<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>
<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>
<?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>
<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…</string>
--- /dev/null
+<?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>
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>
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";
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);
}
+++ /dev/null
-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();
- }
- }
-}
+++ /dev/null
-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);
- }
-}
--- /dev/null
+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));
+ }
+}
--- /dev/null
+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;
+ }
+ }
+}
--- /dev/null
+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);
+ }
+}