Clean up settings shortcuts

 - Remove some bad entries
 - Fix icons to be top-level item specific
 - Add memory

Bug: 20632230
Change-Id: I067b467b9ee354b52ef61b312dc5a89fd7eae2ac
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 4dcaba7..5f1f14c 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -767,7 +767,6 @@
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.DEFAULT" />
-                <category android:name="com.android.settings.SHORTCUT" />
             </intent-filter>
             <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
                 android:value="com.android.settings.notification.ZenModeScheduleRuleSettings" />
@@ -787,7 +786,6 @@
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.DEFAULT" />
-                <category android:name="com.android.settings.SHORTCUT" />
             </intent-filter>
             <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
                 android:value="com.android.settings.notification.ZenModeEventRuleSettings" />
@@ -807,7 +805,6 @@
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.DEFAULT" />
-                <category android:name="com.android.settings.SHORTCUT" />
             </intent-filter>
             <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
                 android:value="com.android.settings.notification.ZenModeExternalRuleSettings" />
@@ -926,6 +923,19 @@
                 android:value="true" />
         </activity>
 
+        <activity android:name="Settings$MemorySettingsActivity"
+                android:label="@string/memory_settings_title"
+                android:taskAffinity="">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="com.android.settings.SHORTCUT" />
+            </intent-filter>
+            <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
+                android:value="com.android.settings.applications.ProcessStatsSummary" />
+            <meta-data android:name="com.android.settings.TOP_LEVEL_HEADER_ID"
+                android:resource="@id/manage_memory" />
+        </activity>
+
         <activity android:name="Settings$AllApplicationsActivity"
                 android:label="@string/applications_settings"
                 android:taskAffinity="">
diff --git a/res/drawable-xxxhdpi/shortcut_base.png b/res/drawable-xxxhdpi/shortcut_base.png
new file mode 100644
index 0000000..cd509df
--- /dev/null
+++ b/res/drawable-xxxhdpi/shortcut_base.png
Binary files differ
diff --git a/res/layout/shortcut_badge.xml b/res/layout/shortcut_badge.xml
new file mode 100644
index 0000000..117b386
--- /dev/null
+++ b/res/layout/shortcut_badge.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2015 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.
+-->
+
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="@dimen/shortcut_size"
+    android:layout_height="@dimen/shortcut_size">
+
+    <ImageView
+        android:layout_width="@dimen/shortcut_size"
+        android:layout_height="@dimen/shortcut_size"
+        android:scaleType="fitXY"
+        android:src="@drawable/shortcut_base" />
+
+    <ImageView
+        android:id="@android:id/icon"
+        android:layout_width="@dimen/shortcut_size"
+        android:layout_height="@dimen/shortcut_size"
+        android:padding="12dp"
+        android:scaleType="fitXY" />
+
+</RelativeLayout>
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index 5891438..5f162b8 100755
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -244,4 +244,7 @@
     <dimen name="mdm_app_info_height">72dp</dimen>
     <dimen name="mdm_app_info_padding_top_bottom">8dp</dimen>
     <dimen name="mdm_app_name_padding_left">16dp</dimen>
+
+    <dimen name="shortcut_size">40dp</dimen>
+    <dimen name="badge_size">10dp</dimen>
 </resources>
diff --git a/src/com/android/settings/CreateShortcut.java b/src/com/android/settings/CreateShortcut.java
index fa2ce7c..637bc6f 100644
--- a/src/com/android/settings/CreateShortcut.java
+++ b/src/com/android/settings/CreateShortcut.java
@@ -17,18 +17,33 @@
 package com.android.settings;
 
 import android.app.LauncherActivity;
+import android.content.Context;
 import android.content.Intent;
+import android.content.pm.ActivityInfo;
+import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
+import android.graphics.Bitmap;
+import android.graphics.Bitmap.Config;
+import android.graphics.Canvas;
+import android.view.ContextThemeWrapper;
+import android.view.LayoutInflater;
 import android.view.View;
+import android.view.View.MeasureSpec;
+import android.widget.ImageView;
 import android.widget.ListView;
 
 import com.android.settings.Settings.TetherSettingsActivity;
+import com.android.settings.dashboard.DashboardCategory;
+import com.android.settings.dashboard.DashboardTile;
 import com.android.settingslib.TetherUtil;
 
+import java.util.ArrayList;
 import java.util.List;
 
 public class CreateShortcut extends LauncherActivity {
 
+    private static final String TOP_LEVEL_HEADER = "com.android.settings.TOP_LEVEL_HEADER_ID";
+
     @Override
     protected Intent getTargetIntent() {
         Intent targetIntent = new Intent(Intent.ACTION_MAIN, null);
@@ -46,10 +61,45 @@
                 Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher_settings));
         intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
         intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, itemForPosition(position).label);
+        ResolveInfo resolveInfo = itemForPosition(position).resolveInfo;
+        ActivityInfo activityInfo = resolveInfo.activityInfo;
+        if (activityInfo.metaData != null && activityInfo.metaData.containsKey(TOP_LEVEL_HEADER)) {
+            int topLevelId = activityInfo.metaData.getInt(TOP_LEVEL_HEADER);
+            int resourceId = getDrawableResource(topLevelId);
+            intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, createIcon(resourceId));
+        }
         setResult(RESULT_OK, intent);
         finish();
     }
 
