SelectAccountDialogFragment.java revision 2013058703aa110a1d7aac87ef408c96a3919472
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.tests.allintents; 18 19import android.accounts.Account; 20import android.accounts.AccountManager; 21import android.app.AlertDialog; 22import android.app.Dialog; 23import android.app.DialogFragment; 24import android.content.DialogInterface; 25import android.os.Bundle; 26import android.view.LayoutInflater; 27import android.view.View; 28import android.view.ViewGroup; 29import android.widget.ArrayAdapter; 30import 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 */ 37public class SelectAccountDialogFragment extends DialogFragment { 38 public static final String TAG = "SelectAccountDialogFragment"; 39 40 private static final String EXTRA_TAG = "tag"; 41 42 @Override 43 public Dialog onCreateDialog(Bundle savedInstanceState) { 44 final Bundle parameters = getArguments(); 45 46 final Account[] accounts = AccountManager.get(getActivity()).getAccounts(); 47 48 final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 49 final LayoutInflater inflater = LayoutInflater.from(builder.getContext()); 50 51 final ArrayAdapter<Account> accountAdapter = new ArrayAdapter<Account>(builder.getContext(), 52 android.R.layout.simple_list_item_2, accounts) { 53 @Override 54 public View getView(int position, View convertView, ViewGroup parent) { 55 final View resultView = convertView == null 56 ? inflater.inflate(android.R.layout.simple_list_item_2, parent, false) 57 : convertView; 58 59 final TextView text1 = (TextView)resultView.findViewById(android.R.id.text1); 60 final TextView text2 = (TextView)resultView.findViewById(android.R.id.text2); 61 62 final Account account = getItem(position); 63 64 text1.setText("Name: " + account.name); 65 text2.setText("Type: " + account.type); 66 67 return resultView; 68 } 69 }; 70 71 final DialogInterface.OnClickListener clickListener = 72 new DialogInterface.OnClickListener() { 73 @Override 74 public void onClick(DialogInterface dialog, int which) { 75 dialog.dismiss(); 76 77 ((Listener) getActivity()).onAccountChosen(accountAdapter.getItem(which), 78 parameters.getInt(EXTRA_TAG)); 79 } 80 }; 81 82 builder.setTitle("Choose account to send to editor"); 83 builder.setSingleChoiceItems(accountAdapter, 0, clickListener); 84 final AlertDialog result = builder.create(); 85 return result; 86 } 87 88 public static Bundle createBundle(int tag) { 89 final Bundle result = new Bundle(); 90 result.putInt(EXTRA_TAG, tag); 91 return result; 92 } 93 94 public interface Listener { 95 void onAccountChosen(Account account, int tag); 96 } 97} 98