[2/4] Use new Icon class in PhoneAccount.

Bug: 21088522
Change-Id: If57c3cb2f1c6630b6bd66e55862bec564e817945
diff --git a/src/com/android/server/telecom/PhoneAccountRegistrar.java b/src/com/android/server/telecom/PhoneAccountRegistrar.java
index dd1fd6e..40f63cb 100644
--- a/src/com/android/server/telecom/PhoneAccountRegistrar.java
+++ b/src/com/android/server/telecom/PhoneAccountRegistrar.java
@@ -26,6 +26,7 @@
 import android.content.pm.UserInfo;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
+import android.graphics.drawable.Icon;
 import android.net.Uri;
 import android.os.Binder;
 import android.os.Process;
@@ -43,7 +44,6 @@
 import android.util.Base64;
 import android.util.Xml;
 
-
 // TODO: Needed for move to system service: import com.android.internal.R;
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.util.FastXmlSerializer;
@@ -56,6 +56,7 @@
 
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -83,8 +84,8 @@
  *
  *  -- About Users and Phone Accounts --
  *
- * When it comes to PhoneAccounts, we store all phone account in a single place,
- * which means that there are three users that we deal with:
+ * We store all phone accounts for all users in a single place, which means that there are three
+ * users that we have to deal with in code:
  * 1) The Android User that is currently active on the device.
  * 2) The user which owns/registers the phone account.
  * 3) The user running the app that is requesting the phone account information.
@@ -92,12 +93,12 @@
  * For example, I have a device with 2 users, primary (A) and secondary (B), and the secondary user
  * has a work profile running as another user (B2). Lets say that user B opens the phone settings
  * (not currently supported, but theoretically speaking), and phone settings queries for a phone
- * account list. Lets also say that an app running in the work profile has registered a phone account.
- * This means that:
+ * account list. Lets also say that an app running in the work profile has registered a phone
+ * account. This means that:
  *
  * Since phone settings always runs as the primary user, We have the following situation:
- * User A (settings) is requesting a list of phone accounts while the active user is User B, and that
- * list contains a phone account for profile User B2.
+ * User A (settings) is requesting a list of phone accounts while the active user is User B, and
+ * that list contains a phone account for profile User B2.
  *
  * In practice, (2) is stored with the phone account handle and is part of the handle's ID. (1) is
  * saved in {@link #mCurrentUserHandle} and (3) we get from Binder.getCallingUser(). We check these
@@ -879,13 +880,13 @@
             serializer.endTag(null, tagName);
         }
 
-        protected void writeBitmapIfNonNull(String tagName, Bitmap value, XmlSerializer serializer)
+        protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer)
                 throws IOException {
-            if (value != null && value.getByteCount() > 0) {
+            if (value != null) {
                 ByteArrayOutputStream stream = new ByteArrayOutputStream();
-                value.compress(Bitmap.CompressFormat.PNG, 100, stream);
-                byte[] imageByteArray = stream.toByteArray();
-                String text = Base64.encodeToString(imageByteArray, 0, imageByteArray.length, 0);
+                value.writeToStream(stream);
+                byte[] iconByteArray = stream.toByteArray();
+                String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0);
 
                 serializer.startTag(null, tagName);
                 serializer.text(text);
@@ -931,11 +932,16 @@
             return arrayEntries;
         }
 
-        protected Bitmap readBitmap(XmlPullParser parser)
-                throws IOException, XmlPullParserException {
+        protected Bitmap readBitmap(XmlPullParser parser) {
             byte[] imageByteArray = Base64.decode(parser.getText(), 0);
             return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
         }
+
+        protected Icon readIcon(XmlPullParser parser) throws IOException {
+            byte[] iconByteArray = Base64.decode(parser.getText(), 0);
+            ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray);
+            return Icon.createFromStream(stream);
+        }
     }
 
     @VisibleForTesting
@@ -1037,6 +1043,7 @@
         private static final String LABEL = "label";
         private static final String SHORT_DESCRIPTION = "short_description";
         private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
+        private static final String ICON = "icon";
 
         @Override
         public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context)
@@ -1053,10 +1060,7 @@
                 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
                 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
                 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
