From 6d035f93e2f2778158f91093930cd1e2f5b91aaf Mon Sep 17 00:00:00 2001 From: Isaac Katzenelson Date: Tue, 7 Mar 2023 17:31:24 +0000 Subject: Added methods to check shared connectivity features availablility. Added methods and flags to check what level of funcionality the service supports. Added flags to enable instant tether and nearby Wi-Fi. The flags will be enabled in an overlay file based on the level of UI support by the OEM. The service checks the flags and provides an API to allow the API's client to decide what level of functionality to provide. Also fixed the naming of flags. Bug: 271934662 Test: atest SharedConnectivityServiceTest Change-Id: Ifba6b73904862dfdc58741856e8b940ae6d6634b --- core/api/system-current.txt | 2 + core/res/res/values/config.xml | 12 +++++- core/res/res/values/symbols.xml | 6 ++- .../app/SharedConnectivityManager.java | 4 +- .../service/SharedConnectivityService.java | 28 +++++++++++++ .../service/SharedConnectivityServiceTest.java | 47 ++++++++++++++++++++++ 6 files changed, 93 insertions(+), 6 deletions(-) diff --git a/core/api/system-current.txt b/core/api/system-current.txt index fb5ee8d94524..995602f27d2b 100644 --- a/core/api/system-current.txt +++ b/core/api/system-current.txt @@ -10158,6 +10158,8 @@ package android.net.wifi.sharedconnectivity.service { public abstract class SharedConnectivityService extends android.app.Service { ctor public SharedConnectivityService(); + method public static boolean areHotspotNetworksEnabledForService(@NonNull android.content.Context); + method public static boolean areKnownNetworksEnabledForService(@NonNull android.content.Context); method @Nullable public final android.os.IBinder onBind(@NonNull android.content.Intent); method public abstract void onConnectHotspotNetwork(@NonNull android.net.wifi.sharedconnectivity.app.HotspotNetwork); method public abstract void onConnectKnownNetwork(@NonNull android.net.wifi.sharedconnectivity.app.KnownNetwork); diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml index 16511a678760..27c477f3f241 100644 --- a/core/res/res/values/config.xml +++ b/core/res/res/values/config.xml @@ -3020,10 +3020,18 @@ >com.android.systemui/com.android.systemui.wifi.WifiDebuggingSecondaryUserActivity - + - + + + + false + + + false - - + + + + diff --git a/wifi/java/src/android/net/wifi/sharedconnectivity/app/SharedConnectivityManager.java b/wifi/java/src/android/net/wifi/sharedconnectivity/app/SharedConnectivityManager.java index 604641520252..15fd817ba73b 100644 --- a/wifi/java/src/android/net/wifi/sharedconnectivity/app/SharedConnectivityManager.java +++ b/wifi/java/src/android/net/wifi/sharedconnectivity/app/SharedConnectivityManager.java @@ -161,9 +161,9 @@ public class SharedConnectivityManager { Resources resources = context.getResources(); try { String servicePackageName = resources.getString( - R.string.shared_connectivity_service_package); + R.string.config_sharedConnectivityServicePackage); String serviceIntentAction = resources.getString( - R.string.shared_connectivity_service_intent_action); + R.string.config_sharedConnectivityServiceIntentAction); return new SharedConnectivityManager(context, servicePackageName, serviceIntentAction); } catch (Resources.NotFoundException e) { Log.e(TAG, "To support shared connectivity service on this device, the service's" diff --git a/wifi/java/src/android/net/wifi/sharedconnectivity/service/SharedConnectivityService.java b/wifi/java/src/android/net/wifi/sharedconnectivity/service/SharedConnectivityService.java index c53da9c15d4d..57108e4aa227 100644 --- a/wifi/java/src/android/net/wifi/sharedconnectivity/service/SharedConnectivityService.java +++ b/wifi/java/src/android/net/wifi/sharedconnectivity/service/SharedConnectivityService.java @@ -25,6 +25,7 @@ import android.annotation.RequiresPermission; import android.annotation.SystemApi; import android.annotation.TestApi; import android.app.Service; +import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.wifi.sharedconnectivity.app.HotspotNetwork; @@ -40,8 +41,11 @@ import android.os.RemoteCallbackList; import android.os.RemoteException; import android.util.Log; +import com.android.internal.R; + import java.util.Collections; import java.util.List; +import java.util.Objects; /** @@ -381,6 +385,30 @@ public abstract class SharedConnectivityService extends Service { mRemoteCallbackList.finishBroadcast(); } + /** + * System and settings UI support on the device for instant tether. + * @return True if the UI can display Instant Tether network data. False otherwise. + */ + public static boolean areHotspotNetworksEnabledForService(@NonNull Context context) { + String servicePackage = context.getResources() + .getString(R.string.config_sharedConnectivityServicePackage); + return Objects.equals(context.getPackageName(), servicePackage) + && context.getResources() + .getBoolean(R.bool.config_hotspotNetworksEnabledForService); + } + + /** + * System and settings UI support on the device for known networks. + * @return True if the UI can display known networks data. False otherwise. + */ + public static boolean areKnownNetworksEnabledForService(@NonNull Context context) { + String servicePackage = context.getResources() + .getString(R.string.config_sharedConnectivityServicePackage); + return Objects.equals(context.getPackageName(), servicePackage) + && context.getResources() + .getBoolean(R.bool.config_knownNetworksEnabledForService); + } + /** * Implementing application should implement this method. * diff --git a/wifi/tests/src/android/net/wifi/sharedconnectivity/service/SharedConnectivityServiceTest.java b/wifi/tests/src/android/net/wifi/sharedconnectivity/service/SharedConnectivityServiceTest.java index 19effe5d6f14..b8b6b767eed3 100644 --- a/wifi/tests/src/android/net/wifi/sharedconnectivity/service/SharedConnectivityServiceTest.java +++ b/wifi/tests/src/android/net/wifi/sharedconnectivity/service/SharedConnectivityServiceTest.java @@ -26,10 +26,12 @@ import static android.net.wifi.sharedconnectivity.app.NetworkProviderInfo.DEVICE import static com.google.common.truth.Truth.assertThat; +import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.when; import android.content.Context; import android.content.Intent; +import android.content.res.Resources; import android.net.wifi.sharedconnectivity.app.HotspotNetwork; import android.net.wifi.sharedconnectivity.app.HotspotNetworkConnectionStatus; import android.net.wifi.sharedconnectivity.app.KnownNetwork; @@ -86,6 +88,9 @@ public class SharedConnectivityServiceTest { @Mock Context mContext; + @Mock + Resources mResources; + static class FakeSharedConnectivityService extends SharedConnectivityService { public void attachBaseContext(Context context) { super.attachBaseContext(context); @@ -180,6 +185,48 @@ public class SharedConnectivityServiceTest { .isEqualTo(KNOWN_NETWORK_CONNECTION_STATUS); } + @Test + public void areHotspotNetworksEnabledForService() { + when(mContext.getResources()).thenReturn(mResources); + when(mContext.getPackageName()).thenReturn("package"); + when(mResources.getString(anyInt())).thenReturn("package"); + when(mResources.getBoolean(anyInt())).thenReturn(true); + + assertThat(SharedConnectivityService.areHotspotNetworksEnabledForService(mContext)) + .isTrue(); + } + + @Test + public void areHotspotNetworksEnabledForService_notSamePackage_shouldReturnFalse() { + when(mContext.getResources()).thenReturn(mResources); + when(mContext.getPackageName()).thenReturn("package"); + when(mResources.getString(anyInt())).thenReturn("other_package"); + when(mResources.getBoolean(anyInt())).thenReturn(true); + + assertThat(SharedConnectivityService.areHotspotNetworksEnabledForService(mContext)) + .isFalse(); + } + + @Test + public void areKnownNetworksEnabledForService() { + when(mContext.getResources()).thenReturn(mResources); + when(mContext.getPackageName()).thenReturn("package"); + when(mResources.getString(anyInt())).thenReturn("package"); + when(mResources.getBoolean(anyInt())).thenReturn(true); + + assertThat(SharedConnectivityService.areKnownNetworksEnabledForService(mContext)).isTrue(); + } + + @Test + public void areKnownNetworksEnabledForService_notSamePackage_shouldReturnFalse() { + when(mContext.getResources()).thenReturn(mResources); + when(mContext.getPackageName()).thenReturn("package"); + when(mResources.getString(anyInt())).thenReturn("other_package"); + when(mResources.getBoolean(anyInt())).thenReturn(true); + + assertThat(SharedConnectivityService.areKnownNetworksEnabledForService(mContext)).isFalse(); + } + private SharedConnectivityService createService() { FakeSharedConnectivityService service = new FakeSharedConnectivityService(); service.attachBaseContext(mContext); -- cgit v1.2.3-59-g8ed1b