1 /* 2 * Copyright (C) 2014 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 17 package com.android.settings.sim; 18 19 import android.app.Activity; 20 import android.app.AlertDialog; 21 import android.app.Dialog; 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.DialogInterface; 25 import android.content.res.Resources; 26 import android.os.Bundle; 27 import android.telecom.PhoneAccount; 28 import android.telecom.PhoneAccountHandle; 29 import android.telecom.TelecomManager; 30 import android.telephony.SubscriptionInfo; 31 import android.telephony.SubscriptionManager; 32 import android.text.TextUtils; 33 import android.util.Log; 34 import android.view.KeyEvent; 35 import android.view.LayoutInflater; 36 import android.view.View; 37 import android.view.ViewGroup; 38 import android.widget.ArrayAdapter; 39 import android.widget.ImageView; 40 import android.widget.ListAdapter; 41 import android.widget.TextView; 42 import android.widget.Toast; 43 44 import com.android.settings.R; 45 import com.android.settings.Utils; 46 import com.android.internal.telephony.Phone; 47 import com.android.internal.telephony.PhoneFactory; 48 49 import java.util.ArrayList; 50 import java.util.Iterator; 51 import java.util.List; 52 53 public class SimDialogActivity extends Activity { 54 private static String TAG = "SimDialogActivity"; 55 56 public static String PREFERRED_SIM = "preferred_sim"; 57 public static String DIALOG_TYPE_KEY = "dialog_type"; 58 public static final int INVALID_PICK = -1; 59 public static final int DATA_PICK = 0; 60 public static final int CALLS_PICK = 1; 61 public static final int SMS_PICK = 2; 62 public static final int PREFERRED_PICK = 3; 63 64 @Override onCreate(Bundle savedInstanceState)65 protected void onCreate(Bundle savedInstanceState) { 66 super.onCreate(savedInstanceState); 67 final Bundle extras = getIntent().getExtras(); 68 final int dialogType = extras.getInt(DIALOG_TYPE_KEY, INVALID_PICK); 69 70 switch (dialogType) { 71 case DATA_PICK: 72 case CALLS_PICK: 73 case SMS_PICK: 74 createDialog(this, dialogType).show(); 75 break; 76 case PREFERRED_PICK: 77 displayPreferredDialog(extras.getInt(PREFERRED_SIM)); 78 break; 79 default: 80 throw new IllegalArgumentException("Invalid dialog type " + dialogType + " sent."); 81 } 82 83 } 84 displayPreferredDialog(final int slotId)85 private void displayPreferredDialog(final int slotId) { 86 final Resources res = getResources(); 87 final Context context = getApplicationContext(); 88 final SubscriptionInfo sir = Utils.findRecordBySlotId(context, slotId); 89 90 if (sir != null) { 91 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 92 alertDialogBuilder.setTitle(R.string.sim_preferred_title); 93 alertDialogBuilder.setMessage(res.getString( 94 R.string.sim_preferred_message, sir.getDisplayName())); 95 96 alertDialogBuilder.setPositiveButton(R.string.yes, new 97 DialogInterface.OnClickListener() { 98 @Override 99 public void onClick(DialogInterface dialog, int id) { 100 final int subId = sir.getSubscriptionId(); 101 PhoneAccountHandle phoneAccountHandle = 102 subscriptionIdToPhoneAccountHandle(subId); 103 setDefaultDataSubId(context, subId); 104 setDefaultSmsSubId(context, subId); 105 setUserSelectedOutgoingPhoneAccount(phoneAccountHandle); 106 finish(); 107 } 108 }); 109 alertDialogBuilder.setNegativeButton(R.string.no, new 110 DialogInterface.OnClickListener() { 111 @Override 112 public void onClick(DialogInterface dialog,int id) { 113 finish(); 114 } 115 }); 116 117 alertDialogBuilder.create().show(); 118 } else { 119 finish(); 120 } 121 } 122 setDefaultDataSubId(final Context context, final int subId)123 private static void setDefaultDataSubId(final Context context, final int subId) { 124 final SubscriptionManager subscriptionManager = SubscriptionManager.from(context); 125 subscriptionManager.setDefaultDataSubId(subId); 126 Toast.makeText(context, R.string.data_switch_started, Toast.LENGTH_LONG).show(); 127 } 128 setDefaultSmsSubId(final Context context, final int subId)129 private static void setDefaultSmsSubId(final Context context, final int subId) { 130 final SubscriptionManager subscriptionManager = SubscriptionManager.from(context); 131 subscriptionManager.setDefaultSmsSubId(subId); 132 } 133 setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle phoneAccount)134 private void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle phoneAccount) { 135 final TelecomManager telecomManager = TelecomManager.from(this); 136 telecomManager.setUserSelectedOutgoingPhoneAccount(phoneAccount); 137 } 138 subscriptionIdToPhoneAccountHandle(final int subId)139 private PhoneAccountHandle subscriptionIdToPhoneAccountHandle(final int subId) { 140 final TelecomManager telecomManager = TelecomManager.from(this); 141 final Iterator<PhoneAccountHandle> phoneAccounts = 142 telecomManager.getCallCapablePhoneAccounts().listIterator(); 143 144 while (phoneAccounts.hasNext()) { 145 final PhoneAccountHandle phoneAccountHandle = phoneAccounts.next(); 146 final PhoneAccount phoneAccount = telecomManager.getPhoneAccount(phoneAccountHandle); 147 final String phoneAccountId = phoneAccountHandle.getId(); 148 149 if (phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) 150 && TextUtils.isDigitsOnly(phoneAccountId) 151 && Integer.parseInt(phoneAccountId) == subId){ 152 return phoneAccountHandle; 153 } 154 } 155 156 return null; 157 } 158 createDialog(final Context context, final int id)159 public Dialog createDialog(final Context context, final int id) { 160 final ArrayList<String> list = new ArrayList<String>(); 161 final SubscriptionManager subscriptionManager = SubscriptionManager.from(context); 162 final List<SubscriptionInfo> subInfoList = 163 subscriptionManager.getActiveSubscriptionInfoList(); 164 final int selectableSubInfoLength = subInfoList == null ? 0 : subInfoList.size(); 165 166 final DialogInterface.OnClickListener selectionListener = 167 new DialogInterface.OnClickListener() { 168 @Override 169 public void onClick(DialogInterface dialog, int value) { 170 171 final SubscriptionInfo sir; 172 173 switch (id) { 174 case DATA_PICK: 175 sir = subInfoList.get(value); 176 setDefaultDataSubId(context, sir.getSubscriptionId()); 177 break; 178 case CALLS_PICK: 179 final TelecomManager telecomManager = 180 TelecomManager.from(context); 181 final List<PhoneAccountHandle> phoneAccountsList = 182 telecomManager.getCallCapablePhoneAccounts(); 183 setUserSelectedOutgoingPhoneAccount( 184 value < 1 ? null : phoneAccountsList.get(value - 1)); 185 break; 186 case SMS_PICK: 187 sir = subInfoList.get(value); 188 setDefaultSmsSubId(context, sir.getSubscriptionId()); 189 break; 190 default: 191 throw new IllegalArgumentException("Invalid dialog type " 192 + id + " in SIM dialog."); 193 } 194 195 finish(); 196 } 197 }; 198 199 Dialog.OnKeyListener keyListener = new Dialog.OnKeyListener() { 200 @Override 201 public boolean onKey(DialogInterface arg0, int keyCode, 202 KeyEvent event) { 203 if (keyCode == KeyEvent.KEYCODE_BACK) { 204 finish(); 205 } 206 return true; 207 } 208 }; 209 210 ArrayList<SubscriptionInfo> callsSubInfoList = new ArrayList<SubscriptionInfo>(); 211 if (id == CALLS_PICK) { 212 final TelecomManager telecomManager = TelecomManager.from(context); 213 final Iterator<PhoneAccountHandle> phoneAccounts = 214 telecomManager.getCallCapablePhoneAccounts().listIterator(); 215 216 list.add(getResources().getString(R.string.sim_calls_ask_first_prefs_title)); 217 callsSubInfoList.add(null); 218 while (phoneAccounts.hasNext()) { 219 final PhoneAccount phoneAccount = 220 telecomManager.getPhoneAccount(phoneAccounts.next()); 221 list.add((String)phoneAccount.getLabel()); 222 // Added check to add entry into callsSubInforList only if phoneAccountId is int 223 // Todo : Might have to change it later based on b/18904714 224 if (phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) && 225 TextUtils.isDigitsOnly(phoneAccount.getAccountHandle().getId())) { 226 final String phoneAccountId = phoneAccount.getAccountHandle().getId(); 227 final SubscriptionInfo sir = Utils.findRecordBySubId(context, 228 Integer.parseInt(phoneAccountId)); 229 callsSubInfoList.add(sir); 230 } else { 231 callsSubInfoList.add(null); 232 } 233 } 234 } else { 235 for (int i = 0; i < selectableSubInfoLength; ++i) { 236 final SubscriptionInfo sir = subInfoList.get(i); 237 CharSequence displayName = sir.getDisplayName(); 238 if (displayName == null) { 239 displayName = ""; 240 } 241 list.add(displayName.toString()); 242 } 243 } 244 245 String[] arr = list.toArray(new String[0]); 246 247 AlertDialog.Builder builder = new AlertDialog.Builder(context); 248 249 ListAdapter adapter = new SelectAccountListAdapter( 250 id == CALLS_PICK ? callsSubInfoList : subInfoList, 251 builder.getContext(), 252 R.layout.select_account_list_item, 253 arr, id); 254 255 switch (id) { 256 case DATA_PICK: 257 builder.setTitle(R.string.select_sim_for_data); 258 break; 259 case CALLS_PICK: 260 builder.setTitle(R.string.select_sim_for_calls); 261 break; 262 case SMS_PICK: 263 builder.setTitle(R.string.sim_card_select_title); 264 break; 265 default: 266 throw new IllegalArgumentException("Invalid dialog type " 267 + id + " in SIM dialog."); 268 } 269 270 Dialog dialog = builder.setAdapter(adapter, selectionListener).create(); 271 dialog.setOnKeyListener(keyListener); 272 273 dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { 274 @Override 275 public void onCancel(DialogInterface dialogInterface) { 276 finish(); 277 } 278 }); 279 280 return dialog; 281 282 } 283 284 private class SelectAccountListAdapter extends ArrayAdapter<String> { 285 private Context mContext; 286 private int mResId; 287 private int mDialogId; 288 private final float OPACITY = 0.54f; 289 private List<SubscriptionInfo> mSubInfoList; 290 SelectAccountListAdapter(List<SubscriptionInfo> subInfoList, Context context, int resource, String[] arr, int dialogId)291 public SelectAccountListAdapter(List<SubscriptionInfo> subInfoList, 292 Context context, int resource, String[] arr, int dialogId) { 293 super(context, resource, arr); 294 mContext = context; 295 mResId = resource; 296 mDialogId = dialogId; 297 mSubInfoList = subInfoList; 298 } 299 300 @Override getView(int position, View convertView, ViewGroup parent)301 public View getView(int position, View convertView, ViewGroup parent) { 302 LayoutInflater inflater = (LayoutInflater) 303 mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 304 View rowView; 305 final ViewHolder holder; 306 307 if (convertView == null) { 308 // Cache views for faster scrolling 309 rowView = inflater.inflate(mResId, null); 310 holder = new ViewHolder(); 311 holder.title = (TextView) rowView.findViewById(R.id.title); 312 holder.summary = (TextView) rowView.findViewById(R.id.summary); 313 holder.icon = (ImageView) rowView.findViewById(R.id.icon); 314 rowView.setTag(holder); 315 } else { 316 rowView = convertView; 317 holder = (ViewHolder) rowView.getTag(); 318 } 319 320 final SubscriptionInfo sir = mSubInfoList.get(position); 321 if (sir == null) { 322 holder.title.setText(getItem(position)); 323 holder.summary.setText(""); 324 holder.icon.setImageDrawable(getResources() 325 .getDrawable(R.drawable.ic_live_help)); 326 holder.icon.setAlpha(OPACITY); 327 } else { 328 holder.title.setText(sir.getDisplayName()); 329 holder.summary.setText(sir.getNumber()); 330 holder.icon.setImageBitmap(sir.createIconBitmap(mContext)); 331 } 332 return rowView; 333 } 334 335 private class ViewHolder { 336 TextView title; 337 TextView summary; 338 ImageView icon; 339 } 340 } 341 } 342