Chiao Cheng | e420de7 | 2012-12-04 14:59:50 -0800 | [diff] [blame] | 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.common.editor; |
| 18 | |
| 19 | import android.app.AlertDialog; |
| 20 | import android.app.Dialog; |
| 21 | import android.app.DialogFragment; |
| 22 | import android.app.Fragment; |
| 23 | import android.app.FragmentManager; |
| 24 | import android.content.DialogInterface; |
| 25 | import android.os.Bundle; |
| 26 | |
| 27 | import com.android.contacts.common.model.account.AccountWithDataSet; |
| 28 | import com.android.contacts.common.util.AccountsListAdapter; |
| 29 | import com.android.contacts.common.util.AccountsListAdapter.AccountListFilter; |
| 30 | |
| 31 | /** |
| 32 | * Shows a dialog asking the user which account to chose. |
| 33 | * |
| 34 | * The result is passed to {@code targetFragment} passed to {@link #show}. |
| 35 | */ |
| 36 | public final class SelectAccountDialogFragment extends DialogFragment { |
| 37 | public static final String TAG = "SelectAccountDialogFragment"; |
| 38 | |
| 39 | private static final String KEY_TITLE_RES_ID = "title_res_id"; |
| 40 | private static final String KEY_LIST_FILTER = "list_filter"; |
| 41 | private static final String KEY_EXTRA_ARGS = "extra_args"; |
| 42 | |
| 43 | public SelectAccountDialogFragment() { // All fragments must have a public default constructor. |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Show the dialog. |
| 48 | * |
| 49 | * @param fragmentManager {@link FragmentManager}. |
| 50 | * @param targetFragment {@link Fragment} that implements {@link Listener}. |
| 51 | * @param titleResourceId resource ID to use as the title. |
| 52 | * @param accountListFilter account filter. |
| 53 | * @param extraArgs Extra arguments, which will later be passed to |
| 54 | * {@link Listener#onAccountChosen}. {@code null} will be converted to |
| 55 | * {@link Bundle#EMPTY}. |
| 56 | */ |
| 57 | public static <F extends Fragment & Listener> void show(FragmentManager fragmentManager, |
| 58 | F targetFragment, int titleResourceId, |
| 59 | AccountListFilter accountListFilter, Bundle extraArgs) { |
| 60 | final Bundle args = new Bundle(); |
| 61 | args.putInt(KEY_TITLE_RES_ID, titleResourceId); |
| 62 | args.putSerializable(KEY_LIST_FILTER, accountListFilter); |
| 63 | args.putBundle(KEY_EXTRA_ARGS, (extraArgs == null) ? Bundle.EMPTY : extraArgs); |
| 64 | |
| 65 | final SelectAccountDialogFragment instance = new SelectAccountDialogFragment(); |
| 66 | instance.setArguments(args); |
| 67 | instance.setTargetFragment(targetFragment, 0); |
| 68 | instance.show(fragmentManager, null); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 73 | final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
| 74 | final Bundle args = getArguments(); |
| 75 | |
| 76 | final AccountListFilter filter = (AccountListFilter) args.getSerializable(KEY_LIST_FILTER); |
| 77 | final AccountsListAdapter accountAdapter = new AccountsListAdapter(builder.getContext(), |
| 78 | filter); |
| 79 | |
| 80 | final DialogInterface.OnClickListener clickListener = |
| 81 | new DialogInterface.OnClickListener() { |
| 82 | @Override |
| 83 | public void onClick(DialogInterface dialog, int which) { |
| 84 | dialog.dismiss(); |
| 85 | |
| 86 | onAccountSelected(accountAdapter.getItem(which)); |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | builder.setTitle(args.getInt(KEY_TITLE_RES_ID)); |
| 91 | builder.setSingleChoiceItems(accountAdapter, 0, clickListener); |
| 92 | final AlertDialog result = builder.create(); |
| 93 | return result; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void onCancel(DialogInterface dialog) { |
| 98 | super.onCancel(dialog); |
| 99 | final Fragment targetFragment = getTargetFragment(); |
| 100 | if (targetFragment != null && targetFragment instanceof Listener) { |
| 101 | final Listener target = (Listener) targetFragment; |
| 102 | target.onAccountSelectorCancelled(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Calls {@link Listener#onAccountChosen} of {@code targetFragment}. |
| 108 | */ |
| 109 | private void onAccountSelected(AccountWithDataSet account) { |
| 110 | final Fragment targetFragment = getTargetFragment(); |
| 111 | if (targetFragment != null && targetFragment instanceof Listener) { |
| 112 | final Listener target = (Listener) targetFragment; |
| 113 | target.onAccountChosen(account, getArguments().getBundle(KEY_EXTRA_ARGS)); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | public interface Listener { |
| 118 | void onAccountChosen(AccountWithDataSet account, Bundle extraArgs); |
| 119 | void onAccountSelectorCancelled(); |
| 120 | } |
| 121 | } |