Merge "Update PhoneAccount icon API (5/6)" into lmp-mr1-dev
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 88e90ae..9a8d0cc 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -17,6 +17,7 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:androidprv="http://schemas.android.com/apk/prv/res/android"
         package="com.android.server.telecom"
+        android:debuggable="true"
         coreApp="true"
         android:sharedUserId="android.uid.phone">
 
diff --git a/src/com/android/server/telecom/CallsManager.java b/src/com/android/server/telecom/CallsManager.java
index 57b9e8e..e3327c3 100644
--- a/src/com/android/server/telecom/CallsManager.java
+++ b/src/com/android/server/telecom/CallsManager.java
@@ -451,6 +451,8 @@
         List<PhoneAccountHandle> accounts =
                 mPhoneAccountRegistrar.getCallCapablePhoneAccounts(handle.getScheme());
 
+        Log.v(this, "startOutgoingCall found accounts = " + accounts);
+
         // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
         // as if a phoneAccount was not specified (does the default behavior instead).
         // Note: We will not attempt to dial with a requested phoneAccount if it is disabled.
diff --git a/src/com/android/server/telecom/PhoneAccountRegistrar.java b/src/com/android/server/telecom/PhoneAccountRegistrar.java
index a156f6a..1d7993d 100644
--- a/src/com/android/server/telecom/PhoneAccountRegistrar.java
+++ b/src/com/android/server/telecom/PhoneAccountRegistrar.java
@@ -84,7 +84,7 @@
 
     private static final String FILE_NAME = "phone-account-registrar-state.xml";
     @VisibleForTesting
-    public static final int EXPECTED_STATE_VERSION = 4;
+    public static final int EXPECTED_STATE_VERSION = 5;
 
     /** Keep in sync with the same in SipSettings.java */
     private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
@@ -394,6 +394,9 @@
      * @param account The {@code PhoneAccount} to add or replace.
      */
     private void addOrReplacePhoneAccount(PhoneAccount account) {
+        Log.d(this, "addOrReplacePhoneAccount(%s -> %s)",
+                account.getAccountHandle(), account);
+
         mState.accounts.add(account);
         // Search for duplicates and remove any that are found.
         for (int i = 0; i < mState.accounts.size() - 1; i++) {
@@ -857,7 +860,8 @@
         private static final String ICON_RES_ID = "icon_res_id";
         private static final String ICON_PACKAGE_NAME = "icon_package_name";
         private static final String ICON_BITMAP = "icon_bitmap";
-        private static final String COLOR = "color";
+        private static final String ICON_TINT = "icon_tint";
+        private static final String HIGHLIGHT_COLOR = "highlight_color";
         private static final String LABEL = "label";
         private static final String SHORT_DESCRIPTION = "short_description";
         private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
@@ -880,7 +884,9 @@
                 writeTextIfNonNull(ICON_RES_ID, Integer.toString(o.getIconResId()), serializer);
                 writeTextIfNonNull(ICON_PACKAGE_NAME, o.getIconPackageName(), serializer);
                 writeBitmapIfNonNull(ICON_BITMAP, o.getIconBitmap(), serializer);
-                writeTextIfNonNull(COLOR, Integer.toString(o.getColor()), serializer);
+                writeTextIfNonNull(ICON_TINT, Integer.toString(o.getIconTint()), serializer);
+                writeTextIfNonNull(HIGHLIGHT_COLOR,
+                        Integer.toString(o.getHighlightColor()), serializer);
                 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
                 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
                 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
@@ -897,10 +903,11 @@
                 Uri address = null;
                 Uri subscriptionAddress = null;
                 int capabilities = 0;
-                int iconResId = 0;
+                int iconResId = PhoneAccount.NO_RESOURCE_ID;
                 String iconPackageName = null;
-                Bitmap icon = null;
-                int color = 0;
+                Bitmap iconBitmap = null;
+                int iconTint = PhoneAccount.NO_COLOR;
+                int highlightColor = PhoneAccount.NO_COLOR;
                 String label = null;
                 String shortDescription = null;
                 List<String> supportedUriSchemes = null;
@@ -928,10 +935,13 @@
                         iconPackageName = parser.getText();
                     } else if (parser.getName().equals(ICON_BITMAP)) {
                         parser.next();
-                        icon = readBitmap(parser);
-                    } else if (parser.getName().equals(COLOR)) {
+                        iconBitmap = readBitmap(parser);
+                    } else if (parser.getName().equals(ICON_TINT)) {
                         parser.next();
-                        color = Integer.parseInt(parser.getText());
+                        iconTint = Integer.parseInt(parser.getText());
+                    } else if (parser.getName().equals(HIGHLIGHT_COLOR)) {
+                        parser.next();
+                        highlightColor = Integer.parseInt(parser.getText());
                     } else if (parser.getName().equals(LABEL)) {
                         parser.next();
                         label = parser.getText();
@@ -964,17 +974,28 @@
                     }
                 }
 
