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

Commit 86c29fe8 authored by Sanket Agarwal's avatar Sanket Agarwal
Browse files

Separate Phone Policy from mechanism

Create a new policy file which interacts using either public APIs (such
as Intents) or uses equivalent functions which are exposed by the
binder. This is an attempt to be able to separate policy such that
different devices can have different policies.

Bug: b/33079907
Test: JUnit tests
Change-Id: I6c5a7ee9eb4e10f5f649794546acde5ec4b297aa
parent b25160eb
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -170,7 +170,7 @@ public class A2dpService extends ProfileService {
        return mStateMachine.getDevicesMatchingConnectionStates(states);
    }

    int getConnectionState(BluetoothDevice device) {
    public int getConnectionState(BluetoothDevice device) {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        return mStateMachine.getConnectionState(device);
    }
+0 −13
Original line number Diff line number Diff line
@@ -235,12 +235,6 @@ final class A2dpStateMachine extends StateMachine {
                    //reject the connection and stay in Disconnected state itself
                    logi("Incoming A2DP rejected");
                    disconnectA2dpNative(getByteAddress(device));
                    // the other profile connection should be initiated
                    AdapterService adapterService = AdapterService.getAdapterService();
                    if (adapterService != null) {
                        adapterService.connectOtherProfile(device,
                                                           AdapterService.PROFILE_CONN_REJECTED);
                    }
                }
                break;
            case CONNECTION_STATE_CONNECTED:
@@ -257,12 +251,6 @@ final class A2dpStateMachine extends StateMachine {
                    //reject the connection and stay in Disconnected state itself
                    logi("Incoming A2DP rejected");
                    disconnectA2dpNative(getByteAddress(device));
                    // the other profile connection should be initiated
                    AdapterService adapterService = AdapterService.getAdapterService();
                    if (adapterService != null) {
                        adapterService.connectOtherProfile(device,
                                                           AdapterService.PROFILE_CONN_REJECTED);
                    }
                }
                break;
            case CONNECTION_STATE_DISCONNECTING:
@@ -820,7 +808,6 @@ final class A2dpStateMachine extends StateMachine {
            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
            mContext.sendBroadcast(intent, ProfileService.BLUETOOTH_PERM);
            log("Connection state " + device + ": " + prevState + "->" + state);
            mService.notifyProfileConnectionStateChanged(device, BluetoothProfile.A2DP, state, prevState);
        }

        @Override
+0 −14
Original line number Diff line number Diff line
@@ -266,12 +266,6 @@ final class A2dpSinkStateMachine extends StateMachine {
                    //reject the connection and stay in Disconnected state itself
                    logi("Incoming A2DP rejected");
                    disconnectA2dpNative(getByteAddress(device));
                    // the other profile connection should be initiated
                    AdapterService adapterService = AdapterService.getAdapterService();
                    if (adapterService != null) {
                        adapterService.connectOtherProfile(device,
                                                           AdapterService.PROFILE_CONN_REJECTED);
                    }
                }
                break;
            case CONNECTION_STATE_CONNECTED:
@@ -288,12 +282,6 @@ final class A2dpSinkStateMachine extends StateMachine {
                    //reject the connection and stay in Disconnected state itself
                    logi("Incoming A2DP rejected");
                    disconnectA2dpNative(getByteAddress(device));
                    // the other profile connection should be initiated
                    AdapterService adapterService = AdapterService.getAdapterService();
                    if (adapterService != null) {
                        adapterService.connectOtherProfile(device,
                                                           AdapterService.PROFILE_CONN_REJECTED);
                    }
                }
                break;
            case CONNECTION_STATE_DISCONNECTING:
@@ -840,8 +828,6 @@ final class A2dpSinkStateMachine extends StateMachine {
//FIXME            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
            mContext.sendBroadcast(intent, ProfileService.BLUETOOTH_PERM);
            log("Connection state " + device + ": " + prevState + "->" + state);
            mService.notifyProfileConnectionStateChanged(device, BluetoothProfile.A2DP_SINK,
                    state, prevState);
        }

        @Override
+40 −0
Original line number Diff line number Diff line
@@ -16,10 +16,17 @@

package com.android.bluetooth.btservice;

import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothA2dpSink;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothHeadsetClient;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.ParcelUuid;
import android.os.UserHandle;
import android.util.Log;
@@ -68,6 +75,24 @@ class AdapterProperties {
    private boolean mIsDebugLogSupported;
    private boolean mIsActivityAndEnergyReporting;

    private 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())) {
                sendConnectionStateChange(BluetoothProfile.HEADSET, intent);
            } else if (BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) {
                sendConnectionStateChange(BluetoothProfile.A2DP, intent);
            } else if (BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED.equals(
                               intent.getAction())) {
                sendConnectionStateChange(BluetoothProfile.HEADSET_CLIENT, intent);
            } else if (BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED.equals(
                               intent.getAction())) {
                sendConnectionStateChange(BluetoothProfile.A2DP_SINK, intent);
            }
        }
    };

    // Lock for all getters and setters.
    // If finer grained locking is needer, more locks
    // can be added here.
@@ -84,6 +109,14 @@ class AdapterProperties {
            mProfileConnectionState.clear();
        }
        mRemoteDevices = remoteDevices;

        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothDevice.ACTION_UUID);
        mService.registerReceiver(mReceiver, filter);
    }

    public void cleanup() {
@@ -92,6 +125,7 @@ class AdapterProperties {
            mProfileConnectionState.clear();
            mProfileConnectionState = null;
        }
        mService.unregisterReceiver(mReceiver);
        mService = null;
        mBondedDevices.clear();
    }
@@ -319,6 +353,12 @@ class AdapterProperties {
        return mDiscovering;
    }

    private void sendConnectionStateChange(int profile, Intent connIntent) {
        BluetoothDevice device = connIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        int prevState = connIntent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, -1);
        int state = connIntent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
        sendConnectionStateChange(device, profile, state, prevState);
    }
    void sendConnectionStateChange(BluetoothDevice device, int profile, int state, int prevState) {
        if (!validateProfileConnectionState(state) ||
                !validateProfileConnectionState(prevState)) {
+92 −549

File changed.

Preview size limit exceeded, changes collapsed.

Loading