Make LocalePicker Fragment.

The logic calling selectFirst() is removed as
- There's no comment why it is needed.
- Actually SetupWizard gets stack as that forces users to see
  WirelessSettings in SetupWizard.

The other changes:
- Move back LocalePickerFragment to LocalePicker.
- Make <activity> for LocalePicker in AndroidManifest <activity-alias>
- Add a short comment about how getComponent(), which should be a key
  for understanding how top-level settings work.
- Modify LanguageSettings so that it corectly points LocalePicker as
  a fragment.

Change-Id: I78d356e40af896ba1aab72fba12c90467371c7b0
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 73d7fc4..87311f0 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -225,14 +225,17 @@
             </intent-filter>
         </activity>
 
-        <activity android:name="LocalePicker" android:label="@string/language_picker_title">
+        <activity-alias android:name="LocalePicker"
+                        android:label="@string/language_picker_title"
+                        android:clearTaskOnLaunch="true"
+                        android:targetActivity="Settings">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <action android:name="android.settings.LOCALE_SETTINGS" />
                 <category android:name="android.intent.category.DEFAULT" />
                 <category android:name="android.intent.category.VOICE_LAUNCH" />
             </intent-filter>
-        </activity>
+        </activity-alias>
 
         <activity-alias android:name="LanguageSettings"
                 android:label="@string/language_keyboard_settings_title"
diff --git a/proguard.flags b/proguard.flags
index 5170dfa..0e1b428 100644
--- a/proguard.flags
+++ b/proguard.flags
@@ -1,5 +1,6 @@
 # Keep all Fragments in this package, which are used by reflection.
 -keep class com.android.settings.*Fragment
+-keep class com.android.settings.*Picker
 -keep class com.android.settings.*Settings
 -keep class com.android.settings.wifi.*Settings
 -keep class com.android.settings.deviceinfo.*
diff --git a/res/xml/language_settings.xml b/res/xml/language_settings.xml
index edcfc6c..730d6ea 100644
--- a/res/xml/language_settings.xml
+++ b/res/xml/language_settings.xml
@@ -19,11 +19,9 @@
         android:title="@string/language_keyboard_settings_title">
 
     <PreferenceScreen
-            android:key="phone_language"
-            android:title="@string/phone_language">
-        <intent android:action="android.intent.action.MAIN"
-                android:targetPackage="com.android.settings"
-                android:targetClass="com.android.settings.LocalePicker"/>
+       android:fragment="com.android.settings.LocalePicker"
+       android:key="phone_language"
+       android:title="@string/phone_language">
     </PreferenceScreen>
 
     <PreferenceCategory android:key="text_category"
diff --git a/src/com/android/settings/LanguageSettings.java b/src/com/android/settings/LanguageSettings.java
index 959329c..2e5588d 100644
--- a/src/com/android/settings/LanguageSettings.java
+++ b/src/com/android/settings/LanguageSettings.java
@@ -16,6 +16,7 @@
 
 package com.android.settings;
 
+import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.Context;
 import android.content.DialogInterface;
@@ -29,6 +30,7 @@
 import android.preference.PreferenceScreen;
 import android.provider.Settings;
 import android.text.TextUtils;
+import android.util.Log;
 import android.view.inputmethod.InputMethodInfo;
 import android.view.inputmethod.InputMethodManager;
 