+    private Bitmap createIcon(int resource) {
+        Context context = new ContextThemeWrapper(this, android.R.style.Theme_Material_Light);
+        View view = LayoutInflater.from(context).inflate(R.layout.shortcut_badge, null);
+        ((ImageView) view.findViewById(android.R.id.icon)).setImageResource(resource);
+
+        int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
+        view.measure(spec, spec);
+        Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
+                Config.ARGB_8888);
+        Canvas canvas = new Canvas(bitmap);
+        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
+        view.draw(canvas);
+        return bitmap;
+    }
+
+    private int getDrawableResource(int topLevelId) {
+        ArrayList<DashboardCategory> categories = new ArrayList<>();
+        SettingsActivity.loadCategoriesFromResource(R.xml.dashboard_categories, categories, this);
+        for (DashboardCategory category : categories) {
+            for (DashboardTile tile : category.tiles) {
+                if (tile.id == topLevelId) {
+                    return tile.iconRes;
+                }
+            }
+        }
+        return 0;
+    }
+
     @Override
     protected boolean onEvaluateShowIcons() {
         return false;
@@ -60,7 +110,8 @@
      * implementation queries for activities.
      */
     protected List<ResolveInfo> onQueryPackageManager(Intent queryIntent) {
-        List<ResolveInfo> activities = super.onQueryPackageManager(queryIntent);
+        List<ResolveInfo> activities = getPackageManager().queryIntentActivities(queryIntent,
+                PackageManager.GET_META_DATA);
         if (activities == null) return null;
         for (int i = activities.size() - 1; i >= 0; i--) {
             ResolveInfo info = activities.get(i);
diff --git a/src/com/android/settings/Settings.java b/src/com/android/settings/Settings.java
index 5fb94f0..ea4f77a 100644
--- a/src/com/android/settings/Settings.java
+++ b/src/com/android/settings/Settings.java
@@ -113,5 +113,6 @@
     public static class TopLevelSettings extends SettingsActivity { /* empty */ }
     public static class ApnSettingsActivity extends SettingsActivity { /* empty */ }
     public static class WifiCallingSettingsActivity extends SettingsActivity { /* empty */ }
+    public static class MemorySettingsActivity extends SettingsActivity { /* empty */ }
 }
 
diff --git a/src/com/android/settings/SettingsActivity.java b/src/com/android/settings/SettingsActivity.java
index 89231df..f376644 100644
--- a/src/com/android/settings/SettingsActivity.java
+++ b/src/com/android/settings/SettingsActivity.java
@@ -1039,7 +1039,7 @@
      */
     private void buildDashboardCategories(List<DashboardCategory> categories) {
         categories.clear();
-        loadCategoriesFromResource(R.xml.dashboard_categories, categories);
+        loadCategoriesFromResource(R.xml.dashboard_categories, categories, this);
         updateTilesList(categories);
     }
 
@@ -1050,10 +1050,11 @@
      * @param resid The XML resource to load and parse.
      * @param target The list in which the parsed categories and tiles should be placed.
      */
-    private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
+    public static void loadCategoriesFromResource(int resid, List<DashboardCategory> target,
+            Context context) {
         XmlResourceParser parser = null;
         try {
-            parser = getResources().getXml(resid);
+            parser = context.getResources().getXml(resid);
             AttributeSet attrs = Xml.asAttributeSet(parser);
 
             int type;
@@ -1082,7 +1083,7 @@
                 if ("dashboard-category".equals(nodeName)) {
                     DashboardCategory category = new DashboardCategory();
 
-                    TypedArray sa = obtainStyledAttributes(
+                    TypedArray sa = context.obtainStyledAttributes(
                             attrs, com.android.internal.R.styleable.PreferenceHeader);
                     category.id = sa.getResourceId(
                             com.android.internal.R.styleable.PreferenceHeader_id,
@@ -1098,12 +1099,13 @@
                         }
                     }
                     sa.recycle();
-                    sa = obtainStyledAttributes(attrs, com.android.internal.R.styleable.Preference);
+                    sa = context.obtainStyledAttributes(attrs,
+                            com.android.internal.R.styleable.Preference);
                     tv = sa.peekValue(
                             com.android.internal.R.styleable.Preference_key);
                     if (tv != null && tv.type == TypedValue.TYPE_STRING) {
                         if (tv.resourceId != 0) {
-                            category.key = getString(tv.resourceId);
+                            category.key = context.getString(tv.resourceId);
                         } else {
                             category.key = tv.string.toString();
                         }
@@ -1121,7 +1123,7 @@
                         if (innerNodeName.equals("dashboard-tile")) {
                             DashboardTile tile = new DashboardTile();
 
-                            sa = obtainStyledAttributes(
+                            sa = context.obtainStyledAttributes(
                                     attrs, com.android.internal.R.styleable.PreferenceHeader);
                             tile.id = sa.getResourceId(
                                     com.android.internal.R.styleable.PreferenceHeader_id,
@@ -1163,11 +1165,13 @@
 
                                 String innerNodeName2 = parser.getName();
                                 if (innerNodeName2.equals("extra")) {
-                                    getResources().parseBundleExtra("extra", attrs, curBundle);
+                                    context.getResources().parseBundleExtra("extra", attrs,
+                                            curBundle);
                                     XmlUtils.skipCurrentTag(parser);
 
                                 } else if (innerNodeName2.equals("intent")) {
-                                    tile.intent = Intent.parseIntent(getResources(), parser, attrs);
+                                    tile.intent = Intent.parseIntent(context.getResources(), parser,
+                                            attrs);
 
                                 } else {
                                     XmlUtils.skipCurrentTag(parser);
@@ -1180,7 +1184,7 @@
                             }
 
                             // Show the SIM Cards setting if there are more than 2 SIMs installed.
-                            if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
+                            if(tile.id != R.id.sim_settings || Utils.showSimCardTile(context)){
                                 category.addTile(tile);
                             }