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

Commit 1543e9a1 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Restrict appops when location is off" into sc-dev am: b0bcca6c

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/14490256

Change-Id: I84f136ed64af9eb641f0d0ca8001d50b313acf5d
parents 38375222 b0bcca6c
Loading
Loading
Loading
Loading
+46 −0
Original line number Diff line number Diff line
@@ -269,6 +269,13 @@ public class LocationManagerService extends ILocationManager.Stub {

        mInjector.getSettingsHelper().addOnLocationEnabledChangedListener(
                this::onLocationModeChanged);
        mInjector.getSettingsHelper().addOnIgnoreSettingsPackageWhitelistChangedListener(
                () -> refreshAppOpsRestrictions(UserHandle.USER_ALL));
        mInjector.getUserInfoHelper().addListener((userId, change) -> {
            if (change == UserInfoHelper.UserListener.USER_STARTED) {
                refreshAppOpsRestrictions(userId);
            }
        });

        // set up passive provider first since it will be required for all other location providers,
        // which are loaded later once the system is ready.
@@ -482,6 +489,8 @@ public class LocationManagerService extends ILocationManager.Stub {
                .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY)
                .addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        mContext.sendBroadcastAsUser(intent, UserHandle.of(userId));

        refreshAppOpsRestrictions(userId);
    }

    @Override
@@ -1347,6 +1356,43 @@ public class LocationManagerService extends ILocationManager.Stub {
        ipw.decreaseIndent();
    }

    private void refreshAppOpsRestrictions(int userId) {
        if (userId == UserHandle.USER_ALL) {
            final int[] runningUserIds = mInjector.getUserInfoHelper().getRunningUserIds();
            for (int i = 0; i < runningUserIds.length; i++) {
                refreshAppOpsRestrictions(runningUserIds[i]);
            }
            return;
        }

        Preconditions.checkArgument(userId >= 0);

        ArraySet<String> packages = new ArraySet<>();
        for (LocationProviderManager manager : mProviderManagers) {
            packages.add(manager.getIdentity().getPackageName());
        }
        packages.add(mContext.getPackageName());
        packages.addAll(mInjector.getSettingsHelper().getIgnoreSettingsPackageWhitelist());
        String[] allowedPackages = packages.toArray(new String[0]);

        boolean enabled = mInjector.getSettingsHelper().isLocationEnabled(userId);

        AppOpsManager appOpsManager = Objects.requireNonNull(
                mContext.getSystemService(AppOpsManager.class));
        appOpsManager.setUserRestrictionForUser(
                AppOpsManager.OP_COARSE_LOCATION,
                enabled,
                LocationManagerService.this,
                allowedPackages,
                userId);
        appOpsManager.setUserRestrictionForUser(
                AppOpsManager.OP_FINE_LOCATION,
                enabled,
                LocationManagerService.this,
                allowedPackages,
                userId);
    }

    private class LocalService extends LocationManagerInternal {

        LocalService() {}