diff options
author | Brian Attwell <brianattwell@google.com> | 2014-07-22 16:04:52 -0700 |
---|---|---|
committer | Brian Attwell <brianattwell@google.com> | 2014-07-23 18:17:32 +0000 |
commit | 81cc3b3d09d9296e521ac3454ad01c6b6c2ba71b (patch) | |
tree | c2adb270b83008a30ac036bc13fc49f4ac423a0e /src/com/android | |
parent | 333091ae754ddfc25714c14b9b89534be24379f9 (diff) | |
download | packages_apps_Contacts-81cc3b3d09d9296e521ac3454ad01c6b6c2ba71b.tar.gz packages_apps_Contacts-81cc3b3d09d9296e521ac3454ad01c6b6c2ba71b.tar.bz2 packages_apps_Contacts-81cc3b3d09d9296e521ac3454ad01c6b6c2ba71b.zip |
Contact picker: add search icon & hide searchview
Hide/unhide the SearchView when you click a seach menu item.
This is the first and more important step for b/16190113.
In a later CL, I might remove the SearchView and replace it
with the custom actionbar view created by dialer & used
in PeopleActivity.
Also merged JoinContactActivity into ContactSelectionActivity.
Bug: 16190113
Change-Id: I04bf7d22dffbf7f104362b17c9064cb386a01abe
Diffstat (limited to 'src/com/android')
5 files changed, 116 insertions, 256 deletions
diff --git a/src/com/android/contacts/activities/ContactSelectionActivity.java b/src/com/android/contacts/activities/ContactSelectionActivity.java index c67a50e68..c35f192d5 100644 --- a/src/com/android/contacts/activities/ContactSelectionActivity.java +++ b/src/com/android/contacts/activities/ContactSelectionActivity.java @@ -27,9 +27,12 @@ import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.Intents.Insert; +import android.provider.ContactsContract.Intents.UI; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; @@ -48,6 +51,7 @@ import com.android.contacts.list.ContactsIntentResolver; import com.android.contacts.list.ContactsRequest; import com.android.contacts.common.list.DirectoryListLoader; import com.android.contacts.list.EmailAddressPickerFragment; +import com.android.contacts.list.JoinContactListFragment; import com.android.contacts.list.LegacyPhoneNumberPickerFragment; import com.android.contacts.list.OnContactPickerActionListener; import com.android.contacts.list.OnEmailAddressPickerActionListener; @@ -71,15 +75,18 @@ public class ContactSelectionActivity extends ContactsActivity private static final int SUBACTIVITY_ADD_TO_EXISTING_CONTACT = 0; private static final String KEY_ACTION_CODE = "actionCode"; + private static final String KEY_SEARCH_MODE = "searchMode"; private static final int DEFAULT_DIRECTORY_RESULT_LIMIT = 20; private ContactsIntentResolver mIntentResolver; protected ContactEntryListFragment<?> mListFragment; private int mActionCode = -1; + private boolean mIsSearchMode; private ContactsRequest mRequest; private SearchView mSearchView; + private View mSearchViewContainer; public ContactSelectionActivity() { mIntentResolver = new ContactsIntentResolver(this); @@ -99,6 +106,7 @@ public class ContactSelectionActivity extends ContactsActivity if (savedState != null) { mActionCode = savedState.getInt(KEY_ACTION_CODE); + mIsSearchMode = savedState.getBoolean(KEY_SEARCH_MODE); } // Extract relevant information from the intent @@ -131,9 +139,9 @@ public class ContactSelectionActivity extends ContactsActivity private void prepareSearchViewAndActionBar() { final ActionBar actionBar = getActionBar(); - final View searchViewContainer = LayoutInflater.from(actionBar.getThemedContext()) + mSearchViewContainer = LayoutInflater.from(actionBar.getThemedContext()) .inflate(R.layout.custom_action_bar, null); - mSearchView = (SearchView) searchViewContainer.findViewById(R.id.search_view); + mSearchView = (SearchView) mSearchViewContainer.findViewById(R.id.search_view); // Postal address pickers (and legacy pickers) don't support search, so just show // "HomeAsUp" button and title. @@ -148,25 +156,39 @@ public class ContactSelectionActivity extends ContactsActivity return; } + actionBar.setDisplayShowHomeEnabled(true); + actionBar.setDisplayHomeAsUpEnabled(true); + // In order to make the SearchView look like "shown via search menu", we need to // manually setup its state. See also DialtactsActivity.java and ActionBarAdapter.java. mSearchView.setIconifiedByDefault(true); mSearchView.setQueryHint(getString(R.string.hint_findContacts)); mSearchView.setIconified(false); + mSearchView.setFocusable(true); mSearchView.setOnQueryTextListener(this); mSearchView.setOnCloseListener(this); mSearchView.setOnQueryTextFocusChangeListener(this); - actionBar.setCustomView(searchViewContainer, + actionBar.setCustomView(mSearchViewContainer, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); actionBar.setDisplayShowCustomEnabled(true); - actionBar.setDisplayShowHomeEnabled(true); - actionBar.setDisplayHomeAsUpEnabled(true); - actionBar.setDisplayShowTitleEnabled(false); - // Clear focus and suppress keyboard show-up. - mSearchView.clearFocus(); + configureSearchMode(); + } + + private void configureSearchMode() { + final ActionBar actionBar = getActionBar(); + if (mIsSearchMode) { + actionBar.setDisplayShowTitleEnabled(false); + mSearchViewContainer.setVisibility(View.VISIBLE); + mSearchView.requestFocus(); + } else { + actionBar.setDisplayShowTitleEnabled(true); + mSearchViewContainer.setVisibility(View.GONE); + mSearchView.setQuery(null, true); + } + invalidateOptionsMenu(); } @Override @@ -175,7 +197,11 @@ public class ContactSelectionActivity extends ContactsActivity case android.R.id.home: // Go back to previous screen, intending "cancel" setResult(RESULT_CANCELED); - finish(); + onBackPressed(); + return true; + case R.id.menu_search: + mIsSearchMode = !mIsSearchMode; + configureSearchMode(); return true; } return super.onOptionsItemSelected(item); @@ -185,6 +211,7 @@ public class ContactSelectionActivity extends ContactsActivity protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt(KEY_ACTION_CODE, mActionCode); + outState.putBoolean(KEY_SEARCH_MODE, mIsSearchMode); } private void configureActivityTitle() { @@ -239,6 +266,11 @@ public class ContactSelectionActivity extends ContactsActivity setTitle(R.string.contactPickerActivityTitle); break; } + + case ContactsRequest.ACTION_PICK_JOIN: { + setTitle(R.string.titleJoinContactDataWith); + break; + } } } @@ -307,10 +339,18 @@ public class ContactSelectionActivity extends ContactsActivity case ContactsRequest.ACTION_PICK_POSTAL: { PostalAddressPickerFragment fragment = new PostalAddressPickerFragment(); + mListFragment = fragment; break; } + case ContactsRequest.ACTION_PICK_JOIN: { + JoinContactListFragment joinFragment = new JoinContactListFragment(); + joinFragment.setTargetContactId(getTargetContactId()); + mListFragment = joinFragment; + break; + } + default: throw new IllegalStateException("Invalid action code: " + mActionCode); } @@ -347,6 +387,9 @@ public class ContactSelectionActivity extends ContactsActivity } else if (mListFragment instanceof EmailAddressPickerFragment) { ((EmailAddressPickerFragment) mListFragment).setOnEmailAddressPickerActionListener( new EmailAddressPickerActionListener()); + } else if (mListFragment instanceof JoinContactListFragment) { + ((JoinContactListFragment) mListFragment).setOnContactPickerActionListener( + new JoinContactActionListener()); } else { throw new IllegalStateException("Unsupported list fragment type: " + mListFragment); } @@ -460,6 +503,27 @@ public class ContactSelectionActivity extends ContactsActivity } } + private final class JoinContactActionListener implements OnContactPickerActionListener { + @Override + public void onPickContactAction(Uri contactUri) { + Intent intent = new Intent(null, contactUri); + setResult(RESULT_OK, intent); + finish(); + } + + @Override + public void onShortcutIntentCreated(Intent intent) { + } + + @Override + public void onCreateNewContactAction() { + } + + @Override + public void onEditContactAction(Uri contactLookupUri) { + } + } + private final class PostalAddressPickerActionListener implements OnPostalAddressPickerActionListener { @Override @@ -546,6 +610,19 @@ public class ContactSelectionActivity extends ContactsActivity } } + private long getTargetContactId() { + Intent intent = getIntent(); + final long targetContactId = intent.getLongExtra(UI.TARGET_CONTACT_ID_EXTRA_KEY, -1); + if (targetContactId == -1) { + Log.e(TAG, "Intent " + intent.getAction() + " is missing required extra: " + + UI.TARGET_CONTACT_ID_EXTRA_KEY); + setResult(RESULT_CANCELED); + finish(); + return -1; + } + return targetContactId; + } + private void startCreateNewContactActivity() { Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI); intent.putExtra(ContactEditorActivity.INTENT_KEY_FINISH_ACTIVITY_ON_SAVE_COMPLETED, true); @@ -574,4 +651,26 @@ public class ContactSelectionActivity extends ContactsActivity } } } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + super.onCreateOptionsMenu(menu); + + final MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.search_menu, menu); + + final MenuItem searchItem = menu.findItem(R.id.menu_search); + searchItem.setVisible(!mIsSearchMode); + return true; + } + + @Override + public void onBackPressed() { + if (mIsSearchMode) { + mIsSearchMode = false; + configureSearchMode(); + } else { + super.onBackPressed(); + } + } } diff --git a/src/com/android/contacts/activities/JoinContactActivity.java b/src/com/android/contacts/activities/JoinContactActivity.java deleted file mode 100644 index c345a01d5..000000000 --- a/src/com/android/contacts/activities/JoinContactActivity.java +++ /dev/null @@ -1,244 +0,0 @@ -/* - * Copyright (C) 2009 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.contacts.activities; - - -import android.app.ActionBar; -import android.app.ActionBar.LayoutParams; -import android.app.Fragment; -import android.content.Context; -import android.content.Intent; -import android.net.Uri; -import android.os.Bundle; -import android.provider.ContactsContract; -import android.text.TextUtils; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.View.OnFocusChangeListener; -import android.view.inputmethod.InputMethodManager; -import android.widget.SearchView; -import android.widget.SearchView.OnCloseListener; -import android.widget.SearchView.OnQueryTextListener; - -import com.android.contacts.ContactsActivity; -import com.android.contacts.R; -import com.android.contacts.common.list.ContactEntryListFragment; -import com.android.contacts.list.JoinContactListFragment; -import com.android.contacts.list.OnContactPickerActionListener; - -/** - * An activity that shows a list of contacts that can be joined with the target contact. - */ -public class JoinContactActivity extends ContactsActivity - implements OnQueryTextListener, OnCloseListener, OnFocusChangeListener { - - private static final String TAG = "JoinContactActivity"; - - /** - * The action for the join contact activity. - * <p> - * Input: extra field {@link #EXTRA_TARGET_CONTACT_ID} is the aggregate ID. - * TODO: move to {@link ContactsContract}. - */ - public static final String JOIN_CONTACT = "com.android.contacts.action.JOIN_CONTACT"; - - /** - * Used with {@link #JOIN_CONTACT} to give it the target for aggregation. - * <p> - * Type: LONG - */ - public static final String EXTRA_TARGET_CONTACT_ID = "com.android.contacts.action.CONTACT_ID"; - - private static final String KEY_TARGET_CONTACT_ID = "targetContactId"; - - private long mTargetContactId; - - private JoinContactListFragment mListFragment; - private SearchView mSearchView; - - @Override - public void onAttachFragment(Fragment fragment) { - if (fragment instanceof JoinContactListFragment) { - mListFragment = (JoinContactListFragment) fragment; - setupActionListener(); - } - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - Intent intent = getIntent(); - mTargetContactId = intent.getLongExtra(EXTRA_TARGET_CONTACT_ID, -1); - if (mTargetContactId == -1) { - Log.e(TAG, "Intent " + intent.getAction() + " is missing required extra: " - + EXTRA_TARGET_CONTACT_ID); - setResult(RESULT_CANCELED); - finish(); - return; - } - - setContentView(R.layout.join_contact_picker); - setTitle(R.string.titleJoinContactDataWith); - - if (mListFragment == null) { - mListFragment = new JoinContactListFragment(); - - getFragmentManager().beginTransaction() - .replace(R.id.list_container, mListFragment) - .commitAllowingStateLoss(); - } - - prepareSearchViewAndActionBar(); - } - - private void setupActionListener() { - mListFragment.setTargetContactId(mTargetContactId); - mListFragment.setOnContactPickerActionListener(new OnContactPickerActionListener() { - @Override - public void onPickContactAction(Uri contactUri) { - Intent intent = new Intent(null, contactUri); - setResult(RESULT_OK, intent); - finish(); - } - - @Override - public void onShortcutIntentCreated(Intent intent) { - } - - @Override - public void onCreateNewContactAction() { - } - - @Override - public void onEditContactAction(Uri contactLookupUri) { - } - }); - } - - private void prepareSearchViewAndActionBar() { - final ActionBar actionBar = getActionBar(); - if (actionBar != null) { - final View searchViewOnLayout = findViewById(R.id.search_view); - if (searchViewOnLayout != null) { - searchViewOnLayout.setVisibility(View.GONE); - } - - final View searchViewLayout = LayoutInflater.from(actionBar.getThemedContext()) - .inflate(R.layout.custom_action_bar, null); - mSearchView = (SearchView) searchViewLayout.findViewById(R.id.search_view); - - // In order to make the SearchView look like "shown via search menu", we need to - // manually setup its state. See also DialtactsActivity.java and ActionBarAdapter.java. - mSearchView.setIconifiedByDefault(true); - mSearchView.setQueryHint(getString(R.string.hint_findContacts)); - mSearchView.setIconified(false); - - mSearchView.setOnQueryTextListener(this); - mSearchView.setOnCloseListener(this); - mSearchView.setOnQueryTextFocusChangeListener(this); - - actionBar.setCustomView(searchViewLayout, - new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); - actionBar.setDisplayShowCustomEnabled(true); - actionBar.setDisplayShowHomeEnabled(true); - actionBar.setDisplayHomeAsUpEnabled(true); - } else { - mSearchView = (SearchView) findViewById(R.id.search_view); - mSearchView.setQueryHint(getString(R.string.hint_findContacts)); - mSearchView.setOnQueryTextListener(this); - mSearchView.setOnQueryTextFocusChangeListener(this); - } - - // Clear focus and suppress keyboard show-up. - mSearchView.clearFocus(); - } - - @Override - public boolean onQueryTextChange(String newText) { - mListFragment.setQueryString(newText, true); - return false; - } - - @Override - public boolean onQueryTextSubmit(String query) { - return false; - } - - @Override - public boolean onClose() { - if (!TextUtils.isEmpty(mSearchView.getQuery())) { - mSearchView.setQuery(null, true); - } - return true; - } - - @Override - public void onFocusChange(View view, boolean hasFocus) { - switch (view.getId()) { - case R.id.search_view: { - if (hasFocus) { - showInputMethod(mSearchView.findFocus()); - } - } - } - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - // Go back to previous screen, intending "cancel" - setResult(RESULT_CANCELED); - finish(); - return true; - } - return super.onOptionsItemSelected(item); - } - - @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - outState.putLong(KEY_TARGET_CONTACT_ID, mTargetContactId); - } - - @Override - protected void onRestoreInstanceState(Bundle savedInstanceState) { - super.onRestoreInstanceState(savedInstanceState); - mTargetContactId = savedInstanceState.getLong(KEY_TARGET_CONTACT_ID); - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == ContactEntryListFragment.ACTIVITY_REQUEST_CODE_PICKER - && resultCode == RESULT_OK) { - mListFragment.onPickerResult(data); - } - } - - private void showInputMethod(View view) { - final InputMethodManager imm = (InputMethodManager) - getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - if (!imm.showSoftInput(view, 0)) { - Log.w(TAG, "Failed to show soft input method."); - } - } - } -} diff --git a/src/com/android/contacts/editor/ContactEditorFragment.java b/src/com/android/contacts/editor/ContactEditorFragment.java index 1b950d2ef..b4ec7f94a 100644 --- a/src/com/android/contacts/editor/ContactEditorFragment.java +++ b/src/com/android/contacts/editor/ContactEditorFragment.java @@ -49,6 +49,7 @@ import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.Groups; import android.provider.ContactsContract.Intents; +import android.provider.ContactsContract.Intents.UI; import android.provider.ContactsContract.QuickContact; import android.provider.ContactsContract.RawContacts; import android.text.TextUtils; @@ -71,7 +72,6 @@ import com.android.contacts.GroupMetaDataLoader; import com.android.contacts.R; import com.android.contacts.activities.ContactEditorAccountsChangedActivity; import com.android.contacts.activities.ContactEditorActivity; -import com.android.contacts.activities.JoinContactActivity; import com.android.contacts.common.model.AccountTypeManager; import com.android.contacts.common.model.ValuesDelta; import com.android.contacts.common.model.account.AccountType; @@ -1377,8 +1377,8 @@ public class ContactEditorFragment extends Fragment implements mContactIdForJoin = ContentUris.parseId(contactLookupUri); mContactWritableForJoin = isContactWritable(); - final Intent intent = new Intent(JoinContactActivity.JOIN_CONTACT); - intent.putExtra(JoinContactActivity.EXTRA_TARGET_CONTACT_ID, mContactIdForJoin); + final Intent intent = new Intent(UI.PICK_JOIN_CONTACT_ACTION); + intent.putExtra(UI.TARGET_CONTACT_ID_EXTRA_KEY, mContactIdForJoin); startActivityForResult(intent, REQUEST_CODE_JOIN); } diff --git a/src/com/android/contacts/list/ContactsIntentResolver.java b/src/com/android/contacts/list/ContactsIntentResolver.java index 838393e84..b4416beda 100644 --- a/src/com/android/contacts/list/ContactsIntentResolver.java +++ b/src/com/android/contacts/list/ContactsIntentResolver.java @@ -173,6 +173,8 @@ public class ContactsIntentResolver { request.setContactUri(data); intent.setAction(Intent.ACTION_DEFAULT); intent.setData(null); + } else if (UI.PICK_JOIN_CONTACT_ACTION.equals(action)) { + request.setActionCode(ContactsRequest.ACTION_PICK_JOIN); } // Allow the title to be set to a custom String using an extra on the intent String title = intent.getStringExtra(UI.TITLE_EXTRA_KEY); diff --git a/src/com/android/contacts/list/ContactsRequest.java b/src/com/android/contacts/list/ContactsRequest.java index 2a26ddffd..7955fde35 100644 --- a/src/com/android/contacts/list/ContactsRequest.java +++ b/src/com/android/contacts/list/ContactsRequest.java @@ -77,6 +77,9 @@ public class ContactsRequest implements Parcelable { /** Show all contacts and activate the specified one */ public static final int ACTION_VIEW_CONTACT = 140; + /** Show contacts recommended for joining with a specified target contact */ + public static final int ACTION_PICK_JOIN = 150; + private boolean mValid = true; private int mActionCode = ACTION_DEFAULT; private Intent mRedirectIntent; |