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