a16a5b4e821dc09e6d2bebd20054bb96a6e345bb
[AndroidWeatherInformation] / app / src / main / java / name / gumartinm / weather / information / widget / WidgetConfigure.java
1 /**
2  * Copyright 2014 Gustavo Martin Morcuende
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package name.gumartinm.weather.information.widget;
17
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;
29
30 import name.gumartinm.weather.information.R;
31
32 public class WidgetConfigure extends AppCompatActivity {
33     private static final String WIDGET_PREFERENCES_NAME = "WIDGET_PREFERENCES";
34         private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
35
36     @Override
37     public void onCreate(final Bundle savedInstanceState) {
38         super.onCreate(savedInstanceState);
39
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);
45
46         // Find the widget id from the intent. 
47         final Intent intent = getIntent();
48         final Bundle extras = intent.getExtras();
49         boolean isActionFromUser = false;
50
51         if (extras != null) {
52             mAppWidgetId = extras.getInt(
53                     AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
54
55             isActionFromUser = extras.getBoolean("actionFromUser", false);
56         }
57         
58         // If they gave us an intent without the widget id, just bail.
59         if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
60                 this.finish();
61         }
62
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);
67         }
68         
69         // Set the view layout resource to use.
70         this.setContentView(R.layout.appwidget_configure);
71
72         /******************* Show/hide country field *******************/
73         String keyPreference = this.getApplicationContext().getString(
74                 R.string.widget_preferences_country_switch_key);
75         String realKeyPreference = keyPreference + "_" + mAppWidgetId;
76
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);
80
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(){
84             @Override
85             public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
86                 if (isChecked) {
87                     buttonView.setText(WidgetConfigure.this.getString(R.string.widget_preferences_country_switch_on_summary));
88                 } else {
89                     buttonView.setText(WidgetConfigure.this.getString(R.string.widget_preferences_country_switch_off_summary));
90                 }
91             }
92         });
93         if (isShowCountry) {
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));
97         } else {
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));
101         }
102
103         /********************* Temperature units  **********************/
104         keyPreference = this.getApplicationContext().getString(
105                 R.string.widget_preferences_temperature_units_key);
106         realKeyPreference = keyPreference + "_" + mAppWidgetId;
107
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);
110
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);
114
115         /**
116          * android:saveEnabled
117          * Controls whether the saving of this view's state is enabled (that is, whether its onSaveInstanceState() method will be called).
118          *
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.
121          *
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.
124          * But I am tired...
125          */
126     }
127
128     @Override
129     public void onResume() {
130         super.onResume();
131
132         final ActionBar actionBar = this.getSupportActionBar();
133         actionBar.setTitle(this.getString(R.string.widget_preferences_action_settings));
134     }
135
136
137     public void onClickRefresh(final View view) {
138         // Push widget update to surface
139         WidgetProvider.refreshAppWidget(this.getApplicationContext(), mAppWidgetId);
140     }
141
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();
147
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());
155
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());
163
164         /****************** Saving to permanent storage  ***************/
165         prefs.commit();
166
167         // Push widget update to surface with newly set prefix
168         WidgetProvider.updateAppWidget(this.getApplicationContext(), mAppWidgetId);
169
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);
174         finish();
175     }
176
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();
180
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);
186
187         /********************* Temperature units  **********************/
188         keyPreference = context.getApplicationContext().getString(
189                 R.string.widget_preferences_temperature_units_key);
190         realKeyPreference = keyPreference + "_" + appWidgetId;
191         prefs.remove(realKeyPreference);
192
193         /****************** Updating permanent storage  ***************/
194         prefs.commit();
195     }
196 }