diff options
author | calderwoodra <calderwoodra@google.com> | 2018-04-18 16:48:57 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-04-18 16:51:32 -0700 |
commit | 0388bfc9717dbf1d67ed4c14f34aa5414a08b39c (patch) | |
tree | dc277edb839034daa01f46b4ae597e63017dbe64 /java/com/android/dialer/speeddial/loader | |
parent | 560eb125b437fef71e624327a024853dd9bc06ea (diff) |
Implement adding favorites.
This change deletes the AddFavoritesActivity in favor of the activity provided
to us by the Intent.ACTION_PICK intent.
Bug: 36841782
Test: SpeedDialUiItemLoaderTest
PiperOrigin-RevId: 193439522
Change-Id: Ie297abd2307c727d746f26ba99961de116636dc4
Diffstat (limited to 'java/com/android/dialer/speeddial/loader')
-rw-r--r-- | java/com/android/dialer/speeddial/loader/SpeedDialUiItemLoader.java | 60 |
1 files changed, 56 insertions, 4 deletions
diff --git a/java/com/android/dialer/speeddial/loader/SpeedDialUiItemLoader.java b/java/com/android/dialer/speeddial/loader/SpeedDialUiItemLoader.java index 9a027de71..9ea84eca2 100644 --- a/java/com/android/dialer/speeddial/loader/SpeedDialUiItemLoader.java +++ b/java/com/android/dialer/speeddial/loader/SpeedDialUiItemLoader.java @@ -17,6 +17,7 @@ package com.android.dialer.speeddial.loader; import android.annotation.TargetApi; +import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.net.Uri; @@ -97,14 +98,61 @@ public final class SpeedDialUiItemLoader { * from {@link Contacts#STREQUENT_PHONE_ONLY}. */ public ListenableFuture<ImmutableList<SpeedDialUiItem>> loadSpeedDialUiItems() { - return dialerFutureSerializer.submitAsync( - () -> backgroundExecutor.submit(this::doInBackground), backgroundExecutor); + return dialerFutureSerializer.submit(this::loadSpeedDialUiItemsInternal, backgroundExecutor); + } + + /** + * Takes a contact uri from {@link Phone#CONTENT_URI} and updates {@link Phone#STARRED} to be + * true, if it isn't already or Inserts the contact into the {@link SpeedDialEntryDatabaseHelper} + */ + public ListenableFuture<ImmutableList<SpeedDialUiItem>> starContact(Uri contactUri) { + return dialerFutureSerializer.submit( + () -> insertNewContactEntry(contactUri), backgroundExecutor); + } + + @WorkerThread + private ImmutableList<SpeedDialUiItem> insertNewContactEntry(Uri contactUri) { + Assert.isWorkerThread(); + try (Cursor cursor = + appContext + .getContentResolver() + .query(contactUri, SpeedDialUiItem.PHONE_PROJECTION, null, null, null)) { + if (cursor == null) { + LogUtil.e("SpeedDialUiItemLoader.insertNewContactEntry", "Cursor was null"); + return loadSpeedDialUiItemsInternal(); + } + Assert.checkArgument(cursor.moveToFirst(), "Cursor should never be empty"); + SpeedDialUiItem item = SpeedDialUiItem.fromCursor(cursor); + + // Star the contact if it isn't starred already, then return. + if (!item.isStarred()) { + ContentValues values = new ContentValues(); + values.put(Phone.STARRED, "1"); + appContext + .getContentResolver() + .update( + Contacts.CONTENT_URI, + values, + Contacts._ID + " = ?", + new String[] {Long.toString(item.contactId())}); + } + + // Insert a new entry into the SpeedDialEntry database + getSpeedDialEntryDao() + .insert( + SpeedDialEntry.builder() + .setLookupKey(item.lookupKey()) + .setContactId(item.contactId()) + .setDefaultChannel(item.defaultChannel()) + .build()); + } + return loadSpeedDialUiItemsInternal(); } @WorkerThread - private ImmutableList<SpeedDialUiItem> doInBackground() { + private ImmutableList<SpeedDialUiItem> loadSpeedDialUiItemsInternal() { Assert.isWorkerThread(); - SpeedDialEntryDao db = new SpeedDialEntryDatabaseHelper(appContext); + SpeedDialEntryDao db = getSpeedDialEntryDao(); // This is the list of contacts that we will display to the user List<SpeedDialUiItem> speedDialUiItems = new ArrayList<>(); @@ -392,4 +440,8 @@ public final class SpeedDialUiItemLoader { } return item.toBuilder().setChannels(newChannelsList.build()).build(); } + + private SpeedDialEntryDao getSpeedDialEntryDao() { + return new SpeedDialEntryDatabaseHelper(appContext); + } } |