1 package name.gumartinm.weather.information.widget;
3 import android.app.ActionBar;
4 import android.app.Activity;
5 import android.appwidget.AppWidgetManager;
6 import android.content.Context;
7 import android.content.Intent;
8 import android.content.SharedPreferences;
9 import android.os.Bundle;
10 import android.view.View;
11 import android.widget.CompoundButton;
12 import android.widget.Spinner;
13 import android.widget.Switch;
15 import name.gumartinm.weather.information.R;
17 public class WidgetConfigure extends Activity {
18 private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
21 public void onCreate(final Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);
24 // Find the widget id from the intent.
25 final Intent intent = getIntent();
26 final Bundle extras = intent.getExtras();
27 boolean isActionFromUser = false;
30 mAppWidgetId = extras.getInt(
31 AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
33 isActionFromUser = extras.getBoolean("actionFromUser", false);
36 // If they gave us an intent without the widget id, just bail.
37 if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
41 if (!isActionFromUser) {
42 // Set the result to CANCELED. This will cause the widget host to cancel
43 // out of the widget placement if they press the back button.
44 this.setResult(RESULT_CANCELED);
47 // Set the view layout resource to use.
48 this.setContentView(R.layout.appwidget_configure);
50 /******************* Show/hide country field *******************/
51 String keyPreference = this.getApplicationContext().getString(
52 R.string.widget_preferences_country_switch_key);
53 String realKeyPreference = keyPreference + "_" + mAppWidgetId;
55 // What was saved to permanent storage (or default values if it is the first time)
56 final boolean isShowCountry = this.getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE)
57 .getBoolean(realKeyPreference, false);
59 // What is shown on the screen
60 final Switch countrySwitch = (Switch) this.findViewById(R.id.weather_appwidget_configure_country);
61 countrySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
63 public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
65 buttonView.setText(WidgetConfigure.this.getString(R.string.widget_preferences_country_switch_on_summary));
67 buttonView.setText(WidgetConfigure.this.getString(R.string.widget_preferences_country_switch_off_summary));
72 countrySwitch.setChecked(true);
73 countrySwitch.setText(this.getString(R.string.widget_preferences_country_switch_on_summary));
75 countrySwitch.setChecked(false);
76 countrySwitch.setText(this.getString(R.string.widget_preferences_country_switch_off_summary));
79 /********************* Temperature units **********************/
80 keyPreference = this.getApplicationContext().getString(
81 R.string.widget_preferences_temperature_units_key);
82 realKeyPreference = keyPreference + "_" + mAppWidgetId;
84 // What was saved to permanent storage (or default values if it is the first time)
85 final int tempValue = this.getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE).getInt(realKeyPreference, 0);
87 // What is shown on the screen
88 final Spinner tempUnits = (Spinner) this.findViewById(R.id.weather_appwidget_configure_temperature_units);
89 tempUnits.setSelection(tempValue);
93 * Controls whether the saving of this view's state is enabled (that is, whether its onSaveInstanceState() method will be called).
95 * After onStart the onSaveInstanceState method will be called for every widget, so
96 * I do not need to do anything else to retrieve the UI's state after changing orientation.
98 * I do not know if this is a good pattern, it does not look like that. I guess, I should use
99 * on Resume instead of onCreate/onStart and implement my own onSaveInstanceState method.
105 public void onResume() {
108 final ActionBar actionBar = this.getActionBar();
109 actionBar.setTitle(this.getString(R.string.widget_preferences_action_settings));
113 public void onClickRefresh(final View view) {
114 // Push widget update to surface
115 WidgetProvider.refreshAppWidget(this.getApplicationContext(), mAppWidgetId);
118 public void onClickOk(final View view) {
119 // Save to permanent storage
120 final SharedPreferences.Editor prefs = this.getSharedPreferences(
121 "WIDGET_PREFERENCES",
122 Context.MODE_PRIVATE).edit();
124 /******************* Show/hide country field *******************/
125 // What is shown on the screen
126 final Switch countrySwitch = (Switch) this.findViewById(R.id.weather_appwidget_configure_country);
127 String keyPreference = this.getApplicationContext().getString(
128 R.string.widget_preferences_country_switch_key);
129 String realKeyPreference = keyPreference + "_" + mAppWidgetId;
130 prefs.putBoolean(realKeyPreference, countrySwitch.isChecked());
132 /********************* Temperature units **********************/
133 // What is shown on the screen
134 final Spinner tempUnits = (Spinner) this.findViewById(R.id.weather_appwidget_configure_temperature_units);
135 keyPreference = this.getApplicationContext().getString(
136 R.string.widget_preferences_temperature_units_key);
137 realKeyPreference = keyPreference + "_" + mAppWidgetId;
138 prefs.putInt(realKeyPreference, tempUnits.getSelectedItemPosition());
140 /****************** Saving to permanent storage ***************/
143 // Push widget update to surface with newly set prefix
144 WidgetProvider.updateAppWidget(this.getApplicationContext(), mAppWidgetId);
146 // Make sure we pass back the original appWidgetId
147 final Intent resultValue = new Intent();
148 resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
149 this.setResult(RESULT_OK, resultValue);
153 public static void deletePreference(final Context context, final int appWidgetId) {
154 final SharedPreferences.Editor prefs = context.getApplicationContext()
155 .getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE).edit();
157 /******************* Show/hide country field *******************/
158 String keyPreference = context.getApplicationContext().getString(
159 R.string.widget_preferences_country_switch_key);
160 String realKeyPreference = keyPreference + "_" + appWidgetId;
161 prefs.remove(realKeyPreference);
163 /********************* Temperature units **********************/
164 keyPreference = context.getApplicationContext().getString(
165 R.string.widget_preferences_temperature_units_key);
166 realKeyPreference = keyPreference + "_" + appWidgetId;
167 prefs.remove(realKeyPreference);
169 /****************** Updating permanent storage ***************/