" : info.getLocale();
+
+ AlertDialog.Builder b = new AlertDialog.Builder(this);
+ b.setMessage(getString(R.string.confirm_remove_locale_1s, localeToRemove));
+ b.setCancelable(false);
+ b.setPositiveButton(R.string.confirm_remove_locale_yes,
+ new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ removeCustomLocale(localeToRemove);
+ dismissDialog(DLG_REMOVE_ID);
+ }
+ });
+ b.setNegativeButton(R.string.confirm_remove_locale_no,
+ new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ dismissDialog(DLG_REMOVE_ID);
+ }
+ });
+
+ return b.create();
+ }
+
+ private void removeCustomLocale(String localeToRemove) {
+ // Get current custom locale list
+ String oldLocales = mPrefs.getString(CUSTOM_LOCALES, "");
+
+ if (DEBUG) {
+ Log.d(TAG, "Remove " + localeToRemove + " from custom locales: " + oldLocales);
+ }
+
+ // Update
+ StringBuilder sb = new StringBuilder();
+ for (String locale : oldLocales.split(CUSTOM_LOCALES_SEP)) {
+ if (locale != null && locale.length() > 0 && !locale.equals(localeToRemove)) {
+ if (sb.length() > 0) {
+ sb.append(CUSTOM_LOCALES_SEP);
+ }
+ sb.append(locale);
+ }
+ }
+
+ String newLocales = sb.toString();
+ if (!newLocales.equals(oldLocales)) {
+ // Save prefs
+ boolean ok = mPrefs.edit().putString(CUSTOM_LOCALES, newLocales).commit();
+ if (DEBUG) {
+ Log.d(TAG, "Prefs commit:" + Boolean.toString(ok) + ". Saved: " + newLocales);
+ }
+
+ Toast.makeText(this,
+ getString(R.string.removed_custom_locale_1s, localeToRemove),
+ Toast.LENGTH_SHORT)
+ .show();
+
+ // Update list view
+ setupLocaleList();
+ }
+ }
+
+ /**
+ * Immutable structure that holds the information displayed by a list view item.
+ */
+ private static class LocaleInfo {
+ private final String mLocale;
+ private final String mDisplayName;
+ private final boolean mIsCustom;
+
+ public LocaleInfo(String locale, String displayName, boolean isCustom) {
+ mLocale = locale;
+ mDisplayName = displayName;
+ mIsCustom = isCustom;
+ }
+
+ public LocaleInfo(String locale, String displayName) {
+ this(locale, displayName, false /*custom*/);
+ }
+
+ public String getLocale() {
+ return mLocale;
+ }
+
+ public boolean isCustom() {
+ return mIsCustom;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ sb.append(mLocale)
+ .append(" - ")
+ .append(mDisplayName);
+ if (mIsCustom) {
+ sb.append(" [Custom]");
+ }
+ return sb.toString();
+ }
+ }
+}
diff --git a/apps/CustomLocale/src/com/android/customlocale2/CustomLocaleReceiver.java b/apps/CustomLocale/src/com/android/customlocale2/CustomLocaleReceiver.java
new file mode 100755
index 000000000..30d886e1e
--- /dev/null
+++ b/apps/CustomLocale/src/com/android/customlocale2/CustomLocaleReceiver.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.customlocale2;
+
+import android.app.Activity;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+//-----------------------------------------------
+
+/**
+ * Broadcast receiver that can change the system's locale.
+ *
+ * This allows an external script such as an automated testing framework
+ * to easily trigger a locale change on an emulator as such:
+ *
+ * $ adb shell am broadcast -a com.android.intent.action.SET_LOCALE \
+ * --es com.android.intent.extra.LOCALE en_US
+ *
+ */
+public class CustomLocaleReceiver extends BroadcastReceiver {
+
+ private static final String TAG = CustomLocaleReceiver.class.getSimpleName();
+ private static final boolean DEBUG = true;
+
+ /** Intent action that triggers this receiver. */
+ public static final String ACTION_SET_LOCALE = "com.android.intent.action.SET_LOCALE";
+ /** An extra String that specifies the locale to set, in the form "en_US". */
+ public static final String EXTRA_LOCALE = "com.android.intent.extra.LOCALE";
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ setResult(Activity.RESULT_CANCELED, null, null);
+ if (intent == null || ! ACTION_SET_LOCALE.equals(intent.getAction())) {
+ if (DEBUG) {
+ Log.d(TAG, "Invalid intent: " + (intent == null ? "null" : intent.toString()));
+ }
+ return;
+ }
+
+ String locale = intent.getStringExtra(EXTRA_LOCALE);
+
+ // Enforce the locale string is either in the form "ab" or "ab_cd"
+ boolean is_ok = locale != null;
+ is_ok = is_ok && (locale.length() == 2 || locale.length() == 5);
+ if (is_ok && locale.length() >= 2) {
+ is_ok = Character.isLetter(locale.charAt(0)) &&
+ Character.isLetter(locale.charAt(1));
+ }
+ if (is_ok && locale.length() == 5) {
+ is_ok = locale.charAt(2) == '_' &&
+ Character.isLetter(locale.charAt(3)) &&
+ Character.isLetter(locale.charAt(4));
+ }
+
+ if (!is_ok && DEBUG) {
+ Log.e(TAG, "Invalid locale: expected ab_CD but got " + locale);
+ } else if (is_ok) {
+ ChangeLocale.changeSystemLocale(locale);
+ setResult(Activity.RESULT_OK, locale, null);
+ }
+ }
+
+}
+
+
diff --git a/apps/CustomLocale/src/com/android/customlocale/NewLocaleDialog.java b/apps/CustomLocale/src/com/android/customlocale2/NewLocaleDialog.java
similarity index 93%
rename from apps/CustomLocale/src/com/android/customlocale/NewLocaleDialog.java
rename to apps/CustomLocale/src/com/android/customlocale2/NewLocaleDialog.java
index 04f72223c..3764fd6fe 100644
--- a/apps/CustomLocale/src/com/android/customlocale/NewLocaleDialog.java
+++ b/apps/CustomLocale/src/com/android/customlocale2/NewLocaleDialog.java
@@ -14,14 +14,12 @@
* limitations under the License.
*/
-package com.android.customlocale;
+package com.android.customlocale2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
-import android.text.TextUtils;
import android.util.Log;
-import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
@@ -41,7 +39,6 @@ public class NewLocaleDialog extends Activity implements View.OnClickListener {
private Button mButtonAdd;
private Button mButtonAddSelect;
private EditText mEditText;
- private boolean mWasEmpty;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -50,7 +47,6 @@ public class NewLocaleDialog extends Activity implements View.OnClickListener {
setContentView(R.layout.new_locale);
mEditText = (EditText) findViewById(R.id.value);
- mWasEmpty = true;
mButtonAdd = (Button) findViewById(R.id.add);
mButtonAdd.setOnClickListener(this);
@@ -62,7 +58,7 @@ public class NewLocaleDialog extends Activity implements View.OnClickListener {
public void onClick(View v) {
String locale = mEditText.getText().toString();
boolean select = v == mButtonAddSelect;
-
+
if (DEBUG) {
Log.d(TAG, "New Locale: " + locale + (select ? " + select" : ""));
}