-                return PhoneAccount.builder(accountHandle, label)
+                // Upgrade older phone accounts with explicit package name
+                if (version < 5) {
+                    if (iconBitmap == null) {
+                        iconPackageName = accountHandle.getComponentName().getPackageName();
+                    }
+                }
+
+                PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label)
                         .setAddress(address)
                         .setSubscriptionAddress(subscriptionAddress)
                         .setCapabilities(capabilities)
-                        .setIconResId(iconResId)
-                        .setIconPackageName(iconPackageName)
-                        .setIconBitmap(icon)
-                        .setColor(color)
                         .setShortDescription(shortDescription)
                         .setSupportedUriSchemes(supportedUriSchemes)
-                        .build();
+                        .setHighlightColor(highlightColor);
+
+                if (iconBitmap == null) {
+                    builder.setIcon(iconPackageName, iconResId, iconTint);
+                } else {
+                    builder.setIcon(iconBitmap);
+                }
+
+                return builder.build();
             }
             return null;
         }
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
index df7e93e..e987dee 100644
--- a/tests/AndroidManifest.xml
+++ b/tests/AndroidManifest.xml
@@ -19,7 +19,6 @@
           package="com.android.server.telecom.tests"
           android:debuggable="true">
 
-    <!-- Test connection service outgoing video preview. -->
     <uses-permission android:name="android.permission.CAMERA" />
     <uses-permission android:name="android.permission.CONTROL_INCALL_EXPERIENCE" />
     <uses-permission android:name="android.permission.REGISTER_CALL_PROVIDER" />
diff --git a/tests/src/com/android/server/telecom/testapps/CallServiceNotifier.java b/tests/src/com/android/server/telecom/testapps/CallServiceNotifier.java
index a0959fa..835863f 100644
--- a/tests/src/com/android/server/telecom/testapps/CallServiceNotifier.java
+++ b/tests/src/com/android/server/telecom/testapps/CallServiceNotifier.java
@@ -26,6 +26,7 @@
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
+import android.graphics.Color;
 import android.net.Uri;
 import android.telecom.PhoneAccount;
 import android.telecom.PhoneAccountHandle;
@@ -99,35 +100,31 @@
         TelecomManager telecomManager =
                 (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
 
-        Bitmap icon = BitmapFactory.decodeResource(
-                context.getResources(),
-                R.drawable.stat_sys_phone_call);
-
         telecomManager.clearAccounts();
 
         telecomManager.registerPhoneAccount(PhoneAccount.builder(
-                        new PhoneAccountHandle(
-                                new ComponentName(context, TestConnectionService.class),
-                                CALL_PROVIDER_ID),
-                        "TelecomTestApp Call Provider")
+                new PhoneAccountHandle(
+                        new ComponentName(context, TestConnectionService.class),
+                        CALL_PROVIDER_ID),
+                "TelecomTestApp Call Provider")
                 .setAddress(Uri.parse("tel:555-TEST"))
                 .setSubscriptionAddress(Uri.parse("tel:555-TEST"))
                 .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)
