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