Merge changes I642a11d6,Ic4b69781

* changes:
  Fixed various speed dial issues.
  Use StorageComponent to get SharedPreference in SpamBlockingPromoHelper
diff --git a/java/com/android/dialer/spam/promo/SpamBlockingPromoHelper.java b/java/com/android/dialer/spam/promo/SpamBlockingPromoHelper.java
index 42fb39f..e9f7cc6 100644
--- a/java/com/android/dialer/spam/promo/SpamBlockingPromoHelper.java
+++ b/java/com/android/dialer/spam/promo/SpamBlockingPromoHelper.java
@@ -23,7 +23,6 @@
 import android.app.PendingIntent;
 import android.content.Context;
 import android.content.DialogInterface.OnDismissListener;
-import android.preference.PreferenceManager;
 import android.support.design.widget.Snackbar;
 import android.support.v4.os.BuildCompat;
 import android.view.View;
@@ -35,6 +34,7 @@
 import com.android.dialer.notification.NotificationChannelId;
 import com.android.dialer.spam.SpamSettings;
 import com.android.dialer.spam.promo.SpamBlockingPromoDialogFragment.OnEnableListener;
+import com.android.dialer.storage.StorageComponent;
 
 /** Helper class for showing spam blocking on-boarding promotions. */
 public class SpamBlockingPromoHelper {
@@ -71,7 +71,8 @@
     }
 
     long lastShowMillis =
-        PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext())
+        StorageComponent.get(context)
+            .unencryptedSharedPrefs()
             .getLong(SPAM_BLOCKING_PROMO_LAST_SHOW_MILLIS, 0);
     long showPeriodMillis =
         ConfigProviderBindings.get(context)
@@ -103,10 +104,11 @@
   }
 
   private void updateLastShowSpamTimestamp() {
-    PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext())
+    StorageComponent.get(context)
+        .unencryptedSharedPrefs()
         .edit()
         .putLong(SPAM_BLOCKING_PROMO_LAST_SHOW_MILLIS, System.currentTimeMillis())
-        .commit();
+        .apply();
   }
 
   /**
diff --git a/java/com/android/dialer/speeddial/SpeedDialFragment.java b/java/com/android/dialer/speeddial/SpeedDialFragment.java
index fc80d23..c118f96 100644
--- a/java/com/android/dialer/speeddial/SpeedDialFragment.java
+++ b/java/com/android/dialer/speeddial/SpeedDialFragment.java
@@ -183,6 +183,7 @@
     super.onPause();
     favoritesListener.hideMenu();
     suggestedListener.onPause();
+    onHidden();
   }
 
   @Override
diff --git a/java/com/android/dialer/speeddial/SquareImageView.java b/java/com/android/dialer/speeddial/SquareImageView.java
deleted file mode 100644
index a12f4d4..0000000
--- a/java/com/android/dialer/speeddial/SquareImageView.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * 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.Context;
-import android.support.annotation.Nullable;
-import android.util.AttributeSet;
-import android.widget.QuickContactBadge;
-
-/** A square {@link android.widget.ImageView} constrained on width. */
-public class SquareImageView extends QuickContactBadge {
-
-  public SquareImageView(Context context, @Nullable AttributeSet attrs) {
-    super(context, attrs);
-    setClickable(false);
-  }
-
-  @Override
-  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
-    super.onMeasure(widthMeasureSpec, widthMeasureSpec);
-  }
-}
diff --git a/java/com/android/dialer/speeddial/loader/SpeedDialUiItem.java b/java/com/android/dialer/speeddial/loader/SpeedDialUiItem.java
index 2b056bb..9e22755 100644
--- a/java/com/android/dialer/speeddial/loader/SpeedDialUiItem.java
+++ b/java/com/android/dialer/speeddial/loader/SpeedDialUiItem.java
@@ -18,6 +18,7 @@
 
 import android.content.res.Resources;
 import android.database.Cursor;
+import android.os.Trace;
 import android.provider.ContactsContract.CommonDataKinds.Phone;
 import android.provider.ContactsContract.Contacts;
 import android.provider.ContactsContract.Data;
@@ -105,7 +106,9 @@
    * <p>If the cursor started at row X, this method will advance to row Y s.t. rows X, X + 1, ... Y
    * - 1 all belong to the same contact (that is, share the same contact id and lookup key).
    */
