Revamped CustomLocale for the SDK.
- Revamped the list UI: replaced the long-press actions by some stateful buttons. Uses the listview's single choice mode. - Added a broadcast receiver to be able to change the system locale using an intent, e.g. sent by adb shell am. This is convenient for automated testing scripts. - Changed package namespace to v2. This makes it easier for an automated testing script to figure that an emulator has the old version and install the new one side-by-side. Change-Id: I1849d2b9d55441490b013cfefcca5d8fa48e22bd
This commit is contained in:
82
apps/CustomLocale/src/com/android/customlocale2/CustomLocaleReceiver.java
Executable file
82
apps/CustomLocale/src/com/android/customlocale2/CustomLocaleReceiver.java
Executable file
@@ -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.
|
||||
* <p/>
|
||||
* This allows an external script such as an automated testing framework
|
||||
* to easily trigger a locale change on an emulator as such:
|
||||
* <pre>
|
||||
* $ adb shell am broadcast -a com.android.intent.action.SET_LOCALE \
|
||||
* --es com.android.intent.extra.LOCALE en_US
|
||||
* </pre>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user