Updating task description when activity is visible.

Bug: 27275448
Change-Id: If92221d30c84c58b5d36db1b254d264a0ff3ec25
diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java
index 332c739..4792dc9 100644
--- a/core/java/android/app/Activity.java
+++ b/core/java/android/app/Activity.java
@@ -815,6 +815,9 @@
     private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE;
     private SpannableStringBuilder mDefaultKeySsb = null;
 
+    private ActivityManager.TaskDescription mTaskDescription =
+            new ActivityManager.TaskDescription();
+
     protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused};
 
     @SuppressWarnings("unused")
@@ -1116,6 +1119,34 @@
     }
 
     /**
+     * Attempts to extract the color from a given drawable.
+     *
+     * @return the extracted color or 0 if no color could be extracted.
+     */
+    private int tryExtractColorFromDrawable(Drawable drawable) {
+        if (drawable instanceof ColorDrawable) {
+            return ((ColorDrawable) drawable).getColor();
+        } else if (drawable instanceof InsetDrawable) {
+            return tryExtractColorFromDrawable(((InsetDrawable) drawable).getDrawable());
+        } else if (drawable instanceof ShapeDrawable) {
+            Paint p = ((ShapeDrawable) drawable).getPaint();
+            if (p != null) {
+                return p.getColor();
+            }
+        } else if (drawable instanceof LayerDrawable) {
+            LayerDrawable ld = (LayerDrawable) drawable;
+            int numLayers = ld.getNumberOfLayers();
+            for (int i = 0; i < numLayers; i++) {
+                int color = tryExtractColorFromDrawable(ld.getDrawable(i));
+                if (color != 0) {
+                    return color;
+                }
+            }
+        }
+        return 0;
+    }
+
+    /**
      * Called when activity start-up is complete (after {@link #onStart}
      * and {@link #onRestoreInstanceState} have been called).  Applications will
      * generally not implement this method; it is intended for system
@@ -1136,6 +1167,36 @@
             mTitleReady = true;
             onTitleChanged(getTitle(), getTitleColor());
         }
+
+        Resources.Theme theme = getTheme();
+        if (theme != null) {
+            // Get the primary color and update the TaskDescription for this activity
+            TypedArray a = theme.obtainStyledAttributes(
+                    com.android.internal.R.styleable.ActivityTaskDescription);
+            if (mTaskDescription.getPrimaryColor() == 0) {
+                int colorPrimary = a.getColor(
+                        com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0);
+                if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) {
+                    mTaskDescription.setPrimaryColor(colorPrimary);
+                }
+            }
+            if (mTaskDescription.getBackgroundColor() == 0) {
+                int windowBgResourceId = a.getResourceId(
+                        com.android.internal.R.styleable.ActivityTaskDescription_windowBackground,
+                        0);
+                int windowBgFallbackResourceId = a.getResourceId(
+                        com.android.internal.R.styleable.ActivityTaskDescription_windowBackgroundFallback,
+                        0);
+                int colorBg = tryExtractColorFromDrawable(DecorView.getResizingBackgroundDrawable(
+                        this, windowBgResourceId, windowBgFallbackResourceId));
+                if (colorBg != 0 && Color.alpha(colorBg) == 0xFF) {
+                    mTaskDescription.setBackgroundColor(colorBg);
+                }
+            }
+            a.recycle();
+            setTaskDescription(mTaskDescription);
+        }
+
         mCalled = true;
     }
 
@@ -3975,57 +4036,6 @@
             }
             theme.applyStyle(resid, false);
         }
-
-        // Get the primary color and update the TaskDescription for this activity
-        if (theme != null) {
-            TypedArray a = theme.obtainStyledAttributes(com.android.internal.R.styleable.Theme);
-            int windowBgResourceId = a.getResourceId(
-                    com.android.internal.R.styleable.Window_windowBackground, 0);
-            int windowBgFallbackResourceId = a.getResourceId(
-                    com.android.internal.R.styleable.Window_windowBackgroundFallback, 0);
-            int colorPrimary = a.getColor(com.android.internal.R.styleable.Theme_colorPrimary, 0);
-            int colorBg = tryExtractColorFromDrawable(DecorView.getResizingBackgroundDrawable(this,
-                    windowBgResourceId, windowBgFallbackResourceId));
-            a.recycle();
-            if (colorPrimary != 0) {
-                ActivityManager.TaskDescription td = new ActivityManager.TaskDescription();
-                if (Color.alpha(colorPrimary) == 0xFF) {
-                    td.setPrimaryColor(colorPrimary);
-                }
-                if (Color.alpha(colorBg) == 0xFF) {
-                    td.setBackgroundColor(colorBg);
-                }
-                setTaskDescription(td);
-            }
-        }
-    }
-
-    /**
-     * Attempts to extract the color from a given drawable.
-     *
-     * @return the extracted color or 0 if no color could be extracted.
-     */
-    private int tryExtractColorFromDrawable(Drawable drawable) {
-        if (drawable instanceof ColorDrawable) {
-            return ((ColorDrawable) drawable).getColor();
-        } else if (drawable instanceof InsetDrawable) {
-            return tryExtractColorFromDrawable(((InsetDrawable) drawable).getDrawable());
-        } else if (drawable instanceof ShapeDrawable) {
-            Paint p = ((ShapeDrawable) drawable).getPaint();
-            if (p != null) {
-                return p.getColor();
-            }
-        } else if (drawable instanceof LayerDrawable) {
-            LayerDrawable ld = (LayerDrawable) drawable;
-            int numLayers = ld.getNumberOfLayers();
-            for (int i = 0; i < numLayers; i++) {
-                int color = tryExtractColorFromDrawable(ld.getDrawable(i));
-                if (color != 0) {
-                    return color;
-                }
-            }
-        }
-        return 0;
     }
 
     /**
@@ -5651,18 +5661,18 @@
      * @param taskDescription The TaskDescription properties that describe the task with this activity
      */
     public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
