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