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.interactions; 18 19 import com.android.contacts.R; 20 import com.android.contacts.editor.SelectAccountDialogFragment; 21 import com.android.contacts.model.AccountTypeManager; 22 import com.android.contacts.model.AccountWithDataSet; 23 import com.android.contacts.util.AccountSelectionUtil; 24 import com.android.contacts.util.AccountsListAdapter.AccountListFilter; 25 import com.android.contacts.vcard.ExportVCardActivity; 26 27 import android.app.AlertDialog; 28 import android.app.Dialog; 29 import android.app.DialogFragment; 30 import android.app.FragmentManager; 31 import android.content.Context; 32 import android.content.DialogInterface; 33 import android.content.Intent; 34 import android.content.res.Resources; 35 import android.database.Cursor; 36 import android.net.Uri; 37 import android.os.Bundle; 38 import android.provider.ContactsContract.Contacts; 39 import android.telephony.TelephonyManager; 40 import android.util.Log; 41 import android.view.LayoutInflater; 42 import android.view.View; 43 import android.view.ViewGroup; 44 import android.widget.ArrayAdapter; 45 import android.widget.TextView; 46 import android.widget.Toast; 47 48 import java.util.List; 49 50 /** 51 * An dialog invoked to import/export contacts. 52 */ 53 public class ImportExportDialogFragment extends DialogFragment 54 implements SelectAccountDialogFragment.Listener { 55 public static final String TAG = "ImportExportDialogFragment"; 56 57 private static final String KEY_RES_ID = "resourceId"; 58 59 private final String[] LOOKUP_PROJECTION = new String[] { 60 Contacts.LOOKUP_KEY 61 }; 62 63 /** Preferred way to show this dialog */ 64 public static void show(FragmentManager fragmentManager) { 65 final ImportExportDialogFragment fragment = new ImportExportDialogFragment(); 66 fragment.show(fragmentManager, ImportExportDialogFragment.TAG); 67 } 68 69 @Override 70 public Dialog onCreateDialog(Bundle savedInstanceState) { 71 // Wrap our context to inflate list items using the correct theme 72 final Resources res = getActivity().getResources(); 73 final LayoutInflater dialogInflater = (LayoutInflater)getActivity() 74 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 75 76 // Adapter that shows a list of string resources 77 final ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(getActivity(), 78 R.layout.select_dialog_item) { 79 @Override 80 public View getView(int position, View convertView, ViewGroup parent) { 81 final TextView result = (TextView)(convertView != null ? convertView : 82 dialogInflater.inflate(R.layout.select_dialog_item, parent, false)); 83 84 final int resId = getItem(position); 85 result.setText(resId); 86 return result; 87 } 88 }; 89 90 if (TelephonyManager.getDefault().hasIccCard()) { 91 adapter.add(R.string.import_from_sim); 92 } 93 if (res.getBoolean(R.bool.config_allow_import_from_sdcard)) { 94 adapter.add(R.string.import_from_sdcard); 95 } 96 if (res.getBoolean(R.bool.config_allow_export_to_sdcard)) { 97 adapter.add(R.string.export_to_sdcard); 98 } 99 if (res.getBoolean(R.bool.config_allow_share_visible_contacts)) { 100 adapter.add(R.string.share_visible_contacts); 101 } 102 103 final DialogInterface.OnClickListener clickListener = 104 new DialogInterface.OnClickListener() { 105 @Override 106 public void onClick(DialogInterface dialog, int which) { 107 boolean dismissDialog; 108 final int resId = adapter.getItem(which); 109 switch (resId) { 110 case R.string.import_from_sim: 111 case R.string.import_from_sdcard: { 112 dismissDialog = handleImportRequest(resId); 113 break; 114 } 115 case R.string.export_to_sdcard: { 116 dismissDialog = true; 117 Intent exportIntent = new Intent(getActivity(), ExportVCardActivity.class); 118 getActivity().startActivity(exportIntent); 119 break; 120 } 121 case R.string.share_visible_contacts: { 122 dismissDialog = true; 123 doShareVisibleContacts(); 124 break; 125 } 126 default: { 127 dismissDialog = true; 128 Log.e(TAG, "Unexpected resource: " 129 + getActivity().getResources().getResourceEntryName(resId)); 130 } 131 } 132 if (dismissDialog) { 133 dialog.dismiss(); 134 } 135 } 136 }; 137 return new AlertDialog.Builder(getActivity()) 138 .setTitle(R.string.dialog_import_export) 139 .setSingleChoiceItems(adapter, -1, clickListener) 140 .create(); 141 } 142 143 private void doShareVisibleContacts() { 144 // TODO move the query into a loader and do this in a background thread 145 final Cursor cursor = getActivity().getContentResolver().query(Contacts.CONTENT_URI, 146 LOOKUP_PROJECTION, Contacts.IN_VISIBLE_GROUP + "!=0", null, null); 147 if (cursor != null) { 148 try { 149 if (!cursor.moveToFirst()) { 150 Toast.makeText(getActivity(), R.string.share_error, Toast.LENGTH_SHORT).show(); 151 return; 152 } 153 154 StringBuilder uriListBuilder = new StringBuilder(); 155 int index = 0; 156 do { 157 if (index != 0) 158 uriListBuilder.append(':'); 159 uriListBuilder.append(cursor.getString(0)); 160 index++; 161 } while (cursor.moveToNext()); 162 Uri uri = Uri.withAppendedPath( 163 Contacts.CONTENT_MULTI_VCARD_URI, 164 Uri.encode(uriListBuilder.toString())); 165 166 final Intent intent = new Intent(Intent.ACTION_SEND); 167 intent.setType(Contacts.CONTENT_VCARD_TYPE); 168 intent.putExtra(Intent.EXTRA_STREAM, uri); 169 getActivity().startActivity(intent); 170 } finally { 171 cursor.close(); 172 } 173 } 174 } 175 176 /** 177 * Handle "import from SIM" and "import from SD". 178 * 179 * @return {@code true} if the dialog show be closed. {@code false} otherwise. 180 */ 181 private boolean handleImportRequest(int resId) { 182 // There are three possibilities: 183 // - more than one accounts -> ask the user 184 // - just one account -> use the account without asking the user 185 // - no account -> use phone-local storage without asking the user 186 final AccountTypeManager accountTypes = AccountTypeManager.getInstance(getActivity()); 187 final List<AccountWithDataSet> accountList = accountTypes.getAccounts(true); 188 final int size = accountList.size(); 189 if (size > 1) { 190 // Send over to the account selector 191 final Bundle args = new Bundle(); 192 args.putInt(KEY_RES_ID, resId); 193 SelectAccountDialogFragment.show( 194 getFragmentManager(), this, 195 R.string.dialog_new_contact_account, 196 AccountListFilter.ACCOUNTS_CONTACT_WRITABLE, args); 197 198 // In this case, because this DialogFragment is used as a target fragment to 199 // SelectAccountDialogFragment, we can't close it yet. We close the dialog when 200 // we get a callback from it. 201 return false; 202 } 203 204 AccountSelectionUtil.doImport(getActivity(), resId, 205 (size == 1 ? accountList.get(0) : null)); 206 return true; // Close the dialog. 207 } 208 209 /** 210 * Called when an account is selected on {@link SelectAccountDialogFragment}. 211 */ 212 @Override 213 public void onAccountChosen(AccountWithDataSet account, Bundle extraArgs) { 214 AccountSelectionUtil.doImport(getActivity(), extraArgs.getInt(KEY_RES_ID), account); 215 216 // At this point the dialog is still showing (which is why we can use getActivity() above) 217 // So close it. 218 dismiss(); 219 } 220 221 @Override 222 public void onAccountSelectorCancelled() { 223 // See onAccountChosen() -- at this point the dialog is still showing. Close it. 224 dismiss(); 225 } 226 } 227