Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 90a37c87 authored by Jack He's avatar Jack He
Browse files

PhonePolicy: Logging and logic refactor

* Do not log arrays by reference
* Do not log intent by reference

Bug: 32998946
Test: make, no functional change
Change-Id: If1d20c1d7ff221a27b0b516a37c919d192efb363
(cherry picked from commit 072c8fa9)
parent 19547931
Loading
Loading
Loading
Loading
+83 −69
Original line number Diff line number Diff line
@@ -78,8 +78,6 @@ class PhonePolicy {
    final private static int MESSAGE_CONNECT_OTHER_PROFILES = 3;
    final private static int MESSAGE_ADAPTER_STATE_TURNED_ON = 4;

    public static final int PROFILE_CONN_CONNECTED = 1;

    // Timeouts
    final private static int CONNECT_OTHER_PROFILES_TIMEOUT = 6000; // 6s

@@ -91,28 +89,40 @@ class PhonePolicy {
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "Received intent " + intent);
            if (BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) {
            String action = intent.getAction();
            if (action == null) {
                errorLog("Received intent with null action");
                return;
            }
            switch (action) {
                case BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED:
                    mHandler.obtainMessage(MESSAGE_PROFILE_CONNECTION_STATE_CHANGED,
                                    BluetoothProfile.HEADSET,
                                    -1, // No-op argument
                                    intent)
                            .sendToTarget();
            } else if (BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) {
                    break;
                case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:
                    mHandler.obtainMessage(MESSAGE_PROFILE_CONNECTION_STATE_CHANGED,
                                    BluetoothProfile.A2DP,
                                    -1, // No-op argument
                                    intent)
                            .sendToTarget();
            } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
                    break;
                case BluetoothAdapter.ACTION_STATE_CHANGED:
                    // Only pass the message on if the adapter has actually changed state from
                    // non-ON to ON. NOTE: ON is the state depicting BREDR ON and not just BLE ON.
                    int newState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
                    if (newState == BluetoothAdapter.STATE_ON) {
                        mHandler.obtainMessage(MESSAGE_ADAPTER_STATE_TURNED_ON).sendToTarget();
                    }
            } else if (BluetoothDevice.ACTION_UUID.equals(intent.getAction())) {
                    break;
                case BluetoothDevice.ACTION_UUID:
                    mHandler.obtainMessage(MESSAGE_PROFILE_INIT_PRIORITIES, intent).sendToTarget();
                    break;
                default:
                    Log.e(TAG, "Received unexpected intent, action=" + action);
                    break;
            }
        }
    };
