From 73b51d5771b31c932a589abc9bb0fe64c52fe102 Mon Sep 17 00:00:00 2001 From: calderwoodra Date: Fri, 8 Dec 2017 20:52:56 -0800 Subject: Implemented adding a new favorites contact flow NUI. This change consists of mainly 3 things: - Update contacts fragment to meet AddFavoriteActivity requirements - Implement AddFavoriteActivity - Passing the contact back to SpeedDialFragment Bug: 36841782 Test: SpeedDialIntegrationTest PiperOrigin-RevId: 178461265 Change-Id: Ib3a13eae311acf6ce10a94df4f2c95b9af120cff --- .../dialer/speeddial/AddFavoriteActivity.java | 109 +++++++++++++++++++++ .../android/dialer/speeddial/AndroidManifest.xml | 13 ++- .../android/dialer/speeddial/SpeedDialCursor.java | 4 - .../dialer/speeddial/SpeedDialFragment.java | 40 +++----- .../speeddial/res/layout/add_favorite_activity.xml | 21 ++++ .../speeddial/res/menu/add_favorite_menu.xml | 24 +++++ .../dialer/speeddial/res/values/strings.xml | 9 +- 7 files changed, 188 insertions(+), 32 deletions(-) create mode 100644 java/com/android/dialer/speeddial/AddFavoriteActivity.java create mode 100644 java/com/android/dialer/speeddial/res/layout/add_favorite_activity.xml create mode 100644 java/com/android/dialer/speeddial/res/menu/add_favorite_menu.xml (limited to 'java/com/android/dialer/speeddial') diff --git a/java/com/android/dialer/speeddial/AddFavoriteActivity.java b/java/com/android/dialer/speeddial/AddFavoriteActivity.java new file mode 100644 index 000000000..eea6e840e --- /dev/null +++ b/java/com/android/dialer/speeddial/AddFavoriteActivity.java @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2017 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.dialer.speeddial; + +import android.content.ContentValues; +import android.net.Uri; +import android.os.Bundle; +import android.provider.ContactsContract.Contacts; +import android.support.annotation.Nullable; +import android.support.annotation.WorkerThread; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.SearchView; +import android.view.Menu; +import android.view.MenuItem; +import android.widget.ImageView; +import com.android.dialer.common.Assert; +import com.android.dialer.common.concurrent.DialerExecutorComponent; +import com.android.dialer.contactsfragment.ContactsFragment; +import com.android.dialer.contactsfragment.ContactsFragment.OnContactSelectedListener; + +/** + * Activity for selecting a single contact and adding it to favorites. + * + *

Contacts are displayed using {@link ContactsFragment}. Contacts are searchable via search bar + * in the toolbar. When a contact is selected, it's uri is passed back in the result data. + */ +public class AddFavoriteActivity extends AppCompatActivity implements OnContactSelectedListener { + + private ContactsFragment contactsFragment; + + @Override + protected void onCreate(@Nullable Bundle bundle) { + super.onCreate(bundle); + setContentView(R.layout.add_favorite_activity); + contactsFragment = ContactsFragment.newAddFavoritesInstance(); + getFragmentManager() + .beginTransaction() + .add(R.id.add_favorite_container, contactsFragment, null) + .commit(); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.add_favorite_menu, menu); + + MenuItem searchItem = menu.findItem(R.id.action_search); + SearchView searchView = (SearchView) searchItem.getActionView(); + searchView.setOnQueryTextListener( + new SearchView.OnQueryTextListener() { + @Override + public boolean onQueryTextSubmit(String query) { + if (!searchView.isIconified()) { + searchView.setIconified(true); + } + searchItem.collapseActionView(); + return false; + } + + @Override + public boolean onQueryTextChange(String s) { + contactsFragment.updateQuery(s); + return false; + } + }); + return true; + } + + @Override + public void onContactSelected(ImageView photo, Uri contactUri, long contactId) { + DialerExecutorComponent.get(this) + .dialerExecutorFactory() + .createUiTaskBuilder( + getFragmentManager(), "mark_contact_favorite", this::markContactStarred) + .onSuccess(output -> finish()) + .onFailure(this::onContactStarredFailed) + .build() + .executeParallel(contactId); + } + + @WorkerThread + private int markContactStarred(long contactId) { + // TODO(calderwoodra): For now, we will just mark contacts as starred. This means that contacts + // will only be able to exist once in favorites until we implement multiple contact avenues. + ContentValues contentValues = new ContentValues(); + contentValues.put(Contacts.STARRED, 1); + + String where = Contacts._ID + " = ?"; + String[] selectionArgs = new String[] {Long.toString(contactId)}; + return getContentResolver().update(Contacts.CONTENT_URI, contentValues, where, selectionArgs); + } + + private void onContactStarredFailed(Throwable throwable) { + throw Assert.createAssertionFailException(throwable.getMessage()); + } +} diff --git a/java/com/android/dialer/speeddial/AndroidManifest.xml b/java/com/android/dialer/speeddial/AndroidManifest.xml index f4f0d82eb..99ab12c4b 100644 --- a/java/com/android/dialer/speeddial/AndroidManifest.xml +++ b/java/com/android/dialer/speeddial/AndroidManifest.xml @@ -13,4 +13,15 @@ ~ See the License for the specific language governing permissions and ~ limitations under the License --> - + + + + + + \ No newline at end of file diff --git a/java/com/android/dialer/speeddial/SpeedDialCursor.java b/java/com/android/dialer/speeddial/SpeedDialCursor.java index 552fab175..1208daefd 100644 --- a/java/com/android/dialer/speeddial/SpeedDialCursor.java +++ b/java/com/android/dialer/speeddial/SpeedDialCursor.java @@ -64,10 +64,6 @@ final class SpeedDialCursor extends MergeCursor { strequentCursor.moveToPosition(-1); while (strequentCursor.moveToNext()) { - if (strequentCursor.getInt(StrequentContactsCursorLoader.PHONE_IS_SUPER_PRIMARY) != 0) { - continue; - } - if (strequentCursor.getPosition() != 0) { long contactId = strequentCursor.getLong(StrequentContactsCursorLoader.PHONE_CONTACT_ID); int position = strequentCursor.getPosition(); diff --git a/java/com/android/dialer/speeddial/SpeedDialFragment.java b/java/com/android/dialer/speeddial/SpeedDialFragment.java index 65e542cd4..08861dae9 100644 --- a/java/com/android/dialer/speeddial/SpeedDialFragment.java +++ b/java/com/android/dialer/speeddial/SpeedDialFragment.java @@ -18,10 +18,10 @@ package com.android.dialer.speeddial; import android.app.Fragment; import android.app.LoaderManager.LoaderCallbacks; +import android.content.Intent; import android.content.Loader; import android.database.Cursor; import android.os.Bundle; -import android.provider.ContactsContract.Contacts; import android.support.annotation.Nullable; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; @@ -35,7 +35,16 @@ import com.android.dialer.speeddial.FavoritesViewHolder.FavoriteContactsListener import com.android.dialer.speeddial.HeaderViewHolder.SpeedDialHeaderListener; import com.android.dialer.speeddial.SuggestionViewHolder.SuggestedContactsListener; -/** Favorites fragment. Contents TBD. TODO(calderwoodra) */ +/** + * Fragment for displaying: + * + *

+ * + *

Suggested contacts built from {@link android.provider.ContactsContract#STREQUENT_PHONE_ONLY}. + */ public class SpeedDialFragment extends Fragment { private static final int STREQUENT_CONTACTS_LOADER_ID = 1; @@ -73,16 +82,16 @@ public class SpeedDialFragment extends Fragment { } @Override - public void onPause() { - super.onPause(); - loaderCallback.unregisterContentObserver(); + public void onResume() { + super.onResume(); + getLoaderManager().restartLoader(STREQUENT_CONTACTS_LOADER_ID, null, loaderCallback); } - private static class SpeedDialFragmentHeaderListener implements SpeedDialHeaderListener { + private class SpeedDialFragmentHeaderListener implements SpeedDialHeaderListener { @Override public void onAddFavoriteClicked() { - // TODO(calderwoodra): implement add favorite screen + startActivity(new Intent(getContext(), AddFavoriteActivity.class)); } } @@ -123,8 +132,6 @@ public class SpeedDialFragment extends Fragment { */ private class SpeedDialFragmentLoaderCallback implements LoaderCallbacks { - private StrequentContactsCursorLoader cursorLoader; - @Override public Loader onCreateLoader(int id, Bundle args) { if (id == STREQUENT_CONTACTS_LOADER_ID) { @@ -135,24 +142,9 @@ public class SpeedDialFragment extends Fragment { @Override public void onLoadFinished(Loader loader, Cursor data) { - cursorLoader = (StrequentContactsCursorLoader) loader; - // Since the original cursor we queried against was modified and closed, we need to register a - // new content observer in order to get updates on changes to our contacts. - getContext() - .getContentResolver() - .registerContentObserver( - Contacts.CONTENT_STREQUENT_URI, - true /* notifyForDescendants*/, - cursorLoader.getContentObserver()); adapter.setCursor((SpeedDialCursor) data); } - public void unregisterContentObserver() { - getContext() - .getContentResolver() - .unregisterContentObserver(cursorLoader.getContentObserver()); - } - @Override public void onLoaderReset(Loader loader) { adapter.setCursor(null); diff --git a/java/com/android/dialer/speeddial/res/layout/add_favorite_activity.xml b/java/com/android/dialer/speeddial/res/layout/add_favorite_activity.xml new file mode 100644 index 000000000..4df2f29b4 --- /dev/null +++ b/java/com/android/dialer/speeddial/res/layout/add_favorite_activity.xml @@ -0,0 +1,21 @@ + + + \ No newline at end of file diff --git a/java/com/android/dialer/speeddial/res/menu/add_favorite_menu.xml b/java/com/android/dialer/speeddial/res/menu/add_favorite_menu.xml new file mode 100644 index 000000000..b11c7f5d6 --- /dev/null +++ b/java/com/android/dialer/speeddial/res/menu/add_favorite_menu.xml @@ -0,0 +1,24 @@ + + +

+ + \ No newline at end of file diff --git a/java/com/android/dialer/speeddial/res/values/strings.xml b/java/com/android/dialer/speeddial/res/values/strings.xml index f814ed6d4..d64d03575 100644 --- a/java/com/android/dialer/speeddial/res/values/strings.xml +++ b/java/com/android/dialer/speeddial/res/values/strings.xml @@ -15,12 +15,15 @@ ~ limitations under the License --> - + Favorites - + Suggestions - + Add + + + Add Favorite \ No newline at end of file -- cgit v1.2.3