-  public static SpeedDialUiItem fromCursor(Resources resources, Cursor cursor) {
+  public static SpeedDialUiItem fromCursor(
+      Resources resources, Cursor cursor, boolean isImsEnabled) {
+    Trace.beginSection("fromCursor");
     Assert.checkArgument(cursor != null);
     Assert.checkArgument(cursor.getCount() != 0);
     String lookupKey = cursor.getString(LOOKUP_KEY);
@@ -141,7 +144,8 @@
               .build();
       channels.add(channel);
 
-      if ((cursor.getInt(CARRIER_PRESENCE) & Data.CARRIER_PRESENCE_VT_CAPABLE) == 1) {
+      if (isImsEnabled
+          && (cursor.getInt(CARRIER_PRESENCE) & Data.CARRIER_PRESENCE_VT_CAPABLE) == 1) {
         // Add another channel if the number is ViLTE reachable
         channels.add(channel.toBuilder().setTechnology(Channel.IMS_VIDEO).build());
       }
@@ -149,6 +153,7 @@
     } while (cursor.moveToNext() && Objects.equals(lookupKey, cursor.getString(LOOKUP_KEY)));
 
     builder.setChannels(ImmutableList.copyOf(channels));
+    Trace.endSection();
     return builder.build();
   }
 
diff --git a/java/com/android/dialer/speeddial/loader/SpeedDialUiItemMutator.java b/java/com/android/dialer/speeddial/loader/SpeedDialUiItemMutator.java
index 1ad37dc..998793e 100644
--- a/java/com/android/dialer/speeddial/loader/SpeedDialUiItemMutator.java
+++ b/java/com/android/dialer/speeddial/loader/SpeedDialUiItemMutator.java
@@ -25,6 +25,7 @@
 import android.net.Uri;
 import android.os.Build.VERSION_CODES;
 import android.os.RemoteException;
+import android.os.Trace;
 import android.provider.ContactsContract;
 import android.provider.ContactsContract.CommonDataKinds.Phone;
 import android.provider.ContactsContract.Contacts;
@@ -46,6 +47,7 @@
 import com.android.dialer.speeddial.database.SpeedDialEntry.Channel;
 import com.android.dialer.speeddial.database.SpeedDialEntryDao;
 import com.android.dialer.speeddial.database.SpeedDialEntryDatabaseHelper;
+import com.android.dialer.util.CallUtil;
 import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
@@ -205,7 +207,9 @@
         return loadSpeedDialUiItemsInternal();
       }
       Assert.checkArgument(cursor.moveToFirst(), "Cursor should never be empty");
-      SpeedDialUiItem item = SpeedDialUiItem.fromCursor(appContext.getResources(), cursor);
+      SpeedDialUiItem item =
+          SpeedDialUiItem.fromCursor(
+              appContext.getResources(), cursor, CallUtil.isVideoEnabled(appContext));
 
       // Star the contact if it isn't starred already, then return.
       if (!item.isStarred()) {
@@ -228,9 +232,12 @@
 
   @WorkerThread
   private ImmutableList<SpeedDialUiItem> loadSpeedDialUiItemsInternal() {
+    Trace.beginSection("loadSpeedDialUiItemsInternal");
     Assert.isWorkerThread();
     contactsPreferences.refreshValue(ContactsPreferences.DISPLAY_ORDER_KEY);
+    Trace.beginSection("getAllEntries");
     SpeedDialEntryDao db = getSpeedDialEntryDao();
+    Trace.endSection(); // getAllEntries
 
     // This is the list of contacts that we will display to the user
     List<SpeedDialUiItem> speedDialUiItems = new ArrayList<>();
@@ -251,6 +258,7 @@
         "Updated entries are incomplete: " + entries.size() + " != " + entriesToUiItems.size());
 
     // Mark the SpeedDialEntries to be updated or deleted
+    Trace.beginSection("updateOrDeleteEntries");
     for (SpeedDialEntry entry : entries) {
       SpeedDialUiItem contact = entriesToUiItems.get(entry);
       // Remove contacts that no longer exist or are no longer starred
@@ -271,12 +279,14 @@
       // These are our existing starred entries
       speedDialUiItems.add(contact);
     }
+    Trace.endSection(); // updateOrDeleteEntries
 
     // Get all Strequent Contacts
     List<SpeedDialUiItem> strequentContacts = getStrequentContacts();
 
     // For each contact, if it isn't starred, add it as a suggestion.
     // If it is starred and not already accounted for above, then insert into the SpeedDialEntry DB.
+    Trace.beginSection("addSuggestions");
     for (SpeedDialUiItem contact : strequentContacts) {
       if (!contact.isStarred()) {
         // Add this contact as a suggestion
@@ -291,12 +301,16 @@
         speedDialUiItems.add(contact);
       }
     }
+    Trace.endSection(); // addSuggestions
 
+    Trace.beginSection("insertUpdateAndDelete");
     ImmutableMap<SpeedDialEntry, Long> insertedEntriesToIdsMap =
         db.insertUpdateAndDelete(
             ImmutableList.copyOf(entriesToInsert),
             ImmutableList.copyOf(entriesToUpdate),
             ImmutableList.copyOf(entriesToDelete));
+    Trace.endSection(); // insertUpdateAndDelete
+    Trace.endSection(); // loadSpeedDialUiItemsInternal
     return speedDialUiItemsWithUpdatedIds(speedDialUiItems, insertedEntriesToIdsMap);
   }
 
