blob: fe3d9d6eb5563e1357a017d9c381a713675755a9 [file] [log] [blame]
Chiao Cheng53d4bf62012-12-04 14:59:50 -08001/*
2 * Copyright (C) 2010 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
Walter Jang55d0e712016-08-31 17:11:36 -070017package com.android.contacts.editor;
Chiao Cheng53d4bf62012-12-04 14:59:50 -080018
Walter Jang867ceeb2016-05-23 11:45:47 -070019import android.app.Activity;
Chiao Cheng53d4bf62012-12-04 14:59:50 -080020import android.app.AlertDialog;
21import android.app.Dialog;
Brian Attwellc2e912c2014-10-27 12:29:44 -070022import android.app.DialogFragment;
Chiao Cheng53d4bf62012-12-04 14:59:50 -080023import android.app.FragmentManager;
24import android.content.DialogInterface;
25import android.os.Bundle;
guanxiongliud0b4e6c2016-07-22 10:38:49 -070026import android.view.View;
27import android.widget.TextView;
Chiao Cheng53d4bf62012-12-04 14:59:50 -080028
Arthur Wang3f6a2442016-12-05 14:51:59 -080029import com.android.contacts.R;
Gary Mai69c182a2016-12-05 13:07:03 -080030import com.android.contacts.model.account.AccountWithDataSet;
31import com.android.contacts.util.AccountsListAdapter;
32import com.android.contacts.util.AccountsListAdapter.AccountListFilter;
Chiao Cheng53d4bf62012-12-04 14:59:50 -080033
34/**
35 * Shows a dialog asking the user which account to chose.
36 *
37 * The result is passed to {@code targetFragment} passed to {@link #show}.
38 */
Brian Attwellc2e912c2014-10-27 12:29:44 -070039public final class SelectAccountDialogFragment extends DialogFragment {
Chiao Cheng53d4bf62012-12-04 14:59:50 -080040 public static final String TAG = "SelectAccountDialogFragment";
41
42 private static final String KEY_TITLE_RES_ID = "title_res_id";
43 private static final String KEY_LIST_FILTER = "list_filter";
44 private static final String KEY_EXTRA_ARGS = "extra_args";
45
Chiao Cheng53d4bf62012-12-04 14:59:50 -080046 /**
47 * Show the dialog.
48 *
49 * @param fragmentManager {@link FragmentManager}.
Chiao Cheng53d4bf62012-12-04 14:59:50 -080050 * @param titleResourceId resource ID to use as the title.
51 * @param accountListFilter account filter.
52 * @param extraArgs Extra arguments, which will later be passed to
53 * {@link Listener#onAccountChosen}. {@code null} will be converted to
54 * {@link Bundle#EMPTY}.
55 */
Wenyi Wangc3322282016-11-11 14:53:29 -080056 public static void show(FragmentManager fragmentManager, int titleResourceId,
Chiao Cheng53d4bf62012-12-04 14:59:50 -080057 AccountListFilter accountListFilter, Bundle extraArgs) {
Wenyi Wangc3322282016-11-11 14:53:29 -080058 show(fragmentManager, titleResourceId, accountListFilter, extraArgs, /* tag */ null);
Walter Jang867ceeb2016-05-23 11:45:47 -070059 }
60
Wenyi Wangc3322282016-11-11 14:53:29 -080061 public static void show(FragmentManager fragmentManager, int titleResourceId,
Walter Jang867ceeb2016-05-23 11:45:47 -070062 AccountListFilter accountListFilter, Bundle extraArgs, String tag) {
Chiao Cheng53d4bf62012-12-04 14:59:50 -080063 final Bundle args = new Bundle();
64 args.putInt(KEY_TITLE_RES_ID, titleResourceId);
65 args.putSerializable(KEY_LIST_FILTER, accountListFilter);
66 args.putBundle(KEY_EXTRA_ARGS, (extraArgs == null) ? Bundle.EMPTY : extraArgs);
67
68 final SelectAccountDialogFragment instance = new SelectAccountDialogFragment();
69 instance.setArguments(args);
Walter Jang867ceeb2016-05-23 11:45:47 -070070 instance.show(fragmentManager, tag);
Chiao Cheng53d4bf62012-12-04 14:59:50 -080071 }
72
73 @Override
74 public Dialog onCreateDialog(Bundle savedInstanceState) {
75 final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
76 final Bundle args = getArguments();
77
78 final AccountListFilter filter = (AccountListFilter) args.getSerializable(KEY_LIST_FILTER);
79 final AccountsListAdapter accountAdapter = new AccountsListAdapter(builder.getContext(),
80 filter);
Wenyi Wang80e02582015-11-03 17:26:54 -080081 accountAdapter.setCustomLayout(R.layout.account_selector_list_item_condensed);
Chiao Cheng53d4bf62012-12-04 14:59:50 -080082
83 final DialogInterface.OnClickListener clickListener =
84 new DialogInterface.OnClickListener() {
85 @Override
86 public void onClick(DialogInterface dialog, int which) {
87 dialog.dismiss();
88
89 onAccountSelected(accountAdapter.getItem(which));
90 }
91 };
92
guanxiongliud0b4e6c2016-07-22 10:38:49 -070093 final TextView title = (TextView) View.inflate(getActivity(), R.layout.dialog_title, null);
94 title.setText(args.getInt(KEY_TITLE_RES_ID));
95 builder.setCustomTitle(title);
Chiao Cheng53d4bf62012-12-04 14:59:50 -080096 builder.setSingleChoiceItems(accountAdapter, 0, clickListener);
97 final AlertDialog result = builder.create();
98 return result;
99 }
100
101 @Override
102 public void onCancel(DialogInterface dialog) {
103 super.onCancel(dialog);
Walter Jang867ceeb2016-05-23 11:45:47 -0700104 final Listener listener = getListener();
105 if (listener != null) {
106 listener.onAccountSelectorCancelled();
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800107 }
108 }
109
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800110 /**
Wenyi Wangc3322282016-11-11 14:53:29 -0800111 * Calls {@link Listener#onAccountChosen}.
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800112 */
113 private void onAccountSelected(AccountWithDataSet account) {
Walter Jang867ceeb2016-05-23 11:45:47 -0700114 final Listener listener = getListener();
115 if (listener != null) {
116 listener.onAccountChosen(account, getArguments().getBundle(KEY_EXTRA_ARGS));
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800117 }
118 }
119
Walter Jang867ceeb2016-05-23 11:45:47 -0700120 private Listener getListener() {
121 Listener listener = null;
Wenyi Wangc3322282016-11-11 14:53:29 -0800122 final Activity activity = getActivity();
123 if (activity != null && activity instanceof Listener) {
124 listener = (Listener) activity;
Walter Jang867ceeb2016-05-23 11:45:47 -0700125 }
126 return listener;
127 }
128
Chiao Cheng53d4bf62012-12-04 14:59:50 -0800129 public interface Listener {
130 void onAccountChosen(AccountWithDataSet account, Bundle extraArgs);
131 void onAccountSelectorCancelled();
132 }
133}