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.appwidget.AppWidgetManager;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.SharedPreferences;
22 import android.os.Bundle;
23 import android.support.v7.app.ActionBar;
24 import android.support.v7.app.AppCompatActivity;
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 AppCompatActivity {
33 private static final String WIDGET_PREFERENCES_NAME = "WIDGET_PREFERENCES";
34 private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
37 public void onCreate(final Bundle savedInstanceState) {
38 super.onCreate(savedInstanceState);
40 final ActionBar actionBar = this.getSupportActionBar();
41 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE);
42 actionBar.setDisplayHomeAsUpEnabled(true);
43 actionBar.setDisplayShowHomeEnabled(true);
44 actionBar.setIcon(R.drawable.ic_launcher);
46 // Find the widget id from the intent.
47 final Intent intent = getIntent();
48 final Bundle extras = intent.getExtras();
49 boolean isActionFromUser = false;
52 mAppWidgetId = extras.getInt(
53 AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
55 isActionFromUser = extras.getBoolean("actionFromUser", false);
58 // If they gave us an intent without the widget id, just bail.
59 if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
63 if (!isActionFromUser) {
64 // Set the result to CANCELED. This will cause the widget host to cancel
65 // out of the widget placement if they press the back button.
66 this.setResult(RESULT_CANCELED);
69 // Set the view layout resource to use.
70 this.setContentView(R.layout.appwidget_configure);
72 /******************* Show/hide country field *******************/
73 String keyPreference = this.getApplicationContext().getString(
74 R.string.widget_preferences_country_switch_key);
75 String realKeyPreference = keyPreference + "_" + mAppWidgetId;
77 // What was saved to permanent storage (or default values if it is the first time)
78 final boolean isShowCountry = this.getSharedPreferences(WIDGET_PREFERENCES_NAME, Context.MODE_PRIVATE)
79 .getBoolean(realKeyPreference, false);
81 // What is shown on the screen
82 final Switch countrySwitch = (Switch) this.findViewById(R.id.weather_appwidget_configure_country);
83 countrySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
85 public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
87 buttonView.setText(WidgetConfigure.this.getString(R.string.widget_preferences_country_switch_on_summary));
89 buttonView.setText(WidgetConfigure.this.getString(R.string.widget_preferences_country_switch_off_summary));
94 // TODO: We must use CheckBox for API 11 -->
95 countrySwitch.setChecked(true);
96 countrySwitch.setText(this.getString(R.string.widget_preferences_country_switch_on_summary));
98 // TODO: We must use CheckBox for API 11 -->
99 countrySwitch.setChecked(false);
100 countrySwitch.setText(this.getString(R.string.widget_preferences_country_switch_off_summary));
103 /********************* Temperature units **********************/
104 keyPreference = this.getApplicationContext().getString(
105 R.string.widget_preferences_temperature_units_key);
106 realKeyPreference = keyPreference + "_" + mAppWidgetId;
108 // What was saved to permanent storage (or default values if it is the first time)
109 final int tempValue = this.getSharedPreferences(WIDGET_PREFERENCES_NAME, Context.MODE_PRIVATE).getInt(realKeyPreference, 0);
111 // What is shown on the screen
112 final Spinner tempUnits = (Spinner) this.findViewById(R.id.weather_appwidget_configure_temperature_units);
113 tempUnits.setSelection(tempValue);
116 * android:saveEnabled
117 * Controls whether the saving of this view's state is enabled (that is, whether its onSaveInstanceState() method will be called).
119 * After onStart the onSaveInstanceState method will be called for every widget, so
120 * I do not need to do anything else to retrieve the UI's state after changing orientation.
122 * I do not know if this is a good pattern, it does not look like that. I guess, I should use
123 * on Resume instead of onCreate/onStart and implement my own onSaveInstanceState method.
129 public void onResume() {
132 final ActionBar actionBar = this.getSupportActionBar();
133 actionBar.setTitle(this.getString(R.string.widget_preferences_action_settings));
137 public void onClickRefresh(final View view) {
138 // Push widget update to surface
139 WidgetProvider.refreshAppWidget(this.getApplicationContext(), mAppWidgetId);
142 public void onClickOk(final View view) {
143 // Save to permanent storage
144 final SharedPreferences.Editor prefs = this.getSharedPreferences(
145 WIDGET_PREFERENCES_NAME,
146 Context.MODE_PRIVATE).edit();
148 /******************* Show/hide country field *******************/
149 // What is shown on the screen
150 final Switch countrySwitch = (Switch) this.findViewById(R.id.weather_appwidget_configure_country);
151 String keyPreference = this.getApplicationContext().getString(
152 R.string.widget_preferences_country_switch_key);
153 String realKeyPreference = keyPreference + "_" + mAppWidgetId;
154 prefs.putBoolean(realKeyPreference, countrySwitch.isChecked());
156 /********************* Temperature units **********************/
157 // What is shown on the screen
158 final Spinner tempUnits = (Spinner) this.findViewById(R.id.weather_appwidget_configure_temperature_units);
159 keyPreference = this.getApplicationContext().getString(
160 R.string.widget_preferences_temperature_units_key);
161 realKeyPreference = keyPreference + "_" + mAppWidgetId;
162 prefs.putInt(realKeyPreference, tempUnits.getSelectedItemPosition());
164 /****************** Saving to permanent storage ***************/
167 // Push widget update to surface with newly set prefix
168 WidgetProvider.updateAppWidget(this.getApplicationContext(), mAppWidgetId);
170 // Make sure we pass back the original appWidgetId
171 final Intent resultValue = new Intent();
172 resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
173 this.setResult(RESULT_OK, resultValue);
177 public static void deletePreference(final Context context, final int appWidgetId) {
178 final SharedPreferences.Editor prefs = context.getApplicationContext()
179 .getSharedPreferences(WIDGET_PREFERENCES_NAME, Context.MODE_PRIVATE).edit();
181 /******************* Show/hide country field *******************/
182 String keyPreference = context.getApplicationContext().getString(
183 R.string.widget_preferences_country_switch_key);
184 String realKeyPreference = keyPreference + "_" + appWidgetId;
185 prefs.remove(realKeyPreference);
187 /********************* Temperature units **********************/
188 keyPreference = context.getApplicationContext().getString(
189 R.string.widget_preferences_temperature_units_key);
190 realKeyPreference = keyPreference + "_" + appWidgetId;
191 prefs.remove(realKeyPreference);
193 /****************** Updating permanent storage ***************/