Improve Intent disambig dialog behavior

Keep track of last chosen activity for a particular intent, similar
to how it is tracked for "Always" choices.
Pre-select the last chosen activity if previously the user picked
"Just once".
Downgrade "Always" to "Last chosen" if there's a new kid on the block,
instead of removing it entirely.
Add methods to set and get last chosen entry.

UI - switch from Grid to List.

Bug: 9958096

Change-Id: Ied57147739a3ade1d36c3a7ec1e8ce77e5c5bb16
diff --git a/services/java/com/android/server/PreferredComponent.java b/services/java/com/android/server/PreferredComponent.java
index bb22545..134b198 100644
--- a/services/java/com/android/server/PreferredComponent.java
+++ b/services/java/com/android/server/PreferredComponent.java
@@ -33,8 +33,16 @@
 import java.util.List;
 
 public class PreferredComponent {
+    private static final String TAG_SET = "set";
+    private static final String ATTR_ALWAYS = "always"; // boolean
+    private static final String ATTR_MATCH = "match"; // number
+    private static final String ATTR_NAME = "name"; // component name
+    private static final String ATTR_SET = "set"; // number
+
     public final int mMatch;
     public final ComponentName mComponent;
+    // Whether this is to be the one that's always chosen. If false, it's the most recently chosen.
+    public boolean mAlways;
 
     private final String[] mSetPackages;
     private final String[] mSetClasses;
@@ -50,10 +58,11 @@
     }
 
     public PreferredComponent(Callbacks callbacks, int match, ComponentName[] set,
-            ComponentName component) {
+            ComponentName component, boolean always) {
         mCallbacks = callbacks;
         mMatch = match&IntentFilter.MATCH_CATEGORY_MASK;
         mComponent = component;
+        mAlways = always;
         mShortComponent = component.flattenToShortString();
         mParseError = null;
         if (set != null) {
@@ -86,15 +95,17 @@
     public PreferredComponent(Callbacks callbacks, XmlPullParser parser)
             throws XmlPullParserException, IOException {
         mCallbacks = callbacks;
-        mShortComponent = parser.getAttributeValue(null, "name");
+        mShortComponent = parser.getAttributeValue(null, ATTR_NAME);
         mComponent = ComponentName.unflattenFromString(mShortComponent);
         if (mComponent == null) {
             mParseError = "Bad activity name " + mShortComponent;
         }
-        String matchStr = parser.getAttributeValue(null, "match");
+        String matchStr = parser.getAttributeValue(null, ATTR_MATCH);
         mMatch = matchStr != null ? Integer.parseInt(matchStr, 16) : 0;
-        String setCountStr = parser.getAttributeValue(null, "set");
+        String setCountStr = parser.getAttributeValue(null, ATTR_SET);
         int setCount = setCountStr != null ? Integer.parseInt(setCountStr) : 0;
+        String alwaysStr = parser.getAttributeValue(null, ATTR_ALWAYS);
+        mAlways = alwaysStr != null ? Boolean.parseBoolean(alwaysStr) : true;
 
         String[] myPackages = setCount > 0 ? new String[setCount] : null;
         String[] myClasses = setCount > 0 ? new String[setCount] : null;
@@ -115,8 +126,8 @@
             String tagName = parser.getName();
             //Log.i(TAG, "Parse outerDepth=" + outerDepth + " depth="
             //        + parser.getDepth() + " tag=" + tagName);
-            if (tagName.equals("set")) {
-                String name = parser.getAttributeValue(null, "name");
+            if (tagName.equals(TAG_SET)) {
+                String name = parser.getAttributeValue(null, ATTR_NAME);
                 if (name == null) {
                     if (mParseError == null) {
                         mParseError = "No name in set tag in preferred activity "
@@ -166,16 +177,17 @@
 
     public void writeToXml(XmlSerializer serializer, boolean full) throws IOException {
         final int NS = mSetClasses != null ? mSetClasses.length : 0;
-        serializer.attribute(null, "name", mShortComponent);
+        serializer.attribute(null, ATTR_NAME, mShortComponent);
         if (full) {
             if (mMatch != 0) {
-                serializer.attribute(null, "match", Integer.toHexString(mMatch));
+                serializer.attribute(null, ATTR_MATCH, Integer.toHexString(mMatch));
             }
-            serializer.attribute(null, "set", Integer.toString(NS));
+            serializer.attribute(null, ATTR_ALWAYS, Boolean.toString(mAlways));
+            serializer.attribute(null, ATTR_SET, Integer.toString(NS));
             for (int s=0; s<NS; s++) {
-                serializer.startTag(null, "set");
-                serializer.attribute(null, "name", mSetComponents[s]);
-                serializer.endTag(null, "set");
+                serializer.startTag(null, TAG_SET);
+                serializer.attribute(null, ATTR_NAME, mSetComponents[s]);
+                serializer.endTag(null, TAG_SET);
             }
         }
     }