• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.contacts.editor;
18 
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.app.Dialog;
22 import android.app.DialogFragment;
23 import android.app.FragmentManager;
24 import android.content.DialogInterface;
25 import android.os.Bundle;
26 import android.view.View;
27 import android.widget.TextView;
28 
29 import com.android.contacts.R;
30 import com.android.contacts.model.AccountTypeManager;
31 import com.android.contacts.model.account.AccountInfo;
32 import com.android.contacts.model.account.AccountWithDataSet;
33 import com.android.contacts.model.account.AccountsLoader;
34 import com.android.contacts.util.AccountsListAdapter;
35 import com.google.common.base.Preconditions;
36 
37 import java.util.List;
38 
39 /**
40  * Shows a dialog asking the user which account to chose.
41  *
42  * The result is passed to {@code targetFragment} passed to {@link #show}.
43  */
44 public final class SelectAccountDialogFragment extends DialogFragment
45         implements AccountsLoader.AccountsListener {
46     public static final String TAG = "SelectAccountDialog";
47 
48     private static final String KEY_TITLE_RES_ID = "title_res_id";
49     private static final String KEY_LIST_FILTER = "list_filter";
50     private static final String KEY_EXTRA_ARGS = "extra_args";
51 
52     private AccountsListAdapter mAccountsAdapter;
53     private AccountTypeManager.AccountFilter mFilter;
54 
55     /**
56      * Show the dialog.
57      *
58      * @param fragmentManager {@link FragmentManager}.
59      * @param titleResourceId resource ID to use as the title.
60      * @param extraArgs Extra arguments, which will later be passed to
61      *     {@link Listener#onAccountChosen}.  {@code null} will be converted to
62      *     {@link Bundle#EMPTY}.
63      */
show(FragmentManager fragmentManager, int titleResourceId, AccountTypeManager.AccountFilter filter, Bundle extraArgs)64     public static void show(FragmentManager fragmentManager, int titleResourceId,
65             AccountTypeManager.AccountFilter filter, Bundle extraArgs) {
66         show(fragmentManager, titleResourceId, filter, extraArgs, /* tag */ null);
67     }
68 
show(FragmentManager fragmentManager, int titleResourceId, AccountTypeManager.AccountFilter filter, Bundle extraArgs, String tag)69     public static void show(FragmentManager fragmentManager, int titleResourceId,
70             AccountTypeManager.AccountFilter filter, Bundle extraArgs, String tag) {
71         final Bundle args = new Bundle();
72         args.putInt(KEY_TITLE_RES_ID, titleResourceId);
73         args.putBundle(KEY_EXTRA_ARGS, (extraArgs == null) ? Bundle.EMPTY : extraArgs);
74         args.putSerializable(KEY_LIST_FILTER, filter);
75 
76         final SelectAccountDialogFragment instance = new SelectAccountDialogFragment();
77         instance.setArguments(args);
78         instance.show(fragmentManager, tag);
79     }
80 
81     @Override
onCreate(Bundle savedInstanceState)82     public void onCreate(Bundle savedInstanceState) {
83         super.onCreate(savedInstanceState);
84         final Bundle args = getArguments();
85         mFilter = (AccountTypeManager.AccountFilter) args.getSerializable(KEY_LIST_FILTER);
86         if (mFilter == null) {
87             mFilter = AccountTypeManager.AccountFilter.ALL;
88         }
89     }
90 
91     @Override
onCreateDialog(Bundle savedInstanceState)92     public Dialog onCreateDialog(Bundle savedInstanceState) {
93         final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
94         final Bundle args = getArguments();
95 
96         mAccountsAdapter = new AccountsListAdapter(builder.getContext());
97         mAccountsAdapter.setCustomLayout(R.layout.account_selector_list_item_condensed);
98 
99         final DialogInterface.OnClickListener clickListener =
100                 new DialogInterface.OnClickListener() {
101             @Override
102             public void onClick(DialogInterface dialog, int which) {
103                 dialog.dismiss();
104 
105                 onAccountSelected(mAccountsAdapter.getItem(which));
106             }
107         };
108 
109         final TextView title = (TextView) View.inflate(getActivity(), R.layout.dialog_title, null);
110         title.setText(args.getInt(KEY_TITLE_RES_ID));
111         builder.setCustomTitle(title);
112         builder.setSingleChoiceItems(mAccountsAdapter, 0, clickListener);
113         final AlertDialog result = builder.create();
114         return result;
115     }
116 
117     @Override
onActivityCreated(Bundle savedInstanceState)118     public void onActivityCreated(Bundle savedInstanceState) {
119         super.onActivityCreated(savedInstanceState);
120         AccountsLoader.loadAccounts(this, 0, mFilter);
121     }
122 
123     @Override
onCancel(DialogInterface dialog)124     public void onCancel(DialogInterface dialog) {
125         super.onCancel(dialog);
126         final Listener listener = getListener();
127         if (listener != null) {
128             listener.onAccountSelectorCancelled();
129         }
130     }
131 
132     /**
133      * Calls {@link Listener#onAccountChosen}.
134      */
onAccountSelected(AccountWithDataSet account)135     private void onAccountSelected(AccountWithDataSet account) {
136         final Listener listener = getListener();
137         if (listener != null) {
138             listener.onAccountChosen(account, getArguments().getBundle(KEY_EXTRA_ARGS));
139         }
140     }
141 
getListener()142     private Listener getListener() {
143         Listener listener = null;
144         final Activity activity = getActivity();
145         if (activity != null && activity instanceof Listener) {
146             listener = (Listener) activity;
147         }
148         return listener;
149     }
150 
151     @Override
onAccountsLoaded(List<AccountInfo> accounts)152     public void onAccountsLoaded(List<AccountInfo> accounts) {
153         Preconditions.checkNotNull(mAccountsAdapter,
154                 "Accounts adapter should have been initialized");
155         mAccountsAdapter.setAccounts(accounts, null);
156     }
157 
158     public interface Listener {
onAccountChosen(AccountWithDataSet account, Bundle extraArgs)159         void onAccountChosen(AccountWithDataSet account, Bundle extraArgs);
onAccountSelectorCancelled()160         void onAccountSelectorCancelled();
161     }
162 }
163