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