blob: 709ce4132dca975eb543f0a09f1458e39c47fa2c [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
17package com.android.contacts.common.widget;
18
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;
Brandon Maxwell089df232015-12-18 12:30:55 -080024import android.graphics.drawable.Icon;
Nancy Chen1821c252014-10-08 12:45:34 -070025import android.os.Bundle;
Yorke Leea2b62e02015-06-10 13:36:46 -070026import android.os.Handler;
27import android.os.ResultReceiver;
Brandon Maxwell62cfa6e2015-12-04 15:27:45 -080028import android.telecom.PhoneAccount;
29import android.telecom.PhoneAccountHandle;
Nancy Chen1821c252014-10-08 12:45:34 -070030import android.telecom.TelecomManager;
Nancy Chen84647562014-11-03 17:08:47 -080031import android.text.TextUtils;
Nancy Chen1821c252014-10-08 12:45:34 -070032import android.view.LayoutInflater;
33import android.view.View;
34import android.view.ViewGroup;
35import android.widget.ArrayAdapter;
Nancy Chenfdb76be2014-10-21 18:53:11 -070036import android.widget.CheckBox;
37import android.widget.CompoundButton;
Nancy Chen1821c252014-10-08 12:45:34 -070038import android.widget.ImageView;
Nancy Chenfdb76be2014-10-21 18:53:11 -070039import android.widget.LinearLayout;
Nancy Chen1821c252014-10-08 12:45:34 -070040import android.widget.ListAdapter;
41import android.widget.TextView;
42
43import com.android.contacts.common.R;
Brandon Maxwell089df232015-12-18 12:30:55 -080044import com.android.contacts.common.compat.PhoneAccountCompat;
Brandon Maxwell62cfa6e2015-12-04 15:27:45 -080045import com.android.contacts.common.compat.PhoneNumberUtilsCompat;
Nancy Chen1821c252014-10-08 12:45:34 -070046
Jay Shraunerebf0a0f2015-04-03 16:01:16 -070047import java.util.ArrayList;
Nancy Chen1821c252014-10-08 12:45:34 -070048import java.util.List;
49
50/**
Nancy Chenfdb76be2014-10-21 18:53:11 -070051 * Dialog that allows the user to select a phone accounts for a given action. Optionally provides
52 * the choice to set the phone account as default.
Nancy Chen1821c252014-10-08 12:45:34 -070053 */
Brian Attwellc2e912c2014-10-27 12:29:44 -070054public class SelectPhoneAccountDialogFragment extends DialogFragment {
Jay Shraunerebf0a0f2015-04-03 16:01:16 -070055 private static final String ARG_TITLE_RES_ID = "title_res_id";
56 private static final String ARG_CAN_SET_DEFAULT = "can_set_default";
57 private static final String ARG_ACCOUNT_HANDLES = "account_handles";
58 private static final String ARG_IS_DEFAULT_CHECKED = "is_default_checked";
Yorke Leea2b62e02015-06-10 13:36:46 -070059 private static final String ARG_LISTENER = "listener";
Jay Shraunerebf0a0f2015-04-03 16:01:16 -070060
Nancy Chen84647562014-11-03 17:08:47 -080061 private int mTitleResId;
Nancy Chenfdb76be2014-10-21 18:53:11 -070062 private boolean mCanSetDefault;
Nancy Chen1821c252014-10-08 12:45:34 -070063 private List<PhoneAccountHandle> mAccountHandles;
64 private boolean mIsSelected;
Nancy Chenfdb76be2014-10-21 18:53:11 -070065 private boolean mIsDefaultChecked;
Nancy Chen1821c252014-10-08 12:45:34 -070066 private TelecomManager mTelecomManager;
67 private SelectPhoneAccountListener mListener;
68
69 /**
Jay Shraunerebf0a0f2015-04-03 16:01:16 -070070 * Create new fragment instance with default title and no option to set as default.
Nancy Chen1821c252014-10-08 12:45:34 -070071 *
Nancy Chen1821c252014-10-08 12:45:34 -070072 * @param accountHandles The {@code PhoneAccountHandle}s available to select from.
Nancy Chen84647562014-11-03 17:08:47 -080073 * @param listener The listener for the results of the account selection.
Nancy Chen1821c252014-10-08 12:45:34 -070074 */
Jay Shraunerebf0a0f2015-04-03 16:01:16 -070075 public static SelectPhoneAccountDialogFragment newInstance(
Nancy Chen1821c252014-10-08 12:45:34 -070076 List<PhoneAccountHandle> accountHandles, SelectPhoneAccountListener listener) {
Jay Shraunerebf0a0f2015-04-03 16:01:16 -070077 return newInstance(R.string.select_account_dialog_title, false,
Nancy Chen84647562014-11-03 17:08:47 -080078 accountHandles, listener);
79 }
80
81 /**
Jay Shraunerebf0a0f2015-04-03 16:01:16 -070082 * Create new fragment instance.
Nancy Chen84647562014-11-03 17:08:47 -080083 * This method also allows specifying a custom title and "set default" checkbox.
84 *
Nancy Chen84647562014-11-03 17:08:47 -080085 * @param titleResId The resource ID for the string to use in the title of the dialog.
86 * @param canSetDefault {@code true} if the dialog should include an option to set the selection
87 * as the default. False otherwise.
88 * @param accountHandles The {@code PhoneAccountHandle}s available to select from.
89 * @param listener The listener for the results of the account selection.
90 */
Jay Shraunerebf0a0f2015-04-03 16:01:16 -070091 public static SelectPhoneAccountDialogFragment newInstance(int titleResId,
Nancy Chen84647562014-11-03 17:08:47 -080092 boolean canSetDefault, List<PhoneAccountHandle> accountHandles,
93 SelectPhoneAccountListener listener) {
Andrew Lee88cbb962015-06-01 12:16:15 -070094 ArrayList<PhoneAccountHandle> accountHandlesCopy = new ArrayList<PhoneAccountHandle>();
95 if (accountHandles != null) {
96 accountHandlesCopy.addAll(accountHandles);
Andrew Lee7e4758e2015-06-01 12:16:15 -070097 }
Jay Shraunerebf0a0f2015-04-03 16:01:16 -070098 SelectPhoneAccountDialogFragment fragment = new SelectPhoneAccountDialogFragment();
99 final Bundle args = new Bundle();
100 args.putInt(ARG_TITLE_RES_ID, titleResId);
101 args.putBoolean(ARG_CAN_SET_DEFAULT, canSetDefault);
Andrew Lee88cbb962015-06-01 12:16:15 -0700102 args.putParcelableArrayList(ARG_ACCOUNT_HANDLES, accountHandlesCopy);
Yorke Leea2b62e02015-06-10 13:36:46 -0700103 args.putParcelable(ARG_LISTENER, listener);
Jay Shraunerebf0a0f2015-04-03 16:01:16 -0700104 fragment.setArguments(args);
105 fragment.setListener(listener);
106 return fragment;
Nancy Chen1821c252014-10-08 12:45:34 -0700107 }
108
Jay Shraunerebf0a0f2015-04-03 16:01:16 -0700109 public SelectPhoneAccountDialogFragment() {
110 }
111
112 public void setListener(SelectPhoneAccountListener listener) {
Nancy Chen1821c252014-10-08 12:45:34 -0700113 mListener = listener;
114 }
115
Yorke Leea2b62e02015-06-10 13:36:46 -0700116 public static class SelectPhoneAccountListener extends ResultReceiver {
117 static final int RESULT_SELECTED = 1;
118 static final int RESULT_DISMISSED = 2;
119
120 static final String EXTRA_SELECTED_ACCOUNT_HANDLE = "extra_selected_account_handle";
121 static final String EXTRA_SET_DEFAULT = "extra_set_default";
122
123 public SelectPhoneAccountListener() {
124 super(new Handler());
125 }
126
127 @Override
128 protected void onReceiveResult(int resultCode, Bundle resultData) {
129 if (resultCode == RESULT_SELECTED) {
130 onPhoneAccountSelected(
131 (PhoneAccountHandle) resultData.getParcelable(
132 EXTRA_SELECTED_ACCOUNT_HANDLE),
133 resultData.getBoolean(EXTRA_SET_DEFAULT));
134 } else if (resultCode == RESULT_DISMISSED) {
135 onDialogDismissed();
136 }
137 }
138
139 public void onPhoneAccountSelected(PhoneAccountHandle selectedAccountHandle,
140 boolean setDefault) {}
141
142 public void onDialogDismissed() {}
Nancy Chen1821c252014-10-08 12:45:34 -0700143 }
144
145 @Override
Jay Shraunerebf0a0f2015-04-03 16:01:16 -0700146 public void onSaveInstanceState(Bundle outState) {
147 super.onSaveInstanceState(outState);
148 outState.putBoolean(ARG_IS_DEFAULT_CHECKED, mIsDefaultChecked);
149 }
150
151 @Override
Nancy Chen1821c252014-10-08 12:45:34 -0700152 public Dialog onCreateDialog(Bundle savedInstanceState) {
Jay Shraunerebf0a0f2015-04-03 16:01:16 -0700153 mTitleResId = getArguments().getInt(ARG_TITLE_RES_ID);
154 mCanSetDefault = getArguments().getBoolean(ARG_CAN_SET_DEFAULT);
155 mAccountHandles = getArguments().getParcelableArrayList(ARG_ACCOUNT_HANDLES);
Yorke Leea2b62e02015-06-10 13:36:46 -0700156 mListener = getArguments().getParcelable(ARG_LISTENER);
Jay Shraunerebf0a0f2015-04-03 16:01:16 -0700157 if (savedInstanceState != null) {
158 mIsDefaultChecked = savedInstanceState.getBoolean(ARG_IS_DEFAULT_CHECKED);
159 }
Nancy Chen1821c252014-10-08 12:45:34 -0700160 mIsSelected = false;
161 mTelecomManager =
162 (TelecomManager) getActivity().getSystemService(Context.TELECOM_SERVICE);
163
164 final DialogInterface.OnClickListener selectionListener =
165 new DialogInterface.OnClickListener() {
166 @Override
167 public void onClick(DialogInterface dialog, int which) {
168 mIsSelected = true;
169 PhoneAccountHandle selectedAccountHandle = mAccountHandles.get(which);
Yorke Leea2b62e02015-06-10 13:36:46 -0700170 final Bundle result = new Bundle();
171 result.putParcelable(SelectPhoneAccountListener.EXTRA_SELECTED_ACCOUNT_HANDLE,
172 selectedAccountHandle);
173 result.putBoolean(SelectPhoneAccountListener.EXTRA_SET_DEFAULT,
174 mIsDefaultChecked);
175 if (mListener != null) {
176 mListener.onReceiveResult(SelectPhoneAccountListener.RESULT_SELECTED, result);
177 }
Nancy Chenfdb76be2014-10-21 18:53:11 -0700178 }
179 };
180
181 final CompoundButton.OnCheckedChangeListener checkListener =
182 new CompoundButton.OnCheckedChangeListener() {
183 @Override
184 public void onCheckedChanged(CompoundButton check, boolean isChecked) {
185 mIsDefaultChecked = isChecked;
Nancy Chen1821c252014-10-08 12:45:34 -0700186 }
187 };
188
189 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Nancy Chen1821c252014-10-08 12:45:34 -0700190 ListAdapter selectAccountListAdapter = new SelectAccountListAdapter(
191 builder.getContext(),
192 R.layout.select_account_list_item,
193 mAccountHandles);
194
Nancy Chen84647562014-11-03 17:08:47 -0800195 AlertDialog dialog = builder.setTitle(mTitleResId)
Nancy Chen1821c252014-10-08 12:45:34 -0700196 .setAdapter(selectAccountListAdapter, selectionListener)
197 .create();
Nancy Chenfdb76be2014-10-21 18:53:11 -0700198
199 if (mCanSetDefault) {
200 // Generate custom checkbox view
201 LinearLayout checkboxLayout = (LinearLayout) getActivity()
202 .getLayoutInflater()
203 .inflate(R.layout.default_account_checkbox, null);
204
205 CheckBox cb =
206 (CheckBox) checkboxLayout.findViewById(R.id.default_account_checkbox_view);
207 cb.setOnCheckedChangeListener(checkListener);
Jay Shraunerebf0a0f2015-04-03 16:01:16 -0700208 cb.setChecked(mIsDefaultChecked);
Nancy Chenfdb76be2014-10-21 18:53:11 -0700209
210 dialog.getListView().addFooterView(checkboxLayout);
211 }
212
213 return dialog;
Nancy Chen1821c252014-10-08 12:45:34 -0700214 }
215
216 private class SelectAccountListAdapter extends ArrayAdapter<PhoneAccountHandle> {
Nancy Chen1821c252014-10-08 12:45:34 -0700217 private int mResId;
218
219 public SelectAccountListAdapter(
220 Context context, int resource, List<PhoneAccountHandle> accountHandles) {
221 super(context, resource, accountHandles);
Nancy Chen1821c252014-10-08 12:45:34 -0700222 mResId = resource;
223 }
224
225 @Override
226 public View getView(int position, View convertView, ViewGroup parent) {
227 LayoutInflater inflater = (LayoutInflater)
Ihab Awadd1360e12014-10-24 11:43:33 -0700228 getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Nancy Chen1821c252014-10-08 12:45:34 -0700229
230 View rowView;
231 final ViewHolder holder;
232
233 if (convertView == null) {
234 // Cache views for faster scrolling
235 rowView = inflater.inflate(mResId, null);
236 holder = new ViewHolder();
Nancy Chen84647562014-11-03 17:08:47 -0800237 holder.labelTextView = (TextView) rowView.findViewById(R.id.label);
238 holder.numberTextView = (TextView) rowView.findViewById(R.id.number);
Nancy Chen1821c252014-10-08 12:45:34 -0700239 holder.imageView = (ImageView) rowView.findViewById(R.id.icon);
240 rowView.setTag(holder);
241 }
242 else {
243 rowView = convertView;
244 holder = (ViewHolder) rowView.getTag();
245 }
246
247 PhoneAccountHandle accountHandle = getItem(position);
248 PhoneAccount account = mTelecomManager.getPhoneAccount(accountHandle);
Yorke Leea2bd6f52015-05-04 18:29:00 -0700249 if (account == null) {
250 return rowView;
251 }
Nancy Chen84647562014-11-03 17:08:47 -0800252 holder.labelTextView.setText(account.getLabel());
253 if (account.getAddress() == null ||
254 TextUtils.isEmpty(account.getAddress().getSchemeSpecificPart())) {
255 holder.numberTextView.setVisibility(View.GONE);
256 } else {
257 holder.numberTextView.setVisibility(View.VISIBLE);
Ihab Awad07685ea2014-12-05 13:37:50 -0800258 holder.numberTextView.setText(
Brandon Maxwell62cfa6e2015-12-04 15:27:45 -0800259 PhoneNumberUtilsCompat.createTtsSpannable(
Ihab Awad07685ea2014-12-05 13:37:50 -0800260 account.getAddress().getSchemeSpecificPart()));
Nancy Chen84647562014-11-03 17:08:47 -0800261 }
Brandon Maxwell089df232015-12-18 12:30:55 -0800262 holder.imageView.setImageDrawable(PhoneAccountCompat.createIconDrawable(account,
263 getContext()));
Nancy Chen1821c252014-10-08 12:45:34 -0700264 return rowView;
265 }
266
267 private class ViewHolder {
Nancy Chen84647562014-11-03 17:08:47 -0800268 TextView labelTextView;
269 TextView numberTextView;
Nancy Chen1821c252014-10-08 12:45:34 -0700270 ImageView imageView;
271 }
272 }
273
274 @Override
Yorke Lee6c4b7b12015-04-06 19:49:27 -0700275 public void onStop() {
Yorke Leea2b62e02015-06-10 13:36:46 -0700276 if (!mIsSelected && mListener != null) {
277 mListener.onReceiveResult(SelectPhoneAccountListener.RESULT_DISMISSED, null);
Nancy Chen1821c252014-10-08 12:45:34 -0700278 }
Yorke Lee6c4b7b12015-04-06 19:49:27 -0700279 super.onStop();
Nancy Chen1821c252014-10-08 12:45:34 -0700280 }
Brian Attwelld46781c2014-10-31 18:45:34 +0000281}