App grid preview for screen zoom

Change-Id: I718468dc03800d125eea1c57c8e5dfbc84aa3367
diff --git a/res/layout/screen_zoom_preview_3.xml b/res/layout/screen_zoom_preview_3.xml
index 7cdb663..6fc7fd0 100644
--- a/res/layout/screen_zoom_preview_3.xml
+++ b/res/layout/screen_zoom_preview_3.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2015 The Android Open Source Project
+<!-- Copyright (C) 2016 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.
@@ -14,72 +14,7 @@
      limitations under the License.
 -->
 
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-              android:layout_width="match_parent"
-              android:layout_height="match_parent"
-              android:orientation="vertical">
-
-    <LinearLayout
-        android:layout_width="match_parent"
-        android:layout_height="?android:attr/actionBarSize"
-        android:orientation="horizontal"
-        android:theme="?android:attr/actionBarTheme"
-        style="?android:attr/actionBarStyle">
-
-        <TextView
-            android:layout_width="0dp"
-            android:layout_height="wrap_content"
-            android:layout_weight="1"
-            android:layout_marginLeft="16dp"
-            android:text="@string/screen_zoom_preview_title"
-            android:textAppearance="@android:style/TextAppearance.Material.Widget.ActionBar.Title" />
-
-        <ImageView
-            android:layout_width="36dp"
-            android:layout_height="48dp"
-            style="?android:attr/actionOverflowButtonStyle" />
-    </LinearLayout>
-
-    <ScrollView
-        android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:scrollbars="none"
-        android:background="?android:attr/colorBackgroundFloating">
-
-        <LinearLayout
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:orientation="vertical">
-
-            <TextView
-                style="@android:style/TextAppearance.Material.Subhead"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:layout_marginLeft="16dp"
-                android:layout_marginTop="16dp"
-                android:text="@string/permissions_label" />
-
-            <include layout="@layout/screen_zoom_preview_item" />
-
-            <View
-                android:layout_width="match_parent"
-                android:layout_height="1dp"
-                android:layout_marginLeft="62dp"
-                android:layout_marginRight="8dp"
-                android:background="#36000000" />
-
-            <include layout="@layout/screen_zoom_preview_item" />
-
-            <View
-                android:layout_width="match_parent"
-                android:layout_height="1dp"
-                android:layout_marginLeft="62dp"
-                android:layout_marginRight="8dp"
-                android:background="#36000000" />
-
-            <include layout="@layout/screen_zoom_preview_item" />
-
-        </LinearLayout>
-
-    </ScrollView>
-</LinearLayout>
+<com.android.settings.display.AppGridView
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent" />
diff --git a/res/layout/screen_zoom_preview_app_icon.xml b/res/layout/screen_zoom_preview_app_icon.xml
new file mode 100644
index 0000000..a9b18b6
--- /dev/null
+++ b/res/layout/screen_zoom_preview_app_icon.xml
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 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.
+-->
+
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+             android:layout_width="match_parent"
+             android:layout_height="wrap_content">
+
+    <LinearLayout
+        android:layout_width="@dimen/screen_zoom_preview_app_icon_width"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center_horizontal|top"
+        android:gravity="center_horizontal"
+        android:orientation="vertical"
+        android:padding="8dp">
+
+        <ImageView
+            android:id="@android:id/icon1"
+            android:layout_width="48dp"
+            android:layout_height="48dp"
+            android:layout_marginBottom="4dp"
+            android:scaleType="centerInside" />
+
+        <TextView
+            android:id="@android:id/text1"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:ellipsize="end"
+            android:gravity="center_horizontal|top"
+            android:singleLine="true"
+            android:textAppearance="@android:style/TextAppearance.Material.Caption" />
+    </LinearLayout>
+</FrameLayout>
\ No newline at end of file
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index 12bdfaa..471d260 100755
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -261,6 +261,7 @@
 
     <!-- Display, Screen zoom -->
     <dimen name="screen_zoom_preview_height">240dp</dimen>
