Merge "Add BIND_TELECOM_CONNECTION_SERVICE permission" into mnc-dev
diff --git a/src/com/android/server/telecom/CallAudioManager.java b/src/com/android/server/telecom/CallAudioManager.java
index d8ebbc9..1b56e97 100644
--- a/src/com/android/server/telecom/CallAudioManager.java
+++ b/src/com/android/server/telecom/CallAudioManager.java
@@ -19,7 +19,7 @@
 import android.content.Context;
 import android.media.AudioManager;
 import android.os.Binder;
-import android.telecom.AudioState;
+import android.telecom.CallAudioState;
 
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.internal.util.Preconditions;
@@ -40,7 +40,7 @@
     private final DockManager mDockManager;
     private final CallsManager mCallsManager;
 
-    private AudioState mAudioState;
+    private CallAudioState mCallAudioState;
     private int mAudioFocusStreamType;
     private boolean mIsRinging;
     private boolean mIsTonePlaying;
@@ -68,8 +68,8 @@
         mAudioFocusStreamType = STREAM_NONE;
     }
 
-    AudioState getAudioState() {
-        return mAudioState;
+    CallAudioState getCallAudioState() {
+        return mCallAudioState;
     }
 
     @Override
@@ -79,8 +79,8 @@
         if (hasFocus() && getForegroundCall() == call) {
             if (!call.isIncoming()) {
                 // Unmute new outgoing call.
-                setSystemAudioState(false, mAudioState.getRoute(),
-                        mAudioState.getSupportedRouteMask());
+                setSystemAudioState(false, mCallAudioState.getRoute(),
+                        mCallAudioState.getSupportedRouteMask());
             }
         }
     }
