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