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
17package com.android.contacts.editor;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Dialog;
22import android.app.DialogFragment;
23import android.app.FragmentManager;
24import android.content.DialogInterface;
25import android.os.Bundle;
26import android.view.View;
27import android.widget.TextView;
28
29import com.android.contacts.R;
30import com.android.contacts.model.AccountTypeManager;
31import com.android.contacts.model.account.AccountInfo;
32import com.android.contacts.model.account.AccountWithDataSet;
33import com.android.contacts.model.account.AccountsLoader;
34import com.android.contacts.util.AccountsListAdapter;
35import com.google.common.base.Preconditions;
36
37import 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 */
44public 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     */
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
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
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
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
118    public void onActivityCreated(Bundle savedInstanceState) {
119        super.onActivityCreated(savedInstanceState);
120        AccountsLoader.loadAccounts(this, 0, mFilter);
121    }
122
123    @Override
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     */
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
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
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 {
159        void onAccountChosen(AccountWithDataSet account, Bundle extraArgs);
160        void onAccountSelectorCancelled();
161    }
162}
163