@@ -388,11 +402,13 @@
   @WorkerThread
   private Map<SpeedDialEntry, SpeedDialUiItem> getSpeedDialUiItemsFromEntries(
       List<SpeedDialEntry> entries) {
+    Trace.beginSection("getSpeedDialUiItemsFromEntries");
     Assert.isWorkerThread();
     // Fetch the contact ids from the SpeedDialEntries
     Set<String> contactIds = new ArraySet<>();
     entries.forEach(entry -> contactIds.add(Long.toString(entry.contactId())));
     if (contactIds.isEmpty()) {
+      Trace.endSection();
       return new ArrayMap<>();
     }
 
@@ -410,7 +426,9 @@
                 null)) {
       Map<SpeedDialEntry, SpeedDialUiItem> map = new ArrayMap<>();
       for (cursor.moveToFirst(); !cursor.isAfterLast(); /* Iterate in the loop */ ) {
-        SpeedDialUiItem item = SpeedDialUiItem.fromCursor(appContext.getResources(), cursor);
+        SpeedDialUiItem item =
+            SpeedDialUiItem.fromCursor(
+                appContext.getResources(), cursor, CallUtil.isVideoEnabled(appContext));
         for (SpeedDialEntry entry : entries) {
           if (entry.contactId() == item.contactId()) {
             // Update the id and pinned position to match it's corresponding SpeedDialEntry.
@@ -442,6 +460,7 @@
       for (SpeedDialEntry entry : entries) {
         map.putIfAbsent(entry, null);
       }
+      Trace.endSection();
       return map;
     }
   }
@@ -467,6 +486,7 @@
 
   @WorkerThread
   private List<SpeedDialUiItem> getStrequentContacts() {
+    Trace.beginSection("getStrequentContacts");
     Assert.isWorkerThread();
     Set<String> contactIds = new ArraySet<>();
 
@@ -482,9 +502,11 @@
             .query(strequentUri, new String[] {Phone.CONTACT_ID}, null, null, null)) {
       if (cursor == null) {
         LogUtil.e("SpeedDialUiItemMutator.getStrequentContacts", "null cursor");
+        Trace.endSection();
         return new ArrayList<>();
       }
       if (cursor.getCount() == 0) {
+        Trace.endSection();
         return new ArrayList<>();
       }
       for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
@@ -507,14 +529,19 @@
       List<SpeedDialUiItem> contacts = new ArrayList<>();
       if (cursor == null) {
         LogUtil.e("SpeedDialUiItemMutator.getStrequentContacts", "null cursor");
+        Trace.endSection();
         return new ArrayList<>();
       }
       if (cursor.getCount() == 0) {
+        Trace.endSection();
         return contacts;
       }
       for (cursor.moveToFirst(); !cursor.isAfterLast(); /* Iterate in the loop */ ) {
-        contacts.add(SpeedDialUiItem.fromCursor(appContext.getResources(), cursor));
+        contacts.add(
+            SpeedDialUiItem.fromCursor(
+                appContext.getResources(), cursor, CallUtil.isVideoEnabled(appContext)));
       }
+      Trace.endSection();
       return contacts;
     }
   }
diff --git a/java/com/android/dialer/speeddial/res/layout/favorite_item_layout.xml b/java/com/android/dialer/speeddial/res/layout/favorite_item_layout.xml
index 134196d..a0bbfbd 100644
--- a/java/com/android/dialer/speeddial/res/layout/favorite_item_layout.xml
+++ b/java/com/android/dialer/speeddial/res/layout/favorite_item_layout.xml
@@ -27,14 +27,15 @@
       android:layout_width="wrap_content"
       android:layout_height="104dp"
       android:layout_gravity="center_horizontal"
-      android:layout_marginBottom="8dp">
+      android:layout_marginBottom="8dp"
+      android:minWidth="128dp">
 
-    <com.android.dialer.speeddial.SquareImageView
+    <QuickContactBadge
         android:id="@+id/avatar"
         android:layout_width="104dp"
         android:layout_height="104dp"
-        android:layout_marginStart="12dp"
-        android:layout_marginEnd="12dp"/>
+        android:layout_gravity="center_horizontal"
+        android:clickable="false"/>
 
     <FrameLayout
         android:id="@+id/video_call_container"