Merge "Independent methods to enable/disable BLE mode"
This commit is contained in:
@@ -916,23 +916,11 @@ public final class BluetoothAdapter {
|
|||||||
if (!isBleScanAlwaysAvailable()) {
|
if (!isBleScanAlwaysAvailable()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
String packageName = ActivityThread.currentPackageName();
|
||||||
int state = getLeState();
|
try {
|
||||||
if (state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_BLE_ON) {
|
return mManagerService.disableBle(packageName, mToken);
|
||||||
String packageName = ActivityThread.currentPackageName();
|
} catch (RemoteException e) {
|
||||||
if (DBG) {
|
Log.e(TAG, "", e);
|
||||||
Log.d(TAG, "disableBLE(): de-registering " + packageName);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
mManagerService.updateBleAppCount(mToken, false, packageName);
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
Log.e(TAG, "", e);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (DBG) {
|
|
||||||
Log.d(TAG, "disableBLE(): Already disabled");
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -973,20 +961,9 @@ public final class BluetoothAdapter {
|
|||||||
if (!isBleScanAlwaysAvailable()) {
|
if (!isBleScanAlwaysAvailable()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
String packageName = ActivityThread.currentPackageName();
|
||||||
try {
|
try {
|
||||||
String packageName = ActivityThread.currentPackageName();
|
return mManagerService.enableBle(packageName, mToken);
|
||||||
mManagerService.updateBleAppCount(mToken, true, packageName);
|
|
||||||
if (isLeEnabled()) {
|
|
||||||
if (DBG) {
|
|
||||||
Log.d(TAG, "enableBLE(): Bluetooth already enabled");
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (DBG) {
|
|
||||||
Log.d(TAG, "enableBLE(): Calling enable");
|
|
||||||
}
|
|
||||||
return mManagerService.enable(packageName);
|
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
Log.e(TAG, "", e);
|
Log.e(TAG, "", e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -747,13 +747,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int updateBleAppCount(IBinder token, boolean enable, String packageName) {
|
private int updateBleAppCount(IBinder token, boolean enable, String packageName) {
|
||||||
// Check if packageName belongs to callingUid
|
|
||||||
final int callingUid = Binder.getCallingUid();
|
|
||||||
final boolean isCallerSystem = UserHandle.getAppId(callingUid) == Process.SYSTEM_UID;
|
|
||||||
if (!isCallerSystem) {
|
|
||||||
checkPackage(callingUid, packageName);
|
|
||||||
}
|
|
||||||
ClientDeathRecipient r = mBleApps.get(token);
|
ClientDeathRecipient r = mBleApps.get(token);
|
||||||
if (r == null && enable) {
|
if (r == null && enable) {
|
||||||
ClientDeathRecipient deathRec = new ClientDeathRecipient(packageName);
|
ClientDeathRecipient deathRec = new ClientDeathRecipient(packageName);
|
||||||
@@ -778,15 +772,96 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
|
|||||||
if (DBG) {
|
if (DBG) {
|
||||||
Slog.d(TAG, appCount + " registered Ble Apps");
|
Slog.d(TAG, appCount + " registered Ble Apps");
|
||||||
}
|
}
|
||||||
if (appCount == 0 && mEnable) {
|
|
||||||
disableBleScanMode();
|
|
||||||
}
|
|
||||||
if (appCount == 0 && !mEnableExternal) {
|
|
||||||
sendBrEdrDownCallback();
|
|
||||||
}
|
|
||||||
return appCount;
|
return appCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean checkBluetoothPermissions(String packageName, boolean requireForeground) {
|
||||||
|
if (isBluetoothDisallowed()) {
|
||||||
|
if (DBG) {
|
||||||
|
Slog.d(TAG, "checkBluetoothPermissions: bluetooth disallowed");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Check if packageName belongs to callingUid
|
||||||
|
final int callingUid = Binder.getCallingUid();
|
||||||
|
final boolean isCallerSystem = UserHandle.getAppId(callingUid) == Process.SYSTEM_UID;
|
||||||
|
if (!isCallerSystem) {
|
||||||
|
checkPackage(callingUid, packageName);
|
||||||
|
|
||||||
|
if (requireForeground && !checkIfCallerIsForegroundUser()) {
|
||||||
|
Slog.w(TAG, "Not allowed for non-active and non system user");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
|
||||||
|
"Need BLUETOOTH ADMIN permission");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean enableBle(String packageName, IBinder token) throws RemoteException {
|
||||||
|
if (!checkBluetoothPermissions(packageName, false)) {
|
||||||
|
if (DBG) {
|
||||||
|
Slog.d(TAG, "enableBle(): bluetooth disallowed");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (DBG) {
|
||||||
|
Slog.d(TAG, "enableBle(" + packageName + "): mBluetooth =" + mBluetooth
|
||||||
|
+ " mBinding = " + mBinding + " mState = "
|
||||||
|
+ BluetoothAdapter.nameForState(mState));
|
||||||
|
}
|
||||||
|
updateBleAppCount(token, true, packageName);
|
||||||
|
|
||||||
|
if (mState == BluetoothAdapter.STATE_ON
|
||||||
|
|| mState == BluetoothAdapter.STATE_BLE_ON
|
||||||
|
|| mState == BluetoothAdapter.STATE_TURNING_ON
|
||||||
|
|| mState == BluetoothAdapter.STATE_TURNING_OFF) {
|
||||||
|
Log.d(TAG, "enableBLE(): Bluetooth already enabled");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
synchronized (mReceiver) {
|
||||||
|
// waive WRITE_SECURE_SETTINGS permission check
|
||||||
|
sendEnableMsg(false,
|
||||||
|
BluetoothProtoEnums.ENABLE_DISABLE_REASON_APPLICATION_REQUEST, packageName);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean disableBle(String packageName, IBinder token) throws RemoteException {
|
||||||
|
if (!checkBluetoothPermissions(packageName, false)) {
|
||||||
|
if (DBG) {
|
||||||
|
Slog.d(TAG, "disableBLE(): bluetooth disallowed");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (DBG) {
|
||||||
|
Slog.d(TAG, "disableBle(" + packageName + "): mBluetooth =" + mBluetooth
|
||||||
|
+ " mBinding = " + mBinding + " mState = "
|
||||||
|
+ BluetoothAdapter.nameForState(mState));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mState == BluetoothAdapter.STATE_OFF) {
|
||||||
|
Slog.d(TAG, "disableBLE(): Already disabled");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
updateBleAppCount(token, false, packageName);
|
||||||
|
|
||||||
|
if (mState == BluetoothAdapter.STATE_BLE_ON && !isBleAppPresent()) {
|
||||||
|
if (mEnable) {
|
||||||
|
disableBleScanMode();
|
||||||
|
}
|
||||||
|
if (!mEnableExternal) {
|
||||||
|
addActiveLog(BluetoothProtoEnums.ENABLE_DISABLE_REASON_APPLICATION_REQUEST,
|
||||||
|
packageName, false);
|
||||||
|
sendBrEdrDownCallback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// Clear all apps using BLE scan only mode.
|
// Clear all apps using BLE scan only mode.
|
||||||
private void clearBleApps() {
|
private void clearBleApps() {
|
||||||
mBleApps.clear();
|
mBleApps.clear();
|
||||||
@@ -862,29 +937,19 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean enableNoAutoConnect(String packageName) {
|
public boolean enableNoAutoConnect(String packageName) {
|
||||||
if (isBluetoothDisallowed()) {
|
if (!checkBluetoothPermissions(packageName, false)) {
|
||||||
if (DBG) {
|
if (DBG) {
|
||||||
Slog.d(TAG, "enableNoAutoConnect(): not enabling - bluetooth disallowed");
|
Slog.d(TAG, "enableNoAutoConnect(): not enabling - bluetooth disallowed");
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if packageName belongs to callingUid
|
|
||||||
final int callingUid = Binder.getCallingUid();
|
|
||||||
final boolean isCallerSystem = UserHandle.getAppId(callingUid) == Process.SYSTEM_UID;
|
|
||||||
if (!isCallerSystem) {
|
|
||||||
checkPackage(callingUid, packageName);
|
|
||||||
}
|
|
||||||
|
|
||||||
mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
|
|
||||||
"Need BLUETOOTH ADMIN permission");
|
|
||||||
|
|
||||||
if (DBG) {
|
if (DBG) {
|
||||||
Slog.d(TAG, "enableNoAutoConnect(): mBluetooth =" + mBluetooth + " mBinding = "
|
Slog.d(TAG, "enableNoAutoConnect(): mBluetooth =" + mBluetooth + " mBinding = "
|
||||||
+ mBinding);
|
+ mBinding);
|
||||||
}
|
}
|
||||||
int callingAppId = UserHandle.getAppId(callingUid);
|
|
||||||
|
|
||||||
|
int callingAppId = UserHandle.getAppId(Binder.getCallingUid());
|
||||||
if (callingAppId != Process.NFC_UID) {
|
if (callingAppId != Process.NFC_UID) {
|
||||||
throw new SecurityException("no permission to enable Bluetooth quietly");
|
throw new SecurityException("no permission to enable Bluetooth quietly");
|
||||||
}
|
}
|
||||||
@@ -899,32 +964,19 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean enable(String packageName) throws RemoteException {
|
public boolean enable(String packageName) throws RemoteException {
|
||||||
final int callingUid = Binder.getCallingUid();
|
if (!checkBluetoothPermissions(packageName, true)) {
|
||||||
final boolean callerSystem = UserHandle.getAppId(callingUid) == Process.SYSTEM_UID;
|
|
||||||
|
|
||||||
if (isBluetoothDisallowed()) {
|
|
||||||
if (DBG) {
|
if (DBG) {
|
||||||
Slog.d(TAG, "enable(): not enabling - bluetooth disallowed");
|
Slog.d(TAG, "enable(): not enabling - bluetooth disallowed");
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!callerSystem) {
|
final int callingUid = Binder.getCallingUid();
|
||||||
// Check if packageName belongs to callingUid
|
final boolean callerSystem = UserHandle.getAppId(callingUid) == Process.SYSTEM_UID;
|
||||||
checkPackage(callingUid, packageName);
|
if (!callerSystem && !isEnabled() && mWirelessConsentRequired
|
||||||
|
&& startConsentUiIfNeeded(packageName,
|
||||||
if (!checkIfCallerIsForegroundUser()) {
|
callingUid, BluetoothAdapter.ACTION_REQUEST_ENABLE)) {
|
||||||
Slog.w(TAG, "enable(): not allowed for non-active and non system user");
|
return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
|
|
||||||
"Need BLUETOOTH ADMIN permission");
|
|
||||||
|
|
||||||
if (!isEnabled() && mWirelessConsentRequired && startConsentUiIfNeeded(packageName,
|
|
||||||
callingUid, BluetoothAdapter.ACTION_REQUEST_ENABLE)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (DBG) {
|
if (DBG) {
|
||||||
@@ -946,25 +998,19 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean disable(String packageName, boolean persist) throws RemoteException {
|
public boolean disable(String packageName, boolean persist) throws RemoteException {
|
||||||
|
if (!checkBluetoothPermissions(packageName, true)) {
|
||||||
|
if (DBG) {
|
||||||
|
Slog.d(TAG, "disable(): not disabling - bluetooth disallowed");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
final int callingUid = Binder.getCallingUid();
|
final int callingUid = Binder.getCallingUid();
|
||||||
final boolean callerSystem = UserHandle.getAppId(callingUid) == Process.SYSTEM_UID;
|
final boolean callerSystem = UserHandle.getAppId(callingUid) == Process.SYSTEM_UID;
|
||||||
|
if (!callerSystem && isEnabled() && mWirelessConsentRequired
|
||||||
if (!callerSystem) {
|
&& startConsentUiIfNeeded(packageName,
|
||||||
// Check if packageName belongs to callingUid
|
callingUid, BluetoothAdapter.ACTION_REQUEST_DISABLE)) {
|
||||||
checkPackage(callingUid, packageName);
|
return false;
|
||||||
|
|
||||||
if (!checkIfCallerIsForegroundUser()) {
|
|
||||||
Slog.w(TAG, "disable(): not allowed for non-active and non system user");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
|
|
||||||
"Need BLUETOOTH ADMIN permission");
|
|
||||||
|
|
||||||
if (isEnabled() && mWirelessConsentRequired && startConsentUiIfNeeded(packageName,
|
|
||||||
callingUid, BluetoothAdapter.ACTION_REQUEST_DISABLE)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (DBG) {
|
if (DBG) {
|
||||||
|
|||||||
Reference in New Issue
Block a user