1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.customlocale2;
18
19import android.app.Activity;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.util.Log;
24
25//-----------------------------------------------
26
27/**
28 * Broadcast receiver that can change the system's locale.
29 * <p/>
30 * This allows an external script such as an automated testing framework
31 * to easily trigger a locale change on an emulator as such:
32 * <pre>
33 * $ adb shell am broadcast -a com.android.intent.action.SET_LOCALE \
34 *                          --es com.android.intent.extra.LOCALE en_US
35 * </pre>
36 */
37public class CustomLocaleReceiver extends BroadcastReceiver {
38
39    private static final String TAG = CustomLocaleReceiver.class.getSimpleName();
40    private static final boolean DEBUG = true;
41
42    /** Intent action that triggers this receiver. */
43    public static final String ACTION_SET_LOCALE = "com.android.intent.action.SET_LOCALE";
44    /** An extra String that specifies the locale to set, in the form "en_US". */
45    public static final String EXTRA_LOCALE = "com.android.intent.extra.LOCALE";
46
47    @Override
48    public void onReceive(Context context, Intent intent) {
49        setResult(Activity.RESULT_CANCELED, null, null);
50        if (intent == null || ! ACTION_SET_LOCALE.equals(intent.getAction())) {
51            if (DEBUG) {
52                Log.d(TAG, "Invalid intent: " + (intent == null ? "null" : intent.toString()));
53            }
54            return;
55        }
56
57        String locale = intent.getStringExtra(EXTRA_LOCALE);
58
59        // Enforce the locale string is either in the form "ab" or "ab_cd"
60        boolean is_ok = locale != null;
61        is_ok = is_ok && (locale.length() == 2 || locale.length() == 5);
62        if (is_ok && locale.length() >= 2) {
63            is_ok = Character.isLetter(locale.charAt(0)) &&
64                    Character.isLetter(locale.charAt(1));
65        }
66        if (is_ok && locale.length() == 5) {
67            is_ok = locale.charAt(2) == '_' &&
68                    Character.isLetter(locale.charAt(3)) &&
69                    Character.isLetter(locale.charAt(4));
70        }
71
72        if (!is_ok && DEBUG) {
73            Log.e(TAG, "Invalid locale: expected ab_CD but got " + locale);
74        } else if (is_ok) {
75            ChangeLocale.changeSystemLocale(locale);
76            setResult(Activity.RESULT_OK, locale, null);
77        }
78    }
79
80}
81
82
83