-                writeTextIfNonNull(ICON_RES_ID, Integer.toString(o.getIconResId()), serializer);
-                writeTextIfNonNull(ICON_PACKAGE_NAME, o.getIconPackageName(), serializer);
-                writeBitmapIfNonNull(ICON_BITMAP, o.getIconBitmap(), serializer);
-                writeTextIfNonNull(ICON_TINT, Integer.toString(o.getIconTint()), serializer);
+                writeIconIfNonNull(ICON, o.getIcon(), serializer);
                 writeTextIfNonNull(HIGHLIGHT_COLOR,
                         Integer.toString(o.getHighlightColor()), serializer);
                 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
@@ -1083,6 +1087,7 @@
                 String label = null;
                 String shortDescription = null;
                 List<String> supportedUriSchemes = null;
+                Icon icon = null;
 
                 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
                     if (parser.getName().equals(ACCOUNT_HANDLE)) {
@@ -1122,6 +1127,9 @@
                         shortDescription = parser.getText();
                     } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
                         supportedUriSchemes = readStringList(parser);
+                    } else if (parser.getName().equals(ICON)) {
+                        parser.next();
+                        icon = readIcon(parser);
                     }
                 }
 
@@ -1161,10 +1169,13 @@
                         .setSupportedUriSchemes(supportedUriSchemes)
                         .setHighlightColor(highlightColor);
 