@@ -105,7 +105,7 @@
 
     @Override
     public void onIncomingCallAnswered(Call call) {
-        int route = mAudioState.getRoute();
+        int route = mCallAudioState.getRoute();
 
         // We do two things:
         // (1) If this is the first call, then we can to turn on bluetooth if available.
@@ -113,10 +113,10 @@
         boolean isOnlyCall = mCallsManager.getCalls().size() == 1;
         if (isOnlyCall && mBluetoothManager.isBluetoothAvailable()) {
             mBluetoothManager.connectBluetoothAudio();
-            route = AudioState.ROUTE_BLUETOOTH;
+            route = CallAudioState.ROUTE_BLUETOOTH;
         }
 
-        setSystemAudioState(false /* isMute */, route, mAudioState.getSupportedRouteMask());
+        setSystemAudioState(false /* isMute */, route, mCallAudioState.getSupportedRouteMask());
 
         if (call.can(android.telecom.Call.Details.CAPABILITY_SPEED_UP_MT_AUDIO)) {
             Log.v(this, "Speed up audio setup for IMS MT call.");
@@ -148,11 +148,12 @@
             return;
         }
 
-        boolean isCurrentlyWiredHeadset = mAudioState.getRoute() == AudioState.ROUTE_WIRED_HEADSET;
+        boolean isCurrentlyWiredHeadset = mCallAudioState.getRoute()
+                == CallAudioState.ROUTE_WIRED_HEADSET;
 
-        int newRoute = mAudioState.getRoute();  // start out with existing route
+        int newRoute = mCallAudioState.getRoute();  // start out with existing route
         if (newIsPluggedIn) {
-            newRoute = AudioState.ROUTE_WIRED_HEADSET;
+            newRoute = CallAudioState.ROUTE_WIRED_HEADSET;
         } else if (isCurrentlyWiredHeadset) {
             Call call = getForegroundCall();
             boolean hasLiveCall = call != null && call.isAlive();
@@ -160,9 +161,9 @@
             if (hasLiveCall) {
                 // In order of preference when a wireless headset is unplugged.
                 if (mWasSpeakerOn) {
-                    newRoute = AudioState.ROUTE_SPEAKER;
+                    newRoute = CallAudioState.ROUTE_SPEAKER;
                 } else {
-                    newRoute = AudioState.ROUTE_EARPIECE;
+                    newRoute = CallAudioState.ROUTE_EARPIECE;
                 }
 
                 // We don't automatically connect to bluetooth when user unplugs their wired headset
@@ -177,7 +178,7 @@
 
         // We need to call this every time even if we do not change the route because the supported
         // routes changed either to include or not include WIRED_HEADSET.
-        setSystemAudioState(mAudioState.isMuted(), newRoute, calculateSupportedRoutes());
+        setSystemAudioState(mCallAudioState.isMuted(), newRoute, calculateSupportedRoutes());
     }
 
     @Override
@@ -190,19 +191,19 @@
         if (isDocked) {
             // Device just docked, turn to speakerphone. Only do so if the route is currently
             // earpiece so that we dont switch out of a BT headset or a wired headset.
-            if (mAudioState.getRoute() == AudioState.ROUTE_EARPIECE) {
-                setAudioRoute(AudioState.ROUTE_SPEAKER);
+            if (mCallAudioState.getRoute() == CallAudioState.ROUTE_EARPIECE) {
+                setAudioRoute(CallAudioState.ROUTE_SPEAKER);
             }
         } else {
             // Device just undocked, remove from speakerphone if possible.
-            if (mAudioState.getRoute() == AudioState.ROUTE_SPEAKER) {
-                setAudioRoute(AudioState.ROUTE_WIRED_OR_EARPIECE);
+            if (mCallAudioState.getRoute() == CallAudioState.ROUTE_SPEAKER) {
+                setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE);
             }
         }
     }
 
     void toggleMute() {
-        mute(!mAudioState.isMuted());
+        mute(!mCallAudioState.isMuted());
     }
 
     void mute(boolean shouldMute) {
@@ -218,16 +219,16 @@
             Log.v(this, "ignoring mute for emergency call");
         }
 
-        if (mAudioState.isMuted() != shouldMute) {
-            setSystemAudioState(shouldMute, mAudioState.getRoute(),
-                    mAudioState.getSupportedRouteMask());
+        if (mCallAudioState.isMuted() != shouldMute) {
+            setSystemAudioState(shouldMute, mCallAudioState.getRoute(),
+                    mCallAudioState.getSupportedRouteMask());
         }
     }
 
     /**
      * Changed the audio route, for example from earpiece to speaker phone.
      *
-     * @param route The new audio route to use. See {@link AudioState}.
+     * @param route The new audio route to use. See {@link CallAudioState}.
      */
     void setAudioRoute(int route) {
         // This can happen even when there are no calls and we don't have focus.
@@ -235,23 +236,23 @@
             return;
         }
 
-        Log.v(this, "setAudioRoute, route: %s", AudioState.audioRouteToString(route));
+        Log.v(this, "setAudioRoute, route: %s", CallAudioState.audioRouteToString(route));
 
         // Change ROUTE_WIRED_OR_EARPIECE to a single entry.
-        int newRoute = selectWiredOrEarpiece(route, mAudioState.getSupportedRouteMask());
+        int newRoute = selectWiredOrEarpiece(route, mCallAudioState.getSupportedRouteMask());
 
         // If route is unsupported, do nothing.
-        if ((mAudioState.getSupportedRouteMask() | newRoute) == 0) {
+        if ((mCallAudioState.getSupportedRouteMask() | newRoute) == 0) {
             Log.wtf(this, "Asking to set to a route that is unsupported: %d", newRoute);
             return;
         }
 
-        if (mAudioState.getRoute() != newRoute) {
+        if (mCallAudioState.getRoute() != newRoute) {
             // Remember the new speaker state so it can be restored when the user plugs and unplugs
             // a headset.
-            mWasSpeakerOn = newRoute == AudioState.ROUTE_SPEAKER;
-            setSystemAudioState(mAudioState.isMuted(), newRoute,
-                    mAudioState.getSupportedRouteMask());
+            mWasSpeakerOn = newRoute == CallAudioState.ROUTE_SPEAKER;
+            setSystemAudioState(mCallAudioState.isMuted(), newRoute,
+                    mCallAudioState.getSupportedRouteMask());
         }
     }
 
@@ -288,16 +289,17 @@
         }
 
         int supportedRoutes = calculateSupportedRoutes();
-        int newRoute = mAudioState.getRoute();
+        int newRoute = mCallAudioState.getRoute();
         if (bluetoothManager.isBluetoothAudioConnectedOrPending()) {
-            newRoute = AudioState.ROUTE_BLUETOOTH;
-        } else if (mAudioState.getRoute() == AudioState.ROUTE_BLUETOOTH) {
-            newRoute = selectWiredOrEarpiece(AudioState.ROUTE_WIRED_OR_EARPIECE, supportedRoutes);
+            newRoute = CallAudioState.ROUTE_BLUETOOTH;
+        } else if (mCallAudioState.getRoute() == CallAudioState.ROUTE_BLUETOOTH) {
+            newRoute = selectWiredOrEarpiece(CallAudioState.ROUTE_WIRED_OR_EARPIECE,
+                    supportedRoutes);
             // Do not switch to speaker when bluetooth disconnects.
             mWasSpeakerOn = false;
         }
 
-        setSystemAudioState(mAudioState.isMuted(), newRoute, supportedRoutes);
+        setSystemAudioState(mCallAudioState.isMuted(), newRoute, supportedRoutes);
     }
 
     boolean isBluetoothAudioOn() {
@@ -308,10 +310,11 @@
         return mBluetoothManager.isBluetoothAvailable();
     }
 
-    private void saveAudioState(AudioState audioState) {
-        mAudioState = audioState;
-        mStatusBarNotifier.notifyMute(mAudioState.isMuted());
-        mStatusBarNotifier.notifySpeakerphone(mAudioState.getRoute() == AudioState.ROUTE_SPEAKER);
+    private void saveAudioState(CallAudioState callAudioState) {
+        mCallAudioState = callAudioState;
+        mStatusBarNotifier.notifyMute(mCallAudioState.isMuted());
+        mStatusBarNotifier.notifySpeakerphone(mCallAudioState.getRoute()
+                == CallAudioState.ROUTE_SPEAKER);
     }
 
     private void onCallUpdated(Call call) {
@@ -338,35 +341,34 @@
             return;
         }
 
-        AudioState oldAudioState = mAudioState;
-        saveAudioState(new AudioState(isMuted, route, supportedRouteMask));
-        if (!force && Objects.equals(oldAudioState, mAudioState)) {
+        CallAudioState oldAudioState = mCallAudioState;
+        saveAudioState(new CallAudioState(isMuted, route, supportedRouteMask));
+        if (!force && Objects.equals(oldAudioState, mCallAudioState)) {
             return;
         }
-        Log.i(this, "changing audio state from %s to %s", oldAudioState, mAudioState);
+        Log.i(this, "changing audio state from %s to %s", oldAudioState, mCallAudioState);
 
         // Mute.
-        if (mAudioState.isMuted() != mAudioManager.isMicrophoneMute()) {
-            Log.i(this, "changing microphone mute state to: %b", mAudioState.isMuted());
-            mAudioManager.setMicrophoneMute(mAudioState.isMuted());
+        if (mCallAudioState.isMuted() != mAudioManager.isMicrophoneMute()) {
+            Log.i(this, "changing microphone mute state to: %b", mCallAudioState.isMuted());
+            mAudioManager.setMicrophoneMute(mCallAudioState.isMuted());
         }
 
         // Audio route.
-        if (mAudioState.getRoute() == AudioState.ROUTE_BLUETOOTH) {
+        if (mCallAudioState.getRoute() == CallAudioState.ROUTE_BLUETOOTH) {
             turnOnSpeaker(false);
             turnOnBluetooth(true);
-        } else if (mAudioState.getRoute() == AudioState.ROUTE_SPEAKER) {
+        } else if (mCallAudioState.getRoute() == CallAudioState.ROUTE_SPEAKER) {
             turnOnBluetooth(false);
             turnOnSpeaker(true);
-        } else if (mAudioState.getRoute() == AudioState.ROUTE_EARPIECE ||
-                mAudioState.getRoute() == AudioState.ROUTE_WIRED_HEADSET) {
+        } else if (mCallAudioState.getRoute() == CallAudioState.ROUTE_EARPIECE ||
+                mCallAudioState.getRoute() == CallAudioState.ROUTE_WIRED_HEADSET) {
             turnOnBluetooth(false);
             turnOnSpeaker(false);
         }
 
-        if (!oldAudioState.equals(mAudioState)) {
-            mCallsManager
-                    .onAudioStateChanged(oldAudioState, mAudioState);
+        if (!oldAudioState.equals(mCallAudioState)) {
+            mCallsManager.onCallAudioStateChanged(oldAudioState, mCallAudioState);
             updateAudioForForegroundCall();
         }
     }
@@ -490,37 +492,37 @@
         // Since they are mutually exclusive and one is ALWAYS valid, we allow a special input of
         // ROUTE_WIRED_OR_EARPIECE so that callers dont have to make a call to check which is
         // supported before calling setAudioRoute.
-        if (route == AudioState.ROUTE_WIRED_OR_EARPIECE) {
-            route = AudioState.ROUTE_WIRED_OR_EARPIECE & supportedRouteMask;
+        if (route == CallAudioState.ROUTE_WIRED_OR_EARPIECE) {
+            route = CallAudioState.ROUTE_WIRED_OR_EARPIECE & supportedRouteMask;
             if (route == 0) {
                 Log.wtf(this, "One of wired headset or earpiece should always be valid.");
                 // assume earpiece in this case.
-                route = AudioState.ROUTE_EARPIECE;
+                route = CallAudioState.ROUTE_EARPIECE;
             }
         }
         return route;
     }
 
     private int calculateSupportedRoutes() {
-        int routeMask = AudioState.ROUTE_SPEAKER;
+        int routeMask = CallAudioState.ROUTE_SPEAKER;
 
         if (mWiredHeadsetManager.isPluggedIn()) {
-            routeMask |= AudioState.ROUTE_WIRED_HEADSET;
+            routeMask |= CallAudioState.ROUTE_WIRED_HEADSET;
         } else {
-            routeMask |= AudioState.ROUTE_EARPIECE;
+            routeMask |= CallAudioState.ROUTE_EARPIECE;
         }
 
         if (mBluetoothManager.isBluetoothAvailable()) {
-            routeMask |=  AudioState.ROUTE_BLUETOOTH;
+            routeMask |=  CallAudioState.ROUTE_BLUETOOTH;
         }
 
         return routeMask;
     }
 
-    private AudioState getInitialAudioState(Call call) {
+    private CallAudioState getInitialAudioState(Call call) {
         int supportedRouteMask = calculateSupportedRoutes();
         int route = selectWiredOrEarpiece(
-                AudioState.ROUTE_WIRED_OR_EARPIECE, supportedRouteMask);
+                CallAudioState.ROUTE_WIRED_OR_EARPIECE, supportedRouteMask);
 
         // We want the UI to indicate that "bluetooth is in use" in two slightly different cases:
         // (a) The obvious case: if a bluetooth headset is currently in use for an ongoing call.
@@ -535,18 +537,18 @@
                 case CallState.DIALING:
                 case CallState.CONNECTING:
                 case CallState.RINGING:
-                    route = AudioState.ROUTE_BLUETOOTH;
+                    route = CallAudioState.ROUTE_BLUETOOTH;
                     break;
                 default:
                     break;
             }
         }
 
-        return new AudioState(false, route, supportedRouteMask);
+        return new CallAudioState(false, route, supportedRouteMask);
     }
 
     private void setInitialAudioState(Call call, boolean force) {
-        AudioState audioState = getInitialAudioState(call);
+        CallAudioState audioState = getInitialAudioState(call);
         Log.v(this, "setInitialAudioState %s, %s", audioState, call);
         setSystemAudioState(
                 force, audioState.isMuted(), audioState.getRoute(),
@@ -556,7 +558,7 @@
     private void updateAudioForForegroundCall() {
         Call call = mCallsManager.getForegroundCall();
         if (call != null && call.getConnectionService() != null) {
-            call.getConnectionService().onAudioStateChanged(call, mAudioState);
+            call.getConnectionService().onCallAudioStateChanged(call, mCallAudioState);
         }
     }
 
@@ -590,7 +592,7 @@
      * @param pw The {@code IndentingPrintWriter} to write the state to.
      */
     public void dump(IndentingPrintWriter pw) {
-        pw.println("mAudioState: " + mAudioState);
+        pw.println("mAudioState: " + mCallAudioState);
         pw.println("mBluetoothManager:");
         pw.increaseIndent();
         mBluetoothManager.dump(pw);
diff --git a/src/com/android/server/telecom/CallsManager.java b/src/com/android/server/telecom/CallsManager.java
index a638a3e..845fe7f 100644
--- a/src/com/android/server/telecom/CallsManager.java
+++ b/src/com/android/server/telecom/CallsManager.java
@@ -25,6 +25,7 @@
 import android.os.Trace;
 import android.provider.CallLog.Calls;
 import android.telecom.AudioState;
+import android.telecom.CallAudioState;
 import android.telecom.Conference;
 import android.telecom.Connection;
 import android.telecom.DisconnectCause;
@@ -73,7 +74,7 @@
         void onIncomingCallAnswered(Call call);
         void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
         void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
-        void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState);
+        void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
         void onRingbackRequested(Call call, boolean ringback);
         void onIsConferencedChanged(Call call);
         void onIsVoipAudioModeChanged(Call call);
@@ -406,8 +407,8 @@
         return false;
     }
 
-    AudioState getAudioState() {
-        return mCallAudioManager.getAudioState();
+    CallAudioState getAudioState() {
+        return mCallAudioManager.getCallAudioState();
     }
 
     boolean isTtySupported() {
@@ -891,10 +892,10 @@
     }
 
     /** Called when the audio state changes. */
-    void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
+    void onCallAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
         Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
         for (CallsManagerListener listener : mListeners) {
-            listener.onAudioStateChanged(oldAudioState, newAudioState);
+            listener.onCallAudioStateChanged(oldAudioState, newAudioState);
         }
     }
 
@@ -1450,7 +1451,7 @@
      */
     private void maybeMoveToSpeakerPhone(Call call) {
         if (call.getStartWithSpeakerphoneOn()) {
-            setAudioRoute(AudioState.ROUTE_SPEAKER);
+            setAudioRoute(CallAudioState.ROUTE_SPEAKER);
             call.setStartWithSpeakerphoneOn(false);
         }
     }
diff --git a/src/com/android/server/telecom/CallsManagerListenerBase.java b/src/com/android/server/telecom/CallsManagerListenerBase.java
index 6b54709..129f9ae 100644
--- a/src/com/android/server/telecom/CallsManagerListenerBase.java
+++ b/src/com/android/server/telecom/CallsManagerListenerBase.java
@@ -17,6 +17,7 @@
 package com.android.server.telecom;
 
 import android.telecom.AudioState;
+import android.telecom.CallAudioState;
 
 /**
  * Provides a default implementation for listeners of CallsManager.
@@ -54,7 +55,8 @@
     }
 
     @Override
-    public void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
+    public void onCallAudioStateChanged(CallAudioState oldAudioState,
+            CallAudioState newAudioState) {
     }
 
     @Override
diff --git a/src/com/android/server/telecom/ConnectionServiceWrapper.java b/src/com/android/server/telecom/ConnectionServiceWrapper.java
index 82b99cc..a6bac09 100644
--- a/src/com/android/server/telecom/ConnectionServiceWrapper.java
+++ b/src/com/android/server/telecom/ConnectionServiceWrapper.java
@@ -25,6 +25,7 @@
 import android.os.RemoteException;
 import android.os.UserHandle;
 import android.telecom.AudioState;
+import android.telecom.CallAudioState;
 import android.telecom.Connection;
 import android.telecom.ConnectionRequest;
 import android.telecom.ConnectionService;
@@ -714,13 +715,13 @@
         }
     }
 
-    /** @see IConnectionService#onAudioStateChanged(String,AudioState) */
-    void onAudioStateChanged(Call activeCall, AudioState audioState) {
+    /** @see IConnectionService#onCallAudioStateChanged(String,CallAudioState) */
+    void onCallAudioStateChanged(Call activeCall, CallAudioState audioState) {
         final String callId = mCallIdMapper.getCallId(activeCall);
-        if (callId != null && isServiceValid("onAudioStateChanged")) {
+        if (callId != null && isServiceValid("onCallAudioStateChanged")) {
             try {
-                logOutgoing("onAudioStateChanged %s %s", callId, audioState);
-                mServiceInterface.onAudioStateChanged(callId, audioState);
+                logOutgoing("onCallAudioStateChanged %s %s", callId, audioState);
+                mServiceInterface.onCallAudioStateChanged(callId, audioState);
             } catch (RemoteException e) {
             }
         }
diff --git a/src/com/android/server/telecom/InCallController.java b/src/com/android/server/telecom/InCallController.java
index 0855659..ebbc673 100644
--- a/src/com/android/server/telecom/InCallController.java
+++ b/src/com/android/server/telecom/InCallController.java
@@ -31,6 +31,7 @@
 import android.os.Trace;
 import android.os.UserHandle;
 import android.telecom.AudioState;
+import android.telecom.CallAudioState;
 import android.telecom.Connection;
 import android.telecom.DefaultDialerManager;
 import android.telecom.InCallService;
@@ -200,13 +201,14 @@
     }
 
     @Override
-    public void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) {
+    public void onCallAudioStateChanged(CallAudioState oldCallAudioState,
+            CallAudioState newCallAudioState) {
         if (!mInCallServices.isEmpty()) {
-            Log.i(this, "Calling onAudioStateChanged, audioState: %s -> %s", oldAudioState,
-                    newAudioState);
+            Log.i(this, "Calling onAudioStateChanged, audioState: %s -> %s", oldCallAudioState,
+                    newCallAudioState);
             for (IInCallService inCallService : mInCallServices.values()) {
                 try {
-                    inCallService.onAudioStateChanged(newAudioState);
+                    inCallService.onCallAudioStateChanged(newCallAudioState);
                 } catch (RemoteException ignored) {
                 }
             }
@@ -381,7 +383,7 @@
                 } catch (RemoteException ignored) {
                 }
             }
-            onAudioStateChanged(
+            onCallAudioStateChanged(
                     null,
                     mCallsManager.getAudioState());
             onCanAddCallChanged(mCallsManager.canAddCall());
diff --git a/src/com/android/server/telecom/PhoneAccountRegistrar.java b/src/com/android/server/telecom/PhoneAccountRegistrar.java
index 013e048..c908b92 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
@@ -888,13 +889,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);
@@ -940,11 +941,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
@@ -1046,6 +1052,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)
@@ -1062,10 +1069,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);
@@ -1092,6 +1096,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)) {
@@ -1131,6 +1136,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);
                     }
                 }
 
