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.tests.allintents; 18 19 import android.accounts.Account; 20 import android.accounts.AccountManager; 21 import android.app.AlertDialog; 22 import android.app.Dialog; 23 import android.app.DialogFragment; 24 import android.content.DialogInterface; 25 import android.os.Bundle; 26 import android.view.LayoutInflater; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.widget.ArrayAdapter; 30 import android.widget.TextView; 31 32 /** 33 * Shows a dialog asking the user which account to chose. 34 * The result is passed back to the owning Activity 35 * Does not perform any action by itself. 36 */ 37 public class SelectAccountDialogFragment extends DialogFragment { 38 public static final String TAG = "SelectAccountDialog"; 39 40 private static final String EXTRA_TAG = "tag"; 41 42 @Override onCreateDialog(Bundle savedInstanceState)43 public Dialog onCreateDialog(Bundle savedInstanceState) { 44 final Bundle parameters = getArguments(); 45 46 AccountManager accountManager = AccountManager.get(getActivity()); 47 Account[] accounts = accountManager.getAccounts(); 48 49 final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 50 final LayoutInflater inflater = LayoutInflater.from(builder.getContext()); 51 52 final ArrayAdapter<Account> accountAdapter = 53 new ArrayAdapter<Account>(builder.getContext(), 54 android.R.layout.simple_list_item_2, accounts) { 55 @Override 56 public View getView(int position, View convertView, ViewGroup parent) { 57 final View resultView = convertView == null 58 ? inflater.inflate(android.R.layout.simple_list_item_2, parent, false) 59 : convertView; 60 61 final TextView text1 = (TextView)resultView.findViewById(android.R.id.text1); 62 final TextView text2 = (TextView)resultView.findViewById(android.R.id.text2); 63 64 final Account account = getItem(position); 65 66 text1.setText("Name: " + account.name); 67 text2.setText("Type: " + account.type); 68 69 return resultView; 70 } 71 }; 72 73 final DialogInterface.OnClickListener clickListener = 74 new DialogInterface.OnClickListener() { 75 @Override 76 public void onClick(DialogInterface dialog, int which) { 77 dialog.dismiss(); 78 79 // We currently do not pass the dataSet argument to the listener. To do so, we would 80 // have to determine the dataSet as it is done in AccountTypeManager. 81 ((Listener) getActivity()).onAccountChosen(accountAdapter.getItem(which), 82 null, parameters.getInt(EXTRA_TAG)); 83 } 84 }; 85 86 builder.setTitle("Choose account to send to editor"); 87 builder.setSingleChoiceItems(accountAdapter, 0, clickListener); 88 final AlertDialog result = builder.create(); 89 return result; 90 } 91 createBundle(int tag)92 public static Bundle createBundle(int tag) { 93 final Bundle result = new Bundle(); 94 result.putInt(EXTRA_TAG, tag); 95 return result; 96 } 97 98 public interface Listener { onAccountChosen(Account account, String dataSet, int tag)99 void onAccountChosen(Account account, String dataSet, int tag); 100 } 101 } 102