Updating ContactGridManager to use GlidePhotoManager for more efficient contact photo creation. If the photoUri does not exist, GlidePhotoManagerImpl will create the needed LetterTileDrawable to use in the contact photo's place.

Bug: 76204286,76206686,76206786
Test: ContactGridManagerTest
PiperOrigin-RevId: 197034807
Change-Id: Icb286e557b21a21029bfa7e7d5a390e4eb889dde
diff --git a/java/com/android/incallui/contactgrid/ContactGridManager.java b/java/com/android/incallui/contactgrid/ContactGridManager.java
index 493f2d5..6d04a27 100644
--- a/java/com/android/incallui/contactgrid/ContactGridManager.java
+++ b/java/com/android/incallui/contactgrid/ContactGridManager.java
@@ -35,6 +35,9 @@
 import android.widget.ViewAnimator;
 import com.android.dialer.common.Assert;
 import com.android.dialer.common.LogUtil;
+import com.android.dialer.configprovider.ConfigProviderBindings;
+import com.android.dialer.glidephotomanager.GlidePhotoManagerComponent;
+import com.android.dialer.glidephotomanager.PhotoInfo;
 import com.android.dialer.lettertile.LetterTileDrawable;
 import com.android.dialer.util.DrawableConverter;
 import com.android.incallui.incall.protocol.ContactPhotoType;
@@ -215,7 +218,8 @@
     }
 
     boolean hasPhoto =
-        primaryInfo.photo() != null && primaryInfo.photoType() == ContactPhotoType.CONTACT;
+        (primaryInfo.photo() != null || primaryInfo.photoUri() != null)
+            && primaryInfo.photoType() == ContactPhotoType.CONTACT;
     if (!hasPhoto && !showAnonymousAvatar) {
       avatarImageView.setVisibility(View.GONE);
       return false;
@@ -297,36 +301,73 @@
       if (hideAvatar) {
         avatarImageView.setVisibility(View.GONE);
       } else if (avatarSize > 0 && updateAvatarVisibility()) {
-        boolean hasPhoto =
-            primaryInfo.photo() != null && primaryInfo.photoType() == ContactPhotoType.CONTACT;
-        // Contact has a photo, don't render a letter tile.
-        if (hasPhoto) {
-          avatarImageView.setBackground(
-              DrawableConverter.getRoundedDrawable(
-                  context, primaryInfo.photo(), avatarSize, avatarSize));
-          // Contact has a name, that isn't a number.
+        if (ConfigProviderBindings.get(context).getBoolean("enable_glide_photo", false)) {
+          loadPhotoWithGlide();
         } else {
-          letterTile.setCanonicalDialerLetterTileDetails(
-              primaryInfo.name(),
-              primaryInfo.contactInfoLookupKey(),
-              LetterTileDrawable.SHAPE_CIRCLE,
-              LetterTileDrawable.getContactTypeFromPrimitives(
-                  primaryCallState.isVoiceMailNumber(),
-                  primaryInfo.isSpam(),
-                  primaryCallState.isBusinessNumber(),
-                  primaryInfo.numberPresentation(),
-                  primaryCallState.isConference()));
-          // By invalidating the avatarImageView we force a redraw of the letter tile.
-          // This is required to properly display the updated letter tile iconography based on the
-          // contact type, because the background drawable reference cached in the view, and the
-          // view is not aware of the mutations made to the background.
-          avatarImageView.invalidate();
-          avatarImageView.setBackground(letterTile);
+          loadPhotoWithLegacy();
         }
       }
     }
   }
 
+  private void loadPhotoWithGlide() {
+    PhotoInfo.Builder photoInfoBuilder =
+        PhotoInfo.newBuilder()
+            .setIsBusiness(primaryInfo.photoType() == ContactPhotoType.BUSINESS)
+            .setIsVoicemail(primaryCallState.isVoiceMailNumber())
+            .setIsSpam(primaryInfo.isSpam());
+
+    // Contact has a name, that is a number.
+    if (primaryInfo.nameIsNumber() && primaryInfo.number() != null) {
+      photoInfoBuilder.setName(primaryInfo.number());
+    } else if (primaryInfo.name() != null) {
+      photoInfoBuilder.setName(primaryInfo.name());
+    }
+
+    if (primaryInfo.number() != null) {
+      photoInfoBuilder.setFormattedNumber(primaryInfo.number());
+    }
+
+    if (primaryInfo.photoUri() != null) {
+      photoInfoBuilder.setPhotoUri(primaryInfo.photoUri().toString());
+    }
+
+    if (primaryInfo.contactInfoLookupKey() != null) {
+      photoInfoBuilder.setLookupUri(primaryInfo.contactInfoLookupKey());
+    }
+
+    GlidePhotoManagerComponent.get(context)
+        .glidePhotoManager()
+        .loadContactPhoto(avatarImageView, photoInfoBuilder.build());
+  }
+
+  private void loadPhotoWithLegacy() {
+    boolean hasPhoto =
+        primaryInfo.photo() != null && primaryInfo.photoType() == ContactPhotoType.CONTACT;
+    if (hasPhoto) {
+      avatarImageView.setBackground(
+          DrawableConverter.getRoundedDrawable(
+              context, primaryInfo.photo(), avatarSize, avatarSize));
+    } else {
+      // Contact has a photo, don't render a letter tile.
+      letterTile.setCanonicalDialerLetterTileDetails(
+          primaryInfo.name(),
+          primaryInfo.contactInfoLookupKey(),
+          LetterTileDrawable.SHAPE_CIRCLE,
+          LetterTileDrawable.getContactTypeFromPrimitives(
+              primaryCallState.isVoiceMailNumber(),
+              primaryInfo.isSpam(),
+              primaryCallState.isBusinessNumber(),
+              primaryInfo.numberPresentation(),
+              primaryCallState.isConference()));
+      // By invalidating the avatarImageView we force a redraw of the letter tile.
+      // This is required to properly display the updated letter tile iconography based on the
+      // contact type, because the background drawable reference cached in the view, and the
+      // view is not aware of the mutations made to the background.
+      avatarImageView.invalidate();
+      avatarImageView.setBackground(letterTile);
+    }
+  }
   /**
    * Updates row 2. For example:
    *
diff --git a/java/com/android/incallui/incall/protocol/PrimaryInfo.java b/java/com/android/incallui/incall/protocol/PrimaryInfo.java
index 63dc39e..5e40cd1 100644
--- a/java/com/android/incallui/incall/protocol/PrimaryInfo.java
+++ b/java/com/android/incallui/incall/protocol/PrimaryInfo.java
@@ -17,6 +17,7 @@
 package com.android.incallui.incall.protocol;
 
 import android.graphics.drawable.Drawable;
+import android.net.Uri;
 import android.support.annotation.Nullable;
 import com.android.dialer.common.LogUtil;
 import com.android.dialer.multimedia.MultimediaData;
@@ -43,6 +44,9 @@
   @Nullable
   public abstract Drawable photo();
 
+  @Nullable
+  public abstract Uri photoUri();
+
   @ContactPhotoType
   public abstract int photoType();
 
@@ -73,6 +77,7 @@
   public static Builder builder() {
     return new AutoValue_PrimaryInfo.Builder();
   }
+
   /** Builder class for primary call info. */
   @AutoValue.Builder
   public abstract static class Builder {
@@ -88,6 +93,8 @@
 
     public abstract Builder setPhoto(Drawable photo);
 
+    public abstract Builder setPhotoUri(Uri photoUri);
+
     public abstract Builder setPhotoType(@ContactPhotoType int photoType);
 
     public abstract Builder setIsSipCall(boolean isSipCall);