Merge "Make app predicates singleton"

This commit is contained in:
TreeHugger Robot
2018-07-19 18:50:36 +00:00
committed by Android (Google) Code Review
10 changed files with 70 additions and 11 deletions

View File

@@ -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;
}

View File

@@ -54,8 +54,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

View File

@@ -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 Context mContext;
private AppOpsManager mAppOpsManager;
public AppLabelPredicate(Context context) {
private static AppLabelPredicate sInstance;
private Context mContext;
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

View File

@@ -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);
}

View File

@@ -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;
}