Debian Codegraph @

templates/other/AppWidget/root/src/app_package/AppWidgetConfigureActivity.java.ftl

raw
1 package ${packageName};
2
3 import android.app.Activity;
4 import android.appwidget.AppWidgetManager;
5 import android.content.Context;
6 import android.content.Intent;
7 import android.content.SharedPreferences;
8 import android.os.Bundle;
9 import android.view.View;
10 import android.widget.EditText;
11 <#if applicationPackage??>
12 import ${applicationPackage}.R;
13 </#if>
14
15 /**
16 * The configuration screen for the {@link ${className} ${className}} AppWidget.
17 */
18 public class ${className}ConfigureActivity extends Activity {
19
20 int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
21 EditText mAppWidgetText;
22 private static final String PREFS_NAME = "${packageName}.${className}";
23 private static final String PREF_PREFIX_KEY = "appwidget_";
24
25 public ${className}ConfigureActivity() {
26 super();
27 }
28
29 @Override
30 public void onCreate(Bundle icicle) {
31 super.onCreate(icicle);
32
33 // Set the result to CANCELED. This will cause the widget host to cancel
34 // out of the widget placement if the user presses the back button.
35 setResult(RESULT_CANCELED);
36
37 setContentView(R.layout.${class_name}_configure);
38 mAppWidgetText = (EditText)findViewById(R.id.appwidget_text);
39 findViewById(R.id.add_button).setOnClickListener(mOnClickListener);
40
41 // Find the widget id from the intent.
42 Intent intent = getIntent();
43 Bundle extras = intent.getExtras();
44 if (extras != null) {
45 mAppWidgetId = extras.getInt(
46 AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
47 }
48
49 // If this activity was started with an intent without an app widget ID, finish with an error.
50 if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
51 finish();
52 return;
53 }
54
55 mAppWidgetText.setText(loadTitlePref(${className}ConfigureActivity.this, mAppWidgetId));
56 }
57
58 View.OnClickListener mOnClickListener = new View.OnClickListener() {
59 public void onClick(View v) {
60 final Context context = ${className}ConfigureActivity.this;
61
62 // When the button is clicked, store the string locally
63 String widgetText = mAppWidgetText.getText().toString();
64 saveTitlePref(context,mAppWidgetId,widgetText);
65
66 // It is the responsibility of the configuration activity to update the app widget
67 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
68 ${className}.updateAppWidget(context, appWidgetManager, mAppWidgetId);
69
70 // Make sure we pass back the original appWidgetId
71 Intent resultValue = new Intent();
72 resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
73 setResult(RESULT_OK, resultValue);
74 finish();
75 }
76 };
77
78 // Write the prefix to the SharedPreferences object for this widget
79 static void saveTitlePref(Context context, int appWidgetId, String text) {
80 SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
81 prefs.putString(PREF_PREFIX_KEY + appWidgetId, text);
82 prefs.apply();
83 }
84
85 // Read the prefix from the SharedPreferences object for this widget.
86 // If there is no preference saved, get the default from a resource
87 static String loadTitlePref(Context context, int appWidgetId) {
88 SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
89 String titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
90 if (titleValue != null) {
91 return titleValue;
92 } else {
93 return context.getString(R.string.appwidget_text);
94 }
95 }
96
97 static void deleteTitlePref(Context context, int appWidgetId) {
98 SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
99 prefs.remove(PREF_PREFIX_KEY + appWidgetId);
100 prefs.apply();
101 }
102 }
103