2 * Copyright 2014 Gustavo Martin Morcuende
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package name.gumartinm.weather.information.widget;
18 import android.app.ActionBar;
19 import android.app.Activity;
20 import android.appwidget.AppWidgetManager;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.SharedPreferences;
24 import android.os.Bundle;
25 import android.view.View;
26 import android.widget.CompoundButton;
27 import android.widget.Spinner;
28 import android.widget.Switch;
30 import name.gumartinm.weather.information.R;
32 public class WidgetConfigure extends Activity {
33 private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
36 public void onCreate(final Bundle savedInstanceState) {
37 super.onCreate(savedInstanceState);
39 // Find the widget id from the intent.
40 final Intent intent = getIntent();
41 final Bundle extras = intent.getExtras();
42 boolean isActionFromUser = false;
45 mAppWidgetId = extras.getInt(
46 AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
48 isActionFromUser = extras.getBoolean("actionFromUser", false);
51 // If they gave us an intent without the widget id, just bail.
52 if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
56 if (!isActionFromUser) {
57 // Set the result to CANCELED. This will cause the widget host to cancel
58 // out of the widget placement if they press the back button.
59 this.setResult(RESULT_CANCELED);
62 // Set the view layout resource to use.
63 this.setContentView(R.layout.appwidget_configure);
65 /******************* Show/hide country field *******************/
66 String keyPreference = this.getApplicationContext().getString(
67 R.string.widget_preferences_country_switch_key);
68 String realKeyPreference = keyPreference + "_" + mAppWidgetId;
70 // What was saved to permanent storage (or default values if it is the first time)
71 final boolean isShowCountry = this.getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE)
72 .getBoolean(realKeyPreference, false);
74 // What is shown on the screen
75 final Switch countrySwitch = (Switch) this.findViewById(R.id.weather_appwidget_configure_country);
76 countrySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
78 public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
80 buttonView.setText(WidgetConfigure.this.getString(R.string.widget_preferences_country_switch_on_summary));
82 buttonView.setText(WidgetConfigure.this.getString(R.string.widget_preferences_country_switch_off_summary));
87 countrySwitch.setChecked(true);
88 countrySwitch.setText(this.getString(R.string.widget_preferences_country_switch_on_summary));
90 countrySwitch.setChecked(false);
91 countrySwitch.setText(this.getString(R.string.widget_preferences_country_switch_off_summary));
94 /********************* Temperature units **********************/
95 keyPreference = this.getApplicationContext().getString(
96 R.string.widget_preferences_temperature_units_key);
97 realKeyPreference = keyPreference + "_" + mAppWidgetId;
99 // What was saved to permanent storage (or default values if it is the first time)
100 final int tempValue = this.getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE).getInt(realKeyPreference, 0);
102 // What is shown on the screen
103 final Spinner tempUnits = (Spinner) this.findViewById(R.id.weather_appwidget_configure_temperature_units);
104 tempUnits.setSelection(tempValue);
107 * android:saveEnabled
108 * Controls whether the saving of this view's state is enabled (that is, whether its onSaveInstanceState() method will be called).
110 * After onStart the onSaveInstanceState method will be called for every widget, so
111 * I do not need to do anything else to retrieve the UI's state after changing orientation.
113 * I do not know if this is a good pattern, it does not look like that. I guess, I should use
114 * on Resume instead of onCreate/onStart and implement my own onSaveInstanceState method.
120 public void onResume() {
123 final ActionBar actionBar = this.getActionBar();
124 actionBar.setTitle(this.getString(R.string.widget_preferences_action_settings));
128 public void onClickRefresh(final View view) {
129 // Push widget update to surface
130 WidgetProvider.refreshAppWidget(this.getApplicationContext(), mAppWidgetId);
133 public void onClickOk(final View view) {
134 // Save to permanent storage
135 final SharedPreferences.Editor prefs = this.getSharedPreferences(
136 "WIDGET_PREFERENCES",
137 Context.MODE_PRIVATE).edit();
139 /******************* Show/hide country field *******************/
140 // What is shown on the screen
141 final Switch countrySwitch = (Switch) this.findViewById(R.id.weather_appwidget_configure_country);
142 String keyPreference = this.getApplicationContext().getString(
143 R.string.widget_preferences_country_switch_key);
144 String realKeyPreference = keyPreference + "_" + mAppWidgetId;
145 prefs.putBoolean(realKeyPreference, countrySwitch.isChecked());
147 /********************* Temperature units **********************/
148 // What is shown on the screen
149 final Spinner tempUnits = (Spinner) this.findViewById(R.id.weather_appwidget_configure_temperature_units);
150 keyPreference = this.getApplicationContext().getString(
151 R.string.widget_preferences_temperature_units_key);
152 realKeyPreference = keyPreference + "_" + mAppWidgetId;
153 prefs.putInt(realKeyPreference, tempUnits.getSelectedItemPosition());
155 /****************** Saving to permanent storage ***************/
158 // Push widget update to surface with newly set prefix
159 WidgetProvider.updateAppWidget(this.getApplicationContext(), mAppWidgetId);
161 // Make sure we pass back the original appWidgetId
162 final Intent resultValue = new Intent();
163 resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
164 this.setResult(RESULT_OK, resultValue);
168 public static void deletePreference(final Context context, final int appWidgetId) {
169 final SharedPreferences.Editor prefs = context.getApplicationContext()
170 .getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE).edit();
172 /******************* Show/hide country field *******************/
173 String keyPreference = context.getApplicationContext().getString(
174 R.string.widget_preferences_country_switch_key);
175 String realKeyPreference = keyPreference + "_" + appWidgetId;
176 prefs.remove(realKeyPreference);
178 /********************* Temperature units **********************/
179 keyPreference = context.getApplicationContext().getString(
180 R.string.widget_preferences_temperature_units_key);
181 realKeyPreference = keyPreference + "_" + appWidgetId;
182 prefs.remove(realKeyPreference);
184 /****************** Updating permanent storage ***************/