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

Commit 341ad63d authored by jackqdyulei's avatar jackqdyulei
Browse files

Make app predicates singleton

1. AppLabelPredicate
2. AppRestrictionPredicate

Change-Id: I1fd8611c5dd7ffa6318bcb23de7a973c111910b6
Fixes: 111323520
Test: RunSettingsRoboTests
parent d3e18dcf
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -136,8 +136,8 @@ public class BatteryTipUtils {
        final List<AppInfo> highUsageApps = BatteryDatabaseManager.getInstance(context)
                .queryAllAnomalies(timeAfterMs, AnomalyDatabaseHelper.State.NEW);
        // Remove it if it doesn't have label or been restricted
        highUsageApps.removeIf(
                new AppLabelPredicate(context).or(new AppRestrictionPredicate(context)));
        highUsageApps.removeIf(AppLabelPredicate.getInstance(context)
                .or(AppRestrictionPredicate.getInstance(context)));

        return highUsageApps;
    }
+2 −2
Original line number Diff line number Diff line
@@ -53,8 +53,8 @@ public class RestrictAppDetector implements BatteryTipDetector {
        mContext = context;
        mPolicy = policy;
        mBatteryDatabaseManager = BatteryDatabaseManager.getInstance(context);
        mAppRestrictionPredicate = new AppRestrictionPredicate(context);
        mAppLabelPredicate = new AppLabelPredicate(context);
        mAppRestrictionPredicate = AppRestrictionPredicate.getInstance(context);
        mAppLabelPredicate = AppLabelPredicate.getInstance(context);
    }

    @Override
+11 −4
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.settings.fuelgauge.batterytip.tips;

import android.app.AppOpsManager;
import android.content.Context;

import com.android.settings.Utils;
@@ -28,12 +27,20 @@ import java.util.function.Predicate;
 * {@link Predicate} for {@link AppInfo} to check whether it has label
 */
public class AppLabelPredicate implements Predicate<AppInfo> {

    private static AppLabelPredicate sInstance;
    private Context mContext;
    private AppOpsManager mAppOpsManager;

    public AppLabelPredicate(Context context) {
    public static AppLabelPredicate getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new AppLabelPredicate(context.getApplicationContext());
        }

        return sInstance;
    }

    private AppLabelPredicate(Context context) {
        mContext = context;
        mAppOpsManager = context.getSystemService(AppOpsManager.class);
    }

    @Override
+11 −1
Original line number Diff line number Diff line
@@ -27,9 +27,19 @@ import java.util.function.Predicate;
 * {@link Predicate} for {@link AppInfo} to check whether it is restricted.
 */
public class AppRestrictionPredicate implements Predicate<AppInfo> {

    private static AppRestrictionPredicate sInstance;
    private AppOpsManager mAppOpsManager;

    public AppRestrictionPredicate(Context context) {
    public static AppRestrictionPredicate getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new AppRestrictionPredicate(context.getApplicationContext());
        }

        return sInstance;
    }

    private AppRestrictionPredicate(Context context) {
        mAppOpsManager = context.getSystemService(AppOpsManager.class);
    }

+1 −1
Original line number Diff line number Diff line
@@ -112,7 +112,7 @@ public class RestrictAppTip extends BatteryTip {
        super.sanityCheck(context);

        // Set it invisible if there is no valid app
        mRestrictAppList.removeIf(new AppLabelPredicate(context));
        mRestrictAppList.removeIf(AppLabelPredicate.getInstance(context));
        if (mRestrictAppList.isEmpty()) {
            mState = StateType.INVISIBLE;
        }
Loading