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
17package com.android.settings;
18
19import android.app.Dialog;
20import android.os.Bundle;
21import android.util.Log;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.ListView;
26import android.content.Context;
27
28import com.android.settings.SettingsPreferenceFragment.SettingsDialogFragment;
29import com.android.settings.DevelopmentSettings;
30
31import java.util.Locale;
32
33public 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
45    public LocalePicker() {
46        super();
47        setLocaleSelectionListener(this);
48    }
49
50    @Override
51    protected boolean isInDeveloperMode() {
52        final boolean showDev = getActivity().getSharedPreferences(DevelopmentSettings.PREF_FILE,
53                Context.MODE_PRIVATE).getBoolean(
54                DevelopmentSettings.PREF_SHOW,
55                android.os.Build.TYPE.equals("eng"));
56        return showDev;
57    }
58
59    @Override
60    public void onCreate(Bundle savedInstanceState) {
61        super.onCreate(savedInstanceState);
62        if (savedInstanceState != null && savedInstanceState.containsKey(SAVE_TARGET_LOCALE)) {
63            mTargetLocale = new Locale(savedInstanceState.getString(SAVE_TARGET_LOCALE));
64        }
65    }
66
67    @Override
68    public View onCreateView(
69            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
70        final View view = super.onCreateView(inflater, container, savedInstanceState);
71        final ListView list = (ListView) view.findViewById(android.R.id.list);
72        Utils.forcePrepareCustomPreferencesList(container, view, list, false);
73        return view;
74    }
75
76    @Override
77    public void onLocaleSelected(final Locale locale) {
78        if (Utils.hasMultipleUsers(getActivity())) {
79            mTargetLocale = locale;
80            showDialog(DLG_SHOW_GLOBAL_WARNING);
81        } else {
82            getActivity().onBackPressed();
83            LocalePicker.updateLocale(locale);
84        }
85    }
86
87    @Override
88    public void onSaveInstanceState(Bundle outState) {
89        super.onSaveInstanceState(outState);
90
91        if (mTargetLocale != null) {
92            outState.putString(SAVE_TARGET_LOCALE, mTargetLocale.toString());
93        }
94    }
95
96    protected void showDialog(int dialogId) {
97        if (mDialogFragment != null) {
98            Log.e(TAG, "Old dialog fragment not null!");
99        }
100        mDialogFragment = new SettingsDialogFragment(this, dialogId);
101        mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId));
102    }
103
104    public Dialog onCreateDialog(final int dialogId) {
105        return Utils.buildGlobalChangeWarningDialog(getActivity(),
106                R.string.global_locale_change_title,
107                new Runnable() {
108                    public void run() {
109                        removeDialog(dialogId);
110                        getActivity().onBackPressed();
111                        LocalePicker.updateLocale(mTargetLocale);
112                    }
113                }
114        );
115    }
116
117    protected void removeDialog(int dialogId) {
118        // mDialogFragment may not be visible yet in parent fragment's onResume().
119        // To be able to dismiss dialog at that time, don't check
120        // mDialogFragment.isVisible().
121        if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
122            mDialogFragment.dismiss();
123        }
124        mDialogFragment = null;
125    }
126}
127