1 /*
2  * Copyright (C) 2015 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 package com.android.settings.sim;
17 
18 import android.app.Activity;
19 import android.app.AlertDialog;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.content.res.Resources;
23 import android.graphics.Paint;
24 import android.graphics.drawable.ShapeDrawable;
25 import android.graphics.drawable.shapes.OvalShape;
26 import android.os.Bundle;
27 import android.telephony.PhoneNumberUtils;
28 import android.telephony.SubscriptionInfo;
29 import android.telephony.SubscriptionManager;
30 import android.telephony.TelephonyManager;
31 import android.text.TextUtils;
32 import android.view.LayoutInflater;
33 import android.view.View;
34 import android.view.ViewGroup;
35 import android.widget.AdapterView;
36 import android.widget.ArrayAdapter;
37 import android.widget.EditText;
38 import android.widget.ImageView;
39 import android.widget.Spinner;
40 import android.widget.TextView;
41 
42 import com.android.settings.R;
43 
44 public class SimPreferenceDialog extends Activity {
45 
46     private Context mContext;
47     private SubscriptionInfo mSubInfoRecord;
48     private int mSlotId;
49     private int[] mTintArr;
50     private String[] mColorStrings;
51     private int mTintSelectorPos;
52     private SubscriptionManager mSubscriptionManager;
53     AlertDialog.Builder mBuilder;
54     View mDialogLayout;
55     private final String SIM_NAME = "sim_name";
56     private final String TINT_POS = "tint_pos";
57 
58     @Override
onCreate(Bundle bundle)59     public void onCreate(Bundle bundle) {
60         super.onCreate(bundle);
61         mContext = this;
62         final Bundle extras = getIntent().getExtras();
63         mSlotId = extras.getInt(SimSettings.EXTRA_SLOT_ID, -1);
64         mSubscriptionManager = SubscriptionManager.from(mContext);
65         mSubInfoRecord = mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(mSlotId);
66         mTintArr = mContext.getResources().getIntArray(com.android.internal.R.array.sim_colors);
67         mColorStrings = mContext.getResources().getStringArray(R.array.color_picker);
68         mTintSelectorPos = 0;
69 
70         mBuilder = new AlertDialog.Builder(mContext);
71         LayoutInflater inflater = (LayoutInflater)mContext
72                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
73         mDialogLayout = inflater.inflate(R.layout.multi_sim_dialog, null);
74         mBuilder.setView(mDialogLayout);
75 
76         createEditDialog(bundle);
77     }
78 
79     @Override
onSaveInstanceState(Bundle savedInstanceState)80     public void onSaveInstanceState(Bundle savedInstanceState) {
81         savedInstanceState.putInt(TINT_POS, mTintSelectorPos);
82 
83         final EditText nameText = (EditText)mDialogLayout.findViewById(R.id.sim_name);
84         savedInstanceState.putString(SIM_NAME, nameText.getText().toString());
85 
86         super.onSaveInstanceState(savedInstanceState);
87 
88     }
89 
90     @Override
onRestoreInstanceState(Bundle savedInstanceState)91     public void onRestoreInstanceState(Bundle savedInstanceState) {
92         super.onRestoreInstanceState(savedInstanceState);
93 
94         int pos = savedInstanceState.getInt(TINT_POS);
95         final Spinner tintSpinner = (Spinner) mDialogLayout.findViewById(R.id.spinner);
96         tintSpinner.setSelection(pos);
97         mTintSelectorPos = pos;
98 
99         EditText nameText = (EditText)mDialogLayout.findViewById(R.id.sim_name);
100         nameText.setText(savedInstanceState.getString(SIM_NAME));
101     }
102 
createEditDialog(Bundle bundle)103     private void createEditDialog(Bundle bundle) {
104         final Resources res = mContext.getResources();
105         EditText nameText = (EditText)mDialogLayout.findViewById(R.id.sim_name);
106         nameText.setText(mSubInfoRecord.getDisplayName());
107 
108         final Spinner tintSpinner = (Spinner) mDialogLayout.findViewById(R.id.spinner);
109         SelectColorAdapter adapter = new SelectColorAdapter(mContext,
110                 R.layout.settings_color_picker_item, mColorStrings);
111         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
112         tintSpinner.setAdapter(adapter);
113 
114         for (int i = 0; i < mTintArr.length; i++) {
115             if (mTintArr[i] == mSubInfoRecord.getIconTint()) {
116                 tintSpinner.setSelection(i);
117                 mTintSelectorPos = i;
118                 break;
119             }
120         }
121 
122         tintSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
123             @Override
124             public void onItemSelected(AdapterView<?> parent, View view,
125                                        int pos, long id){
126                 tintSpinner.setSelection(pos);
127                 mTintSelectorPos = pos;
128             }
129 
130             @Override
131             public void onNothingSelected(AdapterView<?> parent) {
132             }
133         });
134 
135         final TelephonyManager tm = (TelephonyManager) mContext.getSystemService(
136                 Context.TELEPHONY_SERVICE);
137         TextView numberView = (TextView)mDialogLayout.findViewById(R.id.number);
138         final String rawNumber =  tm.getLine1Number(mSubInfoRecord.getSubscriptionId());
139         if (TextUtils.isEmpty(rawNumber)) {
140             numberView.setText(res.getString(com.android.internal.R.string.unknownName));
141         } else {
142             numberView.setText(PhoneNumberUtils.formatNumber(rawNumber));
143         }
144 
145         String simCarrierName = tm.getSimOperatorName(mSubInfoRecord.getSubscriptionId());
146         TextView carrierView = (TextView)mDialogLayout.findViewById(R.id.carrier);
147         carrierView.setText(!TextUtils.isEmpty(simCarrierName) ? simCarrierName :
148                 mContext.getString(com.android.internal.R.string.unknownName));
149 
150         mBuilder.setTitle(String.format(res.getString(R.string.sim_editor_title),
151                 (mSubInfoRecord.getSimSlotIndex() + 1)));
152 
153         mBuilder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
154             @Override
155             public void onClick(DialogInterface dialog, int whichButton) {
156                 final EditText nameText = (EditText)mDialogLayout.findViewById(R.id.sim_name);
157 
158                 String displayName = nameText.getText().toString();
159                 int subId = mSubInfoRecord.getSubscriptionId();
160                 mSubInfoRecord.setDisplayName(displayName);
161                 mSubscriptionManager.setDisplayName(displayName, subId,
162                         SubscriptionManager.NAME_SOURCE_USER_INPUT);
163 
164                 final int tintSelected = tintSpinner.getSelectedItemPosition();
165                 int subscriptionId = mSubInfoRecord.getSubscriptionId();
166                 int tint = mTintArr[tintSelected];
167                 mSubInfoRecord.setIconTint(tint);
168                 mSubscriptionManager.setIconTint(tint, subscriptionId);
169                 dialog.dismiss();
170                 finish();
171             }
172         });
173 
174         mBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
175             @Override
176             public void onClick(DialogInterface dialog, int whichButton) {
177                 dialog.dismiss();
178                 finish();
179             }
180         });
181 
182         mBuilder.create().show();
183     }
184 
185     private class SelectColorAdapter extends ArrayAdapter<CharSequence> {
186         private Context mContext;
187         private int mResId;
188 
SelectColorAdapter( Context context, int resource, String[] arr)189         public SelectColorAdapter(
190                 Context context, int resource, String[] arr) {
191             super(context, resource, arr);
192             mContext = context;
193             mResId = resource;
194         }
195 
196         @Override
getView(int position, View convertView, ViewGroup parent)197         public View getView(int position, View convertView, ViewGroup parent) {
198             LayoutInflater inflater = (LayoutInflater)
199                     mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
200 
201             View rowView;
202             final ViewHolder holder;
203             Resources res = mContext.getResources();
204             int iconSize = res.getDimensionPixelSize(R.dimen.color_swatch_size);
205             int strokeWidth = res.getDimensionPixelSize(R.dimen.color_swatch_stroke_width);
206 
207             if (convertView == null) {
208                 // Cache views for faster scrolling
209                 rowView = inflater.inflate(mResId, null);
210                 holder = new ViewHolder();
211                 ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
212                 drawable.setIntrinsicHeight(iconSize);
213                 drawable.setIntrinsicWidth(iconSize);
214                 drawable.getPaint().setStrokeWidth(strokeWidth);
215                 holder.label = (TextView) rowView.findViewById(R.id.color_text);
216                 holder.icon = (ImageView) rowView.findViewById(R.id.color_icon);
217                 holder.swatch = drawable;
218                 rowView.setTag(holder);
219             } else {
220                 rowView = convertView;
221                 holder = (ViewHolder) rowView.getTag();
222             }
223 
224             holder.label.setText(getItem(position));
225             holder.swatch.getPaint().setColor(mTintArr[position]);
226             holder.swatch.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
227             holder.icon.setVisibility(View.VISIBLE);
228             holder.icon.setImageDrawable(holder.swatch);
229             return rowView;
230         }
231 
232         @Override
getDropDownView(int position, View convertView, ViewGroup parent)233         public View getDropDownView(int position, View convertView, ViewGroup parent) {
234             View rowView = getView(position, convertView, parent);
235             final ViewHolder holder = (ViewHolder) rowView.getTag();
236 
237             if (mTintSelectorPos == position) {
238                 holder.swatch.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
239             } else {
240                 holder.swatch.getPaint().setStyle(Paint.Style.STROKE);
241             }
242             holder.icon.setVisibility(View.VISIBLE);
243             return rowView;
244         }
245 
246         private class ViewHolder {
247             TextView label;
248             ImageView icon;
249             ShapeDrawable swatch;
250         }
251     }
252 }
253