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

Commit ca63ff0e authored by Hui Yu's avatar Hui Yu
Browse files

Use DeviceConfig to set bluetooth broadcasts temp allowlist duration.

The default temp allowlist duration is 10000 ms. Use DeviceConfig
namespace "bluetooth" key "temp_allow_list_duration_ms" to change the
duration.

Bug: 182816627
Test: adb shell device_config put bluetooth temp_allow_list_duration_ms 10000
Test: adb shell device_config get bluetooth temp_allow_list_duration_ms
Change-Id: Ia09e98aa13f771749500739581c0c8ad5a39d5a6
parent 2b288b84
Loading
Loading
Loading
Loading
+11 −12
Original line number Diff line number Diff line
@@ -58,6 +58,7 @@ import android.os.RemoteException;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.DeviceConfig;
import android.provider.Telephony;
import android.util.Log;

@@ -87,22 +88,14 @@ public final class Utils {
    private static final String TAG = "BluetoothUtils";
    private static final int MICROS_PER_UNIT = 625;
    private static final String PTS_TEST_MODE_PROPERTY = "persist.bluetooth.pts";
    private static final String KEY_TEMP_ALLOW_LIST_DURATION_MS = "temp_allow_list_duration_ms";
    private static final long DEFAULT_TEMP_ALLOW_LIST_DURATION_MS = 10_000;

    static final int BD_ADDR_LEN = 6; // bytes
    static final int BD_UUID_LEN = 16; // bytes

    public static final Bundle sTempAllowlistBroadcastOptions;
    static {
        final long durationMs = 10_000;
        final BroadcastOptions bOptions = BroadcastOptions.makeBasic();
        bOptions.setTemporaryAppAllowlist(durationMs,
                TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED,
                PowerExemptionManager.REASON_BLUETOOTH_BROADCAST, "");
        sTempAllowlistBroadcastOptions = bOptions.toBundle();
    }

    /*
     * Special characters
     * Special character
     *
     * (See "What is a phone number?" doc)
     * 'p' --- GSM pause character, same as comma
@@ -929,6 +922,12 @@ public final class Utils {
    }

    public static @NonNull Bundle getTempAllowlistBroadcastOptions() {
        return sTempAllowlistBroadcastOptions;
        final long durationMs = DeviceConfig.getLong(DeviceConfig.NAMESPACE_BLUETOOTH,
                KEY_TEMP_ALLOW_LIST_DURATION_MS, DEFAULT_TEMP_ALLOW_LIST_DURATION_MS);
        final BroadcastOptions bOptions = BroadcastOptions.makeBasic();
        bOptions.setTemporaryAppAllowlist(durationMs,
                TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED,
                PowerExemptionManager.REASON_BLUETOOTH_BROADCAST, "");
        return bOptions.toBundle();
    }
}
+5 −6
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Looper;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaControllerCompat;
@@ -40,10 +41,8 @@ import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.R;
import com.android.bluetooth.TestUtils;
import com.android.bluetooth.Utils;
import com.android.bluetooth.a2dpsink.A2dpSinkService;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.btservice.storage.DatabaseManager;

import org.hamcrest.core.IsInstanceOf;
@@ -200,7 +199,7 @@ public class AvrcpControllerStateMachineTest {
        TestUtils.waitForLooperToFinishScheduledTask(mAvrcpStateMachine.getHandler().getLooper());
        verify(mAvrcpControllerService, timeout(ASYNC_CALL_TIMEOUT_MILLIS).times(2)).sendBroadcast(
                mIntentArgument.capture(), eq(BLUETOOTH_CONNECT),
                same(Utils.sTempAllowlistBroadcastOptions));
                any(Bundle.class));
        Assert.assertThat(mAvrcpStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(AvrcpControllerStateMachine.Connected.class));
        Assert.assertEquals(mAvrcpStateMachine.getState(), BluetoothProfile.STATE_CONNECTED);
@@ -332,7 +331,7 @@ public class AvrcpControllerStateMachineTest {
        verify(mAvrcpControllerService,
                timeout(ASYNC_CALL_TIMEOUT_MILLIS).times(numBroadcastsSent)).sendBroadcast(
                mIntentArgument.capture(), eq(BLUETOOTH_CONNECT),
                same(Utils.sTempAllowlistBroadcastOptions));
                any(Bundle.class));
        Assert.assertEquals(mTestDevice, mIntentArgument.getValue().getParcelableExtra(
                BluetoothDevice.EXTRA_DEVICE));
        Assert.assertEquals(BluetoothAvrcpController.ACTION_CONNECTION_STATE_CHANGED,
@@ -361,7 +360,7 @@ public class AvrcpControllerStateMachineTest {
        verify(mAvrcpControllerService,
                timeout(ASYNC_CALL_TIMEOUT_MILLIS).times(numBroadcastsSent)).sendBroadcast(
                mIntentArgument.capture(), eq(BLUETOOTH_CONNECT),
                same(Utils.sTempAllowlistBroadcastOptions));
                any(Bundle.class));
        Assert.assertEquals(mTestDevice, mIntentArgument.getValue().getParcelableExtra(
                BluetoothDevice.EXTRA_DEVICE));
        Assert.assertEquals(BluetoothAvrcpController.ACTION_CONNECTION_STATE_CHANGED,
@@ -390,7 +389,7 @@ public class AvrcpControllerStateMachineTest {
        verify(mAvrcpControllerService,
                timeout(ASYNC_CALL_TIMEOUT_MILLIS).times(numBroadcastsSent)).sendBroadcast(
                mIntentArgument.capture(), eq(BLUETOOTH_CONNECT),
                same(Utils.sTempAllowlistBroadcastOptions));
                any(Bundle.class));
        Assert.assertEquals(mTestDevice, mIntentArgument.getValue().getParcelableExtra(
                BluetoothDevice.EXTRA_DEVICE));
        Assert.assertEquals(BluetoothAvrcpController.ACTION_CONNECTION_STATE_CHANGED,
+2 −1
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import android.content.pm.PermissionInfo;
import android.content.res.Resources;
import android.media.AudioManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.Looper;
import android.os.PowerManager;
import android.os.Process;
@@ -246,7 +247,7 @@ public class AdapterServiceTest {

        verify(mMockContext, timeout(CONTEXT_SWITCH_MS).times(2 * invocationNumber + 2))
                .sendBroadcast(any(), eq(BLUETOOTH_SCAN),
                        same(Utils.sTempAllowlistBroadcastOptions));
                        any(Bundle.class));
        final int scanMode = mServiceBinder.getScanMode(mAttributionSource);
        Assert.assertTrue(scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE
                || scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
+1 −2
Original line number Diff line number Diff line
@@ -32,7 +32,6 @@ import androidx.test.filters.MediumTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.TestUtils;
import com.android.bluetooth.Utils;

import org.junit.After;
import org.junit.Assert;
@@ -286,7 +285,7 @@ public class BondStateMachineTest {
        if (shouldBroadcast) {
            verify(mAdapterService, times(++mVerifyCount)).sendBroadcastAsUser(
                    intentArgument.capture(), eq(UserHandle.ALL),
                    eq(BLUETOOTH_CONNECT), same(Utils.sTempAllowlistBroadcastOptions));
                    eq(BLUETOOTH_CONNECT), any(Bundle.class));
            verifyBondStateChangeIntent(broadcastOldState, broadcastNewState,
                    intentArgument.getValue());
        } else {
+15 −14
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothProfile;
import android.content.Intent;
import android.os.Bundle;
import android.os.HandlerThread;
import android.os.Message;
import android.os.TestLooperManager;
@@ -85,7 +86,7 @@ public class RemoteDevicesTest {
        // Verify that updating battery level triggers ACTION_BATTERY_LEVEL_CHANGED intent
        mRemoteDevices.updateBatteryLevel(mDevice1, batteryLevel);
        verify(mAdapterService).sendBroadcast(mIntentArgument.capture(), mStringArgument.capture(),
                same(Utils.sTempAllowlistBroadcastOptions));
                any(Bundle.class));
        verifyBatteryLevelChangedIntent(mDevice1, batteryLevel, mIntentArgument);
        Assert.assertEquals(BLUETOOTH_CONNECT, mStringArgument.getValue());

@@ -102,7 +103,7 @@ public class RemoteDevicesTest {
        batteryLevel = 15;
        mRemoteDevices.updateBatteryLevel(mDevice1, batteryLevel);
        verify(mAdapterService, times(2)).sendBroadcast(mIntentArgument.capture(),
                mStringArgument.capture(), same(Utils.sTempAllowlistBroadcastOptions));
                mStringArgument.capture(), any(Bundle.class));
        Assert.assertEquals(BLUETOOTH_CONNECT, mStringArgument.getValue());
        verifyBatteryLevelChangedIntent(mDevice1, batteryLevel, mIntentArgument);

@@ -169,7 +170,7 @@ public class RemoteDevicesTest {
        // Verify that updating battery level triggers ACTION_BATTERY_LEVEL_CHANGED intent
        mRemoteDevices.updateBatteryLevel(mDevice1, batteryLevel);
        verify(mAdapterService).sendBroadcast(mIntentArgument.capture(), mStringArgument.capture(),
                same(Utils.sTempAllowlistBroadcastOptions));
                any(Bundle.class));
        verifyBatteryLevelChangedIntent(mDevice1, batteryLevel, mIntentArgument);
        Assert.assertEquals(BLUETOOTH_CONNECT, mStringArgument.getValue());

@@ -183,7 +184,7 @@ public class RemoteDevicesTest {
        mRemoteDevices.resetBatteryLevel(mDevice1);
        // Verify BATTERY_LEVEL_CHANGED intent is sent after first reset
        verify(mAdapterService, times(2)).sendBroadcast(mIntentArgument.capture(),
                mStringArgument.capture(), same(Utils.sTempAllowlistBroadcastOptions));
                mStringArgument.capture(), any(Bundle.class));
        verifyBatteryLevelChangedIntent(mDevice1, BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
                mIntentArgument);
        Assert.assertEquals(BLUETOOTH_CONNECT, mStringArgument.getValue());
@@ -200,7 +201,7 @@ public class RemoteDevicesTest {
        // Verify that updating battery level triggers ACTION_BATTERY_LEVEL_CHANGED intent again
        mRemoteDevices.updateBatteryLevel(mDevice1, batteryLevel);
        verify(mAdapterService, times(3)).sendBroadcast(mIntentArgument.capture(),
                mStringArgument.capture(), same(Utils.sTempAllowlistBroadcastOptions));
                mStringArgument.capture(), any(Bundle.class));
        verifyBatteryLevelChangedIntent(mDevice1, batteryLevel, mIntentArgument);
        Assert.assertEquals(BLUETOOTH_CONNECT, mStringArgument.getValue());

@@ -217,7 +218,7 @@ public class RemoteDevicesTest {
        // Verify that updating battery level triggers ACTION_BATTERY_LEVEL_CHANGED intent
        mRemoteDevices.updateBatteryLevel(mDevice1, batteryLevel);
        verify(mAdapterService).sendBroadcast(mIntentArgument.capture(), mStringArgument.capture(),
                same(Utils.sTempAllowlistBroadcastOptions));
                any(Bundle.class));
        verifyBatteryLevelChangedIntent(mDevice1, batteryLevel, mIntentArgument);
        Assert.assertEquals(BLUETOOTH_CONNECT, mStringArgument.getValue());

@@ -233,7 +234,7 @@ public class RemoteDevicesTest {
                        BluetoothProfile.STATE_DISCONNECTING, BluetoothProfile.STATE_DISCONNECTED));
        // Verify BATTERY_LEVEL_CHANGED intent is sent after first reset
        verify(mAdapterService, times(2)).sendBroadcast(mIntentArgument.capture(),
                mStringArgument.capture(), same(Utils.sTempAllowlistBroadcastOptions));
                mStringArgument.capture(), any(Bundle.class));
        verifyBatteryLevelChangedIntent(mDevice1, BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
                mIntentArgument);
        Assert.assertEquals(BLUETOOTH_CONNECT, mStringArgument.getValue());
@@ -245,7 +246,7 @@ public class RemoteDevicesTest {
        // Verify that updating battery level triggers ACTION_BATTERY_LEVEL_CHANGED intent again
        mRemoteDevices.updateBatteryLevel(mDevice1, batteryLevel);
        verify(mAdapterService, times(3)).sendBroadcast(mIntentArgument.capture(),
                mStringArgument.capture(), same(Utils.sTempAllowlistBroadcastOptions));
                mStringArgument.capture(), any(Bundle.class));
        verifyBatteryLevelChangedIntent(mDevice1, batteryLevel, mIntentArgument);
        Assert.assertEquals(BLUETOOTH_CONNECT, mStringArgument.getValue());

@@ -262,7 +263,7 @@ public class RemoteDevicesTest {
        // Verify that updating battery level triggers ACTION_BATTERY_LEVEL_CHANGED intent
        mRemoteDevices.updateBatteryLevel(mDevice1, batteryLevel);
        verify(mAdapterService).sendBroadcast(mIntentArgument.capture(), mStringArgument.capture(),
                same(Utils.sTempAllowlistBroadcastOptions));
                any(Bundle.class));
        verifyBatteryLevelChangedIntent(mDevice1, batteryLevel, mIntentArgument);
        Assert.assertEquals(BLUETOOTH_CONNECT, mStringArgument.getValue());

@@ -278,7 +279,7 @@ public class RemoteDevicesTest {
                AbstractionLayer.BT_ACL_STATE_DISCONNECTED, 19); // HCI code 19 remote terminated
        // Verify ACTION_ACL_DISCONNECTED and BATTERY_LEVEL_CHANGED intent are sent
        verify(mAdapterService, times(3)).sendBroadcast(mIntentArgument.capture(),
                mStringArgument.capture(), same(Utils.sTempAllowlistBroadcastOptions));
                mStringArgument.capture(), any(Bundle.class));
        verify(mAdapterService, times(2)).obfuscateAddress(mDevice1);
        verifyBatteryLevelChangedIntent(mDevice1, BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
                mIntentArgument.getAllValues().get(mIntentArgument.getAllValues().size() - 2));
@@ -295,7 +296,7 @@ public class RemoteDevicesTest {
        // Verify that updating battery level triggers ACTION_BATTERY_LEVEL_CHANGED intent again
        mRemoteDevices.updateBatteryLevel(mDevice1, batteryLevel);
        verify(mAdapterService, times(4)).sendBroadcast(mIntentArgument.capture(),
                mStringArgument.capture(), same(Utils.sTempAllowlistBroadcastOptions));
                mStringArgument.capture(), any(Bundle.class));
        verifyBatteryLevelChangedIntent(mDevice1, batteryLevel, mIntentArgument);
        Assert.assertEquals(BLUETOOTH_CONNECT, mStringArgument.getValue());
    }
@@ -311,7 +312,7 @@ public class RemoteDevicesTest {
        mRemoteDevices.onHfIndicatorValueChanged(getHfIndicatorIntent(mDevice1, batteryLevel,
                HeadsetHalConstants.HF_INDICATOR_BATTERY_LEVEL_STATUS));
        verify(mAdapterService).sendBroadcast(mIntentArgument.capture(), mStringArgument.capture(),
                same(Utils.sTempAllowlistBroadcastOptions));
                any(Bundle.class));
        verifyBatteryLevelChangedIntent(mDevice1, batteryLevel, mIntentArgument);
        Assert.assertEquals(BLUETOOTH_CONNECT, mStringArgument.getValue());
    }
@@ -341,7 +342,7 @@ public class RemoteDevicesTest {
                BluetoothAssignedNumbers.PLANTRONICS, BluetoothHeadset.AT_CMD_TYPE_SET,
                getXEventArray(3, 8), mDevice1));
        verify(mAdapterService).sendBroadcast(mIntentArgument.capture(), mStringArgument.capture(),
                same(Utils.sTempAllowlistBroadcastOptions));
                any(Bundle.class));
        verifyBatteryLevelChangedIntent(mDevice1, 42, mIntentArgument);
        Assert.assertEquals(BLUETOOTH_CONNECT, mStringArgument.getValue());
    }
@@ -364,7 +365,7 @@ public class RemoteDevicesTest {
                        10
                }, mDevice1));
        verify(mAdapterService).sendBroadcast(mIntentArgument.capture(), mStringArgument.capture(),
                same(Utils.sTempAllowlistBroadcastOptions));
                any(Bundle.class));
        verifyBatteryLevelChangedIntent(mDevice1, 60, mIntentArgument);
        Assert.assertEquals(BLUETOOTH_CONNECT, mStringArgument.getValue());
    }
Loading