Merge "Convert the Bluetooth NetworkAgent to the @SystemApi way."
diff --git a/src/com/android/bluetooth/hearingaid/HearingAidStateMachine.java b/src/com/android/bluetooth/hearingaid/HearingAidStateMachine.java
index 2d41399..de1b4ee 100644
--- a/src/com/android/bluetooth/hearingaid/HearingAidStateMachine.java
+++ b/src/com/android/bluetooth/hearingaid/HearingAidStateMachine.java
@@ -81,6 +81,7 @@
     private Connecting mConnecting;
     private Disconnecting mDisconnecting;
     private Connected mConnected;
+    private int mConnectionState = BluetoothProfile.STATE_DISCONNECTED;
     private int mLastConnectionState = -1;
 
     private HearingAidService mService;
@@ -132,6 +133,7 @@
         public void enter() {
             Log.i(TAG, "Enter Disconnected(" + mDevice + "): " + messageWhatToString(
                     getCurrentMessage().what));
+            mConnectionState = BluetoothProfile.STATE_DISCONNECTED;
 
             removeDeferredMessages(DISCONNECT);
 
@@ -239,6 +241,7 @@
             Log.i(TAG, "Enter Connecting(" + mDevice + "): "
                     + messageWhatToString(getCurrentMessage().what));
             sendMessageDelayed(CONNECT_TIMEOUT, sConnectTimeoutMs);
+            mConnectionState = BluetoothProfile.STATE_CONNECTING;
             broadcastConnectionState(BluetoothProfile.STATE_CONNECTING, mLastConnectionState);
         }
 
@@ -329,6 +332,7 @@
             Log.i(TAG, "Enter Disconnecting(" + mDevice + "): "
                     + messageWhatToString(getCurrentMessage().what));
             sendMessageDelayed(CONNECT_TIMEOUT, sConnectTimeoutMs);
+            mConnectionState = BluetoothProfile.STATE_DISCONNECTING;
             broadcastConnectionState(BluetoothProfile.STATE_DISCONNECTING, mLastConnectionState);
         }
 
@@ -426,6 +430,7 @@
         public void enter() {
             Log.i(TAG, "Enter Connected(" + mDevice + "): "
                     + messageWhatToString(getCurrentMessage().what));
+            mConnectionState = BluetoothProfile.STATE_CONNECTED;
             removeDeferredMessages(CONNECT);
             broadcastConnectionState(BluetoothProfile.STATE_CONNECTED, mLastConnectionState);
         }
@@ -496,20 +501,7 @@
     }
 
     int getConnectionState() {
-        String currentState = getCurrentState().getName();
-        switch (currentState) {
-            case "Disconnected":
-                return BluetoothProfile.STATE_DISCONNECTED;
-            case "Connecting":
-                return BluetoothProfile.STATE_CONNECTING;
-            case "Connected":
-                return BluetoothProfile.STATE_CONNECTED;
-            case "Disconnecting":
-                return BluetoothProfile.STATE_DISCONNECTING;
-            default:
-                Log.e(TAG, "Bad currentState: " + currentState);
-                return BluetoothProfile.STATE_DISCONNECTED;
-        }
+        return mConnectionState;
     }
 
     BluetoothDevice getDevice() {
@@ -517,7 +509,7 @@
     }
 
     synchronized boolean isConnected() {
-        return getCurrentState() == mConnected;
+        return (getConnectionState() == BluetoothProfile.STATE_CONNECTED);
     }
 
     // This method does not check for error condition (newState == prevState)
diff --git a/src/com/android/bluetooth/hfp/HeadsetStateMachine.java b/src/com/android/bluetooth/hfp/HeadsetStateMachine.java
index 1eb9af6..8a6bc55 100644
--- a/src/com/android/bluetooth/hfp/HeadsetStateMachine.java
+++ b/src/com/android/bluetooth/hfp/HeadsetStateMachine.java
@@ -125,6 +125,7 @@
     private final AudioConnecting mAudioConnecting = new AudioConnecting();
     private final AudioDisconnecting mAudioDisconnecting = new AudioDisconnecting();
     private HeadsetStateBase mPrevState;
+    private HeadsetStateBase mCurrentState;
 
     // Run time dependencies
     private final HeadsetService mHeadsetService;
@@ -222,7 +223,7 @@
 
     public void dump(StringBuilder sb) {
         ProfileService.println(sb, "  mCurrentDevice: " + mDevice);
-        ProfileService.println(sb, "  mCurrentState: " + getCurrentState());
+        ProfileService.println(sb, "  mCurrentState: " + mCurrentState);
         ProfileService.println(sb, "  mPrevState: " + mPrevState);
         ProfileService.println(sb, "  mConnectionState: " + getConnectionState());
         ProfileService.println(sb, "  mAudioState: " + getAudioState());
@@ -253,6 +254,7 @@
     private abstract class HeadsetStateBase extends State {
         @Override
         public void enter() {
+            mCurrentState = this;
             // Crash if mPrevState is null and state is not Disconnected
             if (!(this instanceof Disconnected) && mPrevState == null) {
                 throw new IllegalStateException("mPrevState is null on enter()");
@@ -1439,11 +1441,10 @@
      */
     @VisibleForTesting
     public synchronized int getConnectionState() {
-        HeadsetStateBase state = (HeadsetStateBase) getCurrentState();
-        if (state == null) {
+        if (mCurrentState == null) {
             return BluetoothHeadset.STATE_DISCONNECTED;
         }
-        return state.getConnectionStateInt();
+        return mCurrentState.getConnectionStateInt();
     }
 
     /**
@@ -1454,11 +1455,10 @@
      * {@link BluetoothHeadset#STATE_AUDIO_CONNECTED}
      */
     public synchronized int getAudioState() {
-        HeadsetStateBase state = (HeadsetStateBase) getCurrentState();
-        if (state == null) {
+        if (mCurrentState == null) {
             return BluetoothHeadset.STATE_AUDIO_DISCONNECTED;
         }
-        return state.getAudioStateInt();
+        return mCurrentState.getAudioStateInt();
     }
 
     public long getConnectingTimestampMs() {
@@ -1623,7 +1623,7 @@
         }
         if (volumeType == HeadsetHalConstants.VOLUME_TYPE_SPK) {
             mSpeakerVolume = volume;
-            int flag = (getCurrentState() == mAudioOn) ? AudioManager.FLAG_SHOW_UI : 0;
+            int flag = (mCurrentState == mAudioOn) ? AudioManager.FLAG_SHOW_UI : 0;
             mSystemInterface.getAudioManager()
                     .setStreamVolume(AudioManager.STREAM_BLUETOOTH_SCO, volume, flag);
         } else if (volumeType == HeadsetHalConstants.VOLUME_TYPE_MIC) {