Merge "Remove context from powerUsageFeatureProvider" into main
diff --git a/src/com/android/settings/fuelgauge/AutoRestrictionPreferenceController.java b/src/com/android/settings/fuelgauge/AutoRestrictionPreferenceController.java
index 26fd398..25415f3 100644
--- a/src/com/android/settings/fuelgauge/AutoRestrictionPreferenceController.java
+++ b/src/com/android/settings/fuelgauge/AutoRestrictionPreferenceController.java
@@ -32,12 +32,12 @@
     private static final String KEY_SMART_BATTERY = "auto_restriction";
     private static final int ON = 1;
     private static final int OFF = 0;
-    private PowerUsageFeatureProvider mPowerUsageFeatureProvider;
+    private final PowerUsageFeatureProvider mPowerUsageFeatureProvider;
 
     public AutoRestrictionPreferenceController(Context context) {
         super(context, KEY_SMART_BATTERY);
-        mPowerUsageFeatureProvider = FeatureFactory.getFactory(
-                context).getPowerUsageFeatureProvider(context);
+        mPowerUsageFeatureProvider =
+                FeatureFactory.getFeatureFactory().getPowerUsageFeatureProvider();
     }
 
     @Override
diff --git a/src/com/android/settings/fuelgauge/BatteryBackupHelper.java b/src/com/android/settings/fuelgauge/BatteryBackupHelper.java
index 50f1b90..4dabe12 100644
--- a/src/com/android/settings/fuelgauge/BatteryBackupHelper.java
+++ b/src/com/android/settings/fuelgauge/BatteryBackupHelper.java
@@ -111,8 +111,8 @@
         writeBackupData(data, KEY_BUILD_MANUFACTURER, Build.MANUFACTURER);
         writeBackupData(data, KEY_BUILD_FINGERPRINT, Build.FINGERPRINT);
         // Add customized device build metadata fields.
-        final PowerUsageFeatureProvider provider = FeatureFactory.getFactory(mContext)
-                .getPowerUsageFeatureProvider(mContext);
+        final PowerUsageFeatureProvider provider =
+                FeatureFactory.getFeatureFactory().getPowerUsageFeatureProvider();
         writeBackupData(data, KEY_BUILD_METADATA_1, provider.getBuildMetadata1(mContext));
         writeBackupData(data, KEY_BUILD_METADATA_2, provider.getBuildMetadata2(mContext));
 
@@ -260,8 +260,8 @@
         if (mOptimizationModeBytes == null || mOptimizationModeBytes.length == 0) {
             return;
         }
-        final PowerUsageFeatureProvider provider = FeatureFactory.getFactory(mContext)
-                .getPowerUsageFeatureProvider(mContext);
+        final PowerUsageFeatureProvider provider =
+                FeatureFactory.getFeatureFactory().getPowerUsageFeatureProvider();
         if (!provider.isValidToRestoreOptimizationMode(mDeviceBuildInfoMap)) {
             return;
         }
