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