@@ -132,17 +142,16 @@ class PhonePolicy {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_PROFILE_INIT_PRIORITIES: {
                    Intent intent = (Intent) msg.obj;
                    BluetoothDevice device =
                            (BluetoothDevice) ((Intent) msg.obj)
                                    .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    Parcelable[] uuids =
                            ((Intent) msg.obj).getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);

                    Log.d(TAG, "UUIDs on ACTION_UUID: " + uuids + " for device " + device);
                            intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    Parcelable[] uuids = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
                    debugLog("Received ACTION_UUID for device " + device);
                    if (uuids != null) {
                        ParcelUuid[] uuidsToSend = new ParcelUuid[uuids.length];
                        for (int i = 0; i < uuidsToSend.length; i++) {
                            uuidsToSend[i] = (ParcelUuid) uuids[i];
                            debugLog("index=" + i + "uuid=" + uuidsToSend[i]);
                        }
                        processInitProfilePriorities(device, uuidsToSend);
                    }
@@ -192,7 +201,7 @@ class PhonePolicy {

    // Policy implementation, all functions MUST be private
    private void processInitProfilePriorities(BluetoothDevice device, ParcelUuid[] uuids) {
        debugLog("processInitProfilePriorities() - device " + device + " UUIDs " + uuids);
        debugLog("processInitProfilePriorities() - device " + device);
        HidService hidService = mFactory.getHidService();
        A2dpService a2dpService = mFactory.getA2dpService();
        HeadsetService headsetService = mFactory.getHeadsetService();
@@ -235,11 +244,10 @@ class PhonePolicy {

    private void processProfileStateChanged(
            BluetoothDevice device, int profileId, int nextState, int prevState) {
        // Profiles relevant to phones.
        debugLog("processProfileStateChanged, device=" + device + ", profile=" + profileId + ", "
                + prevState + " -> " + nextState);
        if (((profileId == BluetoothProfile.A2DP) || (profileId == BluetoothProfile.HEADSET))
                && (nextState == BluetoothProfile.STATE_CONNECTED)) {
            debugLog("Profile connected id: " + profileId
                    + " Schedule missing profile connection if any");
            connectOtherProfile(device);
            setProfileAutoConnectionPriority(device, profileId);
        }
@@ -251,7 +259,7 @@ class PhonePolicy {
            return;
        }

        if (mAdapterService.isQuietModeEnabled() == false) {
        if (!mAdapterService.isQuietModeEnabled()) {
            debugLog("autoConnect() - Initiate auto connection on BT on...");
            // Phone profiles.
            autoConnectHeadset();
@@ -262,45 +270,48 @@ class PhonePolicy {
    }

    private void autoConnectHeadset() {
        HeadsetService hsService = mFactory.getHeadsetService();
        final HeadsetService hsService = mFactory.getHeadsetService();
        if (hsService == null) {
            errorLog("autoConnectHeadset() - service is null");
            errorLog("autoConnectHeadset, service is null");
            return;
        }

        BluetoothDevice bondedDevices[] = mAdapterService.getBondedDevices();
        final BluetoothDevice bondedDevices[] = mAdapterService.getBondedDevices();
        if (bondedDevices == null) {
            errorLog("autoConnectHeadset() - devices are null");
            errorLog("autoConnectHeadset, bondedDevices are null");
            return;
        }

        debugLog("autoConnectHeadset() - bondedDevices: " + bondedDevices);
        for (BluetoothDevice device : bondedDevices) {
            debugLog("autoConnectHeadset() - attempt autoconnect with device " + device);
            debugLog("autoConnectHeadset, attempt auto-connect with device " + device);
            if (hsService.getPriority(device) == BluetoothProfile.PRIORITY_AUTO_CONNECT) {
                debugLog("autoConnectHeadset() - Connecting HFP with " + device.toString());
                debugLog("autoConnectHeadset, Connecting HFP with " + device);
                hsService.connect(device);
            }
        }
    }

    private void autoConnectA2dp() {
        A2dpService a2dpSservice = mFactory.getA2dpService();
        BluetoothDevice bondedDevices[] = mAdapterService.getBondedDevices();
        if ((bondedDevices == null) || (a2dpSservice == null)) {
        final A2dpService a2dpService = mFactory.getA2dpService();
        if (a2dpService == null) {
            errorLog("autoConnectA2dp, service is null");
            return;
        }
        final BluetoothDevice bondedDevices[] = mAdapterService.getBondedDevices();
        if (bondedDevices == null) {
            errorLog("autoConnectA2dp, bondedDevices are null");
            return;
        }
        for (BluetoothDevice device : bondedDevices) {
            if (a2dpSservice.getPriority(device) == BluetoothProfile.PRIORITY_AUTO_CONNECT) {
                debugLog("autoConnectA2dp() - Connecting A2DP with " + device.toString());
                a2dpSservice.connect(device);
            debugLog("autoConnectA2dp, attempt auto-connect with device " + device);
            if (a2dpService.getPriority(device) == BluetoothProfile.PRIORITY_AUTO_CONNECT) {
                debugLog("autoConnectA2dp, connecting A2DP with " + device);
                a2dpService.connect(device);
            }
        }
    }

    public void connectOtherProfile(BluetoothDevice device) {
        if ((mHandler.hasMessages(MESSAGE_CONNECT_OTHER_PROFILES) == false)
                && (mAdapterService.isQuietModeEnabled() == false)) {
    private void connectOtherProfile(BluetoothDevice device) {
        if ((!mHandler.hasMessages(MESSAGE_CONNECT_OTHER_PROFILES))
                && (!mAdapterService.isQuietModeEnabled())) {
            Message m = mHandler.obtainMessage(MESSAGE_CONNECT_OTHER_PROFILES);
            m.obj = device;
            mHandler.sendMessageDelayed(m, CONNECT_OTHER_PROFILES_TIMEOUT);
@@ -312,8 +323,9 @@ class PhonePolicy {
    // connect to the device that initiated the connection.  In the event that this function is
    // invoked and there are no current bluetooth connections no new profiles will be connected.
    private void processConnectOtherProfiles(BluetoothDevice device) {
        debugLog("processConnectOtherProfiles() - device " + device);
        debugLog("processConnectOtherProfiles, device=" + device);
        if (mAdapterService.getState() != BluetoothAdapter.STATE_ON) {
            warnLog("processConnectOtherProfiles, adapter is not ON " + mAdapterService.getState());
            return;
        }
        HeadsetService hsService = mFactory.getHeadsetService();
@@ -338,10 +350,8 @@ class PhonePolicy {
            allProfilesEmpty = allProfilesEmpty && panConnDevList.isEmpty();
        }

        debugLog("processConnectOtherProfiles() - allProfilesEmpty " + allProfilesEmpty + " device "
                + device);

        if (allProfilesEmpty) {
            debugLog("processConnectOtherProfiles, all profiles disconnected for " + device);
            // must have connected then disconnected, don't bother connecting others.
            return;
        }
@@ -351,7 +361,7 @@ class PhonePolicy {
                    && (hsService.getPriority(device) >= BluetoothProfile.PRIORITY_ON)
                    && (hsService.getConnectionState(device)
                               == BluetoothProfile.STATE_DISCONNECTED)) {
                debugLog("Retrying connection to HS with device " + device);
                debugLog("Retrying connection to Headset with device " + device);
                hsService.connect(device);
            }
        }
@@ -369,21 +379,13 @@ class PhonePolicy {
                    && (panService.getPriority(device) >= BluetoothProfile.PRIORITY_ON)
                    && (panService.getConnectionState(device)
                               == BluetoothProfile.STATE_DISCONNECTED)) {
                debugLog("Retrying connection to HF with device " + device);
                debugLog("Retrying connection to PAN with device " + device);
                panService.connect(device);
            }
        }
    }

    private void debugLog(String msg) {
        if (DBG) Log.d(TAG, msg);
    }

    private void errorLog(String msg) {
        Log.e(TAG, msg);
    }

    void setProfileAutoConnectionPriority(BluetoothDevice device, int profileId) {
    private void setProfileAutoConnectionPriority(BluetoothDevice device, int profileId) {
        switch (profileId) {
            case BluetoothProfile.HEADSET:
                HeadsetService hsService = mFactory.getHeadsetService();
@@ -406,7 +408,7 @@ class PhonePolicy {
                break;

            default:
                Log.w(TAG, "Attempting to set Auto Connect priority on invalid profile");
                Log.w(TAG, "Tried to set AutoConnect priority on invalid profile " + profileId);
                break;
        }
    }
@@ -430,4 +432,16 @@ class PhonePolicy {
            }
        }
    }

    private static void debugLog(String msg) {
        if (DBG) Log.d(TAG, msg);
    }

    private static void warnLog(String msg) {
        Log.w(TAG, msg);
    }

    private static void errorLog(String msg) {
        Log.e(TAG, msg);
    }
}