-                .setIconBitmap(icon)
+                .setIcon(context, R.drawable.stat_sys_phone_call, Color.RED)
                 .setShortDescription("a short description for the call provider")
                 .setSupportedUriSchemes(Arrays.asList("tel"))
                 .build());
 
         telecomManager.registerPhoneAccount(PhoneAccount.builder(
-                        new PhoneAccountHandle(
-                                new ComponentName(context, TestConnectionService.class),
-                                SIM_SUBSCRIPTION_ID),
-                        "TelecomTestApp SIM Subscription")
+                new PhoneAccountHandle(
+                        new ComponentName(context, TestConnectionService.class),
+                        SIM_SUBSCRIPTION_ID),
+                "TelecomTestApp SIM Subscription")
                 .setAddress(Uri.parse("tel:555-TSIM"))
                 .setSubscriptionAddress(Uri.parse("tel:555-TSIM"))
                 .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER |
-                    PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)
-                .setIconBitmap(icon)
+                        PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)
+                .setIcon(context, R.drawable.stat_sys_phone_call, Color.GREEN)
                 .setShortDescription("a short description for the sim subscription")
                 .build());
 
@@ -139,7 +136,7 @@
                 .setAddress(Uri.parse("tel:555-CMGR"))
                 .setSubscriptionAddress(Uri.parse("tel:555-CMGR"))
                 .setCapabilities(PhoneAccount.CAPABILITY_CONNECTION_MANAGER)
-                .setIconBitmap(icon)
+                .setIcon(context, R.drawable.stat_sys_phone_call, Color.BLUE)
                 .setShortDescription("a short description for the connection manager")
                 .build());
     }
diff --git a/tests/src/com/android/server/telecom/tests/unit/PhoneAccountRegistrarTest.java b/tests/src/com/android/server/telecom/tests/unit/PhoneAccountRegistrarTest.java
index c2beb0b..2610720 100644
--- a/tests/src/com/android/server/telecom/tests/unit/PhoneAccountRegistrarTest.java
+++ b/tests/src/com/android/server/telecom/tests/unit/PhoneAccountRegistrarTest.java
@@ -204,12 +204,7 @@
                 .setAddress(Uri.parse("http://foo.com/" + idx))
                 .setSubscriptionAddress(Uri.parse("tel:555-000" + idx))
                 .setCapabilities(idx)
-                .setIconResId(R.drawable.stat_sys_phone_call)
-                .setIconPackageName("com.android.server.telecom.tests")
-                .setIconBitmap(
-                        BitmapFactory.decodeResource(
-                                getContext().getResources(),
-                                R.drawable.stat_sys_phone_call))
+                .setIcon("com.android.server.telecom.tests", R.drawable.stat_sys_phone_call)
                 .setShortDescription("desc" + idx)
                 .build();
     }
@@ -268,7 +263,8 @@
             assertEquals(a.getIconResId(), b.getIconResId());
             assertEquals(a.getIconPackageName(), b.getIconPackageName());
             assertBitmapEquals(a.getIconBitmap(), b.getIconBitmap());
-            assertEquals(a.getColor(), b.getColor());
+            assertEquals(a.getIconTint(), b.getIconTint());
+            assertEquals(a.getHighlightColor(), b.getHighlightColor());
             assertEquals(a.getLabel(), b.getLabel());
             assertEquals(a.getShortDescription(), b.getShortDescription());
             assertEquals(a.getSupportedUriSchemes(), b.getSupportedUriSchemes());
@@ -276,11 +272,16 @@
     }
 
     private static void assertBitmapEquals(Bitmap a, Bitmap b) {
-        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));
+        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));
+                }
             }
         }
     }