diff --git a/nearby/service/java/com/android/server/nearby/NearbyConfiguration.java b/nearby/service/java/com/android/server/nearby/NearbyConfiguration.java index 76d6942593..4c8067f3a6 100644 --- a/nearby/service/java/com/android/server/nearby/NearbyConfiguration.java +++ b/nearby/service/java/com/android/server/nearby/NearbyConfiguration.java @@ -22,6 +22,8 @@ import android.provider.DeviceConfig; import androidx.annotation.NonNull; import com.android.internal.annotations.GuardedBy; +import com.android.modules.utils.build.SdkLevel; +import com.android.server.nearby.managers.DiscoveryProviderManager; import java.util.concurrent.Executors; @@ -46,6 +48,12 @@ public class NearbyConfiguration { */ public static final String NEARBY_SUPPORT_TEST_APP = "nearby_support_test_app"; + /** + * Flag to control which version of DiscoveryProviderManager should be used. + */ + public static final String NEARBY_REFACTOR_DISCOVERY_MANAGER = + "nearby_refactor_discovery_manager"; + private static final boolean IS_USER_BUILD = "user".equals(Build.TYPE); private final DeviceConfigListener mDeviceConfigListener = new DeviceConfigListener(); @@ -57,11 +65,39 @@ public class NearbyConfiguration { private int mNanoAppMinVersion; @GuardedBy("mDeviceConfigLock") private boolean mSupportTestApp; + @GuardedBy("mDeviceConfigLock") + private boolean mRefactorDiscoveryManager; public NearbyConfiguration() { mDeviceConfigListener.start(); } + /** + * Returns the DeviceConfig namespace for Nearby. The DeviceConfig#NAMESPACE_NEARBY was + * added in UpsideDownCake, in Tiramisu, we use {@link DeviceConfig#NAMESPACE_TETHERING}. + */ + public static String getNamespace() { + if (SdkLevel.isAtLeastU()) { + // DeviceConfig.NAMESPACE_NEARBY + return "nearby"; + } + return DeviceConfig.NAMESPACE_TETHERING; + } + + private static boolean getDeviceConfigBoolean(final String name, final boolean defaultValue) { + final String value = getDeviceConfigProperty(name); + return value != null ? Boolean.parseBoolean(value) : defaultValue; + } + + private static int getDeviceConfigInt(final String name, final int defaultValue) { + final String value = getDeviceConfigProperty(name); + return value != null ? Integer.parseInt(value) : defaultValue; + } + + private static String getDeviceConfigProperty(String name) { + return DeviceConfig.getProperty(getNamespace(), name); + } + /** * Returns whether broadcasting legacy presence spec is enabled. */ @@ -86,11 +122,21 @@ public class NearbyConfiguration { } } + /** + * @return {@code true} if use {@link DiscoveryProviderManager} or use + * DiscoveryProviderManagerLegacy if {@code false}. + */ + public boolean refactorDiscoveryManager() { + synchronized (mDeviceConfigLock) { + return mRefactorDiscoveryManager; + } + } + private class DeviceConfigListener implements DeviceConfig.OnPropertiesChangedListener { public void start() { - DeviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_TETHERING, + DeviceConfig.addOnPropertiesChangedListener(getNamespace(), Executors.newSingleThreadExecutor(), this); - onPropertiesChanged(DeviceConfig.getProperties(DeviceConfig.NAMESPACE_TETHERING)); + onPropertiesChanged(DeviceConfig.getProperties(getNamespace())); } @Override @@ -102,21 +148,9 @@ public class NearbyConfiguration { NEARBY_MAINLINE_NANO_APP_MIN_VERSION, 0 /* defaultValue */); mSupportTestApp = !IS_USER_BUILD && getDeviceConfigBoolean( NEARBY_SUPPORT_TEST_APP, false /* defaultValue */); + mRefactorDiscoveryManager = getDeviceConfigBoolean( + NEARBY_REFACTOR_DISCOVERY_MANAGER, false /* defaultValue */); } } } - - private static boolean getDeviceConfigBoolean(final String name, final boolean defaultValue) { - final String value = getDeviceConfigProperty(name); - return value != null ? Boolean.parseBoolean(value) : defaultValue; - } - - private static int getDeviceConfigInt(final String name, final int defaultValue) { - final String value = getDeviceConfigProperty(name); - return value != null ? Integer.parseInt(value) : defaultValue; - } - - private static String getDeviceConfigProperty(String name) { - return DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TETHERING, name); - } } diff --git a/nearby/tests/unit/src/com/android/server/nearby/NearbyConfigurationTest.java b/nearby/tests/unit/src/com/android/server/nearby/NearbyConfigurationTest.java index ca03081320..5ddfed37eb 100644 --- a/nearby/tests/unit/src/com/android/server/nearby/NearbyConfigurationTest.java +++ b/nearby/tests/unit/src/com/android/server/nearby/NearbyConfigurationTest.java @@ -18,7 +18,6 @@ package com.android.server.nearby; import static android.Manifest.permission.READ_DEVICE_CONFIG; import static android.Manifest.permission.WRITE_DEVICE_CONFIG; -import static android.provider.DeviceConfig.NAMESPACE_TETHERING; import static com.android.server.nearby.NearbyConfiguration.NEARBY_ENABLE_PRESENCE_BROADCAST_LEGACY; import static com.android.server.nearby.NearbyConfiguration.NEARBY_MAINLINE_NANO_APP_MIN_VERSION; @@ -36,6 +35,7 @@ import org.junit.Test; public final class NearbyConfigurationTest { + private static final String NAMESPACE = NearbyConfiguration.getNamespace(); private NearbyConfiguration mNearbyConfiguration; @Before @@ -48,11 +48,11 @@ public final class NearbyConfigurationTest { public void testDeviceConfigChanged() throws InterruptedException { mNearbyConfiguration = new NearbyConfiguration(); - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_SUPPORT_TEST_APP, + DeviceConfig.setProperty(NAMESPACE, NEARBY_SUPPORT_TEST_APP, "false", false); - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_ENABLE_PRESENCE_BROADCAST_LEGACY, + DeviceConfig.setProperty(NAMESPACE, NEARBY_ENABLE_PRESENCE_BROADCAST_LEGACY, "false", false); - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_MAINLINE_NANO_APP_MIN_VERSION, + DeviceConfig.setProperty(NAMESPACE, NEARBY_MAINLINE_NANO_APP_MIN_VERSION, "1", false); Thread.sleep(500); @@ -60,11 +60,11 @@ public final class NearbyConfigurationTest { assertThat(mNearbyConfiguration.isPresenceBroadcastLegacyEnabled()).isFalse(); assertThat(mNearbyConfiguration.getNanoAppMinVersion()).isEqualTo(1); - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_SUPPORT_TEST_APP, + DeviceConfig.setProperty(NAMESPACE, NEARBY_SUPPORT_TEST_APP, "true", false); - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_ENABLE_PRESENCE_BROADCAST_LEGACY, + DeviceConfig.setProperty(NAMESPACE, NEARBY_ENABLE_PRESENCE_BROADCAST_LEGACY, "true", false); - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_MAINLINE_NANO_APP_MIN_VERSION, + DeviceConfig.setProperty(NAMESPACE, NEARBY_MAINLINE_NANO_APP_MIN_VERSION, "3", false); Thread.sleep(500); @@ -76,11 +76,11 @@ public final class NearbyConfigurationTest { @After public void tearDown() { // Sets DeviceConfig values back to default - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_SUPPORT_TEST_APP, + DeviceConfig.setProperty(NAMESPACE, NEARBY_SUPPORT_TEST_APP, "false", true); - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_ENABLE_PRESENCE_BROADCAST_LEGACY, + DeviceConfig.setProperty(NAMESPACE, NEARBY_ENABLE_PRESENCE_BROADCAST_LEGACY, "false", true); - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_MAINLINE_NANO_APP_MIN_VERSION, + DeviceConfig.setProperty(NAMESPACE, NEARBY_MAINLINE_NANO_APP_MIN_VERSION, "0", true); } } diff --git a/nearby/tests/unit/src/com/android/server/nearby/NearbyServiceTest.java b/nearby/tests/unit/src/com/android/server/nearby/NearbyServiceTest.java index a5cbbc8ab1..5b640cc35c 100644 --- a/nearby/tests/unit/src/com/android/server/nearby/NearbyServiceTest.java +++ b/nearby/tests/unit/src/com/android/server/nearby/NearbyServiceTest.java @@ -19,7 +19,6 @@ package com.android.server.nearby; import static android.Manifest.permission.BLUETOOTH_PRIVILEGED; import static android.Manifest.permission.READ_DEVICE_CONFIG; import static android.Manifest.permission.WRITE_DEVICE_CONFIG; -import static android.provider.DeviceConfig.NAMESPACE_TETHERING; import static com.android.server.nearby.NearbyConfiguration.NEARBY_SUPPORT_TEST_APP; @@ -36,6 +35,7 @@ import android.app.UiAutomation; import android.content.Context; import android.nearby.IScanListener; import android.nearby.ScanRequest; +import android.os.IBinder; import android.provider.DeviceConfig; import androidx.test.platform.app.InstrumentationRegistry; @@ -50,6 +50,7 @@ import org.mockito.Mock; public final class NearbyServiceTest { + private static final String NAMESPACE = NearbyConfiguration.getNamespace(); private static final String PACKAGE_NAME = "android.nearby.test"; private Context mContext; private NearbyService mService; @@ -61,10 +62,14 @@ public final class NearbyServiceTest { private IScanListener mScanListener; @Mock private AppOpsManager mMockAppOpsManager; + @Mock + private IBinder mIBinder; @Before public void setUp() { initMocks(this); + when(mScanListener.asBinder()).thenReturn(mIBinder); + mUiAutomation.adoptShellPermissionIdentity( READ_DEVICE_CONFIG, WRITE_DEVICE_CONFIG, BLUETOOTH_PRIVILEGED); mContext = InstrumentationRegistry.getInstrumentation().getContext(); @@ -86,7 +91,7 @@ public final class NearbyServiceTest { @Test public void test_register_noPrivilegedPermission_throwsException() { - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_SUPPORT_TEST_APP, + DeviceConfig.setProperty(NAMESPACE, NEARBY_SUPPORT_TEST_APP, "false", false); mUiAutomation.dropShellPermissionIdentity(); assertThrows(java.lang.SecurityException.class, @@ -96,7 +101,7 @@ public final class NearbyServiceTest { @Test public void test_unregister_noPrivilegedPermission_throwsException() { - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_SUPPORT_TEST_APP, + DeviceConfig.setProperty(NAMESPACE, NEARBY_SUPPORT_TEST_APP, "false", false); mUiAutomation.dropShellPermissionIdentity(); assertThrows(java.lang.SecurityException.class, diff --git a/nearby/tests/unit/src/com/android/server/nearby/managers/BroadcastProviderManagerTest.java b/nearby/tests/unit/src/com/android/server/nearby/managers/BroadcastProviderManagerTest.java index d0008f52cf..bc3821040a 100644 --- a/nearby/tests/unit/src/com/android/server/nearby/managers/BroadcastProviderManagerTest.java +++ b/nearby/tests/unit/src/com/android/server/nearby/managers/BroadcastProviderManagerTest.java @@ -18,7 +18,6 @@ package com.android.server.nearby.managers; import static android.Manifest.permission.READ_DEVICE_CONFIG; import static android.Manifest.permission.WRITE_DEVICE_CONFIG; -import static android.provider.DeviceConfig.NAMESPACE_TETHERING; import static com.android.server.nearby.NearbyConfiguration.NEARBY_ENABLE_PRESENCE_BROADCAST_LEGACY; import static com.android.server.nearby.NearbyConfiguration.NEARBY_SUPPORT_TEST_APP; @@ -40,6 +39,7 @@ import android.provider.DeviceConfig; import androidx.test.core.app.ApplicationProvider; import androidx.test.platform.app.InstrumentationRegistry; +import com.android.server.nearby.NearbyConfiguration; import com.android.server.nearby.provider.BleBroadcastProvider; import com.google.common.util.concurrent.MoreExecutors; @@ -57,6 +57,7 @@ import java.util.Collections; * Unit test for {@link com.android.server.nearby.managers.BroadcastProviderManager}. */ public class BroadcastProviderManagerTest { + private static final String NAMESPACE = NearbyConfiguration.getNamespace(); private static final byte[] IDENTITY = new byte[]{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; private static final int MEDIUM_TYPE_BLE = 0; private static final byte[] SALT = {2, 3}; @@ -82,8 +83,8 @@ public class BroadcastProviderManagerTest { @Before public void setUp() { mUiAutomation.adoptShellPermissionIdentity(WRITE_DEVICE_CONFIG, READ_DEVICE_CONFIG); - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_ENABLE_PRESENCE_BROADCAST_LEGACY, - "true", false); + DeviceConfig.setProperty( + NAMESPACE, NEARBY_ENABLE_PRESENCE_BROADCAST_LEGACY, "true", false); mContext = ApplicationProvider.getApplicationContext(); mBroadcastProviderManager = new BroadcastProviderManager( @@ -116,10 +117,10 @@ public class BroadcastProviderManagerTest { @Test public void testStartAdvertising_featureDisabled() throws Exception { - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_ENABLE_PRESENCE_BROADCAST_LEGACY, - "false", false); - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_SUPPORT_TEST_APP, - "false", false); + DeviceConfig.setProperty( + NAMESPACE, NEARBY_ENABLE_PRESENCE_BROADCAST_LEGACY, "false", false); + DeviceConfig.setProperty( + NAMESPACE, NEARBY_SUPPORT_TEST_APP, "false", false); mBroadcastProviderManager = new BroadcastProviderManager(MoreExecutors.directExecutor(), mBleBroadcastProvider); diff --git a/nearby/tests/unit/src/com/android/server/nearby/provider/ChreCommunicationTest.java b/nearby/tests/unit/src/com/android/server/nearby/provider/ChreCommunicationTest.java index 58b85846e2..a8636199cb 100644 --- a/nearby/tests/unit/src/com/android/server/nearby/provider/ChreCommunicationTest.java +++ b/nearby/tests/unit/src/com/android/server/nearby/provider/ChreCommunicationTest.java @@ -18,7 +18,6 @@ package com.android.server.nearby.provider; import static android.Manifest.permission.READ_DEVICE_CONFIG; import static android.Manifest.permission.WRITE_DEVICE_CONFIG; -import static android.provider.DeviceConfig.NAMESPACE_TETHERING; import static com.android.server.nearby.NearbyConfiguration.NEARBY_MAINLINE_NANO_APP_MIN_VERSION; @@ -40,6 +39,7 @@ import android.provider.DeviceConfig; import androidx.test.platform.app.InstrumentationRegistry; +import com.android.server.nearby.NearbyConfiguration; import com.android.server.nearby.injector.Injector; import org.junit.Before; @@ -54,6 +54,8 @@ import java.util.List; import java.util.concurrent.Executor; public class ChreCommunicationTest { + private static final String NAMESPACE = NearbyConfiguration.getNamespace(); + @Mock Injector mInjector; @Mock Context mContext; @Mock ContextHubManager mManager; @@ -71,8 +73,8 @@ public class ChreCommunicationTest { public void setUp() { InstrumentationRegistry.getInstrumentation().getUiAutomation() .adoptShellPermissionIdentity(WRITE_DEVICE_CONFIG, READ_DEVICE_CONFIG); - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_MAINLINE_NANO_APP_MIN_VERSION, - "1", false); + DeviceConfig.setProperty( + NAMESPACE, NEARBY_MAINLINE_NANO_APP_MIN_VERSION, "1", false); MockitoAnnotations.initMocks(this); when(mInjector.getContextHubManager()).thenReturn(mManager); @@ -111,8 +113,7 @@ public class ChreCommunicationTest { @Test public void testNotReachMinVersion() { - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_MAINLINE_NANO_APP_MIN_VERSION, - "3", false); + DeviceConfig.setProperty(NAMESPACE, NEARBY_MAINLINE_NANO_APP_MIN_VERSION, "3", false); mChreCommunication.start( mChreCallback, Collections.singleton(ChreDiscoveryProvider.NANOAPP_ID)); verify(mTransaction).setOnCompleteListener(mOnQueryCompleteListenerCaptor.capture(), any()); diff --git a/nearby/tests/unit/src/com/android/server/nearby/provider/ChreDiscoveryProviderTest.java b/nearby/tests/unit/src/com/android/server/nearby/provider/ChreDiscoveryProviderTest.java index 25d3f7319f..83fec44aee 100644 --- a/nearby/tests/unit/src/com/android/server/nearby/provider/ChreDiscoveryProviderTest.java +++ b/nearby/tests/unit/src/com/android/server/nearby/provider/ChreDiscoveryProviderTest.java @@ -18,7 +18,6 @@ package com.android.server.nearby.provider; import static android.Manifest.permission.READ_DEVICE_CONFIG; import static android.Manifest.permission.WRITE_DEVICE_CONFIG; -import static android.provider.DeviceConfig.NAMESPACE_TETHERING; import static com.android.server.nearby.NearbyConfiguration.NEARBY_SUPPORT_TEST_APP; @@ -61,6 +60,7 @@ public class ChreDiscoveryProviderTest { @Captor ArgumentCaptor mChreCallbackCaptor; @Captor ArgumentCaptor mNearbyDevice; + private static final String NAMESPACE = NearbyConfiguration.getNamespace(); private static final int DATA_TYPE_CONNECTION_STATUS_KEY = 10; private static final int DATA_TYPE_BATTERY_KEY = 11; private static final int DATA_TYPE_TX_POWER_KEY = 5; @@ -130,7 +130,7 @@ public class ChreDiscoveryProviderTest { boolean isSupportedTestApp = getDeviceConfigBoolean( NEARBY_SUPPORT_TEST_APP, false /* defaultValue */); if (isSupportedTestApp) { - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_SUPPORT_TEST_APP, "false", false); + DeviceConfig.setProperty(NAMESPACE, NEARBY_SUPPORT_TEST_APP, "false", false); } assertThat(new NearbyConfiguration().isTestAppSupported()).isFalse(); @@ -215,7 +215,7 @@ public class ChreDiscoveryProviderTest { assertThat(extendedProperties).containsExactlyElementsIn(expectedExtendedProperties); // Reverts the setting of test app support if (isSupportedTestApp) { - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_SUPPORT_TEST_APP, "true", false); + DeviceConfig.setProperty(NAMESPACE, NEARBY_SUPPORT_TEST_APP, "true", false); assertThat(new NearbyConfiguration().isTestAppSupported()).isTrue(); } } @@ -227,7 +227,7 @@ public class ChreDiscoveryProviderTest { boolean isSupportedTestApp = getDeviceConfigBoolean( NEARBY_SUPPORT_TEST_APP, false /* defaultValue */); if (!isSupportedTestApp) { - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_SUPPORT_TEST_APP, "true", false); + DeviceConfig.setProperty(NAMESPACE, NEARBY_SUPPORT_TEST_APP, "true", false); } assertThat(new NearbyConfiguration().isTestAppSupported()).isTrue(); @@ -316,7 +316,7 @@ public class ChreDiscoveryProviderTest { assertThat(extendedProperties).containsExactlyElementsIn(expectedExtendedProperties); // Reverts the setting of test app support if (!isSupportedTestApp) { - DeviceConfig.setProperty(NAMESPACE_TETHERING, NEARBY_SUPPORT_TEST_APP, "false", false); + DeviceConfig.setProperty(NAMESPACE, NEARBY_SUPPORT_TEST_APP, "false", false); assertThat(new NearbyConfiguration().isTestAppSupported()).isFalse(); } } @@ -327,7 +327,7 @@ public class ChreDiscoveryProviderTest { } private String getDeviceConfigProperty(String name) { - return DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TETHERING, name); + return DeviceConfig.getProperty(NAMESPACE, name); } private static class InLineExecutor implements Executor {