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

Commit bf695389 authored by Automerger Merge Worker's avatar Automerger Merge Worker
Browse files

Merge "Delay initialization of RarelyUsedPackages and package sampling." into...

Merge "Delay initialization of RarelyUsedPackages and package sampling." into rvc-dev am: 86e27477

Change-Id: Iad2e11463f83438816599e6a589bd404f06da29f
parents c7bb3884 86e27477
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -8486,6 +8486,7 @@ message RuntimeAppOpAccess {
        DEFAULT = 0;
        DEFAULT = 0;
        UNIFORM = 1;
        UNIFORM = 1;
        RARELY_USED = 2;
        RARELY_USED = 2;
        BOOT_TIME_SAMPLING = 3;
    }
    }


    // sampling strategy used to collect this message
    // sampling strategy used to collect this message
+6 −1
Original line number Original line Diff line number Diff line
@@ -696,6 +696,10 @@ public class AppOpsManager {
    public static final int SAMPLING_STRATEGY_RARELY_USED =
    public static final int SAMPLING_STRATEGY_RARELY_USED =
            FrameworkStatsLog.RUNTIME_APP_OP_ACCESS__SAMPLING_STRATEGY__RARELY_USED;
            FrameworkStatsLog.RUNTIME_APP_OP_ACCESS__SAMPLING_STRATEGY__RARELY_USED;


    /** @hide */
    public static final int SAMPLING_STRATEGY_BOOT_TIME_SAMPLING =
            FrameworkStatsLog.RUNTIME_APP_OP_ACCESS__SAMPLING_STRATEGY__BOOT_TIME_SAMPLING;

    /**
    /**
     * Strategies used for message sampling
     * Strategies used for message sampling
     * @hide
     * @hide
@@ -704,7 +708,8 @@ public class AppOpsManager {
    @IntDef(prefix = {"SAMPLING_STRATEGY_"}, value = {
    @IntDef(prefix = {"SAMPLING_STRATEGY_"}, value = {
            SAMPLING_STRATEGY_DEFAULT,
            SAMPLING_STRATEGY_DEFAULT,
            SAMPLING_STRATEGY_UNIFORM,
            SAMPLING_STRATEGY_UNIFORM,
            SAMPLING_STRATEGY_RARELY_USED
            SAMPLING_STRATEGY_RARELY_USED,
            SAMPLING_STRATEGY_BOOT_TIME_SAMPLING
    })
    })
    public @interface SamplingStrategy {}
    public @interface SamplingStrategy {}


+20 −12
Original line number Original line Diff line number Diff line
@@ -41,6 +41,7 @@ import static android.app.AppOpsManager.OP_PLAY_AUDIO;
import static android.app.AppOpsManager.OP_RECORD_AUDIO;
import static android.app.AppOpsManager.OP_RECORD_AUDIO;
import static android.app.AppOpsManager.OpEventProxyInfo;
import static android.app.AppOpsManager.OpEventProxyInfo;
import static android.app.AppOpsManager.RestrictionBypass;
import static android.app.AppOpsManager.RestrictionBypass;
import static android.app.AppOpsManager.SAMPLING_STRATEGY_BOOT_TIME_SAMPLING;
import static android.app.AppOpsManager.SAMPLING_STRATEGY_RARELY_USED;
import static android.app.AppOpsManager.SAMPLING_STRATEGY_RARELY_USED;
import static android.app.AppOpsManager.SAMPLING_STRATEGY_UNIFORM;
import static android.app.AppOpsManager.SAMPLING_STRATEGY_UNIFORM;
import static android.app.AppOpsManager.UID_STATE_BACKGROUND;
import static android.app.AppOpsManager.UID_STATE_BACKGROUND;
@@ -244,6 +245,7 @@ public class AppOpsService extends IAppOpsService.Stub {


    private static final int MAX_UNFORWARED_OPS = 10;
    private static final int MAX_UNFORWARED_OPS = 10;
    private static final int MAX_UNUSED_POOLED_OBJECTS = 3;
    private static final int MAX_UNUSED_POOLED_OBJECTS = 3;
    private static final int RARELY_USED_PACKAGES_INITIALIZATION_DELAY_MILLIS = 300000;


    //TODO: remove this when development is done.
    //TODO: remove this when development is done.
    private static final int TEMP_PROCESS_CAPABILITY_FOREGROUND_LOCATION = 1 << 31;
    private static final int TEMP_PROCESS_CAPABILITY_FOREGROUND_LOCATION = 1 << 31;
@@ -1657,17 +1659,14 @@ public class AppOpsService extends IAppOpsService.Stub {
            }
            }
        }, packageAddedFilter);
        }, packageAddedFilter);


        List<String> packageNames = getPackageNamesForSampling();
        mHandler.postDelayed(new Runnable() {
        synchronized (this) {
            resamplePackageAndAppOpLocked(packageNames);
        }

        AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
            @Override
            @Override
            public void run() {
            public void run() {
                List<String> packageNames = getPackageNamesForSampling();
                resamplePackageAndAppOpLocked(packageNames);
                initializeRarelyUsedPackagesList(new ArraySet<>(packageNames));
                initializeRarelyUsedPackagesList(new ArraySet<>(packageNames));
            }
            }
        });
        }, RARELY_USED_PACKAGES_INITIALIZATION_DELAY_MILLIS);


        PackageManagerInternal packageManagerInternal = LocalServices.getService(
        PackageManagerInternal packageManagerInternal = LocalServices.getService(
                PackageManagerInternal.class);
                PackageManagerInternal.class);
@@ -5618,7 +5617,7 @@ public class AppOpsService extends IAppOpsService.Stub {
        int uid = Binder.getCallingUid();
        int uid = Binder.getCallingUid();
        Objects.requireNonNull(packageName);
        Objects.requireNonNull(packageName);
        synchronized (this) {
        synchronized (this) {
            switchPackageIfRarelyUsedLocked(packageName);
            switchPackageIfBootTimeOrRarelyUsedLocked(packageName);
            if (!packageName.equals(mSampledPackage)) {
            if (!packageName.equals(mSampledPackage)) {
                return new MessageSamplingConfig(OP_NONE, 0,
                return new MessageSamplingConfig(OP_NONE, 0,
                        Instant.now().plus(1, ChronoUnit.HOURS).toEpochMilli());
                        Instant.now().plus(1, ChronoUnit.HOURS).toEpochMilli());
@@ -5648,7 +5647,7 @@ public class AppOpsService extends IAppOpsService.Stub {
    private void reportRuntimeAppOpAccessMessageAsyncLocked(int uid,
    private void reportRuntimeAppOpAccessMessageAsyncLocked(int uid,
            @NonNull String packageName, int opCode, @Nullable String attributionTag,
            @NonNull String packageName, int opCode, @Nullable String attributionTag,
            @NonNull String message) {
            @NonNull String message) {
        switchPackageIfRarelyUsedLocked(packageName);
        switchPackageIfBootTimeOrRarelyUsedLocked(packageName);
        if (!Objects.equals(mSampledPackage, packageName)) {
        if (!Objects.equals(mSampledPackage, packageName)) {
            return;
            return;
        }
        }
@@ -5699,11 +5698,16 @@ public class AppOpsService extends IAppOpsService.Stub {


    /**
    /**
     * Checks if package is in the list of rarely used package and starts watching the new package
     * Checks if package is in the list of rarely used package and starts watching the new package
     * to collect incoming message.
     * to collect incoming message or if collection is happening in first minutes since boot.
     * @param packageName
     * @param packageName
     */
     */
    private void switchPackageIfRarelyUsedLocked(@NonNull String packageName) {
    private void switchPackageIfBootTimeOrRarelyUsedLocked(@NonNull String packageName) {
        if (mRarelyUsedPackages.contains(packageName)) {
        if (mSampledPackage == null) {
            if (ThreadLocalRandom.current().nextFloat() < 0.1f) {
                mSamplingStrategy = SAMPLING_STRATEGY_BOOT_TIME_SAMPLING;
                resampleAppOpForPackageLocked(packageName);
            }
        } else if (mRarelyUsedPackages.contains(packageName)) {
            mRarelyUsedPackages.remove(packageName);
            mRarelyUsedPackages.remove(packageName);
            if (ThreadLocalRandom.current().nextFloat() < 0.5f) {
            if (ThreadLocalRandom.current().nextFloat() < 0.5f) {
                mSamplingStrategy = SAMPLING_STRATEGY_RARELY_USED;
                mSamplingStrategy = SAMPLING_STRATEGY_RARELY_USED;
@@ -5765,6 +5769,10 @@ public class AppOpsService extends IAppOpsService.Stub {
                            }
                            }
                        }
                        }
                        synchronized (this) {
                        synchronized (this) {
                            int numPkgs = mRarelyUsedPackages.size();
                            for (int i = 0; i < numPkgs; i++) {
                                candidates.add(mRarelyUsedPackages.valueAt(i));
                            }
                            mRarelyUsedPackages = candidates;
                            mRarelyUsedPackages = candidates;
                        }
                        }
                    }
                    }