Home | History | Annotate | Download | only in incallui
      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.incallui;
     18 
     19 import android.telecom.PhoneAccount;
     20 import android.telecom.PhoneAccountHandle;
     21 
     22 import android.app.AlertDialog;
     23 import android.app.Dialog;
     24 import android.app.DialogFragment;
     25 import android.app.FragmentManager;
     26 import android.content.Context;
     27 import android.content.DialogInterface;
     28 import android.os.Bundle;
     29 import android.telecom.TelecomManager;
     30 import android.view.LayoutInflater;
     31 import android.view.View;
     32 import android.view.ViewGroup;
     33 import android.widget.ArrayAdapter;
     34 import android.widget.ImageView;
     35 import android.widget.ListAdapter;
     36 import android.widget.TextView;
     37 
     38 import com.android.contacts.common.R;
     39 
     40 import java.util.List;
     41 
     42 /**
     43  * Dialog that allows the user to switch between default SIM cards
     44  */
     45 public class SelectPhoneAccountDialogFragment extends DialogFragment {
     46     private List<PhoneAccountHandle> mAccountHandles;
     47     private boolean mIsSelected;
     48     private TelecomManager mTelecomManager;
     49 
     50     /**
     51      * Shows the account selection dialog.
     52      * This is the preferred way to show this dialog.
     53      *
     54      * @param fragmentManager The fragment manager.
     55      * @param accountHandles The {@code PhoneAccountHandle}s available to select from.
     56      */
     57     public static void showAccountDialog(FragmentManager fragmentManager,
     58             List<PhoneAccountHandle> accountHandles) {
     59         SelectPhoneAccountDialogFragment fragment =
     60                 new SelectPhoneAccountDialogFragment(accountHandles);
     61         fragment.show(fragmentManager, "selectAccount");
     62     }
     63 
     64     public SelectPhoneAccountDialogFragment(List<PhoneAccountHandle> accountHandles) {
     65         super();
     66         mAccountHandles = accountHandles;
     67     }
     68 
     69     @Override
     70     public Dialog onCreateDialog(Bundle savedInstanceState) {
     71         mIsSelected = false;
     72         mTelecomManager =
     73                 (TelecomManager) getActivity().getSystemService(Context.TELECOM_SERVICE);
     74 
     75         final DialogInterface.OnClickListener selectionListener =
     76                 new DialogInterface.OnClickListener() {
     77             @Override
     78             public void onClick(DialogInterface dialog, int which) {
     79                 mIsSelected = true;
     80                 PhoneAccountHandle selectedAccountHandle = mAccountHandles.get(which);
     81                 InCallPresenter.getInstance().handleAccountSelection(selectedAccountHandle);
     82             }
     83         };
     84 
     85         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
     86 
     87         ListAdapter selectAccountListAdapter = new SelectAccountListAdapter(
     88                 builder.getContext(),
     89                 R.layout.select_account_list_item,
     90                 mAccountHandles);
     91 
     92         return builder.setTitle(R.string.select_account_dialog_title)
     93                 .setAdapter(selectAccountListAdapter, selectionListener)
     94                 .create();
     95     }
     96 
     97     private class SelectAccountListAdapter extends ArrayAdapter<PhoneAccountHandle> {
     98         private Context mContext;
     99         private int mResId;
    100 
    101         public SelectAccountListAdapter(
    102                 Context context, int resource, List<PhoneAccountHandle> accountHandles) {
    103             super(context, resource, accountHandles);
    104             mContext = context;
    105             mResId = resource;
    106         }
    107 
    108         @Override
    109         public View getView(int position, View convertView, ViewGroup parent) {
    110             LayoutInflater inflater = (LayoutInflater)
    111                     mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    112 
    113             View rowView;
    114             final ViewHolder holder;
    115 
    116             if (convertView == null) {
    117                 // Cache views for faster scrolling
    118                 rowView = inflater.inflate(mResId, null);
    119                 holder = new ViewHolder();
    120                 holder.textView = (TextView) rowView.findViewById(R.id.text);
    121                 holder.imageView = (ImageView) rowView.findViewById(R.id.icon);
    122                 rowView.setTag(holder);
    123             }
    124             else {
    125                 rowView = convertView;
    126                 holder = (ViewHolder) rowView.getTag();
    127             }
    128 
    129             PhoneAccountHandle accountHandle = getItem(position);
    130             PhoneAccount account = mTelecomManager.getPhoneAccount(accountHandle);
    131             holder.textView.setText(account.getLabel());
    132             holder.imageView.setImageDrawable(account.getIcon(mContext));
    133             return rowView;
    134         }
    135 
    136         private class ViewHolder {
    137             TextView textView;
    138             ImageView imageView;
    139         }
    140     }
    141 
    142     @Override
    143     public void onPause() {
    144         if (!mIsSelected) {
    145             InCallPresenter.getInstance().cancelAccountSelection();
    146         }
    147         super.onPause();
    148     }
    149 }