<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"
<!-- 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>
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;
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";
@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;
}
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) {
// 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)
// 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)
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);
+// }
}
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 {
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);
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);
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);
package de.example.exampletdd.widget;
+import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
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];
}
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];
// 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 =
}
- // 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
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);
}
//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);
}
}