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 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 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 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 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.getLine1NumberForSubscriber( 139 mSubInfoRecord.getSubscriptionId()); 140 if (TextUtils.isEmpty(rawNumber)) { 141 numberView.setText(res.getString(com.android.internal.R.string.unknownName)); 142 } else { 143 numberView.setText(PhoneNumberUtils.formatNumber(rawNumber)); 144 } 145 146 String simCarrierName = tm.getSimOperatorNameForSubscription(mSubInfoRecord 147 .getSubscriptionId()); 148 TextView carrierView = (TextView)mDialogLayout.findViewById(R.id.carrier); 149 carrierView.setText(!TextUtils.isEmpty(simCarrierName) ? simCarrierName : 150 mContext.getString(com.android.internal.R.string.unknownName)); 151 152 mBuilder.setTitle(String.format(res.getString(R.string.sim_editor_title), 153 (mSubInfoRecord.getSimSlotIndex() + 1))); 154 155 mBuilder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() { 156 @Override 157 public void onClick(DialogInterface dialog, int whichButton) { 158 final EditText nameText = (EditText)mDialogLayout.findViewById(R.id.sim_name); 159 160 String displayName = nameText.getText().toString(); 161 int subId = mSubInfoRecord.getSubscriptionId(); 162 mSubInfoRecord.setDisplayName(displayName); 163 mSubscriptionManager.setDisplayName(displayName, subId, 164 SubscriptionManager.NAME_SOURCE_USER_INPUT); 165 166 final int tintSelected = tintSpinner.getSelectedItemPosition(); 167 int subscriptionId = mSubInfoRecord.getSubscriptionId(); 168 int tint = mTintArr[tintSelected]; 169 mSubInfoRecord.setIconTint(tint); 170 mSubscriptionManager.setIconTint(tint, subscriptionId); 171 dialog.dismiss(); 172 finish(); 173 } 174 }); 175 176 mBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 177 @Override 178 public void onClick(DialogInterface dialog, int whichButton) { 179 dialog.dismiss(); 180 finish(); 181 } 182 }); 183 184 mBuilder.create().show(); 185 } 186 187 private class SelectColorAdapter extends ArrayAdapter<CharSequence> { 188 private Context mContext; 189 private int mResId; 190 191 public SelectColorAdapter( 192 Context context, int resource, String[] arr) { 193 super(context, resource, arr); 194 mContext = context; 195 mResId = resource; 196 } 197 198 @Override 199 public View getView(int position, View convertView, ViewGroup parent) { 200 LayoutInflater inflater = (LayoutInflater) 201 mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 202 203 View rowView; 204 final ViewHolder holder; 205 Resources res = mContext.getResources(); 206 int iconSize = res.getDimensionPixelSize(R.dimen.color_swatch_size); 207 int strokeWidth = res.getDimensionPixelSize(R.dimen.color_swatch_stroke_width); 208 209 if (convertView == null) { 210 // Cache views for faster scrolling 211 rowView = inflater.inflate(mResId, null); 212 holder = new ViewHolder(); 213 ShapeDrawable drawable = new ShapeDrawable(new OvalShape()); 214 drawable.setIntrinsicHeight(iconSize); 215 drawable.setIntrinsicWidth(iconSize); 216 drawable.getPaint().setStrokeWidth(strokeWidth); 217 holder.label = (TextView) rowView.findViewById(R.id.color_text); 218 holder.icon = (ImageView) rowView.findViewById(R.id.color_icon); 219 holder.swatch = drawable; 220 rowView.setTag(holder); 221 } else { 222 rowView = convertView; 223 holder = (ViewHolder) rowView.getTag(); 224 } 225 226 holder.label.setText(getItem(position)); 227 holder.swatch.getPaint().setColor(mTintArr[position]); 228 holder.swatch.getPaint().setStyle(Paint.Style.FILL_AND_STROKE); 229 holder.icon.setVisibility(View.VISIBLE); 230 holder.icon.setImageDrawable(holder.swatch); 231 return rowView; 232 } 233 234 @Override 235 public View getDropDownView(int position, View convertView, ViewGroup parent) { 236 View rowView = getView(position, convertView, parent); 237 final ViewHolder holder = (ViewHolder) rowView.getTag(); 238 239 if (mTintSelectorPos == position) { 240 holder.swatch.getPaint().setStyle(Paint.Style.FILL_AND_STROKE); 241 } else { 242 holder.swatch.getPaint().setStyle(Paint.Style.STROKE); 243 } 244 holder.icon.setVisibility(View.VISIBLE); 245 return rowView; 246 } 247 248 private class ViewHolder { 249 TextView label; 250 ImageView icon; 251 ShapeDrawable swatch; 252 } 253 } 254 } 255