ContactSelectionActivity.java revision 4d174aad97cd382f810e3bf1a7f1f4f4772be118
1/* 2 * Copyright (C) 2007 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.activities; 18 19import com.android.contacts.R; 20import com.android.contacts.list.ContactEntryListFragment; 21import com.android.contacts.list.ContactPickerFragment; 22import com.android.contacts.list.ContactsIntentResolver; 23import com.android.contacts.list.ContactsRequest; 24import com.android.contacts.list.DefaultContactBrowseListFragment; 25import com.android.contacts.list.DirectoryListLoader; 26import com.android.contacts.list.OnContactBrowserActionListener; 27import com.android.contacts.list.OnContactPickerActionListener; 28import com.android.contacts.list.OnPhoneNumberPickerActionListener; 29import com.android.contacts.list.OnPostalAddressPickerActionListener; 30import com.android.contacts.list.PhoneNumberPickerFragment; 31import com.android.contacts.list.PostalAddressPickerFragment; 32import com.android.contacts.widget.ContextMenuAdapter; 33 34import android.app.Activity; 35import android.app.Fragment; 36import android.content.Intent; 37import android.net.Uri; 38import android.os.Bundle; 39import android.provider.ContactsContract.Contacts; 40import android.text.TextUtils; 41import android.view.MenuItem; 42import android.view.View; 43import android.widget.SearchView; 44import android.widget.SearchView.OnQueryChangeListener; 45 46/** 47 * Displays a list of contacts (or phone numbers or postal addresses) for the 48 * purposes of selecting one. 49 */ 50public class ContactSelectionActivity extends Activity 51 implements View.OnCreateContextMenuListener, OnQueryChangeListener { 52 private static final String TAG = "ContactSelectionActivity"; 53 54 private static final String KEY_ACTION_CODE = "actionCode"; 55 private static final int DEFAULT_DIRECTORY_RESULT_LIMIT = 20; 56 57 private ContactsIntentResolver mIntentResolver; 58 protected ContactEntryListFragment<?> mListFragment; 59 60 private int mActionCode = -1; 61 62 private boolean mSearchInitiated; 63 64 private ContactsRequest mRequest; 65 private SearchView mSearchView; 66 67 public ContactSelectionActivity() { 68 mIntentResolver = new ContactsIntentResolver(this); 69 } 70 71 @Override 72 public void onAttachFragment(Fragment fragment) { 73 if (fragment instanceof ContactEntryListFragment<?>) { 74 mListFragment = (ContactEntryListFragment<?>) fragment; 75 setupActionListener(); 76 } 77 } 78 79 @Override 80 protected void onCreate(Bundle savedState) { 81 super.onCreate(savedState); 82 83 if (savedState != null) { 84 mActionCode = savedState.getInt(KEY_ACTION_CODE); 85 } 86 87 // Extract relevant information from the intent 88 mRequest = mIntentResolver.resolveIntent(getIntent()); 89 if (!mRequest.isValid()) { 90 setResult(RESULT_CANCELED); 91 finish(); 92 return; 93 } 94 95 Intent redirect = mRequest.getRedirectIntent(); 96 if (redirect != null) { 97 // Need to start a different activity 98 startActivity(redirect); 99 finish(); 100 return; 101 } 102 103 configureActivityTitle(); 104 105 setContentView(R.layout.contact_picker); 106 107 configureListFragment(); 108 109 mSearchView = (SearchView)findViewById(R.id.search_view); 110 mSearchView.setQueryHint(getString(R.string.hint_findContacts)); 111 mSearchView.setOnQueryChangeListener(this); 112 } 113 114 @Override 115 protected void onSaveInstanceState(Bundle outState) { 116 super.onSaveInstanceState(outState); 117 outState.putInt(KEY_ACTION_CODE, mActionCode); 118 } 119 120 private void configureActivityTitle() { 121 if (mRequest.getActivityTitle() != null) { 122 setTitle(mRequest.getActivityTitle()); 123 return; 124 } 125 126 int actionCode = mRequest.getActionCode(); 127 switch (actionCode) { 128 case ContactsRequest.ACTION_INSERT_OR_EDIT_CONTACT: { 129 setTitle(R.string.contactPickerActivityTitle); 130 break; 131 } 132 133 case ContactsRequest.ACTION_PICK_CONTACT: { 134 setTitle(R.string.contactPickerActivityTitle); 135 break; 136 } 137 138 case ContactsRequest.ACTION_PICK_OR_CREATE_CONTACT: { 139 setTitle(R.string.contactPickerActivityTitle); 140 break; 141 } 142 143 case ContactsRequest.ACTION_CREATE_SHORTCUT_CONTACT: { 144 setTitle(R.string.shortcutActivityTitle); 145 break; 146 } 147 148 case ContactsRequest.ACTION_PICK_PHONE: { 149 setTitle(R.string.contactPickerActivityTitle); 150 break; 151 } 152 153 case ContactsRequest.ACTION_CREATE_SHORTCUT_CALL: { 154 setTitle(R.string.callShortcutActivityTitle); 155 break; 156 } 157 158 case ContactsRequest.ACTION_CREATE_SHORTCUT_SMS: { 159 setTitle(R.string.messageShortcutActivityTitle); 160 break; 161 } 162 163 case ContactsRequest.ACTION_PICK_POSTAL: { 164 setTitle(R.string.contactPickerActivityTitle); 165 break; 166 } 167 } 168 } 169 170 /** 171 * Creates the fragment based on the current request. 172 */ 173 public void configureListFragment() { 174 if (mActionCode == mRequest.getActionCode()) { 175 return; 176 } 177 178 mActionCode = mRequest.getActionCode(); 179 switch (mActionCode) { 180 case ContactsRequest.ACTION_INSERT_OR_EDIT_CONTACT: { 181 DefaultContactBrowseListFragment fragment = new DefaultContactBrowseListFragment(); 182 fragment.setEditMode(true); 183 fragment.setCreateContactEnabled(true); 184 fragment.setDirectorySearchMode(DirectoryListLoader.SEARCH_MODE_CONTACT_SHORTCUT); 185 mListFragment = fragment; 186 break; 187 } 188 189 case ContactsRequest.ACTION_PICK_CONTACT: { 190 ContactPickerFragment fragment = new ContactPickerFragment(); 191 fragment.setSearchMode(mRequest.isSearchMode()); 192 mListFragment = fragment; 193 break; 194 } 195 196 case ContactsRequest.ACTION_PICK_OR_CREATE_CONTACT: { 197 ContactPickerFragment fragment = new ContactPickerFragment(); 198 fragment.setCreateContactEnabled(!mRequest.isSearchMode()); 199 mListFragment = fragment; 200 break; 201 } 202 203 case ContactsRequest.ACTION_CREATE_SHORTCUT_CONTACT: { 204 ContactPickerFragment fragment = new ContactPickerFragment(); 205 fragment.setCreateContactEnabled(!mRequest.isSearchMode()); 206 fragment.setSearchMode(mRequest.isSearchMode()); 207 fragment.setQueryString(mRequest.getQueryString()); 208 fragment.setShortcutRequested(true); 209 mListFragment = fragment; 210 break; 211 } 212 213 case ContactsRequest.ACTION_PICK_PHONE: { 214 PhoneNumberPickerFragment fragment = new PhoneNumberPickerFragment(); 215 mListFragment = fragment; 216 break; 217 } 218 219 case ContactsRequest.ACTION_CREATE_SHORTCUT_CALL: { 220 PhoneNumberPickerFragment fragment = new PhoneNumberPickerFragment(); 221 fragment.setShortcutAction(Intent.ACTION_CALL); 222 fragment.setSearchMode(mRequest.isSearchMode()); 223 224 mListFragment = fragment; 225 break; 226 } 227 228 case ContactsRequest.ACTION_CREATE_SHORTCUT_SMS: { 229 PhoneNumberPickerFragment fragment = new PhoneNumberPickerFragment(); 230 fragment.setShortcutAction(Intent.ACTION_SENDTO); 231 232 mListFragment = fragment; 233 break; 234 } 235 236 case ContactsRequest.ACTION_PICK_POSTAL: { 237 PostalAddressPickerFragment fragment = new PostalAddressPickerFragment(); 238 mListFragment = fragment; 239 break; 240 } 241 242 default: 243 throw new IllegalStateException("Invalid action code: " + mActionCode); 244 } 245 246 mListFragment.setLegacyCompatibilityMode(mRequest.isLegacyCompatibilityMode()); 247 mListFragment.setContactsRequest(mRequest); 248 mListFragment.setSearchMode(mRequest.isSearchMode()); 249 mListFragment.setQueryString(mRequest.getQueryString()); 250 mListFragment.setDirectoryResultLimit(DEFAULT_DIRECTORY_RESULT_LIMIT); 251 252 getFragmentManager().openTransaction() 253 .replace(R.id.list_container, mListFragment) 254 .commit(); 255 } 256 257 public void setupActionListener() { 258 if (mListFragment instanceof DefaultContactBrowseListFragment) { 259 ((DefaultContactBrowseListFragment) mListFragment).setOnContactListActionListener( 260 new ContactBrowserActionListener()); 261 } else if (mListFragment instanceof ContactPickerFragment) { 262 ((ContactPickerFragment) mListFragment).setOnContactPickerActionListener( 263 new ContactPickerActionListener()); 264 } else if (mListFragment instanceof PhoneNumberPickerFragment) { 265 ((PhoneNumberPickerFragment) mListFragment).setOnPhoneNumberPickerActionListener( 266 new PhoneNumberPickerActionListener()); 267 } else if (mListFragment instanceof PostalAddressPickerFragment) { 268 ((PostalAddressPickerFragment) mListFragment).setOnPostalAddressPickerActionListener( 269 new PostalAddressPickerActionListener()); 270 } else { 271 throw new IllegalStateException("Unsupported list fragment type: " + mListFragment); 272 } 273 } 274 275 private final class ContactBrowserActionListener implements OnContactBrowserActionListener { 276 @Override 277 public void onViewContactAction(Uri contactLookupUri) { 278 throw new UnsupportedOperationException(); 279 } 280 281 @Override 282 public void onCreateNewContactAction() { 283 Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI); 284 Bundle extras = getIntent().getExtras(); 285 if (extras != null) { 286 intent.putExtras(extras); 287 } 288 startActivity(intent); 289 } 290 291 @Override 292 public void onEditContactAction(Uri contactLookupUri) { 293 Intent intent = new Intent(Intent.ACTION_EDIT, contactLookupUri); 294 Bundle extras = getIntent().getExtras(); 295 if (extras != null) { 296 intent.putExtras(extras); 297 } 298 startActivity(intent); 299 } 300 301 @Override 302 public void onAddToFavoritesAction(Uri contactUri) { 303 throw new UnsupportedOperationException(); 304 } 305 306 @Override 307 public void onRemoveFromFavoritesAction(Uri contactUri) { 308 throw new UnsupportedOperationException(); 309 } 310 311 @Override 312 public void onCallContactAction(Uri contactUri) { 313 throw new UnsupportedOperationException(); 314 } 315 316 @Override 317 public void onSmsContactAction(Uri contactUri) { 318 throw new UnsupportedOperationException(); 319 } 320 321 @Override 322 public void onDeleteContactAction(Uri contactUri) { 323 throw new UnsupportedOperationException(); 324 } 325 326 @Override 327 public void onFinishAction() { 328 onBackPressed(); 329 } 330 } 331 332 private final class ContactPickerActionListener implements OnContactPickerActionListener { 333 @Override 334 public void onCreateNewContactAction() { 335 Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI); 336 startActivityAndForwardResult(intent); 337 } 338 339 @Override 340 public void onPickContactAction(Uri contactUri) { 341 Intent intent = new Intent(); 342 setResult(RESULT_OK, intent.setData(contactUri)); 343 finish(); 344 } 345 346 @Override 347 public void onShortcutIntentCreated(Intent intent) { 348 setResult(RESULT_OK, intent); 349 finish(); 350 } 351 } 352 353 private final class PhoneNumberPickerActionListener implements 354 OnPhoneNumberPickerActionListener { 355 @Override 356 public void onPickPhoneNumberAction(Uri dataUri) { 357 Intent intent = new Intent(); 358 setResult(RESULT_OK, intent.setData(dataUri)); 359 finish(); 360 } 361 362 @Override 363 public void onShortcutIntentCreated(Intent intent) { 364 setResult(RESULT_OK, intent); 365 finish(); 366 } 367 } 368 369 private final class PostalAddressPickerActionListener implements 370 OnPostalAddressPickerActionListener { 371 @Override 372 public void onPickPostalAddressAction(Uri dataUri) { 373 Intent intent = new Intent(); 374 setResult(RESULT_OK, intent.setData(dataUri)); 375 finish(); 376 } 377 } 378 379 public void startActivityAndForwardResult(final Intent intent) { 380 intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); 381 382 // Forward extras to the new activity 383 Bundle extras = getIntent().getExtras(); 384 if (extras != null) { 385 intent.putExtras(extras); 386 } 387 startActivity(intent); 388 finish(); 389 } 390 391 @Override 392 public boolean onContextItemSelected(MenuItem item) { 393 ContextMenuAdapter menuAdapter = mListFragment.getContextMenuAdapter(); 394 if (menuAdapter != null) { 395 return menuAdapter.onContextItemSelected(item); 396 } 397 398 return super.onContextItemSelected(item); 399 } 400 401 @Override 402 public boolean onQueryTextChanged(String newText) { 403 mListFragment.setQueryString(newText); 404 mListFragment.setSearchMode(!TextUtils.isEmpty(newText)); 405 return false; 406 } 407 408 @Override 409 public boolean onSubmitQuery(String query) { 410 return false; 411 } 412} 413