Skip to content

Commit

Permalink
Fix admin enable/disable of Bluetooth file sharing
Browse files Browse the repository at this point in the history
This is an extremely partial fix for a mostly-broken feature. For
fully-managed devices, this fixes the toggle for the file sharing
option, by detecting which Bluetooth package should be used rather than
just guessing.

However, on work profiles, the file sharing option does not really work
anyway, it just infinitely loads. In addition, it is not correctly
disabled when Bluetooth stops, so the icon just remains but doesn't
do anything.

A full fix would add the enable/disable sequence for *work* UserHandles
to BluetoothOppManager, so the LAUNCHER_ACTIVITY is properly enabled and
disabled. In addition, it would fix the issue described in the linked
bug to prevent infinite loading (dedicated bug: b/112625123).

Tag: #stability
Ignore-AOSP-First: For T?
Bug: 237389774
Test: Manual, verifying the *badged* BT file sharing icon appears/disappears as needed

Change-Id: I070987bc884947246c372c65691c4866ebd1e7c7
(cherry picked from commit 36362f254b029bd96d00705a141eff3345864b5b)
Merged-In: I070987bc884947246c372c65691c4866ebd1e7c7
  • Loading branch information
rahularya50 authored and Android Build Coastguard Worker committed Jul 29, 2022
1 parent 8954a1f commit 9827aba
Showing 1 changed file with 49 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.ContentObserver;
Expand Down Expand Up @@ -2892,24 +2893,56 @@ private boolean isBluetoothDisallowed() {
*/
private void updateOppLauncherComponentState(UserHandle userHandle,
boolean bluetoothSharingDisallowed) {
final ComponentName oppLauncherComponent = new ComponentName(
mContext.getPackageManager().getPackagesForUid(Process.BLUETOOTH_UID)[0],
"com.android.bluetooth.opp.BluetoothOppLauncherActivity");
int newState;
if (bluetoothSharingDisallowed) {
newState = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
} else if (BluetoothProperties.isProfileOppEnabled().orElse(false)) {
newState = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
} else {
newState = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
}
try {
mContext.createContextAsUser(userHandle, 0)
.getPackageManager()
.setComponentEnabledSetting(oppLauncherComponent, newState,
PackageManager.DONT_KILL_APP);
int newState;
if (bluetoothSharingDisallowed) {
newState = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
} else if (BluetoothProperties.isProfileOppEnabled().orElse(false)) {
newState = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
} else {
newState = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
}

String launcherActivity = "com.android.bluetooth.opp.BluetoothOppLauncherActivity";

PackageManager packageManager = mContext.createContextAsUser(userHandle, 0)
.getPackageManager();
var allPackages = packageManager.getPackagesForUid(Process.BLUETOOTH_UID);
for (String candidatePackage : allPackages) {
PackageInfo packageInfo;
try {
// note: we need the package manager for the SYSTEM user, not our userHandle
packageInfo = mContext.getPackageManager().getPackageInfo(
candidatePackage,
PackageManager.PackageInfoFlags.of(PackageManager.GET_ACTIVITIES));
} catch (PackageManager.NameNotFoundException e) {
// ignore, try next package
Log.e(TAG, "Could not find package " + candidatePackage);
continue;
} catch (Exception e) {
Log.e(TAG, "Error while loading package" + e);
continue;
}
if (packageInfo.activities == null) {
continue;
}
for (var activity : packageInfo.activities) {
if (launcherActivity.equals(activity.name)) {
final ComponentName oppLauncherComponent = new ComponentName(
candidatePackage, launcherActivity
);
packageManager.setComponentEnabledSetting(
oppLauncherComponent, newState, PackageManager.DONT_KILL_APP
);
return;
}
}
}

Log.e(TAG,
"Cannot toggle BluetoothOppLauncherActivity, could not find it in any package");
} catch (Exception e) {
// The component was not found, do nothing.
Log.e(TAG, "updateOppLauncherComponentState failed: " + e);
}
}

Expand Down

0 comments on commit 9827aba

Please sign in to comment.