Home | History | Annotate | Download | only in settings
      1 /*
      2  * Copyright (C) 2010 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.settings;
     18 
     19 import android.app.Dialog;
     20 import android.os.Bundle;
     21 import android.util.Log;
     22 import android.view.LayoutInflater;
     23 import android.view.View;
     24 import android.view.ViewGroup;
     25 import android.widget.ListView;
     26 import com.android.settings.SettingsPreferenceFragment.SettingsDialogFragment;
     27 import java.util.Locale;
     28 
     29 public class LocalePicker extends com.android.internal.app.LocalePicker
     30         implements com.android.internal.app.LocalePicker.LocaleSelectionListener,
     31         DialogCreatable {
     32 
     33     private static final String TAG = "LocalePicker";
     34 
     35     private SettingsDialogFragment mDialogFragment;
     36     private static final int DLG_SHOW_GLOBAL_WARNING = 1;
     37     private static final String SAVE_TARGET_LOCALE = "locale";
     38 
     39     private Locale mTargetLocale;
     40 
     41     public LocalePicker() {
     42         super();
     43         setLocaleSelectionListener(this);
     44     }
     45 
     46     @Override
     47     public void onCreate(Bundle savedInstanceState) {
     48         super.onCreate(savedInstanceState);
     49         if (savedInstanceState != null && savedInstanceState.containsKey(SAVE_TARGET_LOCALE)) {
     50             mTargetLocale = new Locale(savedInstanceState.getString(SAVE_TARGET_LOCALE));
     51         }
     52     }
     53 
     54     @Override
     55     public View onCreateView(
     56             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     57         final View view = super.onCreateView(inflater, container, savedInstanceState);
     58         final ListView list = (ListView) view.findViewById(android.R.id.list);
     59         Utils.forcePrepareCustomPreferencesList(container, view, list, false);
     60         return view;
     61     }
     62 
     63     @Override
     64     public void onLocaleSelected(final Locale locale) {
     65         if (Utils.hasMultipleUsers(getActivity())) {
     66             mTargetLocale = locale;
     67             showDialog(DLG_SHOW_GLOBAL_WARNING);
     68         } else {
     69             getActivity().onBackPressed();
     70             LocalePicker.updateLocale(locale);
     71         }
     72     }
     73 
     74     @Override
     75     public void onSaveInstanceState(Bundle outState) {
     76         super.onSaveInstanceState(outState);
     77 
     78         if (mTargetLocale != null) {
     79             outState.putString(SAVE_TARGET_LOCALE, mTargetLocale.toString());
     80         }
     81     }
     82 
     83     protected void showDialog(int dialogId) {
     84         if (mDialogFragment != null) {
     85             Log.e(TAG, "Old dialog fragment not null!");
     86         }
     87         mDialogFragment = new SettingsDialogFragment(this, dialogId);
     88         mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId));
     89     }
     90 
     91     public Dialog onCreateDialog(final int dialogId) {
     92         return Utils.buildGlobalChangeWarningDialog(getActivity(),
     93                 R.string.global_locale_change_title,
     94                 new Runnable() {
     95                     public void run() {
     96                         removeDialog(dialogId);
     97                         getActivity().onBackPressed();
     98                         LocalePicker.updateLocale(mTargetLocale);
     99                     }
    100                 }
    101         );
    102     }
    103 
    104     protected void removeDialog(int dialogId) {
    105         // mDialogFragment may not be visible yet in parent fragment's onResume().
    106         // To be able to dismiss dialog at that time, don't check
    107         // mDialogFragment.isVisible().
    108         if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
    109             mDialogFragment.dismiss();
    110         }
    111         mDialogFragment = null;
    112     }
    113 }
    114