12013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann/* 22013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * Copyright (C) 2010 The Android Open Source Project 32013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * 42013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * Licensed under the Apache License, Version 2.0 (the "License"); 52013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * you may not use this file except in compliance with the License. 62013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * You may obtain a copy of the License at 72013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * 82013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * http://www.apache.org/licenses/LICENSE-2.0 92013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * 102013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * Unless required by applicable law or agreed to in writing, software 112013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * distributed under the License is distributed on an "AS IS" BASIS, 122013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 132013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * See the License for the specific language governing permissions and 142013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * limitations under the License 152013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann */ 162013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 172013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannpackage com.android.contacts.tests.allintents; 182013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 19b5a6a925e44225748c915672ff9e2120164af657Flavio Lerdaimport android.accounts.Account; 20b5a6a925e44225748c915672ff9e2120164af657Flavio Lerdaimport android.accounts.AccountManager; 212013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannimport android.app.AlertDialog; 222013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannimport android.app.Dialog; 232013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannimport android.app.DialogFragment; 242013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannimport android.content.DialogInterface; 252013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannimport android.os.Bundle; 262013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannimport android.view.LayoutInflater; 272013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannimport android.view.View; 282013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannimport android.view.ViewGroup; 292013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannimport android.widget.ArrayAdapter; 302013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannimport android.widget.TextView; 312013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 322013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann/** 332013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * Shows a dialog asking the user which account to chose. 342013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * The result is passed back to the owning Activity 352013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann * Does not perform any action by itself. 362013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann */ 372013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmannpublic class SelectAccountDialogFragment extends DialogFragment { 382013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann public static final String TAG = "SelectAccountDialogFragment"; 392013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 402013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann private static final String EXTRA_TAG = "tag"; 412013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 422013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann @Override 432013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann public Dialog onCreateDialog(Bundle savedInstanceState) { 442013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann final Bundle parameters = getArguments(); 452013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 46b5a6a925e44225748c915672ff9e2120164af657Flavio Lerda AccountManager accountManager = AccountManager.get(getActivity()); 47b5a6a925e44225748c915672ff9e2120164af657Flavio Lerda Account[] accounts = accountManager.getAccounts(); 482013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 492013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 502013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann final LayoutInflater inflater = LayoutInflater.from(builder.getContext()); 512013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 52b5a6a925e44225748c915672ff9e2120164af657Flavio Lerda final ArrayAdapter<Account> accountAdapter = 53b5a6a925e44225748c915672ff9e2120164af657Flavio Lerda new ArrayAdapter<Account>(builder.getContext(), 542b3f3c54d3beb017b2f59f19e9ce0ecc3e039dbcDave Santoro android.R.layout.simple_list_item_2, accounts) { 552013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann @Override 562013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann public View getView(int position, View convertView, ViewGroup parent) { 572013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann final View resultView = convertView == null 582013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann ? inflater.inflate(android.R.layout.simple_list_item_2, parent, false) 592013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann : convertView; 602013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 612013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann final TextView text1 = (TextView)resultView.findViewById(android.R.id.text1); 622013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann final TextView text2 = (TextView)resultView.findViewById(android.R.id.text2); 632013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 64b5a6a925e44225748c915672ff9e2120164af657Flavio Lerda final Account account = getItem(position); 652013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 662013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann text1.setText("Name: " + account.name); 672013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann text2.setText("Type: " + account.type); 682013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 692013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann return resultView; 702013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann } 712013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann }; 722013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 732013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann final DialogInterface.OnClickListener clickListener = 742013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann new DialogInterface.OnClickListener() { 752013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann @Override 762013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann public void onClick(DialogInterface dialog, int which) { 772013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann dialog.dismiss(); 782013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 79b5a6a925e44225748c915672ff9e2120164af657Flavio Lerda // We currently do not pass the dataSet argument to the listener. To do so, we would 80b5a6a925e44225748c915672ff9e2120164af657Flavio Lerda // have to determine the dataSet as it is done in AccountTypeManager. 812013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann ((Listener) getActivity()).onAccountChosen(accountAdapter.getItem(which), 82b5a6a925e44225748c915672ff9e2120164af657Flavio Lerda null, parameters.getInt(EXTRA_TAG)); 832013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann } 842013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann }; 852013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 862013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann builder.setTitle("Choose account to send to editor"); 872013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann builder.setSingleChoiceItems(accountAdapter, 0, clickListener); 882013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann final AlertDialog result = builder.create(); 892013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann return result; 902013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann } 912013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 922013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann public static Bundle createBundle(int tag) { 932013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann final Bundle result = new Bundle(); 942013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann result.putInt(EXTRA_TAG, tag); 952013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann return result; 962013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann } 972013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann 982013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann public interface Listener { 99b5a6a925e44225748c915672ff9e2120164af657Flavio Lerda void onAccountChosen(Account account, String dataSet, int tag); 1002013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann } 1012013058703aa110a1d7aac87ef408c96a3919472Daniel Lehmann} 102