Home | History | Annotate | Download | only in customlocale2
      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 
     17 package com.android.customlocale2;
     18 
     19 
     20 import java.util.Locale;
     21 
     22 import android.app.ActivityManagerNative;
     23 import android.app.IActivityManager;
     24 import android.content.res.Configuration;
     25 import android.os.RemoteException;
     26 import android.util.Log;
     27 
     28 /**
     29  * Helper class to change the system locale.
     30  */
     31 public final class ChangeLocale {
     32 
     33     private static final String TAG = ChangeLocale.class.getSimpleName();
     34     private static final boolean DEBUG = true;
     35 
     36     /**
     37      * Sets the system locale to the new one specified.
     38      *
     39      * @param locale A locale name in the form "ab_AB". Must not be null or empty.
     40      * @return True if the locale was succesfully changed.
     41      */
     42     public static boolean changeSystemLocale(String locale) {
     43         if (DEBUG) {
     44             Log.d(TAG, "Change locale to: " + locale);
     45         }
     46 
     47         try {
     48             IActivityManager am = ActivityManagerNative.getDefault();
     49             Configuration config = am.getConfiguration();
     50 
     51             Locale loc = null;
     52 
     53             String[] langCountry = locale.split("_");
     54             if (langCountry.length == 2) {
     55                 loc = new Locale(langCountry[0], langCountry[1]);
     56             } else {
     57                 loc = new Locale(locale);
     58             }
     59 
     60             config.locale = loc;
     61 
     62             // indicate this isn't some passing default - the user wants this
     63             // remembered
     64             config.userSetLocale = true;
     65 
     66             am.updateConfiguration(config);
     67 
     68             return true;
     69 
     70         } catch (RemoteException e) {
     71             if (DEBUG) {
     72                 Log.e(TAG, "Change locale failed", e);
     73             }
     74         }
     75 
     76         return false;
     77     }
     78 }
     79