@@ -37,7 +39,8 @@
 import java.util.List;
 
 public class LanguageSettings extends SettingsPreferenceFragment {
-    
+    private static final String TAG = LanguageSettings.class.getSimpleName();
+
     private static final String KEY_PHONE_LANGUAGE = "phone_language";
     private static final String KEY_INPUT_METHOD = "input_method";
 
@@ -233,6 +236,15 @@
         if (Utils.isMonkeyRunning()) {
             return false;
         }
+        final String fragmentClass = preference.getFragment();
+        if (fragmentClass != null) {
+            final Activity activity = getActivity();
+            if (activity instanceof com.android.settings.Settings) {
+                return ((com.android.settings.Settings) activity).showFragment(preference);
+            } else {
+                Log.w(TAG, "Fragment is available while the parent is not Settings Activity.");
+            }
+        }
 
         if (preference instanceof CheckBoxPreference) {
             final CheckBoxPreference chkPref = (CheckBoxPreference) preference;
diff --git a/src/com/android/settings/LocalePicker.java b/src/com/android/settings/LocalePicker.java
index f669712..65c738a 100644
--- a/src/com/android/settings/LocalePicker.java
+++ b/src/com/android/settings/LocalePicker.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007 The Android Open Source Project
+ * Copyright (C) 2010 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,12 +17,177 @@
 package com.android.settings;
 
 import android.app.Activity;
+import android.app.ActivityManagerNative;
+import android.app.IActivityManager;
+import android.app.ListFragment;
+import android.app.backup.BackupManager;
+import android.content.res.Configuration;
+import android.content.res.Resources;
 import android.os.Bundle;
+import android.os.RemoteException;
+import android.util.Log;
+import android.view.View;
+import android.widget.ArrayAdapter;
+import android.widget.ListView;
 
-public class LocalePicker extends Activity {
-    @Override
-    public void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-        setContentView(R.layout.locale_picker);
+import java.text.Collator;
+import java.util.Arrays;
+import java.util.Locale;
+
+public class LocalePicker extends ListFragment {
+    private static final String TAG = "LocalePicker";
+    private static final boolean DEBUG = false;
+
+    Loc[] mLocales;
+    String[] mSpecialLocaleCodes;
+    String[] mSpecialLocaleNames;
+
+    Activity mActivity;
+
+    private static class Loc implements Comparable<Loc> {
+        static Collator sCollator = Collator.getInstance();
+
+        String label;
+        Locale locale;
+
+        public Loc(String label, Locale locale) {
+            this.label = label;
+            this.locale = locale;
+        }
+
+        @Override
+        public String toString() {
+            return this.label;
+        }
+
+        @Override
+        public int compareTo(Loc another) {
+            return sCollator.compare(this.label, another.label);
+        }
     }
-}
+
+    private void setUpLocaleList() {
+        final Resources resources = mActivity.getResources();
+        mSpecialLocaleCodes = resources.getStringArray(R.array.special_locale_codes);
+        mSpecialLocaleNames = resources.getStringArray(R.array.special_locale_names);
+
+        final String[] locales = mActivity.getAssets().getLocales();
+        Arrays.sort(locales);
+        final int origSize = locales.length;
+        Loc[] preprocess = new Loc[origSize];
+        int finalSize = 0;
+        for (int i = 0 ; i < origSize; i++ ) {
+            String s = locales[i];
+            int len = s.length();
+            if (len == 5) {
+                String language = s.substring(0, 2);
+                String country = s.substring(3, 5);
+                Locale l = new Locale(language, country);
+
+                if (finalSize == 0) {
+                    if (DEBUG) {
+                        Log.v(TAG, "adding initial "+ toTitleCase(l.getDisplayLanguage(l)));
+                    }
+                    preprocess[finalSize++] =
+                            new Loc(toTitleCase(l.getDisplayLanguage(l)), l);
+                } else {
+                    // check previous entry:
+                    //  same lang and a country -> upgrade to full name and
+                    //    insert ours with full name
+                    //  diff lang -> insert ours with lang-only name
+                    if (preprocess[finalSize-1].locale.getLanguage().equals(
+                            language)) {
+                        if (DEBUG) {
+                            Log.v(TAG, "backing up and fixing "+
+                                    preprocess[finalSize-1].label+" to "+
+                                    getDisplayName(preprocess[finalSize-1].locale));
+                        }
+                        preprocess[finalSize-1].label = toTitleCase(
+                                getDisplayName(preprocess[finalSize-1].locale));
+                        if (DEBUG) {
+                            Log.v(TAG, "  and adding "+ toTitleCase(getDisplayName(l)));
+                        }
+                        preprocess[finalSize++] =
+                                new Loc(toTitleCase(getDisplayName(l)), l);
+                    } else {
+                        String displayName;
+                        if (s.equals("zz_ZZ")) {
+                            displayName = "Pseudo...";
+                        } else {
+                            displayName = toTitleCase(l.getDisplayLanguage(l));
+                        }
+                        if (DEBUG) {
+                            Log.v(TAG, "adding "+displayName);
+                        }
+                        preprocess[finalSize++] = new Loc(displayName, l);
+                    }
+                }
+            }
+        }
+        mLocales = new Loc[finalSize];
+        for (int i = 0; i < finalSize ; i++) {
+            mLocales[i] = preprocess[i];
+        }
+        Arrays.sort(mLocales);
+        final int layoutId = R.layout.locale_picker_item;
+        final int fieldId = R.id.locale;
+        final ArrayAdapter<Loc> adapter =
+                new ArrayAdapter<Loc>(mActivity, layoutId, fieldId, mLocales);
+        setListAdapter(adapter);
+    }
+
+    @Override
+    public void onActivityCreated(final Bundle savedInstanceState) {
+        super.onActivityCreated(savedInstanceState);
+        mActivity = getActivity();
+        setUpLocaleList();
+    }
+
+    private static String toTitleCase(String s) {
+        if (s.length() == 0) {
+            return s;
+        }
+
+        return Character.toUpperCase(s.charAt(0)) + s.substring(1);
+    }
+
+    private String getDisplayName(Locale l) {
+        String code = l.toString();
+
+        for (int i = 0; i < mSpecialLocaleCodes.length; i++) {
+            if (mSpecialLocaleCodes[i].equals(code)) {
+                return mSpecialLocaleNames[i];
+            }
+        }
+
+        return l.getDisplayName(l);
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+        getListView().requestFocus();
+    }
+
+    @Override
+    public void onListItemClick(ListView l, View v, int position, long id) {
+        try {
+            IActivityManager am = ActivityManagerNative.getDefault();
+            Configuration config = am.getConfiguration();
+
+            Loc loc = mLocales[position];
+            config.locale = loc.locale;
+
+            // indicate this isn't some passing default - the user wants this remembered
+            config.userSetLocale = true;
+
+            am.updateConfiguration(config);
+            // Trigger the dirty bit for the Settings Provider.
+            BackupManager.dataChanged("com.android.providers.settings");
+        } catch (RemoteException e) {
+            // Intentionally left blank
+        }
+
+        mActivity.finish();
+    }
+}
\ No newline at end of file
diff --git a/src/com/android/settings/LocalePickerFragment.java b/src/com/android/settings/LocalePickerFragment.java
deleted file mode 100644
index 08f43d8..0000000
--- a/src/com/android/settings/LocalePickerFragment.java
+++ /dev/null
@@ -1,193 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.settings;
-
-import android.app.Activity;
-import android.app.ActivityManagerNative;
-import android.app.IActivityManager;
-import android.app.ListFragment;
-import android.app.backup.BackupManager;
-import android.content.res.Configuration;
-import android.content.res.Resources;
-import android.os.Bundle;
-import android.os.RemoteException;
-import android.util.Log;
-import android.view.View;
-import android.widget.ArrayAdapter;
-import android.widget.ListView;
-
-import java.text.Collator;
-import java.util.Arrays;
-import java.util.Locale;
-
-public class LocalePickerFragment extends ListFragment {
-    private static final String TAG = "LocalePickerFragment";
-    private static final boolean DEBUG = false;
-
-    Loc[] mLocales;
-    String[] mSpecialLocaleCodes;
-    String[] mSpecialLocaleNames;
-
-    Activity mActivity;
-
-    private static class Loc implements Comparable<Loc> {
-        static Collator sCollator = Collator.getInstance();
-
-        String label;
-        Locale locale;
-
-        public Loc(String label, Locale locale) {
-            this.label = label;
-            this.locale = locale;
-        }
-
-        @Override
-        public String toString() {
-            return this.label;
-        }
-
-        @Override
-        public int compareTo(Loc another) {
-            return sCollator.compare(this.label, another.label);
-        }
-    }
-
-    private void setUpLocaleList() {
-        final Resources resources = mActivity.getResources();
-        mSpecialLocaleCodes = resources.getStringArray(R.array.special_locale_codes);
-        mSpecialLocaleNames = resources.getStringArray(R.array.special_locale_names);
-
-        final String[] locales = mActivity.getAssets().getLocales();
-        Arrays.sort(locales);
-        final int origSize = locales.length;
-        Loc[] preprocess = new Loc[origSize];
-        int finalSize = 0;
-        for (int i = 0 ; i < origSize; i++ ) {
-            String s = locales[i];
-            int len = s.length();
-            if (len == 5) {
-                String language = s.substring(0, 2);
-                String country = s.substring(3, 5);
-                Locale l = new Locale(language, country);
-
-                if (finalSize == 0) {
-                    if (DEBUG) {
-                        Log.v(TAG, "adding initial "+ toTitleCase(l.getDisplayLanguage(l)));
-                    }
-                    preprocess[finalSize++] =
-                            new Loc(toTitleCase(l.getDisplayLanguage(l)), l);
-                } else {
-                    // check previous entry:
-                    //  same lang and a country -> upgrade to full name and
-                    //    insert ours with full name
-                    //  diff lang -> insert ours with lang-only name
-                    if (preprocess[finalSize-1].locale.getLanguage().equals(
-                            language)) {
-                        if (DEBUG) {
-                            Log.v(TAG, "backing up and fixing "+
-                                    preprocess[finalSize-1].label+" to "+
-                                    getDisplayName(preprocess[finalSize-1].locale));
-                        }
-                        preprocess[finalSize-1].label = toTitleCase(
-                                getDisplayName(preprocess[finalSize-1].locale));
-                        if (DEBUG) {
-                            Log.v(TAG, "  and adding "+ toTitleCase(getDisplayName(l)));
-                        }
-                        preprocess[finalSize++] =
-                                new Loc(toTitleCase(getDisplayName(l)), l);
-                    } else {
-                        String displayName;
-                        if (s.equals("zz_ZZ")) {
-                            displayName = "Pseudo...";
-                        } else {
-                            displayName = toTitleCase(l.getDisplayLanguage(l));
-                        }
-                        if (DEBUG) {
-                            Log.v(TAG, "adding "+displayName);
-                        }
-                        preprocess[finalSize++] = new Loc(displayName, l);
-                    }
-                }
-            }
-        }
-        mLocales = new Loc[finalSize];
-        for (int i = 0; i < finalSize ; i++) {
-            mLocales[i] = preprocess[i];
-        }
-        Arrays.sort(mLocales);
-        final int layoutId = R.layout.locale_picker_item;
-        final int fieldId = R.id.locale;
-        final ArrayAdapter<Loc> adapter =
-                new ArrayAdapter<Loc>(mActivity, layoutId, fieldId, mLocales);
-        setListAdapter(adapter);
-    }
-
-    @Override
-    public void onActivityCreated(final Bundle savedInstanceState) {
-        super.onActivityCreated(savedInstanceState);
-        mActivity = getActivity();
-        setUpLocaleList();
-    }
-
-    private static String toTitleCase(String s) {
-        if (s.length() == 0) {
-            return s;
-        }
-
-        return Character.toUpperCase(s.charAt(0)) + s.substring(1);
-    }
-
-    private String getDisplayName(Locale l) {
-        String code = l.toString();
-
-        for (int i = 0; i < mSpecialLocaleCodes.length; i++) {
-            if (mSpecialLocaleCodes[i].equals(code)) {
-                return mSpecialLocaleNames[i];
-            }
-        }
-
-        return l.getDisplayName(l);
-    }
-
-    @Override
-    public void onResume() {
-        super.onResume();
-        getListView().requestFocus();
-    }
-
-    @Override
-    public void onListItemClick(ListView l, View v, int position, long id) {
-        try {
-            IActivityManager am = ActivityManagerNative.getDefault();
-            Configuration config = am.getConfiguration();
-
-            Loc loc = mLocales[position];
-            config.locale = loc.locale;
-
-            // indicate this isn't some passing default - the user wants this remembered
-            config.userSetLocale = true;
-
-            am.updateConfiguration(config);
-            // Trigger the dirty bit for the Settings Provider.
-            BackupManager.dataChanged("com.android.providers.settings");
-        } catch (RemoteException e) {
-            // Intentionally left blank
-        }
-
-        mActivity.finish();
-    }
-}
\ No newline at end of file
diff --git a/src/com/android/settings/Settings.java b/src/com/android/settings/Settings.java
index 9195d77..460ca96 100644
--- a/src/com/android/settings/Settings.java
+++ b/src/com/android/settings/Settings.java
@@ -87,6 +87,11 @@
             if (initialFragment != null) {
                 showFragment(initialFragment, initialArguments);
             } else {
+                // Intent#getCompontent() lets us get Fragment name, even when the Intent is
+                // given via <activity-alias>.
+                //
+                // e.g. When we reach here via "ChildSetting" activity-alias,
+                // we should get the name here instead of targetActivity ("Settings").
                 if (intent.getComponent().getClassName().equals(this.getClass().getName())) {
                     showFragment(TopLevelSettings.class.getName(), null);
                 } else {