Merge "DO NOT MERGE: Persist UWB State in AOSP stack." into sc-qpr1-dev

This commit is contained in:
Joy Babafemi
2021-09-14 03:28:56 +00:00
committed by Android (Google) Code Review
3 changed files with 29 additions and 1 deletions

View File

@@ -82,6 +82,7 @@ public class UwbServiceImplTest {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
when(mUwbInjector.getVendorService()).thenReturn(mVendorService); when(mUwbInjector.getVendorService()).thenReturn(mVendorService);
when(mUwbInjector.checkUwbRangingPermissionForDataDelivery(any(), any())).thenReturn(true); when(mUwbInjector.checkUwbRangingPermissionForDataDelivery(any(), any())).thenReturn(true);
when(mUwbInjector.isPersistedUwbStateEnabled()).thenReturn(true);
when(mVendorService.asBinder()).thenReturn(mVendorServiceBinder); when(mVendorService.asBinder()).thenReturn(mVendorServiceBinder);
mUwbServiceImpl = new UwbServiceImpl(mContext, mUwbInjector); mUwbServiceImpl = new UwbServiceImpl(mContext, mUwbInjector);
} }

View File

@@ -21,10 +21,13 @@ import static android.content.PermissionChecker.PERMISSION_GRANTED;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.content.AttributionSource; import android.content.AttributionSource;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.PermissionChecker; import android.content.PermissionChecker;
import android.os.IBinder; import android.os.IBinder;
import android.os.ServiceManager; import android.os.ServiceManager;
import android.provider.Settings;
import android.uwb.AdapterState;
import android.uwb.IUwbAdapter; import android.uwb.IUwbAdapter;
@@ -80,4 +83,16 @@ public class UwbInjector {
mContext, UWB_RANGING, -1, attributionSource, message); mContext, UWB_RANGING, -1, attributionSource, message);
return permissionCheckResult == PERMISSION_GRANTED; return permissionCheckResult == PERMISSION_GRANTED;
} }
/** Returns true if UWB state saved in Settings is enabled. */
public boolean isPersistedUwbStateEnabled() {
final ContentResolver cr = mContext.getContentResolver();
try {
return Settings.Global.getInt(cr, Settings.Global.UWB_ENABLED)
== AdapterState.STATE_ENABLED_ACTIVE;
} catch (Settings.SettingNotFoundException e) {
Settings.Global.putInt(cr, Settings.Global.UWB_ENABLED, AdapterState.STATE_DISABLED);
return false;
}
}
} }

View File

@@ -18,13 +18,16 @@ package com.android.server.uwb;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.content.AttributionSource; import android.content.AttributionSource;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.os.Binder; import android.os.Binder;
import android.os.IBinder; import android.os.IBinder;
import android.os.PersistableBundle; import android.os.PersistableBundle;
import android.os.RemoteException; import android.os.RemoteException;
import android.provider.Settings;
import android.util.ArrayMap; import android.util.ArrayMap;
import android.util.Log; import android.util.Log;
import android.uwb.AdapterState;
import android.uwb.IUwbAdapter; import android.uwb.IUwbAdapter;
import android.uwb.IUwbAdapterStateCallbacks; import android.uwb.IUwbAdapterStateCallbacks;
import android.uwb.IUwbRangingCallbacks; import android.uwb.IUwbRangingCallbacks;
@@ -225,13 +228,15 @@ public class UwbServiceImpl extends IUwbAdapter.Stub implements IBinder.DeathRec
mVendorUwbAdapter = null; mVendorUwbAdapter = null;
} }
private synchronized IUwbAdapter getVendorUwbAdapter() throws IllegalStateException { private synchronized IUwbAdapter getVendorUwbAdapter()
throws IllegalStateException, RemoteException {
if (mVendorUwbAdapter != null) return mVendorUwbAdapter; if (mVendorUwbAdapter != null) return mVendorUwbAdapter;
mVendorUwbAdapter = mUwbInjector.getVendorService(); mVendorUwbAdapter = mUwbInjector.getVendorService();
if (mVendorUwbAdapter == null) { if (mVendorUwbAdapter == null) {
throw new IllegalStateException("No vendor service found!"); throw new IllegalStateException("No vendor service found!");
} }
Log.i(TAG, "Retrieved vendor service"); Log.i(TAG, "Retrieved vendor service");
mVendorUwbAdapter.setEnabled(mUwbInjector.isPersistedUwbStateEnabled());
linkToVendorServiceDeath(); linkToVendorServiceDeath();
return mVendorUwbAdapter; return mVendorUwbAdapter;
} }
@@ -320,6 +325,13 @@ public class UwbServiceImpl extends IUwbAdapter.Stub implements IBinder.DeathRec
@Override @Override
public synchronized void setEnabled(boolean enabled) throws RemoteException { public synchronized void setEnabled(boolean enabled) throws RemoteException {
persistUwbState(enabled);
getVendorUwbAdapter().setEnabled(enabled); getVendorUwbAdapter().setEnabled(enabled);
} }
private void persistUwbState(boolean enabled) {
final ContentResolver cr = mContext.getContentResolver();
int state = enabled ? AdapterState.STATE_ENABLED_ACTIVE : AdapterState.STATE_DISABLED;
Settings.Global.putInt(cr, Settings.Global.UWB_ENABLED, state);
}
} }