+    <dimen name="screen_zoom_preview_app_icon_width">88dp</dimen>
 
     <!-- Accessibility Settings -->
     <dimen name="accessibility_layout_margin_start_end">24dp</dimen>
@@ -275,4 +276,5 @@
     <dimen name="screen_magnification_video_width">170dp</dimen>
     <dimen name="screen_magnification_video_height">311dp</dimen>
     <dimen name="screen_magnification_video_margin_top">40dp</dimen>
+
 </resources>
diff --git a/src/com/android/settings/display/AppGridView.java b/src/com/android/settings/display/AppGridView.java
new file mode 100644
index 0000000..e11558c
--- /dev/null
+++ b/src/com/android/settings/display/AppGridView.java
@@ -0,0 +1,146 @@
+/*
+ * Copyright (C) 2016 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.settings.display;
+
+import com.android.settings.R;
+
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.graphics.drawable.Drawable;
+import android.util.AttributeSet;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.GridView;
+import android.widget.ImageView;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class AppGridView extends GridView {
+    public AppGridView(Context context) {
+        this(context, null);
+    }
+
+    public AppGridView(Context context, AttributeSet attrs) {
+        this(context, attrs, 0);
+    }
+
+    public AppGridView(Context context, AttributeSet attrs, int defStyleAttr) {
+        this(context, attrs, defStyleAttr);
+    }
+
+    public AppGridView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleResId) {
+        super(context, attrs, defStyleAttr, defStyleResId);
+
+        setNumColumns(AUTO_FIT);
+
+        final int columnWidth = getResources().getDimensionPixelSize(
+                R.dimen.screen_zoom_preview_app_icon_width);
+        setColumnWidth(columnWidth);
+
+        setAdapter(new AppsAdapter(context, R.layout.screen_zoom_preview_app_icon,
+                android.R.id.text1, android.R.id.icon1));
+    }
+
+    /**
+     * Loads application labels and icons.
+     */
+    private static class AppsAdapter extends ArrayAdapter<ActivityEntry> {
+        private final PackageManager mPackageManager;
+        private final int mIconResId;
+
+        public AppsAdapter(Context context, int layout, int textResId, int iconResId) {
+            super(context, layout, textResId);
+
+            mIconResId = iconResId;
+            mPackageManager = context.getPackageManager();
+
+            loadAllApps();
+        }
+
+        @Override
+        public View getView(int position, View convertView, ViewGroup parent) {
+            final View view = super.getView(position, convertView, parent);
+            final ActivityEntry entry = getItem(position);
+            final ImageView iconView = (ImageView) view.findViewById(mIconResId);
+            final Drawable icon = entry.info.loadIcon(mPackageManager);
+            iconView.setImageDrawable(icon);
+            return view;
+        }
+
+        @Override
+        public boolean hasStableIds() {
+            return true;
+        }
+
+        @Override
+        public long getItemId(int position) {
+            return position;
+        }
+
+        @Override
+        public boolean isEnabled(int position) {
+            return false;
+        }
+
+        private void loadAllApps() {
+            final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
+            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
+
+            final PackageManager pm = mPackageManager;
+            final ArrayList<ActivityEntry> results = new ArrayList<>();
+            final List<ResolveInfo> infos = pm.queryIntentActivities(mainIntent, 0);
+            for (ResolveInfo info : infos) {
+                final CharSequence label = info.loadLabel(pm);
+                if (label != null) {
+                    results.add(new ActivityEntry(info, label.toString()));
+                }
+            }
+
+            Collections.sort(results);
+
+            addAll(results);
+        }
+    }
+
+    /**
+     * Class used for caching the activity label and icon.
+     */
+    private static class ActivityEntry implements Comparable<ActivityEntry> {
+        public final ResolveInfo info;
+        public final String label;
+
+        public ActivityEntry(ResolveInfo info, String label) {
+            this.info = info;
+            this.label = label;
+        }
+
+        @Override
+        public int compareTo(ActivityEntry entry) {
+            return label.compareToIgnoreCase(entry.label);
+        }
+
+        @Override
+        public String toString() {
+            return label;
+        }
+    }
+}