From a7089833503d54fff0bf3db40d0e0655a8a196e1 Mon Sep 17 00:00:00 2001 From: Arthur Ishiguro Date: Thu, 15 Oct 2020 08:37:36 -0700 Subject: [PATCH 1/2] Simplifies Location setting handling in ContextHubService No functional change. Does minor simplications to prepare for upcoming changes to handle additional setting changes. Bug: 166845383 Test: Compile Change-Id: I0b6ed0e62c05e92a4c7e5c987b15bb8c172dc47d --- .../server/location/ContextHubService.java | 8 +--- .../server/location/IContextHubWrapper.java | 38 +++++++------------ 2 files changed, 15 insertions(+), 31 deletions(-) diff --git a/services/core/java/com/android/server/location/ContextHubService.java b/services/core/java/com/android/server/location/ContextHubService.java index 264c611d45fe7..87f7aefbd9ebc 100644 --- a/services/core/java/com/android/server/location/ContextHubService.java +++ b/services/core/java/com/android/server/location/ContextHubService.java @@ -26,8 +26,6 @@ import android.hardware.contexthub.V1_0.HubAppInfo; import android.hardware.contexthub.V1_0.IContexthubCallback; import android.hardware.contexthub.V1_0.Result; import android.hardware.contexthub.V1_0.TransactionResult; -import android.hardware.contexthub.V1_1.Setting; -import android.hardware.contexthub.V1_1.SettingValue; import android.hardware.location.ContextHubInfo; import android.hardware.location.ContextHubMessage; import android.hardware.location.ContextHubTransaction; @@ -196,7 +194,7 @@ public class ContextHubService extends IContextHubService.Stub { } mDefaultClientMap = Collections.unmodifiableMap(defaultClientMap); - if (mContextHubWrapper.supportsSettingNotifications()) { + if (mContextHubWrapper.supportsLocationSettingNotifications()) { sendLocationSettingUpdate(); mContext.getContentResolver().registerContentObserver( Settings.Secure.getUriFor(Settings.Secure.LOCATION_MODE), @@ -925,9 +923,7 @@ public class ContextHubService extends IContextHubService.Stub { private void sendLocationSettingUpdate() { boolean enabled = mContext.getSystemService(LocationManager.class) .isLocationEnabledForUser(UserHandle.CURRENT); - - mContextHubWrapper.onSettingChanged(Setting.LOCATION, - enabled ? SettingValue.ENABLED : SettingValue.DISABLED); + mContextHubWrapper.onLocationSettingChanged(enabled); } private String getCallingPackageName() { diff --git a/services/core/java/com/android/server/location/IContextHubWrapper.java b/services/core/java/com/android/server/location/IContextHubWrapper.java index 79fa5c7b3a95a..4315d86df96b5 100644 --- a/services/core/java/com/android/server/location/IContextHubWrapper.java +++ b/services/core/java/com/android/server/location/IContextHubWrapper.java @@ -45,12 +45,7 @@ public abstract class IContextHubWrapper { Log.i(TAG, "Context Hub HAL service not found"); } - ContextHubWrapperV1_0 wrapper = null; - if (proxy != null) { - wrapper = new ContextHubWrapperV1_0(proxy); - } - - return wrapper; + return (proxy == null) ? null : new ContextHubWrapperV1_0(proxy); } /** @@ -69,12 +64,7 @@ public abstract class IContextHubWrapper { Log.i(TAG, "Context Hub HAL service not found"); } - ContextHubWrapperV1_1 wrapper = null; - if (proxy != null) { - wrapper = new ContextHubWrapperV1_1(proxy); - } - - return wrapper; + return (proxy == null) ? null : new ContextHubWrapperV1_1(proxy); } /** @@ -83,19 +73,16 @@ public abstract class IContextHubWrapper { public abstract android.hardware.contexthub.V1_0.IContexthub getHub(); /** - * @return True if this version of the Contexthub HAL supports setting notifications. + * @return True if this version of the Contexthub HAL supports Location setting notifications. */ - public abstract boolean supportsSettingNotifications(); + public abstract boolean supportsLocationSettingNotifications(); /** - * Notifies the Contexthub implementation of a user setting change. + * Notifies the Contexthub implementation of a user Location setting change. * - * @param setting The user setting that has changed. MUST be one of the values from the - * {@link Setting} enum - * @param newValue The value of the user setting that changed. MUST be one of the values - * from the {@link SettingValue} enum. + * @param enabled True if the Location setting has been enabled. */ - public abstract void onSettingChanged(byte setting, byte newValue); + public abstract void onLocationSettingChanged(boolean enabled); private static class ContextHubWrapperV1_0 extends IContextHubWrapper { private android.hardware.contexthub.V1_0.IContexthub mHub; @@ -108,11 +95,11 @@ public abstract class IContextHubWrapper { return mHub; } - public boolean supportsSettingNotifications() { + public boolean supportsLocationSettingNotifications() { return false; } - public void onSettingChanged(byte setting, byte newValue) {} + public void onLocationSettingChanged(boolean enabled) {} } private static class ContextHubWrapperV1_1 extends IContextHubWrapper { @@ -126,13 +113,14 @@ public abstract class IContextHubWrapper { return mHub; } - public boolean supportsSettingNotifications() { + public boolean supportsLocationSettingNotifications() { return true; } - public void onSettingChanged(byte setting, byte newValue) { + public void onLocationSettingChanged(boolean enabled) { try { - mHub.onSettingChanged(setting, newValue); + mHub.onSettingChanged(Setting.LOCATION, + enabled ? SettingValue.ENABLED : SettingValue.DISABLED); } catch (RemoteException e) { Log.e(TAG, "Failed to send setting change to Contexthub", e); } From 8368e94109946ccf5af41f49462969bab412c204 Mon Sep 17 00:00:00 2001 From: Arthur Ishiguro Date: Wed, 14 Oct 2020 13:39:09 -0700 Subject: [PATCH 2/2] Add Context Hub HAL 1.2 in IContextHubWrapper Bug: 166845383 Test: Flash and verify HAL 1.2 can be found if present Change-Id: Ia8dc423f449c1e179ce450a1ee7954a964bae6cb --- Android.bp | 1 + .../server/location/ContextHubService.java | 5 +- .../server/location/IContextHubWrapper.java | 82 +++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) diff --git a/Android.bp b/Android.bp index 1f17932be895e..1c16e466703cf 100644 --- a/Android.bp +++ b/Android.bp @@ -466,6 +466,7 @@ java_library { "android.hardware.cas-V1.2-java", "android.hardware.contexthub-V1.0-java", "android.hardware.contexthub-V1.1-java", + "android.hardware.contexthub-V1.2-java", "android.hardware.gnss-V1.0-java", "android.hardware.gnss-V2.1-java", "android.hardware.health-V1.0-java-constants", diff --git a/services/core/java/com/android/server/location/ContextHubService.java b/services/core/java/com/android/server/location/ContextHubService.java index 87f7aefbd9ebc..885aa093410c5 100644 --- a/services/core/java/com/android/server/location/ContextHubService.java +++ b/services/core/java/com/android/server/location/ContextHubService.java @@ -258,7 +258,10 @@ public class ContextHubService extends IContextHubService.Stub { * @return the IContextHubWrapper interface */ private IContextHubWrapper getContextHubWrapper() { - IContextHubWrapper wrapper = IContextHubWrapper.maybeConnectTo1_1(); + IContextHubWrapper wrapper = IContextHubWrapper.maybeConnectTo1_2(); + if (wrapper == null) { + wrapper = IContextHubWrapper.maybeConnectTo1_1(); + } if (wrapper == null) { wrapper = IContextHubWrapper.maybeConnectTo1_0(); } diff --git a/services/core/java/com/android/server/location/IContextHubWrapper.java b/services/core/java/com/android/server/location/IContextHubWrapper.java index 4315d86df96b5..0c0ba10a2fd0b 100644 --- a/services/core/java/com/android/server/location/IContextHubWrapper.java +++ b/services/core/java/com/android/server/location/IContextHubWrapper.java @@ -67,6 +67,25 @@ public abstract class IContextHubWrapper { return (proxy == null) ? null : new ContextHubWrapperV1_1(proxy); } + /** + * Attempts to connect to the Contexthub HAL 1.2 service, if it exists. + * + * @return A valid IContextHubWrapper if the connection was successful, null otherwise. + */ + @Nullable + public static IContextHubWrapper maybeConnectTo1_2() { + android.hardware.contexthub.V1_2.IContexthub proxy = null; + try { + proxy = android.hardware.contexthub.V1_2.IContexthub.getService(true /* retry */); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException while attaching to Context Hub HAL proxy", e); + } catch (NoSuchElementException e) { + Log.i(TAG, "Context Hub HAL service not found"); + } + + return (proxy == null) ? null : new ContextHubWrapperV1_2(proxy); + } + /** * @return A valid instance of Contexthub HAL 1.0. */ @@ -84,6 +103,19 @@ public abstract class IContextHubWrapper { */ public abstract void onLocationSettingChanged(boolean enabled); + /** + * @return True if this version of the Contexthub HAL supports WiFi availability setting + * notifications. + */ + public abstract boolean supportsWifiSettingNotifications(); + + /** + * Notifies the Contexthub implementation of a user WiFi availability setting change. + * + * @param enabled true if the WiFi availability setting has been enabled. + */ + public abstract void onWifiSettingChanged(boolean enabled); + private static class ContextHubWrapperV1_0 extends IContextHubWrapper { private android.hardware.contexthub.V1_0.IContexthub mHub; @@ -99,7 +131,13 @@ public abstract class IContextHubWrapper { return false; } + public boolean supportsWifiSettingNotifications() { + return false; + } + public void onLocationSettingChanged(boolean enabled) {} + + public void onWifiSettingChanged(boolean enabled) {} } private static class ContextHubWrapperV1_1 extends IContextHubWrapper { @@ -117,6 +155,10 @@ public abstract class IContextHubWrapper { return true; } + public boolean supportsWifiSettingNotifications() { + return false; + } + public void onLocationSettingChanged(boolean enabled) { try { mHub.onSettingChanged(Setting.LOCATION, @@ -125,5 +167,45 @@ public abstract class IContextHubWrapper { Log.e(TAG, "Failed to send setting change to Contexthub", e); } } + + public void onWifiSettingChanged(boolean enabled) {} + } + + private static class ContextHubWrapperV1_2 extends IContextHubWrapper { + private android.hardware.contexthub.V1_2.IContexthub mHub; + + ContextHubWrapperV1_2(android.hardware.contexthub.V1_2.IContexthub hub) { + mHub = hub; + } + + public android.hardware.contexthub.V1_0.IContexthub getHub() { + return mHub; + } + + public boolean supportsLocationSettingNotifications() { + return true; + } + + public boolean supportsWifiSettingNotifications() { + return true; + } + + public void onLocationSettingChanged(boolean enabled) { + sendSettingChanged(Setting.LOCATION, + enabled ? SettingValue.ENABLED : SettingValue.DISABLED); + } + + public void onWifiSettingChanged(boolean enabled) { + sendSettingChanged(android.hardware.contexthub.V1_2.Setting.WIFI_AVAILABLE, + enabled ? SettingValue.ENABLED : SettingValue.DISABLED); + } + + private void sendSettingChanged(byte setting, byte newValue) { + try { + mHub.onSettingChanged_1_2(setting, newValue); + } catch (RemoteException e) { + Log.e(TAG, "Failed to send setting change to Contexthub", e); + } + } } }