-                if (iconBitmap == null) {
-                    builder.setIcon(iconPackageName, iconResId, iconTint);
-                } else {
-                    builder.setIcon(iconBitmap);
+                if (icon != null) {
+                    builder.setIcon(icon);
+                } else if (iconBitmap != null) {
+                    builder.setIcon(Icon.createWithBitmap(iconBitmap));
+                } else if (!TextUtils.isEmpty(iconPackageName)) {
+                    builder.setIcon(Icon.createWithResource(iconPackageName, iconResId));
+                    // TODO: Need to set tint.
                 }
 
                 return builder.build();
@@ -1173,7 +1184,8 @@
         }
 
         /**
-         * Determines if the SIP call settings specify to use SIP for all calls, including PSTN calls.
+         * Determines if the SIP call settings specify to use SIP for all calls, including PSTN
+         * calls.
          *
          * @param context The context.
          * @return {@code True} if SIP should be used for all calls.
diff --git a/testapps/src/com/android/server/telecom/testapps/CallServiceNotifier.java b/testapps/src/com/android/server/telecom/testapps/CallServiceNotifier.java
index d40f92d..cd0800e 100644
--- a/testapps/src/com/android/server/telecom/testapps/CallServiceNotifier.java
+++ b/testapps/src/com/android/server/telecom/testapps/CallServiceNotifier.java
@@ -27,6 +27,7 @@
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Color;
+import android.graphics.drawable.Icon;
 import android.net.Uri;
 import android.telecom.PhoneAccount;
 import android.telecom.PhoneAccountHandle;
@@ -111,7 +112,9 @@
                 .setSubscriptionAddress(Uri.parse("tel:555-TEST"))
                 .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER |
                         PhoneAccount.CAPABILITY_VIDEO_CALLING)
-                .setIcon(context, R.drawable.stat_sys_phone_call, Color.RED)
+                .setIcon(Icon.createWithResource(
+                        context.getResources(), R.drawable.stat_sys_phone_call))
+                // TODO: Add icon tint (Color.RED)
                 .setHighlightColor(Color.RED)
                 .setShortDescription("a short description for the call provider")
                 .setSupportedUriSchemes(Arrays.asList("tel"))
@@ -127,7 +130,9 @@
                 .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER |
                         PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION |
                         PhoneAccount.CAPABILITY_VIDEO_CALLING)
-                .setIcon(context, R.drawable.stat_sys_phone_call, Color.GREEN)
+                .setIcon(Icon.createWithResource(
+                        context.getResources(), R.drawable.stat_sys_phone_call))
+                // TODO: Add icon tint (Color.GREEN)
                 .setHighlightColor(Color.GREEN)
                 .setShortDescription("a short description for the sim subscription")
                 .build());
@@ -140,7 +145,9 @@
                 .setAddress(Uri.parse("tel:555-CMGR"))
                 .setSubscriptionAddress(Uri.parse("tel:555-CMGR"))
                 .setCapabilities(PhoneAccount.CAPABILITY_CONNECTION_MANAGER)
-                .setIcon(context, R.drawable.stat_sys_phone_call, Color.BLUE)
+                .setIcon(Icon.createWithResource(
+                        context.getResources(), R.drawable.stat_sys_phone_call))
+                // TODO: Add icon tint (Color.BLUE)
                 .setShortDescription("a short description for the connection manager")
                 .build());
     }
diff --git a/tests/src/com/android/server/telecom/tests/PhoneAccountRegistrarTest.java b/tests/src/com/android/server/telecom/tests/PhoneAccountRegistrarTest.java
index 34575a7..8674b09 100644
--- a/tests/src/com/android/server/telecom/tests/PhoneAccountRegistrarTest.java
+++ b/tests/src/com/android/server/telecom/tests/PhoneAccountRegistrarTest.java
@@ -32,6 +32,7 @@
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
+import android.graphics.drawable.Icon;
 import android.net.Uri;
 import android.os.Parcel;
 import android.telecom.PhoneAccount;
@@ -226,10 +227,9 @@
                         .setAddress(Uri.parse("tel:123456"))
                         .setCapabilities(23)
                         .setHighlightColor(0xf0f0f0)
-                        .setIcon(
-                                "com.android.server.telecom.tests",
-                                R.drawable.stat_sys_phone_call,
-                                0xfefefe)
+                        .setIcon(Icon.createWithResource(
+                                "com.android.server.telecom.tests", R.drawable.stat_sys_phone_call))
+                        // TODO: set icon tint (0xfefefe)
                         .setShortDescription("short description")
                         .setSubscriptionAddress(Uri.parse("tel:2345678"))
                         .setSupportedUriSchemes(Arrays.asList("tel", "sip"))
@@ -239,10 +239,10 @@
                         .setAddress(Uri.parse("tel:123456"))
                         .setCapabilities(23)
                         .setHighlightColor(0xf0f0f0)
-                        .setIcon(
+                        .setIcon(Icon.createWithBitmap(
                                 BitmapFactory.decodeResource(
                                         getContext().getResources(),
-                                        R.drawable.stat_sys_phone_call))
+                                        R.drawable.stat_sys_phone_call)))
                         .setShortDescription("short description")
                         .setSubscriptionAddress(Uri.parse("tel:2345678"))
                         .setSupportedUriSchemes(Arrays.asList("tel", "sip"))
@@ -273,7 +273,8 @@
                 .setAddress(Uri.parse("http://foo.com/" + idx))
                 .setSubscriptionAddress(Uri.parse("tel:555-000" + idx))
                 .setCapabilities(idx)
-                .setIcon("com.android.server.telecom.tests", R.drawable.stat_sys_phone_call)
+                .setIcon(Icon.createWithResource(
+                            "com.android.server.telecom.tests", R.drawable.stat_sys_phone_call))
                 .setShortDescription("desc" + idx)
                 .build();
     }
@@ -343,10 +344,7 @@
             assertEquals(a.getAddress(), b.getAddress());
             assertEquals(a.getSubscriptionAddress(), b.getSubscriptionAddress());
             assertEquals(a.getCapabilities(), b.getCapabilities());
-            assertEquals(a.getIconResId(), b.getIconResId());
-            assertEquals(a.getIconPackageName(), b.getIconPackageName());
-            assertBitmapEquals(a.getIconBitmap(), b.getIconBitmap());
-            assertEquals(a.getIconTint(), b.getIconTint());
+            assertEquals(a.getIcon().toString(), b.getIcon().toString());
             assertEquals(a.getHighlightColor(), b.getHighlightColor());
             assertEquals(a.getLabel(), b.getLabel());
             assertEquals(a.getShortDescription(), b.getShortDescription());
@@ -354,21 +352,6 @@
         }
     }
 
-    private static void assertBitmapEquals(Bitmap a, Bitmap b) {
-        if (a == null || b == null) {
-            assertEquals(null, a);
-            assertEquals(null, b);
-        } else {
-            assertEquals(a.getWidth(), b.getWidth());
-            assertEquals(a.getHeight(), b.getHeight());
-            for (int x = 0; x < a.getWidth(); x++) {
-                for (int y = 0; y < a.getHeight(); y++) {
-                    assertEquals(a.getPixel(x, y), b.getPixel(x, y));
-                }
-            }
-        }
-    }
-
     private static void assertStateEquals(
             PhoneAccountRegistrar.State a, PhoneAccountRegistrar.State b) {
         assertPhoneAccountHandleEquals(a.defaultOutgoing, b.defaultOutgoing);