Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9b9e7577 authored by William Escande's avatar William Escande Committed by Gerrit Code Review
Browse files

Merge changes Ib9530a88,I070987bc

* changes:
  Fix admin enable/disable of Bluetooth file sharing, part 2
  Fix admin enable/disable of Bluetooth file sharing
parents 3e23b5f8 e880ba62
Loading
Loading
Loading
Loading
+55 −16
Original line number Diff line number Diff line
@@ -63,6 +63,7 @@ import android.content.Intent;
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;
@@ -2972,9 +2973,7 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
     */
    private void updateOppLauncherComponentState(UserHandle userHandle,
            boolean bluetoothSharingDisallowed) {
        final ComponentName oppLauncherComponent = new ComponentName(
                mContext.getPackageManager().getPackagesForUid(Process.BLUETOOTH_UID)[0],
                "com.android.bluetooth.opp.BluetoothOppLauncherActivity");
        try {
            int newState;
            if (bluetoothSharingDisallowed) {
                newState = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
@@ -2983,13 +2982,53 @@ public class BluetoothManagerService extends IBluetoothManager.Stub {
            } else {
                newState = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
            }

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

            PackageManager systemPackageManager = mContext.getPackageManager();
            PackageManager userPackageManager = mContext.createContextAsUser(userHandle, 0)
                                                        .getPackageManager();
            var allPackages = systemPackageManager.getPackagesForUid(Process.BLUETOOTH_UID);
            for (String candidatePackage : allPackages) {
                Log.v(TAG, "Searching package " + candidatePackage);
                PackageInfo packageInfo;
                try {
            mContext.createContextAsUser(userHandle, 0)
                .getPackageManager()
                .setComponentEnabledSetting(oppLauncherComponent, newState,
                        PackageManager.DONT_KILL_APP);
                    packageInfo = systemPackageManager.getPackageInfo(
                        candidatePackage,
                        PackageManager.PackageInfoFlags.of(
                            PackageManager.GET_ACTIVITIES
                            | PackageManager.MATCH_ANY_USER
                            | PackageManager.MATCH_UNINSTALLED_PACKAGES
                            | PackageManager.MATCH_DISABLED_COMPONENTS));
                } 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) {
                    Log.v(TAG, "Checking activity " + activity.name);
                    if (launcherActivity.equals(activity.name)) {
                        final ComponentName oppLauncherComponent = new ComponentName(
                                candidatePackage, launcherActivity
                        );
                        userPackageManager.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);
        }
    }