1e420de7fca9005d7084be275f268962a14094a22Chiao Cheng/*
2e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * Copyright (C) 2010 The Android Open Source Project
3e420de7fca9005d7084be275f268962a14094a22Chiao Cheng *
4e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License");
5e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * you may not use this file except in compliance with the License.
6e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * You may obtain a copy of the License at
7e420de7fca9005d7084be275f268962a14094a22Chiao Cheng *
8e420de7fca9005d7084be275f268962a14094a22Chiao Cheng *      http://www.apache.org/licenses/LICENSE-2.0
9e420de7fca9005d7084be275f268962a14094a22Chiao Cheng *
10e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * Unless required by applicable law or agreed to in writing, software
11e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS,
12e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * See the License for the specific language governing permissions and
14e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * limitations under the License
15e420de7fca9005d7084be275f268962a14094a22Chiao Cheng */
16e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
17e420de7fca9005d7084be275f268962a14094a22Chiao Chengpackage com.android.contacts.common.editor;
18e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
19e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.app.AlertDialog;
20e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.app.Dialog;
21e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.app.DialogFragment;
22e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.app.Fragment;
23e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.app.FragmentManager;
24e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.content.DialogInterface;
25e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport android.os.Bundle;
26e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
27e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport com.android.contacts.common.model.account.AccountWithDataSet;
28e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport com.android.contacts.common.util.AccountsListAdapter;
29e420de7fca9005d7084be275f268962a14094a22Chiao Chengimport com.android.contacts.common.util.AccountsListAdapter.AccountListFilter;
30e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
31e420de7fca9005d7084be275f268962a14094a22Chiao Cheng/**
32e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * Shows a dialog asking the user which account to chose.
33e420de7fca9005d7084be275f268962a14094a22Chiao Cheng *
34e420de7fca9005d7084be275f268962a14094a22Chiao Cheng * The result is passed to {@code targetFragment} passed to {@link #show}.
35e420de7fca9005d7084be275f268962a14094a22Chiao Cheng */
36e420de7fca9005d7084be275f268962a14094a22Chiao Chengpublic final class SelectAccountDialogFragment extends DialogFragment {
37e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    public static final String TAG = "SelectAccountDialogFragment";
38e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
39e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    private static final String KEY_TITLE_RES_ID = "title_res_id";
40e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    private static final String KEY_LIST_FILTER = "list_filter";
41e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    private static final String KEY_EXTRA_ARGS = "extra_args";
42e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
43e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    public SelectAccountDialogFragment() { // All fragments must have a public default constructor.
44e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    }
45e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
46e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    /**
47e420de7fca9005d7084be275f268962a14094a22Chiao Cheng     * Show the dialog.
48e420de7fca9005d7084be275f268962a14094a22Chiao Cheng     *
49e420de7fca9005d7084be275f268962a14094a22Chiao Cheng     * @param fragmentManager {@link FragmentManager}.
50e420de7fca9005d7084be275f268962a14094a22Chiao Cheng     * @param targetFragment {@link Fragment} that implements {@link Listener}.
51e420de7fca9005d7084be275f268962a14094a22Chiao Cheng     * @param titleResourceId resource ID to use as the title.
52e420de7fca9005d7084be275f268962a14094a22Chiao Cheng     * @param accountListFilter account filter.
53e420de7fca9005d7084be275f268962a14094a22Chiao Cheng     * @param extraArgs Extra arguments, which will later be passed to
54e420de7fca9005d7084be275f268962a14094a22Chiao Cheng     *     {@link Listener#onAccountChosen}.  {@code null} will be converted to
55e420de7fca9005d7084be275f268962a14094a22Chiao Cheng     *     {@link Bundle#EMPTY}.
56e420de7fca9005d7084be275f268962a14094a22Chiao Cheng     */
57e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    public static <F extends Fragment & Listener> void show(FragmentManager fragmentManager,
58e420de7fca9005d7084be275f268962a14094a22Chiao Cheng            F targetFragment, int titleResourceId,
59e420de7fca9005d7084be275f268962a14094a22Chiao Cheng            AccountListFilter accountListFilter, Bundle extraArgs) {
60e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        final Bundle args = new Bundle();
61e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        args.putInt(KEY_TITLE_RES_ID, titleResourceId);
62e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        args.putSerializable(KEY_LIST_FILTER, accountListFilter);
63e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        args.putBundle(KEY_EXTRA_ARGS, (extraArgs == null) ? Bundle.EMPTY : extraArgs);
64e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
65e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        final SelectAccountDialogFragment instance = new SelectAccountDialogFragment();
66e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        instance.setArguments(args);
67e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        instance.setTargetFragment(targetFragment, 0);
68e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        instance.show(fragmentManager, null);
69e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    }
70e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
71e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    @Override
72e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    public Dialog onCreateDialog(Bundle savedInstanceState) {
73e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
74e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        final Bundle args = getArguments();
75e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
76e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        final AccountListFilter filter = (AccountListFilter) args.getSerializable(KEY_LIST_FILTER);
77e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        final AccountsListAdapter accountAdapter = new AccountsListAdapter(builder.getContext(),
78e420de7fca9005d7084be275f268962a14094a22Chiao Cheng                filter);
79e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
80e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        final DialogInterface.OnClickListener clickListener =
81e420de7fca9005d7084be275f268962a14094a22Chiao Cheng                new DialogInterface.OnClickListener() {
82e420de7fca9005d7084be275f268962a14094a22Chiao Cheng            @Override
83e420de7fca9005d7084be275f268962a14094a22Chiao Cheng            public void onClick(DialogInterface dialog, int which) {
84e420de7fca9005d7084be275f268962a14094a22Chiao Cheng                dialog.dismiss();
85e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
86e420de7fca9005d7084be275f268962a14094a22Chiao Cheng                onAccountSelected(accountAdapter.getItem(which));
87e420de7fca9005d7084be275f268962a14094a22Chiao Cheng            }
88e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        };
89e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
90e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        builder.setTitle(args.getInt(KEY_TITLE_RES_ID));
91e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        builder.setSingleChoiceItems(accountAdapter, 0, clickListener);
92e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        final AlertDialog result = builder.create();
93e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        return result;
94e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    }
95e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
96e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    @Override
97e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    public void onCancel(DialogInterface dialog) {
98e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        super.onCancel(dialog);
99e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        final Fragment targetFragment = getTargetFragment();
100e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        if (targetFragment != null && targetFragment instanceof Listener) {
101e420de7fca9005d7084be275f268962a14094a22Chiao Cheng            final Listener target = (Listener) targetFragment;
102e420de7fca9005d7084be275f268962a14094a22Chiao Cheng            target.onAccountSelectorCancelled();
103e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        }
104e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    }
105e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
106e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    /**
107e420de7fca9005d7084be275f268962a14094a22Chiao Cheng     * Calls {@link Listener#onAccountChosen} of {@code targetFragment}.
108e420de7fca9005d7084be275f268962a14094a22Chiao Cheng     */
109e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    private void onAccountSelected(AccountWithDataSet account) {
110e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        final Fragment targetFragment = getTargetFragment();
111e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        if (targetFragment != null && targetFragment instanceof Listener) {
112e420de7fca9005d7084be275f268962a14094a22Chiao Cheng            final Listener target = (Listener) targetFragment;
113e420de7fca9005d7084be275f268962a14094a22Chiao Cheng            target.onAccountChosen(account, getArguments().getBundle(KEY_EXTRA_ARGS));
114e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        }
115e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    }
116e420de7fca9005d7084be275f268962a14094a22Chiao Cheng
117e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    public interface Listener {
118e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        void onAccountChosen(AccountWithDataSet account, Bundle extraArgs);
119e420de7fca9005d7084be275f268962a14094a22Chiao Cheng        void onAccountSelectorCancelled();
120e420de7fca9005d7084be275f268962a14094a22Chiao Cheng    }
121e420de7fca9005d7084be275f268962a14094a22Chiao Cheng}
122