-        ActivityManager.TaskDescription td;
-        // Scale the icon down to something reasonable if it is provided
-        if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
-            final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
-            final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size, true);
-            td = new ActivityManager.TaskDescription(taskDescription);
-            td.setIcon(icon);
-        } else {
-            td = taskDescription;
+        if (mTaskDescription != taskDescription) {
+            mTaskDescription.copyFrom(taskDescription);
+            // Scale the icon down to something reasonable if it is provided
+            if (taskDescription.getIconFilename() == null && taskDescription.getIcon() != null) {
+                final int size = ActivityManager.getLauncherLargeIconSizeInner(this);
+                final Bitmap icon = Bitmap.createScaledBitmap(taskDescription.getIcon(), size, size,
+                        true);
+                mTaskDescription.setIcon(icon);
+            }
         }
         try {
-            ActivityManagerNative.getDefault().setTaskDescription(mToken, td);
+            ActivityManagerNative.getDefault().setTaskDescription(mToken, mTaskDescription);
         } catch (RemoteException e) {
         }
     }
diff --git a/core/java/android/app/ActivityManager.java b/core/java/android/app/ActivityManager.java
index 7771139..b1927d0 100644
--- a/core/java/android/app/ActivityManager.java
+++ b/core/java/android/app/ActivityManager.java
@@ -946,11 +946,19 @@
          * Creates a copy of another TaskDescription.
          */
         public TaskDescription(TaskDescription td) {
-            mLabel = td.mLabel;
-            mIcon = td.mIcon;
-            mIconFilename = td.mIconFilename;
-            mColorPrimary = td.mColorPrimary;
-            mColorBackground = td.mColorBackground;
+            copyFrom(td);
+        }
+
+        /**
+         * Copies this the values from another TaskDescription.
+         * @hide
+         */
+        public void copyFrom(TaskDescription other) {
+            mLabel = other.mLabel;
+            mIcon = other.mIcon;
+            mIconFilename = other.mIconFilename;
+            mColorPrimary = other.mColorPrimary;
+            mColorBackground = other.mColorBackground;
         }
 
         private TaskDescription(Parcel source) {
diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml
index 3a45da9..7c02128 100644
--- a/core/res/res/values/attrs.xml
+++ b/core/res/res/values/attrs.xml
@@ -8182,4 +8182,18 @@
         <!-- The current color for the offset inside the gradient. -->
         <attr name="color" />
     </declare-styleable>
+
+    <!-- @hide Attributes which will be read by the Activity to intialize the 
+               base activity TaskDescription. -->
+    <declare-styleable name="ActivityTaskDescription">
+        <!-- @hide From Theme.colorPrimary, used for the TaskDescription primary 
+                   color. -->
+        <attr name="colorPrimary" />
+        <!-- @hide From Theme.windowBackground, used for calculating the 
+                   TaskDescription background color. -->
+        <attr name="windowBackground" />
+        <!-- @hide From Theme.windowBackgroundFallback, used for calculating the 
+                   TaskDescription background color. -->
+        <attr name="windowBackgroundFallback" />
+    </declare-styleable>
 </resources>