blob: da2b468da63a8855f7127a324fb1362fdf9c0a38 [file] [log] [blame]
Nancy Chen1821c252014-10-08 12:45:34 -07001/*
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
Gary Mai69c182a2016-12-05 13:07:03 -080017package com.android.contacts.widget;
Nancy Chen1821c252014-10-08 12:45:34 -070018
Nancy Chen1821c252014-10-08 12:45:34 -070019import android.app.AlertDialog;
20import android.app.Dialog;
Brian Attwellc2e912c2014-10-27 12:29:44 -070021import android.app.DialogFragment;
Nancy Chen1821c252014-10-08 12:45:34 -070022import android.content.Context;
23import android.content.DialogInterface;
24import android.os.Bundle;
Yorke Leea2b62e02015-06-10 13:36:46 -070025import android.os.Handler;
26import android.os.ResultReceiver;
Brandon Maxwell62cfa6e2015-12-04 15:27:45 -080027import android.telecom.PhoneAccount;
28import android.telecom.PhoneAccountHandle;
Nancy Chen1821c252014-10-08 12:45:34 -070029import android.telecom.TelecomManager;
Nancy Chen84647562014-11-03 17:08:47 -080030import android.text.TextUtils;
Nancy Chen1821c252014-10-08 12:45:34 -070031import android.view.LayoutInflater;
32import android.view.View;
33import android.view.ViewGroup;
34import android.widget.ArrayAdapter;
Nancy Chenfdb76be2014-10-21 18:53:11 -070035import android.widget.CheckBox;
36import android.widget.CompoundButton;
Nancy Chen1821c252014-10-08 12:45:34 -070037import android.widget.ImageView;
Nancy Chenfdb76be2014-10-21 18:53:11 -070038import android.widget.LinearLayout;
Nancy Chen1821c252014-10-08 12:45:34 -070039import android.widget.ListAdapter;
40import android.widget.TextView;
41
Arthur Wang3f6a2442016-12-05 14:51:59 -080042import com.android.contacts.R;
Gary Mai69c182a2016-12-05 13:07:03 -080043import com.android.contacts.compat.PhoneAccountCompat;
44import com.android.contacts.compat.PhoneNumberUtilsCompat;
Nancy Chen1821c252014-10-08 12:45:34 -070045
Jay Shraunerebf0a0f2015-04-03 16:01:16 -070046import java.util.ArrayList;
Nancy Chen1821c252014-10-08 12:45:34 -070047import java.util.List;
48
49/**
Nancy Chenfdb76be2014-10-21 18:53:11 -070050 * Dialog that allows the user to select a phone accounts for a given action. Optionally provides
51 * the choice to set the phone account as default.
Nancy Chen1821c252014-10-08 12:45:34 -070052 */
Brian Attwellc2e912c2014-10-27 12:29:44 -070053public class SelectPhoneAccountDialogFragment extends DialogFragment {
Jay Shraunerebf0a0f2015-04-03 16:01:16 -070054 private static final String ARG_TITLE_RES_ID = "title_res_id";
55 private static final String ARG_CAN_SET_DEFAULT = "can_set_default";
56 private static final String ARG_ACCOUNT_HANDLES = "account_handles";
57 private static final String ARG_IS_DEFAULT_CHECKED = "is_default_checked";
Yorke Leea2b62e02015-06-10 13:36:46 -070058 private static final String ARG_LISTENER = "listener";
Jay Shraunerebf0a0f2015-04-03 16:01:16 -070059
Nancy Chen84647562014-11-03 17:08:47 -080060 private int mTitleResId;
Nancy Chenfdb76be2014-10-21 18:53:11 -070061 private boolean mCanSetDefault;
Nancy Chen1821c252014-10-08 12:45:34 -070062 private List<PhoneAccountHandle> mAccountHandles;
63 private boolean mIsSelected;
Nancy Chenfdb76be2014-10-21 18:53:11 -070064 private boolean mIsDefaultChecked;
Nancy Chen1821c252014-10-08 12:45:34 -070065 private TelecomManager mTelecomManager;
66 private SelectPhoneAccountListener mListener;
67
68 /**
Jay Shraunerebf0a0f2015-04-03 16:01:16 -070069 * Create new fragment instance with default title and no option to set as default.
Nancy Chen1821c252014-10-08 12:45:34 -070070 *
Nancy Chen1821c252014-10-08 12:45:34 -070071 * @param accountHandles The {@code PhoneAccountHandle}s available to select from.
Nancy Chen84647562014-11-03 17:08:47 -080072 * @param listener The listener for the results of the account selection.
Nancy Chen1821c252014-10-08 12:45:34 -070073 */
Jay Shraunerebf0a0f2015-04-03 16:01:16 -070074 public static SelectPhoneAccountDialogFragment newInstance(
Nancy Chen1821c252014-10-08 12:45:34 -070075 List<PhoneAccountHandle> accountHandles, SelectPhoneAccountListener listener) {
Jay Shraunerebf0a0f2015-04-03 16:01:16 -070076 return newInstance(R.string.select_account_dialog_title, false,
Nancy Chen84647562014-11-03 17:08:47 -080077 accountHandles, listener);
78 }
79
80 /**
Jay Shraunerebf0a0f2015-04-03 16:01:16 -070081 * Create new fragment instance.
Nancy Chen84647562014-11-03 17:08:47 -080082 * This method also allows specifying a custom title and "set default" checkbox.
83 *
Nancy Chen84647562014-11-03 17:08:47 -080084 * @param titleResId The resource ID for the string to use in the title of the dialog.
85 * @param canSetDefault {@code true} if the dialog should include an option to set the selection
86 * as the default. False otherwise.
87 * @param accountHandles The {@code PhoneAccountHandle}s available to select from.
88 * @param listener The listener for the results of the account selection.
89 */
Jay Shraunerebf0a0f2015-04-03 16:01:16 -070090 public static SelectPhoneAccountDialogFragment newInstance(int titleResId,
Nancy Chen84647562014-11-03 17:08:47 -080091 boolean canSetDefault, List<PhoneAccountHandle> accountHandles,
92 SelectPhoneAccountListener listener) {
Andrew Lee88cbb962015-06-01 12:16:15 -070093 ArrayList<PhoneAccountHandle> accountHandlesCopy = new ArrayList<PhoneAccountHandle>();
94 if (accountHandles != null) {
95 accountHandlesCopy.addAll(accountHandles);
Andrew Lee7e4758e2015-06-01 12:16:15 -070096 }
Jay Shraunerebf0a0f2015-04-03 16:01:16 -070097 SelectPhoneAccountDialogFragment fragment = new SelectPhoneAccountDialogFragment();
98 final Bundle args = new Bundle();
99 args.putInt(ARG_TITLE_RES_ID, titleResId);
100 args.putBoolean(ARG_CAN_SET_DEFAULT, canSetDefault);
Andrew Lee88cbb962015-06-01 12:16:15 -0700101 args.putParcelableArrayList(ARG_ACCOUNT_HANDLES, accountHandlesCopy);
Yorke Leea2b62e02015-06-10 13:36:46 -0700102 args.putParcelable(ARG_LISTENER, listener);
Jay Shraunerebf0a0f2015-04-03 16:01:16 -0700103 fragment.setArguments(args);
104 fragment.setListener(listener);
105 return fragment;
Nancy Chen1821c252014-10-08 12:45:34 -0700106 }
107
Jay Shraunerebf0a0f2015-04-03 16:01:16 -0700108 public SelectPhoneAccountDialogFragment() {
109 }
110
111 public void setListener(SelectPhoneAccountListener listener) {
Nancy Chen1821c252014-10-08 12:45:34 -0700112 mListener = listener;
113 }
114
Yorke Leea2b62e02015-06-10 13:36:46 -0700115 public static class SelectPhoneAccountListener extends ResultReceiver {
116 static final int RESULT_SELECTED = 1;
117 static final int RESULT_DISMISSED = 2;
118
119 static final String EXTRA_SELECTED_ACCOUNT_HANDLE = "extra_selected_account_handle";
120 static final String EXTRA_SET_DEFAULT = "extra_set_default";
121
122 public SelectPhoneAccountListener() {
123 super(new Handler());
124 }
125
126 @Override
127 protected void onReceiveResult(int resultCode, Bundle resultData) {
128 if (resultCode == RESULT_SELECTED) {
129 onPhoneAccountSelected(
130 (PhoneAccountHandle) resultData.getParcelable(
131 EXTRA_SELECTED_ACCOUNT_HANDLE),
132 resultData.getBoolean(EXTRA_SET_DEFAULT));
133 } else if (resultCode == RESULT_DISMISSED) {
134 onDialogDismissed();
135 }
136 }
137
138 public void onPhoneAccountSelected(PhoneAccountHandle selectedAccountHandle,
139 boolean setDefault) {}
140
141 public void onDialogDismissed() {}
Nancy Chen1821c252014-10-08 12:45:34 -0700142 }
143
144 @Override
Jay Shraunerebf0a0f2015-04-03 16:01:16 -0700145 public void onSaveInstanceState(Bundle outState) {
146 super.onSaveInstanceState(outState);
147 outState.putBoolean(ARG_IS_DEFAULT_CHECKED, mIsDefaultChecked);
148 }
149
150 @Override
Nancy Chen1821c252014-10-08 12:45:34 -0700151 public Dialog onCreateDialog(Bundle savedInstanceState) {
Jay Shraunerebf0a0f2015-04-03 16:01:16 -0700152 mTitleResId = getArguments().getInt(ARG_TITLE_RES_ID);
153 mCanSetDefault = getArguments().getBoolean(ARG_CAN_SET_DEFAULT);
154 mAccountHandles = getArguments().getParcelableArrayList(ARG_ACCOUNT_HANDLES);
Yorke Leea2b62e02015-06-10 13:36:46 -0700155 mListener = getArguments().getParcelable(ARG_LISTENER);
Jay Shraunerebf0a0f2015-04-03 16:01:16 -0700156 if (savedInstanceState != null) {
157 mIsDefaultChecked = savedInstanceState.getBoolean(ARG_IS_DEFAULT_CHECKED);
158 }
Nancy Chen1821c252014-10-08 12:45:34 -0700159 mIsSelected = false;
160 mTelecomManager =
161 (TelecomManager) getActivity().getSystemService(Context.TELECOM_SERVICE);
162
163 final DialogInterface.OnClickListener selectionListener =
164 new DialogInterface.OnClickListener() {
165 @Override
166 public void onClick(DialogInterface dialog, int which) {
167 mIsSelected = true;
168 PhoneAccountHandle selectedAccountHandle = mAccountHandles.get(which);
Yorke Leea2b62e02015-06-10 13:36:46 -0700169 final Bundle result = new Bundle();
170 result.putParcelable(SelectPhoneAccountListener.EXTRA_SELECTED_ACCOUNT_HANDLE,
171 selectedAccountHandle);
172 result.putBoolean(SelectPhoneAccountListener.EXTRA_SET_DEFAULT,
173 mIsDefaultChecked);
174 if (mListener != null) {
175 mListener.onReceiveResult(SelectPhoneAccountListener.RESULT_SELECTED, result);
176 }
Nancy Chenfdb76be2014-10-21 18:53:11 -0700177 }
178 };
179
180 final CompoundButton.OnCheckedChangeListener checkListener =
181 new CompoundButton.OnCheckedChangeListener() {
182 @Override
183 public void onCheckedChanged(CompoundButton check, boolean isChecked) {
184 mIsDefaultChecked = isChecked;
Nancy Chen1821c252014-10-08 12:45:34 -0700185 }
186 };
187
188 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Nancy Chen1821c252014-10-08 12:45:34 -0700189 ListAdapter selectAccountListAdapter = new SelectAccountListAdapter(
190 builder.getContext(),
191 R.layout.select_account_list_item,
192 mAccountHandles);
193
Nancy Chen84647562014-11-03 17:08:47 -0800194 AlertDialog dialog = builder.setTitle(mTitleResId)
Nancy Chen1821c252014-10-08 12:45:34 -0700195 .setAdapter(selectAccountListAdapter, selectionListener)
196 .create();
Nancy Chenfdb76be2014-10-21 18:53:11 -0700197
198 if (mCanSetDefault) {
199 // Generate custom checkbox view
200 LinearLayout checkboxLayout = (LinearLayout) getActivity()
201 .getLayoutInflater()
202 .inflate(R.layout.default_account_checkbox, null);
203
204 CheckBox cb =
205 (CheckBox) checkboxLayout.findViewById(R.id.default_account_checkbox_view);
206 cb.setOnCheckedChangeListener(checkListener);
Jay Shraunerebf0a0f2015-04-03 16:01:16 -0700207 cb.setChecked(mIsDefaultChecked);
Nancy Chenfdb76be2014-10-21 18:53:11 -0700208
209 dialog.getListView().addFooterView(checkboxLayout);
210 }
211
212 return dialog;
Nancy Chen1821c252014-10-08 12:45:34 -0700213 }
214
215 private class SelectAccountListAdapter extends ArrayAdapter<PhoneAccountHandle> {
Nancy Chen1821c252014-10-08 12:45:34 -0700216 private int mResId;
217
218 public SelectAccountListAdapter(
219 Context context, int resource, List<PhoneAccountHandle> accountHandles) {
220 super(context, resource, accountHandles);
Nancy Chen1821c252014-10-08 12:45:34 -0700221 mResId = resource;
222 }
223
224 @Override
225 public View getView(int position, View convertView, ViewGroup parent) {
226 LayoutInflater inflater = (LayoutInflater)
Ihab Awadd1360e12014-10-24 11:43:33 -0700227 getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Nancy Chen1821c252014-10-08 12:45:34 -0700228
229 View rowView;
230 final ViewHolder holder;
231
232 if (convertView == null) {
233 // Cache views for faster scrolling
234 rowView = inflater.inflate(mResId, null);
235 holder = new ViewHolder();
Nancy Chen84647562014-11-03 17:08:47 -0800236 holder.labelTextView = (TextView) rowView.findViewById(R.id.label);
237 holder.numberTextView = (TextView) rowView.findViewById(R.id.number);
Nancy Chen1821c252014-10-08 12:45:34 -0700238 holder.imageView = (ImageView) rowView.findViewById(R.id.icon);
239 rowView.setTag(holder);
240 }
241 else {
242 rowView = convertView;
243 holder = (ViewHolder) rowView.getTag();
244 }
245
246 PhoneAccountHandle accountHandle = getItem(position);
247 PhoneAccount account = mTelecomManager.getPhoneAccount(accountHandle);
Yorke Leea2bd6f52015-05-04 18:29:00 -0700248 if (account == null) {
249 return rowView;
250 }
Nancy Chen84647562014-11-03 17:08:47 -0800251 holder.labelTextView.setText(account.getLabel());
252 if (account.getAddress() == null ||
253 TextUtils.isEmpty(account.getAddress().getSchemeSpecificPart())) {
254 holder.numberTextView.setVisibility(View.GONE);
255 } else {
256 holder.numberTextView.setVisibility(View.VISIBLE);
Ihab Awad07685ea2014-12-05 13:37:50 -0800257 holder.numberTextView.setText(
Brandon Maxwell62cfa6e2015-12-04 15:27:45 -0800258 PhoneNumberUtilsCompat.createTtsSpannable(
Ihab Awad07685ea2014-12-05 13:37:50 -0800259 account.getAddress().getSchemeSpecificPart()));
Nancy Chen84647562014-11-03 17:08:47 -0800260 }
Brandon Maxwell089df232015-12-18 12:30:55 -0800261 holder.imageView.setImageDrawable(PhoneAccountCompat.createIconDrawable(account,
262 getContext()));
Nancy Chen1821c252014-10-08 12:45:34 -0700263 return rowView;
264 }
265
266 private class ViewHolder {
Nancy Chen84647562014-11-03 17:08:47 -0800267 TextView labelTextView;
268 TextView numberTextView;
Nancy Chen1821c252014-10-08 12:45:34 -0700269 ImageView imageView;
270 }
271 }
272
273 @Override
Yorke Lee6c4b7b12015-04-06 19:49:27 -0700274 public void onStop() {
Yorke Leea2b62e02015-06-10 13:36:46 -0700275 if (!mIsSelected && mListener != null) {
276 mListener.onReceiveResult(SelectPhoneAccountListener.RESULT_DISMISSED, null);
Nancy Chen1821c252014-10-08 12:45:34 -0700277 }
Yorke Lee6c4b7b12015-04-06 19:49:27 -0700278 super.onStop();
Nancy Chen1821c252014-10-08 12:45:34 -0700279 }
Brian Attwelld46781c2014-10-31 18:45:34 +0000280}