@@ -1170,10 +1178,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();
@@ -1182,7 +1193,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/testapps/src/com/android/server/telecom/testapps/TestConnectionManager.java b/testapps/src/com/android/server/telecom/testapps/TestConnectionManager.java
index a27be39..2982916 100644
--- a/testapps/src/com/android/server/telecom/testapps/TestConnectionManager.java
+++ b/testapps/src/com/android/server/telecom/testapps/TestConnectionManager.java
@@ -18,6 +18,7 @@
 
 import android.net.Uri;
 import android.telecom.AudioState;
+import android.telecom.CallAudioState;
 import android.telecom.Conference;
 import android.telecom.Connection;
 import android.telecom.ConnectionRequest;
@@ -172,8 +173,8 @@
         }
 
         @Override
-        public void onAudioStateChanged(AudioState state) {
-            mRemote.setAudioState(state);
+        public void onCallAudioStateChanged(CallAudioState state) {
+            mRemote.setCallAudioState(state);
         }
 
         private void setState(int state) {
diff --git a/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java b/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java
index b7682b4..72dae74 100644
--- a/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java
+++ b/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java
@@ -242,9 +242,6 @@
             setActive();
         }
 
-        @Override
-        public void onAudioStateChanged(AudioState state) { }
-
         public void setTestVideoCallProvider(TestVideoProvider testVideoCallProvider) {
             mTestVideoCallProvider = testVideoCallProvider;
         }
diff --git a/tests/src/com/android/server/telecom/tests/InCallServiceFixture.java b/tests/src/com/android/server/telecom/tests/InCallServiceFixture.java
index 3c4ff8d..2dd4b97 100644
--- a/tests/src/com/android/server/telecom/tests/InCallServiceFixture.java
+++ b/tests/src/com/android/server/telecom/tests/InCallServiceFixture.java
@@ -25,6 +25,7 @@
 import android.os.IInterface;
 import android.os.RemoteException;
 import android.telecom.AudioState;
+import android.telecom.CallAudioState;
 import android.telecom.ParcelableCall;
 
 import java.util.HashMap;
@@ -37,7 +38,7 @@
 
     public String mLatestCallId;
     public IInCallAdapter mInCallAdapter;
-    public AudioState mAudioState;
+    public CallAudioState mCallAudioState;
     public final Map<String, ParcelableCall> mCallById = new HashMap<>();
     public final Map<String, String> mPostDialById = new HashMap<>();
     public final Map<String, String> mPostDialWaitById = new HashMap<>();
@@ -88,8 +89,8 @@
         }
 
         @Override
-        public void onAudioStateChanged(AudioState audioState) throws RemoteException {
-            mAudioState = audioState;
+        public void onCallAudioStateChanged(CallAudioState audioState) throws RemoteException {
+            mCallAudioState = audioState;
         }
 
         @Override
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);