• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
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 
17 package com.example.android.apis.appwidget;
18 
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.EditText;
27 
28 import java.util.ArrayList;
29 
30 // Need the following import to get access to the app resources, since this
31 // class is in a sub-package.
32 import com.example.android.apis.R;
33 
34 /**
35  * The configuration screen for the ExampleAppWidgetProvider widget sample.
36  */
37 public class ExampleAppWidgetConfigure extends Activity {
38     static final String TAG = "ExampleAppWidgetConfigure";
39 
40     private static final String PREFS_NAME
41             = "com.example.android.apis.appwidget.ExampleAppWidgetProvider";
42     private static final String PREF_PREFIX_KEY = "prefix_";
43 
44     int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
45     EditText mAppWidgetPrefix;
46 
ExampleAppWidgetConfigure()47     public ExampleAppWidgetConfigure() {
48         super();
49     }
50 
51     @Override
onCreate(Bundle icicle)52     public void onCreate(Bundle icicle) {
53         super.onCreate(icicle);
54 
55         // Set the result to CANCELED.  This will cause the widget host to cancel
56         // out of the widget placement if they press the back button.
57         setResult(RESULT_CANCELED);
58 
59         // Set the view layout resource to use.
60         setContentView(R.layout.appwidget_configure);
61 
62         // Find the EditText
63         mAppWidgetPrefix = (EditText)findViewById(R.id.appwidget_prefix);
64 
65         // Bind the action for the save button.
66         findViewById(R.id.save_button).setOnClickListener(mOnClickListener);
67 
68         // Find the widget id from the intent.
69         Intent intent = getIntent();
70         Bundle extras = intent.getExtras();
71         if (extras != null) {
72             mAppWidgetId = extras.getInt(
73                     AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
74         }
75 
76         // If they gave us an intent without the widget id, just bail.
77         if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
78             finish();
79         }
80 
81         mAppWidgetPrefix.setText(loadTitlePref(ExampleAppWidgetConfigure.this, mAppWidgetId));
82     }
83 
84     View.OnClickListener mOnClickListener = new View.OnClickListener() {
85         public void onClick(View v) {
86             final Context context = ExampleAppWidgetConfigure.this;
87 
88             // When the button is clicked, save the string in our prefs and return that they
89             // clicked OK.
90             String titlePrefix = mAppWidgetPrefix.getText().toString();
91             saveTitlePref(context, mAppWidgetId, titlePrefix);
92 
93             // Push widget update to surface with newly set prefix
94             AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
95             ExampleAppWidgetProvider.updateAppWidget(context, appWidgetManager,
96                     mAppWidgetId, titlePrefix);
97 
98             // Make sure we pass back the original appWidgetId
99             Intent resultValue = new Intent();
100             resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
101             setResult(RESULT_OK, resultValue);
102             finish();
103         }
104     };
105 
106     // Write the prefix to the SharedPreferences object for this widget
saveTitlePref(Context context, int appWidgetId, String text)107     static void saveTitlePref(Context context, int appWidgetId, String text) {
108         SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
109         prefs.putString(PREF_PREFIX_KEY + appWidgetId, text);
110         prefs.commit();
111     }
112 
113     // Read the prefix from the SharedPreferences object for this widget.
114     // If there is no preference saved, get the default from a resource
loadTitlePref(Context context, int appWidgetId)115     static String loadTitlePref(Context context, int appWidgetId) {
116         SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
117         String prefix = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
118         if (prefix != null) {
119             return prefix;
120         } else {
121             return context.getString(R.string.appwidget_prefix_default);
122         }
123     }
124 
deleteTitlePref(Context context, int appWidgetId)125     static void deleteTitlePref(Context context, int appWidgetId) {
126     }
127 
loadAllTitlePrefs(Context context, ArrayList<Integer> appWidgetIds, ArrayList<String> texts)128     static void loadAllTitlePrefs(Context context, ArrayList<Integer> appWidgetIds,
129             ArrayList<String> texts) {
130     }
131 }
132 
133 
134 
135