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