Fix SimPreference dialog from closing on orientation change.

Bug: 19041463
Change-Id: I8c91fd6f01bbeb7561365dbb76ba3d4c05b76efc
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index e3a7e96..7c46cfd 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -2468,6 +2468,11 @@
                 android:resource="@id/sim_settings" />
         </activity>
 
+        <activity android:name=".sim.SimPreferenceDialog"
+            android:theme="@android:style/Theme.Material.Light.Dialog.NoActionBar"
+            android:excludeFromRecents="true">
+        </activity>
+
         <activity android:name=".sim.SimDialogActivity"
                 android:theme="@android:style/Theme.Material.Light.Dialog.NoActionBar"
                 android:label="@string/sim_settings_title"
diff --git a/src/com/android/settings/sim/SimPreferenceDialog.java b/src/com/android/settings/sim/SimPreferenceDialog.java
new file mode 100644
index 0000000..f03b452
--- /dev/null
+++ b/src/com/android/settings/sim/SimPreferenceDialog.java
@@ -0,0 +1,254 @@
+/*
+ * Copyright (C) 2015 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.sim;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.res.Resources;
+import android.graphics.Paint;
+import android.graphics.drawable.ShapeDrawable;
+import android.graphics.drawable.shapes.OvalShape;
+import android.os.Bundle;
+import android.telephony.PhoneNumberUtils;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
+import android.text.TextUtils;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.AdapterView;
+import android.widget.ArrayAdapter;
+import android.widget.EditText;
+import android.widget.ImageView;
+import android.widget.Spinner;
+import android.widget.TextView;
+
+import com.android.settings.R;
+
+public class SimPreferenceDialog extends Activity {
+
+    private Context mContext;
+    private SubscriptionInfo mSubInfoRecord;
+    private int mSlotId;
+    private int[] mTintArr;
+    private String[] mColorStrings;
+    private int mTintSelectorPos;
+    private SubscriptionManager mSubscriptionManager;
+    AlertDialog.Builder mBuilder;
+    View mDialogLayout;
+    private final String SIM_NAME = "sim_name";
+    private final String TINT_POS = "tint_pos";
+
+    @Override
+    public void onCreate(Bundle bundle) {
+        super.onCreate(bundle);
+        mContext = this;
+        final Bundle extras = getIntent().getExtras();
+        mSlotId = extras.getInt(SimSettings.EXTRA_SLOT_ID, -1);
+        mSubscriptionManager = SubscriptionManager.from(mContext);
+        mSubInfoRecord = mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(mSlotId);
+        mTintArr = mContext.getResources().getIntArray(com.android.internal.R.array.sim_colors);
+        mColorStrings = mContext.getResources().getStringArray(R.array.color_picker);
+        mTintSelectorPos = 0;
+
+        mBuilder = new AlertDialog.Builder(mContext);
+        LayoutInflater inflater = (LayoutInflater)mContext
+                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+        mDialogLayout = inflater.inflate(R.layout.multi_sim_dialog, null);
+        mBuilder.setView(mDialogLayout);
+
+        createEditDialog(bundle);
+    }
+
+    @Override
+    public void onSaveInstanceState(Bundle savedInstanceState) {
+        savedInstanceState.putInt(TINT_POS, mTintSelectorPos);
+
+        final EditText nameText = (EditText)mDialogLayout.findViewById(R.id.sim_name);
+        savedInstanceState.putString(SIM_NAME, nameText.getText().toString());
+
+        super.onSaveInstanceState(savedInstanceState);
+
+    }
+
+    @Override
+    public void onRestoreInstanceState(Bundle savedInstanceState) {
+        super.onRestoreInstanceState(savedInstanceState);
+
+        int pos = savedInstanceState.getInt(TINT_POS);
+        final Spinner tintSpinner = (Spinner) mDialogLayout.findViewById(R.id.spinner);
+        tintSpinner.setSelection(pos);
+        mTintSelectorPos = pos;
+
+        EditText nameText = (EditText)mDialogLayout.findViewById(R.id.sim_name);
+        nameText.setText(savedInstanceState.getString(SIM_NAME));
+    }
+
+    private void createEditDialog(Bundle bundle) {
+        final Resources res = mContext.getResources();
+        EditText nameText = (EditText)mDialogLayout.findViewById(R.id.sim_name);
+        nameText.setText(mSubInfoRecord.getDisplayName());
+
+        final Spinner tintSpinner = (Spinner) mDialogLayout.findViewById(R.id.spinner);
+        SelectColorAdapter adapter = new SelectColorAdapter(mContext,
+                R.layout.settings_color_picker_item, mColorStrings);
+        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+        tintSpinner.setAdapter(adapter);
+
+        for (int i = 0; i < mTintArr.length; i++) {
+            if (mTintArr[i] == mSubInfoRecord.getIconTint()) {
+                tintSpinner.setSelection(i);
+                mTintSelectorPos = i;
+                break;
+            }
+        }
+
+        tintSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
+            @Override
+            public void onItemSelected(AdapterView<?> parent, View view,
+                                       int pos, long id){
+                tintSpinner.setSelection(pos);
+                mTintSelectorPos = pos;
+            }
+
+            @Override
+            public void onNothingSelected(AdapterView<?> parent) {
+            }
+        });
+
+        final TelephonyManager tm = (TelephonyManager) mContext.getSystemService(
+                Context.TELEPHONY_SERVICE);
+        TextView numberView = (TextView)mDialogLayout.findViewById(R.id.number);
+        final String rawNumber =  tm.getLine1NumberForSubscriber(
+                mSubInfoRecord.getSubscriptionId());
+        if (TextUtils.isEmpty(rawNumber)) {
+            numberView.setText(res.getString(com.android.internal.R.string.unknownName));
+        } else {
+            numberView.setText(PhoneNumberUtils.formatNumber(rawNumber));
+        }
+
+        String simCarrierName = tm.getSimOperatorNameForSubscription(mSubInfoRecord
+                .getSubscriptionId());
+        TextView carrierView = (TextView)mDialogLayout.findViewById(R.id.carrier);
+        carrierView.setText(!TextUtils.isEmpty(simCarrierName) ? simCarrierName :
+                mContext.getString(com.android.internal.R.string.unknownName));
+
+        mBuilder.setTitle(String.format(res.getString(R.string.sim_editor_title),
+                (mSubInfoRecord.getSimSlotIndex() + 1)));
+
+        mBuilder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int whichButton) {
+                final EditText nameText = (EditText)mDialogLayout.findViewById(R.id.sim_name);
+
+                String displayName = nameText.getText().toString();
+                int subId = mSubInfoRecord.getSubscriptionId();
+                mSubInfoRecord.setDisplayName(displayName);
+                mSubscriptionManager.setDisplayName(displayName, subId,
+                        SubscriptionManager.NAME_SOURCE_USER_INPUT);
+
+                final int tintSelected = tintSpinner.getSelectedItemPosition();
+                int subscriptionId = mSubInfoRecord.getSubscriptionId();
+                int tint = mTintArr[tintSelected];
+                mSubInfoRecord.setIconTint(tint);
+                mSubscriptionManager.setIconTint(tint, subscriptionId);
+                dialog.dismiss();
+                finish();
+            }
+        });
+
+        mBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int whichButton) {
+                dialog.dismiss();
+                finish();
+            }
+        });
+
+        mBuilder.create().show();
+    }
+
+    private class SelectColorAdapter extends ArrayAdapter<CharSequence> {
+        private Context mContext;
+        private int mResId;
+
+        public SelectColorAdapter(
+                Context context, int resource, String[] arr) {
+            super(context, resource, arr);
+            mContext = context;
+            mResId = resource;
+        }
+
+        @Override
+        public View getView(int position, View convertView, ViewGroup parent) {
+            LayoutInflater inflater = (LayoutInflater)
+                    mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+
+            View rowView;
+            final ViewHolder holder;
+            Resources res = mContext.getResources();
+            int iconSize = res.getDimensionPixelSize(R.dimen.color_swatch_size);
+            int strokeWidth = res.getDimensionPixelSize(R.dimen.color_swatch_stroke_width);
+
+            if (convertView == null) {
+                // Cache views for faster scrolling
+                rowView = inflater.inflate(mResId, null);
+                holder = new ViewHolder();
+                ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
+                drawable.setIntrinsicHeight(iconSize);
+                drawable.setIntrinsicWidth(iconSize);
+                drawable.getPaint().setStrokeWidth(strokeWidth);
+                holder.label = (TextView) rowView.findViewById(R.id.color_text);
+                holder.icon = (ImageView) rowView.findViewById(R.id.color_icon);
+                holder.swatch = drawable;
+                rowView.setTag(holder);
+            } else {
+                rowView = convertView;
+                holder = (ViewHolder) rowView.getTag();
+            }
+
+            holder.label.setText(getItem(position));
+            holder.swatch.getPaint().setColor(mTintArr[position]);
+            holder.swatch.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
+            holder.icon.setVisibility(View.VISIBLE);
+            holder.icon.setImageDrawable(holder.swatch);
+            return rowView;
+        }
+
+        @Override
+        public View getDropDownView(int position, View convertView, ViewGroup parent) {
+            View rowView = getView(position, convertView, parent);
+            final ViewHolder holder = (ViewHolder) rowView.getTag();
+
+            if (mTintSelectorPos == position) {
+                holder.swatch.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
+            } else {
+                holder.swatch.getPaint().setStyle(Paint.Style.STROKE);
+            }
+            holder.icon.setVisibility(View.VISIBLE);
+            return rowView;
+        }
+
+        private class ViewHolder {
+            TextView label;
+            ImageView icon;
+            ShapeDrawable swatch;
+        }
+    }
+}
diff --git a/src/com/android/settings/sim/SimSettings.java b/src/com/android/settings/sim/SimSettings.java
index 6d7ee4f..23e6275 100644
--- a/src/com/android/settings/sim/SimSettings.java
+++ b/src/com/android/settings/sim/SimSettings.java
@@ -16,15 +16,10 @@
 
 package com.android.settings.sim;
 
-import android.app.AlertDialog;
 import android.content.Context;
-import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.res.Resources;
-import android.graphics.Paint;
 import android.graphics.drawable.BitmapDrawable;
-import android.graphics.drawable.ShapeDrawable;
-import android.graphics.drawable.shapes.OvalShape;
 import android.os.Bundle;
 import android.preference.Preference;
 import android.preference.PreferenceScreen;
@@ -33,20 +28,10 @@
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
-import android.telephony.PhoneNumberUtils;
 import android.telecom.PhoneAccountHandle;
 import android.telecom.TelecomManager;
 import android.text.TextUtils;
 import android.util.Log;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.widget.ArrayAdapter;
-import android.widget.EditText;
-import android.widget.ImageView;
-import android.widget.Spinner;
-import android.widget.TextView;
-import android.view.ViewGroup;
-import android.widget.AdapterView;
 import com.android.internal.logging.MetricsLogger;
 import com.android.settings.RestrictedSettingsFragment;
 import com.android.settings.Utils;
@@ -68,6 +53,7 @@
     private static final String KEY_CELLULAR_DATA = "sim_cellular_data";
     private static final String KEY_CALLS = "sim_calls";
     private static final String KEY_SMS = "sim_sms";
+    public static final String EXTRA_SLOT_ID = "slot_id";
 
     /**
      * By UX design we use only one Subscription Information(SubInfo) record per SIM slot.
@@ -250,7 +236,9 @@
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 
         if (preference instanceof SimPreference) {
-            ((SimPreference)preference).createEditDialog((SimPreference)preference);
+            Intent newIntent = new Intent(context, SimPreferenceDialog.class);
+            newIntent.putExtra(EXTRA_SLOT_ID, ((SimPreference)preference).getSlotId());
+            startActivity(newIntent);
         } else if (findPreference(KEY_CELLULAR_DATA) == preference) {
             intent.putExtra(SimDialogActivity.DIALOG_TYPE_KEY, SimDialogActivity.DATA_PICK);
             context.startActivity(intent);
@@ -265,13 +253,10 @@
         return true;
     }
 
-    private class SimPreference extends Preference{
+    private class SimPreference extends Preference {
         private SubscriptionInfo mSubInfoRecord;
         private int mSlotId;
-        private int[] mTintArr;
         Context mContext;
-        private String[] mColorStrings;
-        private int mTintSelectorPos;
 
         public SimPreference(Context context, SubscriptionInfo subInfoRecord, int slotId) {
             super(context);
@@ -281,9 +266,6 @@
             mSlotId = slotId;
             setKey("sim" + mSlotId);
             update();
-            mTintArr = context.getResources().getIntArray(com.android.internal.R.array.sim_colors);
-            mColorStrings = context.getResources().getStringArray(R.array.color_picker);
-            mTintSelectorPos = 0;
         }
 
         public void update() {
@@ -307,172 +289,9 @@
             }
         }
 
-        public SubscriptionInfo getSubInfoRecord() {
-            return mSubInfoRecord;
+        private int getSlotId() {
+            return mSlotId;
         }
-
-        public void createEditDialog(SimPreference simPref) {
-            final Resources res = mContext.getResources();
-
-            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
-
-            LayoutInflater inflater = (LayoutInflater)mContext
-                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
-            final View dialogLayout = inflater.inflate(
-                    R.layout.multi_sim_dialog, null);
-            builder.setView(dialogLayout);
-
-            EditText nameText = (EditText)dialogLayout.findViewById(R.id.sim_name);
-            nameText.setText(mSubInfoRecord.getDisplayName());
-
-            final Spinner tintSpinner = (Spinner) dialogLayout.findViewById(R.id.spinner);
-            SelectColorAdapter adapter = new SelectColorAdapter(getContext(),
-                     R.layout.settings_color_picker_item, mColorStrings);
-            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
-            tintSpinner.setAdapter(adapter);
-
-            for (int i = 0; i < mTintArr.length; i++) {
-                if (mTintArr[i] == mSubInfoRecord.getIconTint()) {
-                    tintSpinner.setSelection(i);
-                    mTintSelectorPos = i;
-                    break;
-                }
-            }
-
-            tintSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
-                @Override
-                public void onItemSelected(AdapterView<?> parent, View view,
-                    int pos, long id){
-                    tintSpinner.setSelection(pos);
-                    mTintSelectorPos = pos;
-                }
-
-                @Override
-                public void onNothingSelected(AdapterView<?> parent) {
-                }
-            });
-
-            TextView numberView = (TextView)dialogLayout.findViewById(R.id.number);
-            final String rawNumber = getPhoneNumber(mSubInfoRecord);
-            if (TextUtils.isEmpty(rawNumber)) {
-                numberView.setText(res.getString(com.android.internal.R.string.unknownName));
-            } else {
-                numberView.setText(PhoneNumberUtils.formatNumber(rawNumber));
-            }
-
-            final TelephonyManager tm =
-                        (TelephonyManager) mContext.getSystemService(
-                        Context.TELEPHONY_SERVICE);
-            String simCarrierName = tm.getSimOperatorNameForSubscription(mSubInfoRecord
-                        .getSubscriptionId());
-            TextView carrierView = (TextView)dialogLayout.findViewById(R.id.carrier);
-            carrierView.setText(!TextUtils.isEmpty(simCarrierName) ? simCarrierName :
-                    getContext().getString(com.android.internal.R.string.unknownName));
-
-            builder.setTitle(String.format(res.getString(R.string.sim_editor_title),
-                    (mSubInfoRecord.getSimSlotIndex() + 1)));
-
-            builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
-                @Override
-                public void onClick(DialogInterface dialog, int whichButton) {
-                    final EditText nameText = (EditText)dialogLayout.findViewById(R.id.sim_name);
-
-                    String displayName = nameText.getText().toString();
-                    int subId = mSubInfoRecord.getSubscriptionId();
-                    mSubInfoRecord.setDisplayName(displayName);
-                    mSubscriptionManager.setDisplayName(displayName, subId,
-                            SubscriptionManager.NAME_SOURCE_USER_INPUT);
-
-                    final int tintSelected = tintSpinner.getSelectedItemPosition();
-                    int subscriptionId = mSubInfoRecord.getSubscriptionId();
-                    int tint = mTintArr[tintSelected];
-                    mSubInfoRecord.setIconTint(tint);
-                    mSubscriptionManager.setIconTint(tint, subscriptionId);
-
-                    updateAllOptions();
-                    update();
-                }
-            });
-
-            builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
-                @Override
-                public void onClick(DialogInterface dialog, int whichButton) {
-                    dialog.dismiss();
-                }
-            });
-
-            builder.create().show();
-        }
-
-        private class SelectColorAdapter extends ArrayAdapter<CharSequence> {
-            private Context mContext;
-            private int mResId;
-
-            public SelectColorAdapter(
-                Context context, int resource, String[] arr) {
-                super(context, resource, arr);
-                mContext = context;
-                mResId = resource;
-            }
-
-            @Override
-            public View getView(int position, View convertView, ViewGroup parent) {
-                LayoutInflater inflater = (LayoutInflater)
-                    mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
-
-                View rowView;
-                final ViewHolder holder;
-                Resources res = mContext.getResources();
-                int iconSize = res.getDimensionPixelSize(R.dimen.color_swatch_size);
-                int strokeWidth = res.getDimensionPixelSize(R.dimen.color_swatch_stroke_width);
-
-                if (convertView == null) {
-                    // Cache views for faster scrolling
-                    rowView = inflater.inflate(mResId, null);
-                    holder = new ViewHolder();
-                    ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
-                    drawable.setIntrinsicHeight(iconSize);
-                    drawable.setIntrinsicWidth(iconSize);
-                    drawable.getPaint().setStrokeWidth(strokeWidth);
-                    holder.label = (TextView) rowView.findViewById(R.id.color_text);
-                    holder.icon = (ImageView) rowView.findViewById(R.id.color_icon);
-                    holder.swatch = drawable;
-                    rowView.setTag(holder);
-                } else {
-                    rowView = convertView;
-                    holder = (ViewHolder) rowView.getTag();
-                }
-
-                holder.label.setText(getItem(position));
-                holder.swatch.getPaint().setColor(mTintArr[position]);
-                holder.swatch.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
-                holder.icon.setVisibility(View.VISIBLE);
-                holder.icon.setImageDrawable(holder.swatch);
-                return rowView;
-            }
-
-            @Override
-            public View getDropDownView(int position, View convertView, ViewGroup parent) {
-                View rowView = getView(position, convertView, parent);
-                final ViewHolder holder = (ViewHolder) rowView.getTag();
-
-                if (mTintSelectorPos == position) {
-                    holder.swatch.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
-                } else {
-                    holder.swatch.getPaint().setStyle(Paint.Style.STROKE);
-                }
-                holder.icon.setVisibility(View.VISIBLE);
-                return rowView;
-            }
-
-            private class ViewHolder {
-                TextView label;
-                ImageView icon;
-                ShapeDrawable swatch;
-            }
-        }
-
-
     }
 
     // Returns the line1Number. Line1number should always be read from TelephonyManager since it can