diff --git a/src/com/android/settings/fuelgauge/BatteryInfo.java b/src/com/android/settings/fuelgauge/BatteryInfo.java
index 27d7154..9f8c2d0 100644
--- a/src/com/android/settings/fuelgauge/BatteryInfo.java
+++ b/src/com/android/settings/fuelgauge/BatteryInfo.java
@@ -104,8 +104,8 @@
 
                 // Add projection if we have an estimate.
                 if (remainingTimeUs != 0) {
-                    PowerUsageFeatureProvider provider = FeatureFactory.getFactory(context)
-                            .getPowerUsageFeatureProvider(context);
+                    PowerUsageFeatureProvider provider =
+                            FeatureFactory.getFeatureFactory().getPowerUsageFeatureProvider();
                     if (!mCharging && provider.isEnhancedBatteryPredictionEnabled(context)) {
                         points = provider.getEnhancedBatteryPredictionCurve(context, startTime);
                     } else {
@@ -201,7 +201,7 @@
 
         final long startTime = System.currentTimeMillis();
         PowerUsageFeatureProvider provider =
-                FeatureFactory.getFactory(context).getPowerUsageFeatureProvider(context);
+                FeatureFactory.getFeatureFactory().getPowerUsageFeatureProvider();
         final long elapsedRealtimeUs =
                 PowerUtil.convertMsToUs(SystemClock.elapsedRealtime());
 
@@ -211,8 +211,7 @@
         final boolean discharging =
                 batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0;
 
-        if (discharging && provider != null
-                && provider.isEnhancedBatteryPredictionEnabled(context)) {
+        if (discharging && provider.isEnhancedBatteryPredictionEnabled(context)) {
             Estimate estimate = provider.getEnhancedBatteryPrediction(context);
             if (estimate != null) {
                 Estimate.storeCachedEstimate(context, estimate);
diff --git a/src/com/android/settings/fuelgauge/BatteryUtils.java b/src/com/android/settings/fuelgauge/BatteryUtils.java
index 1f7e3ec..171c76b 100644
--- a/src/com/android/settings/fuelgauge/BatteryUtils.java
+++ b/src/com/android/settings/fuelgauge/BatteryUtils.java
@@ -135,8 +135,8 @@
         mContext = context;
         mPackageManager = context.getPackageManager();
         mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
-        mPowerUsageFeatureProvider = FeatureFactory.getFactory(context)
-                .getPowerUsageFeatureProvider(context);
+        mPowerUsageFeatureProvider =
+                FeatureFactory.getFeatureFactory().getPowerUsageFeatureProvider();
     }
 
     public long getProcessTimeMs(@StatusType int type, @Nullable BatteryStats.Uid uid,
@@ -626,8 +626,8 @@
             if (Settings.Global.getInt(context.getContentResolver(),
                     SETTINGS_GLOBAL_DOCK_DEFENDER_BYPASS, 0) == 1) {
                 return DockDefenderMode.TEMPORARILY_BYPASSED;
-            } else if (batteryInfo.isBatteryDefender && FeatureFactory.getFactory(context)
-                    .getPowerUsageFeatureProvider(context)
+            } else if (batteryInfo.isBatteryDefender && FeatureFactory.getFeatureFactory()
+                    .getPowerUsageFeatureProvider()
                     .isExtraDefend()) {
                 return DockDefenderMode.ACTIVE;
             } else if (!batteryInfo.isBatteryDefender) {
diff --git a/src/com/android/settings/fuelgauge/DebugEstimatesLoader.java b/src/com/android/settings/fuelgauge/DebugEstimatesLoader.java
index 62f2780..dda83a8 100644
--- a/src/com/android/settings/fuelgauge/DebugEstimatesLoader.java
+++ b/src/com/android/settings/fuelgauge/DebugEstimatesLoader.java
@@ -48,7 +48,7 @@
     public List<BatteryInfo> loadInBackground() {
         Context context = getContext();
         PowerUsageFeatureProvider powerUsageFeatureProvider =
-                FeatureFactory.getFactory(context).getPowerUsageFeatureProvider(context);
+                FeatureFactory.getFeatureFactory().getPowerUsageFeatureProvider();
 
         // get stuff we'll need for both BatteryInfo
         final long elapsedRealtimeUs = PowerUtil.convertMsToUs(
diff --git a/src/com/android/settings/fuelgauge/SmartBatteryPreferenceController.java b/src/com/android/settings/fuelgauge/SmartBatteryPreferenceController.java
index 26c0ab9..630cf82 100644
--- a/src/com/android/settings/fuelgauge/SmartBatteryPreferenceController.java
+++ b/src/com/android/settings/fuelgauge/SmartBatteryPreferenceController.java
@@ -37,12 +37,12 @@
     private static final String KEY_SMART_BATTERY = "smart_battery";
     private static final int ON = 1;
     private static final int OFF = 0;
-    private PowerUsageFeatureProvider mPowerUsageFeatureProvider;
+    private final PowerUsageFeatureProvider mPowerUsageFeatureProvider;
 
     public SmartBatteryPreferenceController(Context context) {
         super(context, KEY_SMART_BATTERY);
-        mPowerUsageFeatureProvider = FeatureFactory.getFactory(context)
-                .getPowerUsageFeatureProvider(context);
+        mPowerUsageFeatureProvider =
+                FeatureFactory.getFeatureFactory().getPowerUsageFeatureProvider();
     }
 
     @Override
diff --git a/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobService.java b/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobService.java
index 93a95f3..34fd72d 100644
--- a/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobService.java
+++ b/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionJobService.java
@@ -98,10 +98,10 @@
             final UserManager userManager = getSystemService(UserManager.class);
             final PowerAllowlistBackend powerAllowlistBackend =
                     PowerAllowlistBackend.getInstance(context);
-            final PowerUsageFeatureProvider powerUsageFeatureProvider = FeatureFactory
-                    .getFactory(this).getPowerUsageFeatureProvider(this);
-            final MetricsFeatureProvider metricsFeatureProvider = FeatureFactory
-                    .getFactory(this).getMetricsFeatureProvider();
+            final PowerUsageFeatureProvider powerUsageFeatureProvider =
+                    FeatureFactory.getFeatureFactory().getPowerUsageFeatureProvider();
+            final MetricsFeatureProvider metricsFeatureProvider =
+                    FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
 
             for (JobWorkItem item = dequeueWork(params); item != null; item = dequeueWork(params)) {
                 saveAnomalyToDatabase(context, userManager,
diff --git a/src/com/android/settings/fuelgauge/batterytip/detectors/BatteryDefenderDetector.java b/src/com/android/settings/fuelgauge/batterytip/detectors/BatteryDefenderDetector.java
index 8b7d4c1..55d2ee4 100644
--- a/src/com/android/settings/fuelgauge/batterytip/detectors/BatteryDefenderDetector.java
+++ b/src/com/android/settings/fuelgauge/batterytip/detectors/BatteryDefenderDetector.java
@@ -38,7 +38,7 @@
     @Override
     public BatteryTip detect() {
         final boolean isBasicBatteryDefend = mBatteryInfo.isBatteryDefender
-                && !FeatureFactory.getFactory(mContext).getPowerUsageFeatureProvider(mContext)
+                && !FeatureFactory.getFeatureFactory().getPowerUsageFeatureProvider()
                 .isExtraDefend();
         final int state = isBasicBatteryDefend
                 ? BatteryTip.StateType.NEW : BatteryTip.StateType.INVISIBLE;
diff --git a/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTip.java b/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTip.java
index 4a8f98b..536e5f8 100644
--- a/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTip.java
+++ b/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTip.java
@@ -109,8 +109,8 @@
 
     private void resumeCharging(Context context) {
         final Intent intent =
-                FeatureFactory.getFactory(context)
-                        .getPowerUsageFeatureProvider(context)
+                FeatureFactory.getFeatureFactory()
+                        .getPowerUsageFeatureProvider()
                         .getResumeChargeIntent(false);
         if (intent != null) {
             context.sendBroadcast(intent);
diff --git a/src/com/android/settings/fuelgauge/batterytip/tips/DockDefenderTip.java b/src/com/android/settings/fuelgauge/batterytip/tips/DockDefenderTip.java
index 327f974..c8cb8f2 100644
--- a/src/com/android/settings/fuelgauge/batterytip/tips/DockDefenderTip.java
+++ b/src/com/android/settings/fuelgauge/batterytip/tips/DockDefenderTip.java
@@ -153,8 +153,8 @@
 
     private void resumeCharging(Context context) {
         final Intent intent =
-                FeatureFactory.getFactory(context)
-                        .getPowerUsageFeatureProvider(context)
+                FeatureFactory.getFeatureFactory()
+                        .getPowerUsageFeatureProvider()
                         .getResumeChargeIntent(true);
         if (intent != null) {
             context.sendBroadcast(intent);
diff --git a/src/com/android/settings/fuelgauge/batteryusage/BatteryDiffData.java b/src/com/android/settings/fuelgauge/batteryusage/BatteryDiffData.java
index 47ae568..90a5f19 100644
--- a/src/com/android/settings/fuelgauge/batteryusage/BatteryDiffData.java
+++ b/src/com/android/settings/fuelgauge/batteryusage/BatteryDiffData.java
@@ -53,7 +53,7 @@
 
         if (!isAccumulated) {
             final PowerUsageFeatureProvider featureProvider =
-                    FeatureFactory.getFactory(context).getPowerUsageFeatureProvider(context);
+                    FeatureFactory.getFeatureFactory().getPowerUsageFeatureProvider();
             purgeBatteryDiffData(featureProvider);
             combineBatteryDiffEntry(
                     context, featureProvider, systemAppsPackageNames, systemAppsUids);
diff --git a/src/com/android/settings/fuelgauge/batteryusage/BatteryUsageBroadcastReceiver.java b/src/com/android/settings/fuelgauge/batteryusage/BatteryUsageBroadcastReceiver.java
index ed5f182..ab5ab8a 100644
--- a/src/com/android/settings/fuelgauge/batteryusage/BatteryUsageBroadcastReceiver.java
+++ b/src/com/android/settings/fuelgauge/batteryusage/BatteryUsageBroadcastReceiver.java
@@ -64,8 +64,8 @@
         final String action = intent.getAction();
         Log.d(TAG, "onReceive:" + action);
         DatabaseUtils.recordDateTime(context, action);
-        final String fullChargeIntentAction = FeatureFactory.getFactory(context)
-                .getPowerUsageFeatureProvider(context)
+        final String fullChargeIntentAction = FeatureFactory.getFeatureFactory()
+                .getPowerUsageFeatureProvider()
                 .getFullChargeIntentAction();
         switch (action) {
             case Intent.ACTION_BATTERY_LEVEL_CHANGED:
@@ -107,8 +107,8 @@
         }
 
         final boolean delayHourlyJobWhenBooting =
-                FeatureFactory.getFactory(context)
-                        .getPowerUsageFeatureProvider(context)
+                FeatureFactory.getFeatureFactory()
+                        .getPowerUsageFeatureProvider()
                         .delayHourlyJobWhenBooting();
         final long broadcastDelay = sBroadcastDelayFromBoot - SystemClock.elapsedRealtime();
         // If current boot time is smaller than expected delay, cancel sending the broadcast.
diff --git a/src/com/android/settings/fuelgauge/batteryusage/BootBroadcastReceiver.java b/src/com/android/settings/fuelgauge/batteryusage/BootBroadcastReceiver.java
index 920670f..67462b3 100644
--- a/src/com/android/settings/fuelgauge/batteryusage/BootBroadcastReceiver.java
+++ b/src/com/android/settings/fuelgauge/batteryusage/BootBroadcastReceiver.java
@@ -91,8 +91,8 @@
 
     private long getRescheduleTimeForBootAction(Context context) {
         final boolean delayHourlyJobWhenBooting =
-                FeatureFactory.getFactory(context)
-                        .getPowerUsageFeatureProvider(context)
+                FeatureFactory.getFeatureFactory()
+                        .getPowerUsageFeatureProvider()
                         .delayHourlyJobWhenBooting();
         return delayHourlyJobWhenBooting
                 ? RESCHEDULE_FOR_BOOT_ACTION_WITH_DELAY
diff --git a/src/com/android/settings/fuelgauge/batteryusage/DataProcessor.java b/src/com/android/settings/fuelgauge/batteryusage/DataProcessor.java
index 6914c30..6de3f59 100644
--- a/src/com/android/settings/fuelgauge/batteryusage/DataProcessor.java
+++ b/src/com/android/settings/fuelgauge/batteryusage/DataProcessor.java
@@ -321,8 +321,8 @@
         long numEventsFetched = 0;
         long numAllEventsFetched = 0;
         final Set<String> ignoreScreenOnTimeTaskRootSet =
-                FeatureFactory.getFactory(context)
-                        .getPowerUsageFeatureProvider(context)
+                FeatureFactory.getFeatureFactory()
+                        .getPowerUsageFeatureProvider()
                         .getIgnoreScreenOnTimeTaskRootSet();
         for (final long userId : usageEventsMap.keySet()) {
             final UsageEvents usageEvents = usageEventsMap.get(userId);
diff --git a/src/com/android/settings/fuelgauge/batteryusage/PeriodicJobManager.java b/src/com/android/settings/fuelgauge/batteryusage/PeriodicJobManager.java
index 8c0e66c..0e15093 100644
--- a/src/com/android/settings/fuelgauge/batteryusage/PeriodicJobManager.java
+++ b/src/com/android/settings/fuelgauge/batteryusage/PeriodicJobManager.java
@@ -97,8 +97,8 @@
     static long getTriggerAtMillis(Context context, Clock clock, final boolean fromBoot) {
         long currentTimeMillis = clock.millis();
         final boolean delayHourlyJobWhenBooting =
-                FeatureFactory.getFactory(context)
-                        .getPowerUsageFeatureProvider(context)
+                FeatureFactory.getFeatureFactory()
+                        .getPowerUsageFeatureProvider()
                         .delayHourlyJobWhenBooting();
         // Rounds to the previous nearest time slot and shifts to the next one.
         long timeSlotUnit = Duration.ofMinutes(DATA_FETCH_INTERVAL_MINUTE).toMillis();
diff --git a/src/com/android/settings/fuelgauge/batteryusage/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/batteryusage/PowerUsageSummary.java
index 0bec490..a0c5843 100644
--- a/src/com/android/settings/fuelgauge/batteryusage/PowerUsageSummary.java
+++ b/src/com/android/settings/fuelgauge/batteryusage/PowerUsageSummary.java
@@ -246,9 +246,7 @@
 
     @VisibleForTesting
     void initFeatureProvider() {
-        final Context context = getContext();
-        mPowerFeatureProvider = FeatureFactory.getFactory(context)
-                .getPowerUsageFeatureProvider(context);
+        mPowerFeatureProvider = FeatureFactory.getFeatureFactory().getPowerUsageFeatureProvider();
     }
 
     @VisibleForTesting
diff --git a/src/com/android/settings/overlay/FeatureFactory.kt b/src/com/android/settings/overlay/FeatureFactory.kt
index 3e0d088..9b1f0f4 100644
--- a/src/com/android/settings/overlay/FeatureFactory.kt
+++ b/src/com/android/settings/overlay/FeatureFactory.kt
@@ -68,7 +68,7 @@
 
     abstract val metricsFeatureProvider: MetricsFeatureProvider
 
-    abstract fun getPowerUsageFeatureProvider(context: Context): PowerUsageFeatureProvider
+    abstract val powerUsageFeatureProvider: PowerUsageFeatureProvider
 
     /**
      * Retrieves implementation for Battery Status feature.
diff --git a/src/com/android/settings/overlay/FeatureFactoryImpl.kt b/src/com/android/settings/overlay/FeatureFactoryImpl.kt
index 9955e63..f627c4f 100644
--- a/src/com/android/settings/overlay/FeatureFactoryImpl.kt
+++ b/src/com/android/settings/overlay/FeatureFactoryImpl.kt
@@ -79,8 +79,6 @@
         )
     }
 
-    private val powerUsageFeatureProvider by lazy { PowerUsageFeatureProviderImpl(appContext) }
-
     private val userFeatureProvider by lazy { UserFeatureProviderImpl(appContext) }
 
     private val contextualCardFeatureProvider by lazy {
@@ -92,7 +90,7 @@
 
     override val metricsFeatureProvider by lazy { SettingsMetricsFeatureProvider() }
 
-    override fun getPowerUsageFeatureProvider(context: Context) = powerUsageFeatureProvider
+    override val powerUsageFeatureProvider by lazy { PowerUsageFeatureProviderImpl(appContext) }
 
     override val batteryStatusFeatureProvider by lazy {
         BatteryStatusFeatureProviderImpl(appContext)
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/BatteryInfoLoaderTest.java b/tests/robotests/src/com/android/settings/fuelgauge/BatteryInfoLoaderTest.java
index 0dfabb9..b787239 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/BatteryInfoLoaderTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/BatteryInfoLoaderTest.java
@@ -57,7 +57,7 @@
     public void setUp() {
         MockitoAnnotations.initMocks(this);
         mContext = spy(RuntimeEnvironment.application);
-        FakeFeatureFactory.setupForTest().getPowerUsageFeatureProvider(mContext);
+        FakeFeatureFactory.setupForTest().getPowerUsageFeatureProvider();
 
         doReturn(mContext).when(mContext).getApplicationContext();
         when(mContext.getSystemService(eq(Context.BATTERY_STATS_SERVICE)))
diff --git a/tests/robotests/src/com/android/settings/testutils/FakeFeatureFactory.java b/tests/robotests/src/com/android/settings/testutils/FakeFeatureFactory.java
index 5bac5226..0f4c255 100644
--- a/tests/robotests/src/com/android/settings/testutils/FakeFeatureFactory.java
+++ b/tests/robotests/src/com/android/settings/testutils/FakeFeatureFactory.java
@@ -162,8 +162,9 @@
         return batterySettingsFeatureProvider;
     }
 
+    @NotNull
     @Override
-    public PowerUsageFeatureProvider getPowerUsageFeatureProvider(Context context) {
+    public PowerUsageFeatureProvider getPowerUsageFeatureProvider() {
         return powerUsageFeatureProvider;
     }
 
diff --git a/tests/spa_unit/src/com/android/settings/testutils/FakeFeatureFactory.kt b/tests/spa_unit/src/com/android/settings/testutils/FakeFeatureFactory.kt
index 2f3885b..f732bce 100644
--- a/tests/spa_unit/src/com/android/settings/testutils/FakeFeatureFactory.kt
+++ b/tests/spa_unit/src/com/android/settings/testutils/FakeFeatureFactory.kt
@@ -67,9 +67,8 @@
 
     override val metricsFeatureProvider = mockMetricsFeatureProvider
 
-    override fun getPowerUsageFeatureProvider(context: Context): PowerUsageFeatureProvider {
-        TODO("Not yet implemented")
-    }
+    override val powerUsageFeatureProvider: PowerUsageFeatureProvider
+        get() = TODO("Not yet implemented")
 
     override val batteryStatusFeatureProvider: BatteryStatusFeatureProvider
         get() = TODO("Not yet implemented")
diff --git a/tests/unit/src/com/android/settings/testutils/FakeFeatureFactory.java b/tests/unit/src/com/android/settings/testutils/FakeFeatureFactory.java
index ad62cd7..e0d3eca 100644
--- a/tests/unit/src/com/android/settings/testutils/FakeFeatureFactory.java
+++ b/tests/unit/src/com/android/settings/testutils/FakeFeatureFactory.java
@@ -161,8 +161,9 @@
         return batterySettingsFeatureProvider;
     }
 
+    @NotNull
     @Override
-    public PowerUsageFeatureProvider getPowerUsageFeatureProvider(Context context) {
+    public PowerUsageFeatureProvider getPowerUsageFeatureProvider() {
         return powerUsageFeatureProvider;
     }