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