Search UI clean up

- Resolve spacing issue when work profile is installed
- Cache play icons and use icon shape
- Only draw focus indicator for the first result

Bug: 170487752
Bug: 170665892
Change-Id: I864d2e796786637132e127ef9b418c0a76c74d6e
diff --git a/res/layout/all_apps_tabs.xml b/res/layout/all_apps_tabs.xml
index 2accd2d..c684881 100644
--- a/res/layout/all_apps_tabs.xml
+++ b/res/layout/all_apps_tabs.xml
@@ -26,7 +26,6 @@
     android:clipChildren="true"
     android:clipToPadding="false"
     android:descendantFocusability="afterDescendants"
-    android:paddingTop="@dimen/all_apps_header_top_padding"
     launcher:pageIndicator="@+id/tabs" >
 
     <include layout="@layout/all_apps_rv_layout" />
diff --git a/src/com/android/launcher3/allapps/AllAppsContainerView.java b/src/com/android/launcher3/allapps/AllAppsContainerView.java
index 5079469..75ab00a 100644
--- a/src/com/android/launcher3/allapps/AllAppsContainerView.java
+++ b/src/com/android/launcher3/allapps/AllAppsContainerView.java
@@ -585,10 +585,6 @@
         int padding = mHeader.getMaxTranslation();
         for (int i = 0; i < mAH.length; i++) {
             mAH[i].padding.top = padding;
-            if (FeatureFlags.ENABLE_DEVICE_SEARCH.get() && mUsingTabs) {
-                //add extra space between tabs and recycler view
-                mAH[i].padding.top += mLauncher.getDeviceProfile().edgeMarginPx;
-            }
             mAH[i].applyPadding();
         }
     }
diff --git a/src/com/android/launcher3/allapps/AllAppsPagedView.java b/src/com/android/launcher3/allapps/AllAppsPagedView.java
index f640c3e..eae9c0a 100644
--- a/src/com/android/launcher3/allapps/AllAppsPagedView.java
+++ b/src/com/android/launcher3/allapps/AllAppsPagedView.java
@@ -20,6 +20,8 @@
 import android.view.MotionEvent;
 
 import com.android.launcher3.PagedView;
