[DO NOT MERGE] Handle IllegalStateException in APM event handling. am: 60a456a0a6
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/16576688 Change-Id: If0517cac3a8955660e1276682efbc6825e751ed5
This commit is contained in:
@@ -82,6 +82,8 @@ 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(mUwbInjector.isAirplaneModeOn()).thenReturn(false);
|
||||||
when(mVendorService.asBinder()).thenReturn(mVendorServiceBinder);
|
when(mVendorService.asBinder()).thenReturn(mVendorServiceBinder);
|
||||||
mUwbServiceImpl = new UwbServiceImpl(mContext, mUwbInjector);
|
mUwbServiceImpl = new UwbServiceImpl(mContext, mUwbInjector);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,23 @@ 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_ENABLED_ACTIVE);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns true if airplane mode is turned on. */
|
||||||
|
public boolean isAirplaneModeOn() {
|
||||||
|
return Settings.Global.getInt(mContext.getContentResolver(),
|
||||||
|
Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,13 +18,19 @@ package com.android.server.uwb;
|
|||||||
|
|
||||||
import android.annotation.NonNull;
|
import android.annotation.NonNull;
|
||||||
import android.content.AttributionSource;
|
import android.content.AttributionSource;
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
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 +231,20 @@ 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");
|
||||||
|
long token = Binder.clearCallingIdentity();
|
||||||
|
try {
|
||||||
|
mVendorUwbAdapter.setEnabled(isEnabled());
|
||||||
|
} finally {
|
||||||
|
Binder.restoreCallingIdentity(token);
|
||||||
|
}
|
||||||
linkToVendorServiceDeath();
|
linkToVendorServiceDeath();
|
||||||
return mVendorUwbAdapter;
|
return mVendorUwbAdapter;
|
||||||
}
|
}
|
||||||
@@ -239,6 +252,7 @@ public class UwbServiceImpl extends IUwbAdapter.Stub implements IBinder.DeathRec
|
|||||||
UwbServiceImpl(@NonNull Context context, @NonNull UwbInjector uwbInjector) {
|
UwbServiceImpl(@NonNull Context context, @NonNull UwbInjector uwbInjector) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mUwbInjector = uwbInjector;
|
mUwbInjector = uwbInjector;
|
||||||
|
registerAirplaneModeReceiver();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void enforceUwbPrivilegedPermission() {
|
private void enforceUwbPrivilegedPermission() {
|
||||||
@@ -320,6 +334,34 @@ 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 {
|
||||||
getVendorUwbAdapter().setEnabled(enabled);
|
persistUwbState(enabled);
|
||||||
|
getVendorUwbAdapter().setEnabled(isEnabled());
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void registerAirplaneModeReceiver() {
|
||||||
|
mContext.registerReceiver(new BroadcastReceiver() {
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
handleAirplaneModeEvent();
|
||||||
|
}
|
||||||
|
}, new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleAirplaneModeEvent() {
|
||||||
|
try {
|
||||||
|
getVendorUwbAdapter().setEnabled(isEnabled());
|
||||||
|
} catch (RemoteException | IllegalStateException e) {
|
||||||
|
Log.e(TAG, "Unable to set UWB Adapter state.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isEnabled() {
|
||||||
|
return mUwbInjector.isPersistedUwbStateEnabled() && !mUwbInjector.isAirplaneModeOn();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user