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