+import com.android.launcher3.R;
+import com.android.launcher3.config.FeatureFlags;
 
 public class AllAppsPagedView extends PagedView<PersonalWorkSlidingTabStrip> {
 
@@ -37,6 +39,9 @@
 
     public AllAppsPagedView(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
+        int topPadding = FeatureFlags.ENABLE_DEVICE_SEARCH.get() ? 0
+                : context.getResources().getDimensionPixelOffset(
+                        R.dimen.all_apps_header_top_padding);
     }
 
     @Override
diff --git a/src/com/android/launcher3/allapps/AllAppsSectionDecorator.java b/src/com/android/launcher3/allapps/AllAppsSectionDecorator.java
index f2a1f85..c131697 100644
--- a/src/com/android/launcher3/allapps/AllAppsSectionDecorator.java
+++ b/src/com/android/launcher3/allapps/AllAppsSectionDecorator.java
@@ -26,6 +26,7 @@
 import androidx.recyclerview.widget.RecyclerView;
 
 import com.android.launcher3.R;
+import com.android.launcher3.allapps.AllAppsGridAdapter.AppsGridLayoutManager;
 import com.android.launcher3.allapps.search.SearchSectionInfo;
 import com.android.launcher3.util.Themes;
 
@@ -90,7 +91,10 @@
         if (mAppsView.getFloatingHeaderView().getFocusedChild() == null
                 && mAppsView.getApps().getFocusedChild() != null) {
             int index = mAppsView.getApps().getFocusedChildIndex();
-            if (index >= 0 && index < parent.getChildCount()) {
+            AppsGridLayoutManager layoutManager = (AppsGridLayoutManager)
+                    mAppsView.getActiveRecyclerView().getLayoutManager();
+            if (layoutManager.findFirstVisibleItemPosition() == index
+                    && index < parent.getChildCount()) {
                 decorationHandler.onFocusDraw(c, parent.getChildAt(index));
             }
         }
diff --git a/src/com/android/launcher3/views/SearchResultPlayItem.java b/src/com/android/launcher3/views/SearchResultPlayItem.java
index d05a29e..c7133fd 100644
--- a/src/com/android/launcher3/views/SearchResultPlayItem.java
+++ b/src/com/android/launcher3/views/SearchResultPlayItem.java
@@ -44,12 +44,14 @@
 import com.android.launcher3.allapps.AllAppsGridAdapter.AdapterItemWithPayload;
 import com.android.launcher3.allapps.search.AllAppsSearchBarController;
 import com.android.launcher3.icons.BitmapRenderer;
+import com.android.launcher3.util.Themes;
 import com.android.systemui.plugins.AllAppsSearchPlugin;
 import com.android.systemui.plugins.shared.SearchTarget;
 import com.android.systemui.plugins.shared.SearchTargetEvent;
 
 import java.io.IOException;
 import java.net.URL;
+import java.net.URLConnection;
 
 /**
  * A View representing a PlayStore item.
@@ -58,7 +60,6 @@
         AllAppsSearchBarController.PayloadResultHandler<Bundle> {
 
     private static final int BITMAP_CROP_MASK_COLOR = 0xff424242;
-    private static final float ICON_RADIUS_FACTOR = .5f;
 
     private final DeviceProfile mDeviceProfile;
     private View mIconView;
@@ -71,6 +72,7 @@
     private final Object[] mTargetInfo = createTargetInfo();
 
     final Paint mIconPaint = new Paint();
+    final Rect mTempRect = new Rect();
 
 
     public SearchResultPlayItem(Context context) {
@@ -125,13 +127,15 @@
         mIconView.setBackgroundResource(R.drawable.ic_deepshortcut_placeholder);
         UI_HELPER_EXECUTOR.execute(() -> {
             try {
-//                TODO: Handle caching
                 URL url = new URL(bundle.getString("icon_url"));
-                Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
-                BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(),
-                        Bitmap.createScaledBitmap(getRoundedBitmap(bitmap),
-                                mDeviceProfile.allAppsIconSizePx, mDeviceProfile.allAppsIconSizePx,
-                                false));
+                URLConnection con = url.openConnection();
+//                TODO: monitor memory and investigate if it's better to use glide
+                con.addRequestProperty("Cache-Control", "max-age: 0");
+                con.setUseCaches(true);
+                Bitmap bitmap = BitmapFactory.decodeStream(con.getInputStream());
+                BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), getRoundedBitmap(
+                        Bitmap.createScaledBitmap(bitmap, mDeviceProfile.allAppsIconSizePx,
+                                mDeviceProfile.allAppsIconSizePx, false)));
                 mIconView.post(() -> mIconView.setBackground(bitmapDrawable));
             } catch (IOException e) {
                 e.printStackTrace();
@@ -141,24 +145,23 @@
 
 
     private Bitmap getRoundedBitmap(Bitmap bitmap) {
-        int iconSize = bitmap.getWidth();
+        final int iconSize = bitmap.getWidth();
+        final float radius = Themes.getDialogCornerRadius(getContext());
 
         Bitmap output = BitmapRenderer.createHardwareBitmap(iconSize, iconSize, (canvas) -> {
-            final Rect rect = new Rect(0, 0, iconSize, iconSize);
-            final RectF rectF = new RectF(rect);
+            mTempRect.set(0, 0, iconSize, iconSize);
+            final RectF rectF = new RectF(mTempRect);
 
             mIconPaint.setAntiAlias(true);
+            mIconPaint.reset();
             canvas.drawARGB(0, 0, 0, 0);
             mIconPaint.setColor(BITMAP_CROP_MASK_COLOR);
-            int radius = (int) (iconSize * ICON_RADIUS_FACTOR);
             canvas.drawRoundRect(rectF, radius, radius, mIconPaint);
 
             mIconPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
-            canvas.drawBitmap(bitmap, rect, rect, mIconPaint);
+            canvas.drawBitmap(bitmap, mTempRect, mTempRect, mIconPaint);
         });
-
         return output;
-
     }