Merge "Change ranking update aconfig flag to use main bug" into main
diff --git a/apex/jobscheduler/service/java/com/android/server/job/controllers/IdleController.java b/apex/jobscheduler/service/java/com/android/server/job/controllers/IdleController.java
index a25af71..47d3fd5 100644
--- a/apex/jobscheduler/service/java/com/android/server/job/controllers/IdleController.java
+++ b/apex/jobscheduler/service/java/com/android/server/job/controllers/IdleController.java
@@ -18,13 +18,16 @@
 
 import static com.android.server.job.JobSchedulerService.sElapsedRealtimeClock;
 
+import android.annotation.NonNull;
 import android.content.Context;
 import android.content.pm.PackageManager;
 import android.os.UserHandle;
+import android.provider.DeviceConfig;
 import android.util.ArraySet;
 import android.util.IndentingPrintWriter;
 import android.util.proto.ProtoOutputStream;
 
+import com.android.internal.annotations.GuardedBy;
 import com.android.server.job.JobSchedulerService;
 import com.android.server.job.StateControllerProto;
 import com.android.server.job.controllers.idle.CarIdlenessTracker;
@@ -89,6 +92,19 @@
         }
     }
 
+    @Override
+    public void processConstantLocked(@NonNull DeviceConfig.Properties properties,
+            @NonNull String key) {
+        mIdleTracker.processConstant(properties, key);
+    }
+
+    @Override
+    @GuardedBy("mLock")
+    public void onBatteryStateChangedLocked() {
+        mIdleTracker.onBatteryStateChanged(
+                mService.isBatteryCharging(), mService.isBatteryNotLow());
+    }
+
     /**
      * State-change notifications from the idleness tracker
      */
@@ -119,7 +135,16 @@
         } else {
             mIdleTracker = new DeviceIdlenessTracker();
         }
-        mIdleTracker.startTracking(ctx, this);
+        mIdleTracker.startTracking(ctx, mService, this);
+    }
+
+    @Override
+    public void dumpConstants(IndentingPrintWriter pw) {
+        pw.println();
+        pw.println("IdleController:");
+        pw.increaseIndent();
+        mIdleTracker.dumpConstants(pw);
+        pw.decreaseIndent();
     }
 
     @Override
diff --git a/apex/jobscheduler/service/java/com/android/server/job/controllers/idle/CarIdlenessTracker.java b/apex/jobscheduler/service/java/com/android/server/job/controllers/idle/CarIdlenessTracker.java
index c458cae..ba0e633 100644
--- a/apex/jobscheduler/service/java/com/android/server/job/controllers/idle/CarIdlenessTracker.java
+++ b/apex/jobscheduler/service/java/com/android/server/job/controllers/idle/CarIdlenessTracker.java
@@ -16,10 +16,13 @@
 
 package com.android.server.job.controllers.idle;
 
+import android.annotation.NonNull;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.provider.DeviceConfig;
+import android.util.IndentingPrintWriter;
 import android.util.Log;
 import android.util.Slog;
 import android.util.proto.ProtoOutputStream;
@@ -73,7 +76,8 @@
     }
 
     @Override
-    public void startTracking(Context context, IdlenessListener listener) {
+    public void startTracking(Context context, JobSchedulerService service,
+            IdlenessListener listener) {
         mIdleListener = listener;
 
         IntentFilter filter = new IntentFilter();
@@ -94,6 +98,15 @@
         context.registerReceiver(this, filter, null, AppSchedulingModuleThread.getHandler());
     }
 
+    /** Process the specified constant and update internal constants if relevant. */
+    public void processConstant(@NonNull DeviceConfig.Properties properties,
+            @NonNull String key) {
+    }
+
+    @Override
+    public void onBatteryStateChanged(boolean isCharging, boolean isBatteryNotLow) {
+    }
+
     @Override
     public void dump(PrintWriter pw) {
         pw.print("  mIdle: "); pw.println(mIdle);
@@ -119,6 +132,10 @@
     }
 
     @Override
+    public void dumpConstants(IndentingPrintWriter pw) {
+    }
+
+    @Override
     public void onReceive(Context context, Intent intent) {
         final String action = intent.getAction();
         logIfDebug("Received action: " + action);
diff --git a/apex/jobscheduler/service/java/com/android/server/job/controllers/idle/DeviceIdlenessTracker.java b/apex/jobscheduler/service/java/com/android/server/job/controllers/idle/DeviceIdlenessTracker.java
index c943e73..7dd3d13 100644
--- a/apex/jobscheduler/service/java/com/android/server/job/controllers/idle/DeviceIdlenessTracker.java
+++ b/apex/jobscheduler/service/java/com/android/server/job/controllers/idle/DeviceIdlenessTracker.java
@@ -17,9 +17,12 @@
 package com.android.server.job.controllers.idle;
 
 import static android.app.UiModeManager.PROJECTION_TYPE_NONE;
+import static android.text.format.DateUtils.HOUR_IN_MILLIS;
+import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
 
 import static com.android.server.job.JobSchedulerService.sElapsedRealtimeClock;
 
+import android.annotation.NonNull;
 import android.app.AlarmManager;
 import android.app.UiModeManager;
 import android.content.BroadcastReceiver;
@@ -27,10 +30,13 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.os.PowerManager;
+import android.provider.DeviceConfig;
+import android.util.IndentingPrintWriter;
 import android.util.Log;
 import android.util.Slog;
 import android.util.proto.ProtoOutputStream;
 
+import com.android.internal.annotations.VisibleForTesting;
 import com.android.server.AppSchedulingModuleThread;
 import com.android.server.am.ActivityManagerService;
 import com.android.server.job.JobSchedulerService;
@@ -45,17 +51,38 @@
     private static final boolean DEBUG = JobSchedulerService.DEBUG
             || Log.isLoggable(TAG, Log.DEBUG);
 
+    /** Prefix to use with all constant keys in order to "sub-namespace" the keys. */
+    private static final String IC_DIT_CONSTANT_PREFIX = "ic_dit_";
+    @VisibleForTesting
+    static final String KEY_INACTIVITY_IDLE_THRESHOLD_MS =
+            IC_DIT_CONSTANT_PREFIX + "inactivity_idle_threshold_ms";
+    @VisibleForTesting
+    static final String KEY_INACTIVITY_STABLE_POWER_IDLE_THRESHOLD_MS =
+            IC_DIT_CONSTANT_PREFIX + "inactivity_idle_stable_power_threshold_ms";
+    private static final String KEY_IDLE_WINDOW_SLOP_MS =
+            IC_DIT_CONSTANT_PREFIX + "idle_window_slop_ms";
+
     private AlarmManager mAlarm;
     private PowerManager mPowerManager;
 
     // After construction, mutations of idle/screen-on/projection states will only happen
     // on the JobScheduler thread, either in onReceive(), in an alarm callback, or in on.*Changed.
     private long mInactivityIdleThreshold;
+    private long mInactivityStablePowerIdleThreshold;
     private long mIdleWindowSlop;
+    /** Stable power is defined as "charging + battery not low." */
+    private boolean mIsStablePower;
     private boolean mIdle;
     private boolean mScreenOn;
     private boolean mDockIdle;
     private boolean mProjectionActive;
+
+    /**
+     * Time (in the elapsed realtime timebase) when the idleness check was scheduled. This should
+     * be a negative value if the device is not in state to be considered idle.
+     */
+    private long mIdlenessCheckScheduledElapsed = -1;
+
     private IdlenessListener mIdleListener;
     private final UiModeManager.OnProjectionStateChangedListener mOnProjectionStateChangedListener =
             this::onProjectionStateChanged;
@@ -76,10 +103,14 @@
     }
 
     @Override
-    public void startTracking(Context context, IdlenessListener listener) {
+    public void startTracking(Context context, JobSchedulerService service,
+            IdlenessListener listener) {
         mIdleListener = listener;
         mInactivityIdleThreshold = context.getResources().getInteger(
                 com.android.internal.R.integer.config_jobSchedulerInactivityIdleThreshold);
+        mInactivityStablePowerIdleThreshold = context.getResources().getInteger(
+                com.android.internal.R.integer
+                        .config_jobSchedulerInactivityIdleThresholdOnStablePower);
         mIdleWindowSlop = context.getResources().getInteger(
                 com.android.internal.R.integer.config_jobSchedulerIdleWindowSlop);
         mAlarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
@@ -107,6 +138,46 @@
         context.getSystemService(UiModeManager.class).addOnProjectionStateChangedListener(
                 UiModeManager.PROJECTION_TYPE_ALL, AppSchedulingModuleThread.getExecutor(),
                 mOnProjectionStateChangedListener);
+
+        mIsStablePower = service.isBatteryCharging() && service.isBatteryNotLow();
+    }
+
+    /** Process the specified constant and update internal constants if relevant. */
+    public void processConstant(@NonNull DeviceConfig.Properties properties,
+            @NonNull String key) {
+        switch (key) {
+            case KEY_INACTIVITY_IDLE_THRESHOLD_MS:
+                // Keep the threshold in the range [1 minute, 4 hours].
+                mInactivityIdleThreshold = Math.max(MINUTE_IN_MILLIS, Math.min(4 * HOUR_IN_MILLIS,
+                        properties.getLong(key, mInactivityIdleThreshold)));
+                // Don't bother updating any pending alarms. Just wait until the next time we
+                // attempt to check for idle state to use the new value.
+                break;
+            case KEY_INACTIVITY_STABLE_POWER_IDLE_THRESHOLD_MS:
+                // Keep the threshold in the range [1 minute, 4 hours].
+                mInactivityStablePowerIdleThreshold = Math.max(MINUTE_IN_MILLIS,
+                        Math.min(4 * HOUR_IN_MILLIS,
+                                properties.getLong(key, mInactivityStablePowerIdleThreshold)));
+                // Don't bother updating any pending alarms. Just wait until the next time we
+                // attempt to check for idle state to use the new value.
+                break;
+            case KEY_IDLE_WINDOW_SLOP_MS:
+                // Keep the slop in the range [1 minute, 15 minutes].
+                mIdleWindowSlop = Math.max(MINUTE_IN_MILLIS, Math.min(15 * MINUTE_IN_MILLIS,
+                        properties.getLong(key, mIdleWindowSlop)));
+                // Don't bother updating any pending alarms. Just wait until the next time we
+                // attempt to check for idle state to use the new value.
+                break;
+        }
+    }
+
+    @Override
+    public void onBatteryStateChanged(boolean isCharging, boolean isBatteryNotLow) {
+        final boolean isStablePower = isCharging && isBatteryNotLow;
+        if (mIsStablePower != isStablePower) {
+            mIsStablePower = isStablePower;
+            maybeScheduleIdlenessCheck("stable power changed");
+        }
     }
 
     private void onProjectionStateChanged(@UiModeManager.ProjectionType int activeProjectionTypes,
@@ -134,8 +205,10 @@
     public void dump(PrintWriter pw) {
         pw.print("  mIdle: "); pw.println(mIdle);
         pw.print("  mScreenOn: "); pw.println(mScreenOn);
+        pw.print("  mIsStablePower: "); pw.println(mIsStablePower);
         pw.print("  mDockIdle: "); pw.println(mDockIdle);
         pw.print("  mProjectionActive: "); pw.println(mProjectionActive);
+        pw.print("  mIdlenessCheckScheduledElapsed: "); pw.println(mIdlenessCheckScheduledElapsed);
     }
 
     @Override
@@ -162,6 +235,17 @@
     }
 
     @Override
+    public void dumpConstants(IndentingPrintWriter pw) {
+        pw.println("DeviceIdlenessTracker:");
+        pw.increaseIndent();
+        pw.print(KEY_INACTIVITY_IDLE_THRESHOLD_MS, mInactivityIdleThreshold).println();
+        pw.print(KEY_INACTIVITY_STABLE_POWER_IDLE_THRESHOLD_MS, mInactivityStablePowerIdleThreshold)
+                .println();
+        pw.print(KEY_IDLE_WINDOW_SLOP_MS, mIdleWindowSlop).println();
+        pw.decreaseIndent();
+    }
+
+    @Override
     public void onReceive(Context context, Intent intent) {
         final String action = intent.getAction();
         if (DEBUG) {
@@ -220,9 +304,24 @@
     private void maybeScheduleIdlenessCheck(String reason) {
         if ((!mScreenOn || mDockIdle) && !mProjectionActive) {
             final long nowElapsed = sElapsedRealtimeClock.millis();
-            final long when = nowElapsed + mInactivityIdleThreshold;
+            final long inactivityThresholdMs = mIsStablePower
+                    ? mInactivityStablePowerIdleThreshold : mInactivityIdleThreshold;
+            if (mIdlenessCheckScheduledElapsed >= 0) {
+                if (mIdlenessCheckScheduledElapsed + inactivityThresholdMs <= nowElapsed) {
+                    if (DEBUG) {
+                        Slog.v(TAG, "Previous idle check @ " + mIdlenessCheckScheduledElapsed
+                                + " allows device to be idle now");
+                    }
+                    handleIdleTrigger();
+                    return;
+                }
+            } else {
+                mIdlenessCheckScheduledElapsed = nowElapsed;
+            }
+            final long when = mIdlenessCheckScheduledElapsed + inactivityThresholdMs;
             if (DEBUG) {
-                Slog.v(TAG, "Scheduling idle : " + reason + " now:" + nowElapsed + " when=" + when);
+                Slog.v(TAG, "Scheduling idle : " + reason + " now:" + nowElapsed
+                        + " checkElapsed=" + mIdlenessCheckScheduledElapsed + " when=" + when);
             }
             mAlarm.setWindow(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                     when, mIdleWindowSlop, "JS idleness",
@@ -232,6 +331,7 @@
 
     private void cancelIdlenessCheck() {
         mAlarm.cancel(mIdleAlarmListener);
+        mIdlenessCheckScheduledElapsed = -1;
     }
 
     private void handleIdleTrigger() {
diff --git a/apex/jobscheduler/service/java/com/android/server/job/controllers/idle/IdlenessTracker.java b/apex/jobscheduler/service/java/com/android/server/job/controllers/idle/IdlenessTracker.java
index cdab7e5..92ad4df 100644
--- a/apex/jobscheduler/service/java/com/android/server/job/controllers/idle/IdlenessTracker.java
+++ b/apex/jobscheduler/service/java/com/android/server/job/controllers/idle/IdlenessTracker.java
@@ -16,9 +16,14 @@
 
 package com.android.server.job.controllers.idle;
 
+import android.annotation.NonNull;
 import android.content.Context;
+import android.provider.DeviceConfig;
+import android.util.IndentingPrintWriter;
 import android.util.proto.ProtoOutputStream;
 
+import com.android.server.job.JobSchedulerService;
+
 import java.io.PrintWriter;
 
 public interface IdlenessTracker {
@@ -29,7 +34,7 @@
      * non-interacting state.  When the idle state changes thereafter, the given
      * listener must be called to report the new state.
      */
-    void startTracking(Context context, IdlenessListener listener);
+    void startTracking(Context context, JobSchedulerService service, IdlenessListener listener);
 
     /**
      * Report whether the device is currently considered "idle" for purposes of
@@ -40,6 +45,12 @@
      */
     boolean isIdle();
 
+    /** Process the specified constant and update internal constants if relevant. */
+    void processConstant(@NonNull DeviceConfig.Properties properties, @NonNull String key);
+
+    /** Called when the battery state changes. */
+    void onBatteryStateChanged(boolean isCharging, boolean isBatteryNotLow);
+
     /**
      * Dump useful information about tracked idleness-related state in plaintext.
      */
@@ -49,4 +60,7 @@
      * Dump useful information about tracked idleness-related state to proto.
      */
     void dump(ProtoOutputStream proto, long fieldId);
+
+    /** Dump any internal constants the tracker may have. */
+    void dumpConstants(IndentingPrintWriter pw);
 }
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index dbce054..862e537 100644
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -4177,6 +4177,10 @@
     <!-- Inactivity threshold (in milliseconds) used in JobScheduler. JobScheduler will consider
          the device to be "idle" after being inactive for this long. -->
     <integer name="config_jobSchedulerInactivityIdleThreshold">1860000</integer>
+    <!-- Inactivity threshold (in milliseconds) used in JobScheduler. JobScheduler will consider
+         the device to be "idle" after being inactive for this long if the device is on stable
+         power. Stable power is defined as "charging + battery not low". -->
+    <integer name="config_jobSchedulerInactivityIdleThresholdOnStablePower">1860000</integer>
     <!-- The alarm window (in milliseconds) that JobScheduler uses to enter the idle state -->
     <integer name="config_jobSchedulerIdleWindowSlop">300000</integer>
 
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index a2f0086..e646548 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -2882,6 +2882,7 @@
   <java-symbol type="integer" name="config_defaultNightMode" />
 
   <java-symbol type="integer" name="config_jobSchedulerInactivityIdleThreshold" />
+  <java-symbol type="integer" name="config_jobSchedulerInactivityIdleThresholdOnStablePower" />
   <java-symbol type="integer" name="config_jobSchedulerIdleWindowSlop" />
   <java-symbol type="bool" name="config_jobSchedulerRestrictBackgroundUser" />
   <java-symbol type="integer" name="config_jobSchedulerUserGracePeriod" />
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/custom/data/repository/CustomTilePackageUpdatesRepository.kt b/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/custom/data/repository/CustomTilePackageUpdatesRepository.kt
new file mode 100644
index 0000000..6d7d88f
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/custom/data/repository/CustomTilePackageUpdatesRepository.kt
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.qs.tiles.impl.custom.data.repository
+
+import android.os.UserHandle
+import com.android.systemui.common.coroutine.ConflatedCallbackFlow
+import com.android.systemui.qs.external.TileServiceManager
+import com.android.systemui.qs.pipeline.shared.TileSpec
+import com.android.systemui.qs.tiles.impl.custom.di.bound.CustomTileBoundScope
+import com.android.systemui.qs.tiles.impl.custom.di.bound.CustomTileUser
+import javax.inject.Inject
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.channels.awaitClose
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.SharingStarted
+import kotlinx.coroutines.flow.onEach
+import kotlinx.coroutines.flow.shareIn
+
+interface CustomTilePackageUpdatesRepository {
+
+    val packageChanges: Flow<Unit>
+}
+
+@CustomTileBoundScope
+class CustomTilePackageUpdatesRepositoryImpl
+@Inject
+constructor(
+    tileSpec: TileSpec.CustomTileSpec,
+    @CustomTileUser user: UserHandle,
+    serviceManager: TileServiceManager,
+    defaultsRepository: CustomTileDefaultsRepository,
+    @CustomTileBoundScope boundScope: CoroutineScope,
+) : CustomTilePackageUpdatesRepository {
+
+    override val packageChanges: Flow<Unit> =
+        ConflatedCallbackFlow.conflatedCallbackFlow {
+                serviceManager.setTileChangeListener { changedComponentName ->
+                    if (changedComponentName == tileSpec.componentName) {
+                        trySend(Unit)
+                    }
+                }
+
+                awaitClose { serviceManager.setTileChangeListener(null) }
+            }
+            .onEach { defaultsRepository.requestNewDefaults(user, tileSpec.componentName, true) }
+            .shareIn(boundScope, SharingStarted.WhileSubscribed())
+}
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/custom/di/bound/CustomTileBoundComponent.kt b/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/custom/di/bound/CustomTileBoundComponent.kt
index e33b3e9..d382d20 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/custom/di/bound/CustomTileBoundComponent.kt
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/custom/di/bound/CustomTileBoundComponent.kt
@@ -23,7 +23,7 @@
 
 /** @see CustomTileBoundScope */
 @CustomTileBoundScope
-@Subcomponent
+@Subcomponent(modules = [CustomTileBoundModule::class])
 interface CustomTileBoundComponent {
 
     @Subcomponent.Builder
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/custom/di/bound/CustomTileBoundModule.kt b/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/custom/di/bound/CustomTileBoundModule.kt
new file mode 100644
index 0000000..889424a
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/custom/di/bound/CustomTileBoundModule.kt
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.qs.tiles.impl.custom.di.bound
+
+import com.android.systemui.qs.tiles.impl.custom.data.repository.CustomTilePackageUpdatesRepository
+import com.android.systemui.qs.tiles.impl.custom.data.repository.CustomTilePackageUpdatesRepositoryImpl
+import dagger.Binds
+import dagger.Module
+
+@Module
+interface CustomTileBoundModule {
+
+    @Binds
+    fun bindCustomTilePackageUpdatesRepository(
+        impl: CustomTilePackageUpdatesRepositoryImpl
+    ): CustomTilePackageUpdatesRepository
+}
diff --git a/packages/SystemUI/src/com/android/systemui/shade/NotificationShadeWindowViewController.java b/packages/SystemUI/src/com/android/systemui/shade/NotificationShadeWindowViewController.java
index a2627ed..a2ca49d 100644
--- a/packages/SystemUI/src/com/android/systemui/shade/NotificationShadeWindowViewController.java
+++ b/packages/SystemUI/src/com/android/systemui/shade/NotificationShadeWindowViewController.java
@@ -113,6 +113,7 @@
     private final SysUIKeyEventHandler mSysUIKeyEventHandler;
     private final PrimaryBouncerInteractor mPrimaryBouncerInteractor;
     private final AlternateBouncerInteractor mAlternateBouncerInteractor;
+    private final QuickSettingsController mQuickSettingsController;
     private GestureDetector mPulsingWakeupGestureHandler;
     private GestureDetector mDreamingWakeupGestureHandler;
     private View mBrightnessMirror;
@@ -188,6 +189,7 @@
             BouncerMessageInteractor bouncerMessageInteractor,
             BouncerLogger bouncerLogger,
             SysUIKeyEventHandler sysUIKeyEventHandler,
+            QuickSettingsController quickSettingsController,
             PrimaryBouncerInteractor primaryBouncerInteractor,
             AlternateBouncerInteractor alternateBouncerInteractor,
             SelectedUserInteractor selectedUserInteractor) {
@@ -220,6 +222,7 @@
         mSysUIKeyEventHandler = sysUIKeyEventHandler;
         mPrimaryBouncerInteractor = primaryBouncerInteractor;
         mAlternateBouncerInteractor = alternateBouncerInteractor;
+        mQuickSettingsController = quickSettingsController;
 
         // This view is not part of the newly inflated expanded status bar.
         mBrightnessMirror = mView.findViewById(R.id.brightness_mirror_container);
@@ -454,6 +457,16 @@
                         && !bouncerShowing
                         && !mStatusBarStateController.isDozing()) {
                     if (mDragDownHelper.isDragDownEnabled()) {
+                        if (mFeatureFlagsClassic.isEnabled(Flags.MIGRATE_NSSL)) {
+                            // When on lockscreen, if the touch originates at the top of the screen
+                            // go directly to QS and not the shade
+                            if (mQuickSettingsController.shouldQuickSettingsIntercept(
+                                    ev.getX(), ev.getY(), 0)) {
+                                mShadeLogger.d("NSWVC: QS intercepted");
+                                return true;
+                            }
+                        }
+
                         // This handles drag down over lockscreen
                         boolean result = mDragDownHelper.onInterceptTouchEvent(ev);
                         if (mFeatureFlagsClassic.isEnabled(Flags.MIGRATE_NSSL)) {
diff --git a/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/impl/custom/CustomTilePackageUpdatesRepositoryTest.kt b/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/impl/custom/CustomTilePackageUpdatesRepositoryTest.kt
new file mode 100644
index 0000000..4a22113
--- /dev/null
+++ b/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/impl/custom/CustomTilePackageUpdatesRepositoryTest.kt
@@ -0,0 +1,119 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.qs.tiles.impl.custom
+
+import android.content.ComponentName
+import android.os.UserHandle
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import androidx.test.filters.SmallTest
+import com.android.systemui.SysuiTestCase
+import com.android.systemui.qs.external.TileLifecycleManager
+import com.android.systemui.qs.external.TileServiceManager
+import com.android.systemui.qs.pipeline.shared.TileSpec
+import com.android.systemui.qs.tiles.impl.custom.data.repository.CustomTilePackageUpdatesRepository
+import com.android.systemui.qs.tiles.impl.custom.data.repository.CustomTilePackageUpdatesRepositoryImpl
+import com.android.systemui.qs.tiles.impl.custom.data.repository.FakeCustomTileDefaultsRepository
+import com.android.systemui.qs.tiles.impl.custom.data.repository.FakeCustomTileDefaultsRepository.DefaultsRequest
+import com.android.systemui.util.mockito.capture
+import com.google.common.truth.Truth.assertThat
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.flow.launchIn
+import kotlinx.coroutines.flow.onEach
+import kotlinx.coroutines.test.StandardTestDispatcher
+import kotlinx.coroutines.test.TestScope
+import kotlinx.coroutines.test.runCurrent
+import kotlinx.coroutines.test.runTest
+import org.junit.Before
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.mockito.ArgumentCaptor
+import org.mockito.Captor
+import org.mockito.Mock
+import org.mockito.Mockito.verify
+import org.mockito.MockitoAnnotations
+
+@OptIn(ExperimentalCoroutinesApi::class)
+@SmallTest
+@RunWith(AndroidJUnit4::class)
+class CustomTilePackageUpdatesRepositoryTest : SysuiTestCase() {
+
+    @Mock private lateinit var tileServiceManager: TileServiceManager
+
+    @Captor
+    private lateinit var listenerCaptor: ArgumentCaptor<TileLifecycleManager.TileChangeListener>
+
+    private val defaultsRepository = FakeCustomTileDefaultsRepository()
+    private val testDispatcher = StandardTestDispatcher()
+    private val testScope = TestScope(testDispatcher)
+
+    private lateinit var underTest: CustomTilePackageUpdatesRepository
+
+    @Before
+    fun setup() {
+        MockitoAnnotations.initMocks(this)
+
+        underTest =
+            CustomTilePackageUpdatesRepositoryImpl(
+                TileSpec.create(COMPONENT_1),
+                USER,
+                tileServiceManager,
+                defaultsRepository,
+                testScope.backgroundScope,
+            )
+    }
+
+    @Test
+    fun packageChangesUpdatesDefaults() =
+        testScope.runTest {
+            val events = mutableListOf<Unit>()
+            underTest.packageChanges.onEach { events.add(it) }.launchIn(backgroundScope)
+            runCurrent()
+            verify(tileServiceManager).setTileChangeListener(capture(listenerCaptor))
+
+            emitPackageChange()
+            runCurrent()
+
+            assertThat(events).hasSize(1)
+            assertThat(defaultsRepository.defaultsRequests).isNotEmpty()
+            assertThat(defaultsRepository.defaultsRequests.last())
+                .isEqualTo(DefaultsRequest(USER, COMPONENT_1, true))
+        }
+
+    @Test
+    fun packageChangesEmittedOnlyForTheTile() =
+        testScope.runTest {
+            val events = mutableListOf<Unit>()
+            underTest.packageChanges.onEach { events.add(it) }.launchIn(backgroundScope)
+            runCurrent()
+            verify(tileServiceManager).setTileChangeListener(capture(listenerCaptor))
+
+            emitPackageChange(COMPONENT_2)
+            runCurrent()
+
+            assertThat(events).isEmpty()
+        }
+
+    private fun emitPackageChange(componentName: ComponentName = COMPONENT_1) {
+        listenerCaptor.value.onTileChanged(componentName)
+    }
+
+    private companion object {
+        val USER = UserHandle(0)
+        val COMPONENT_1 = ComponentName("pkg.test.1", "cls.test")
+        val COMPONENT_2 = ComponentName("pkg.test.2", "cls.test")
+    }
+}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/shade/NotificationShadeWindowViewControllerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/shade/NotificationShadeWindowViewControllerTest.kt
index 4e3e165..5459779 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/shade/NotificationShadeWindowViewControllerTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/shade/NotificationShadeWindowViewControllerTest.kt
@@ -97,6 +97,7 @@
 import com.android.systemui.util.mockito.eq
 import com.android.systemui.util.time.FakeSystemClock
 import com.google.common.truth.Truth.assertThat
+import java.util.Optional
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.flow.emptyFlow
 import kotlinx.coroutines.test.TestScope
@@ -111,9 +112,8 @@
 import org.mockito.Mockito.never
 import org.mockito.Mockito.times
 import org.mockito.Mockito.verify
-import org.mockito.MockitoAnnotations
-import java.util.Optional
 import org.mockito.Mockito.`when` as whenever
+import org.mockito.MockitoAnnotations
 
 @OptIn(ExperimentalCoroutinesApi::class)
 @SmallTest
@@ -140,6 +140,7 @@
     @Mock private lateinit var stackScrollLayoutController: NotificationStackScrollLayoutController
     @Mock private lateinit var statusBarKeyguardViewManager: StatusBarKeyguardViewManager
     @Mock private lateinit var statusBarWindowStateController: StatusBarWindowStateController
+    @Mock private lateinit var quickSettingsController: QuickSettingsController
     @Mock
     private lateinit var lockscreenShadeTransitionController: LockscreenShadeTransitionController
     @Mock private lateinit var lockIconViewController: LockIconViewController
@@ -166,7 +167,7 @@
     @Mock lateinit var alternateBouncerInteractor: AlternateBouncerInteractor
     private val notificationLaunchAnimationRepository = NotificationLaunchAnimationRepository()
     private val notificationLaunchAnimationInteractor =
-            NotificationLaunchAnimationInteractor(notificationLaunchAnimationRepository)
+        NotificationLaunchAnimationInteractor(notificationLaunchAnimationRepository)
 
     private lateinit var fakeClock: FakeSystemClock
     private lateinit var interactionEventHandlerCaptor: ArgumentCaptor<InteractionEventHandler>
@@ -274,6 +275,7 @@
                 ),
                 BouncerLogger(logcatLogBuffer("BouncerLog")),
                 sysUIKeyEventHandler,
+                quickSettingsController,
                 primaryBouncerInteractor,
                 alternateBouncerInteractor,
                 mSelectedUserInteractor,
@@ -460,9 +462,11 @@
         // AND alternate bouncer doesn't want the touch
         whenever(statusBarKeyguardViewManager.shouldInterceptTouchEvent(DOWN_EVENT))
             .thenReturn(false)
+        // AND quick settings controller doesn't want it
+        whenever(quickSettingsController.shouldQuickSettingsIntercept(any(), any(), any()))
+            .thenReturn(false)
         // AND the lock icon wants the touch
-        whenever(lockIconViewController.willHandleTouchWhileDozing(DOWN_EVENT))
-                .thenReturn(true)
+        whenever(lockIconViewController.willHandleTouchWhileDozing(DOWN_EVENT)).thenReturn(true)
 
         featureFlagsClassic.set(MIGRATE_NSSL, true)
 
@@ -476,10 +480,31 @@
         whenever(sysuiStatusBarStateController.isDozing).thenReturn(true)
         // AND alternate bouncer doesn't want the touch
         whenever(statusBarKeyguardViewManager.shouldInterceptTouchEvent(DOWN_EVENT))
-                .thenReturn(false)
+            .thenReturn(false)
         // AND the lock icon does NOT want the touch
-        whenever(lockIconViewController.willHandleTouchWhileDozing(DOWN_EVENT))
-                .thenReturn(false)
+        whenever(lockIconViewController.willHandleTouchWhileDozing(DOWN_EVENT)).thenReturn(false)
+        // AND quick settings controller doesn't want it
+        whenever(quickSettingsController.shouldQuickSettingsIntercept(any(), any(), any()))
+            .thenReturn(false)
+
+        featureFlagsClassic.set(MIGRATE_NSSL, true)
+
+        // THEN touch should NOT be intercepted by NotificationShade
+        assertThat(interactionEventHandler.shouldInterceptTouchEvent(DOWN_EVENT)).isTrue()
+    }
+
+    @Test
+    fun shouldInterceptTouchEvent_dozing_touchInStatusBar_touchIntercepted() {
+        // GIVEN dozing
+        whenever(sysuiStatusBarStateController.isDozing).thenReturn(true)
+        // AND alternate bouncer doesn't want the touch
+        whenever(statusBarKeyguardViewManager.shouldInterceptTouchEvent(DOWN_EVENT))
+            .thenReturn(false)
+        // AND the lock icon does NOT want the touch
+        whenever(lockIconViewController.willHandleTouchWhileDozing(DOWN_EVENT)).thenReturn(false)
+        // AND quick settings controller DOES want it
+        whenever(quickSettingsController.shouldQuickSettingsIntercept(any(), any(), any()))
+            .thenReturn(true)
 
         featureFlagsClassic.set(MIGRATE_NSSL, true)
 
diff --git a/packages/SystemUI/tests/src/com/android/systemui/shade/NotificationShadeWindowViewTest.kt b/packages/SystemUI/tests/src/com/android/systemui/shade/NotificationShadeWindowViewTest.kt
index 3d5d26a..a6ab6a5 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/shade/NotificationShadeWindowViewTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/shade/NotificationShadeWindowViewTest.kt
@@ -121,6 +121,7 @@
     @Mock private lateinit var notificationStackScrollLayout: NotificationStackScrollLayout
     @Mock private lateinit var notificationShadeDepthController: NotificationShadeDepthController
     @Mock private lateinit var notificationShadeWindowController: NotificationShadeWindowController
+    @Mock private lateinit var quickSettingsController: QuickSettingsController
     @Mock
     private lateinit var notificationStackScrollLayoutController:
         NotificationStackScrollLayoutController
@@ -264,6 +265,7 @@
                 ),
                 BouncerLogger(logcatLogBuffer("BouncerLog")),
                 Mockito.mock(SysUIKeyEventHandler::class.java),
+                quickSettingsController,
                 primaryBouncerInteractor,
                 alternateBouncerInteractor,
                 mSelectedUserInteractor,
diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/qs/tiles/impl/custom/data/repository/FakeCustomTilePackageUpdatesRepository.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/qs/tiles/impl/custom/data/repository/FakeCustomTilePackageUpdatesRepository.kt
new file mode 100644
index 0000000..8f972f5
--- /dev/null
+++ b/packages/SystemUI/tests/utils/src/com/android/systemui/qs/tiles/impl/custom/data/repository/FakeCustomTilePackageUpdatesRepository.kt
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.qs.tiles.impl.custom.data.repository
+
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.MutableSharedFlow
+
+class FakeCustomTilePackageUpdatesRepository : CustomTilePackageUpdatesRepository {
+
+    private val mutablePackageChanges = MutableSharedFlow<Unit>()
+
+    override val packageChanges: Flow<Unit>
+        get() = mutablePackageChanges
+
+    suspend fun emitPackageChange() {
+        mutablePackageChanges.emit(Unit)
+    }
+}
diff --git a/services/core/java/com/android/server/am/SettingsToPropertiesMapper.java b/services/core/java/com/android/server/am/SettingsToPropertiesMapper.java
index c1a2482..e6cdbb5 100644
--- a/services/core/java/com/android/server/am/SettingsToPropertiesMapper.java
+++ b/services/core/java/com/android/server/am/SettingsToPropertiesMapper.java
@@ -128,6 +128,7 @@
         "biometrics",
         "biometrics_framework",
         "biometrics_integration",
+        "camera_hal",
         "camera_platform",
         "car_framework",
         "car_perception",
diff --git a/services/core/java/com/android/server/pm/PackageInstallerService.java b/services/core/java/com/android/server/pm/PackageInstallerService.java
index 305e353..a4d8632 100644
--- a/services/core/java/com/android/server/pm/PackageInstallerService.java
+++ b/services/core/java/com/android/server/pm/PackageInstallerService.java
@@ -58,6 +58,7 @@
 import android.content.pm.PackageManager;
 import android.content.pm.ParceledListSlice;
 import android.content.pm.VersionedPackage;
+import android.content.pm.parsing.FrameworkParsingPackageUtils;
 import android.graphics.Bitmap;
 import android.net.Uri;
 import android.os.Binder;
@@ -666,17 +667,22 @@
 
         // App package name and label length is restricted so that really long strings aren't
         // written to disk.
-        if (params.appPackageName != null
-                && params.appPackageName.length() > SessionParams.MAX_PACKAGE_NAME_LENGTH) {
+        if (params.appPackageName != null && !isValidPackageName(params.appPackageName)) {
             params.appPackageName = null;
         }
 
         params.appLabel = TextUtils.trimToSize(params.appLabel,
                 PackageItemInfo.MAX_SAFE_LABEL_LENGTH);
 
-        String requestedInstallerPackageName = (params.installerPackageName != null
-                && params.installerPackageName.length() < SessionParams.MAX_PACKAGE_NAME_LENGTH)
-                ? params.installerPackageName : installerPackageName;
+        // Validate installer package name.
+        if (params.installerPackageName != null && !isValidPackageName(
+                params.installerPackageName)) {
+            params.installerPackageName = null;
+        }
+
+        var requestedInstallerPackageName =
+                params.installerPackageName != null ? params.installerPackageName
+                        : installerPackageName;
 
         if (PackageManagerServiceUtils.isRootOrShell(callingUid)
                 || PackageInstallerSession.isSystemDataLoaderInstallation(params)
@@ -1105,6 +1111,19 @@
         return Integer.parseInt(sessionId);
     }
 
+    private static boolean isValidPackageName(@NonNull String packageName) {
+        if (packageName.length() > SessionParams.MAX_PACKAGE_NAME_LENGTH) {
+            return false;
+        }
+        // "android" is a valid package name
+        var errorMessage = FrameworkParsingPackageUtils.validateName(
+                packageName, /* requireSeparator= */ false, /* requireFilename */ true);
+        if (errorMessage != null) {
+            return false;
+        }
+        return true;
+    }
+
     private File getTmpSessionDir(String volumeUuid) {
         return Environment.getDataAppDirectory(volumeUuid);
     }
diff --git a/services/core/java/com/android/server/pm/parsing/PackageInfoUtils.java b/services/core/java/com/android/server/pm/parsing/PackageInfoUtils.java
index 2ad8bcf..9e20805 100644
--- a/services/core/java/com/android/server/pm/parsing/PackageInfoUtils.java
+++ b/services/core/java/com/android/server/pm/parsing/PackageInfoUtils.java
@@ -379,10 +379,7 @@
         ai.privateFlags |= flag(state.isInstantApp(), ApplicationInfo.PRIVATE_FLAG_INSTANT)
                 | flag(state.isVirtualPreload(), ApplicationInfo.PRIVATE_FLAG_VIRTUAL_PRELOAD)
                 | flag(state.isHidden(), ApplicationInfo.PRIVATE_FLAG_HIDDEN);
-        if ((flags & PackageManager.MATCH_QUARANTINED_COMPONENTS) == 0
-                && state.isQuarantined()) {
-            ai.enabled = false;
-        } else  if (state.getEnabledState() == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
+        if (state.getEnabledState() == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
             ai.enabled = true;
         } else if (state.getEnabledState()
                 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
diff --git a/services/tests/mockingservicestests/src/com/android/server/job/controllers/idle/DeviceIdlenessTrackerTest.java b/services/tests/mockingservicestests/src/com/android/server/job/controllers/idle/DeviceIdlenessTrackerTest.java
new file mode 100644
index 0000000..09935f2
--- /dev/null
+++ b/services/tests/mockingservicestests/src/com/android/server/job/controllers/idle/DeviceIdlenessTrackerTest.java
@@ -0,0 +1,210 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.job.controllers.idle;
+
+import static android.text.format.DateUtils.HOUR_IN_MILLIS;
+import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
+
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.inOrder;
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession;
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.when;
+import static com.android.server.job.JobSchedulerService.sElapsedRealtimeClock;
+import static com.android.server.job.controllers.idle.DeviceIdlenessTracker.KEY_INACTIVITY_IDLE_THRESHOLD_MS;
+import static com.android.server.job.controllers.idle.DeviceIdlenessTracker.KEY_INACTIVITY_STABLE_POWER_IDLE_THRESHOLD_MS;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
+import android.app.AlarmManager;
+import android.app.UiModeManager;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.res.Resources;
+import android.os.PowerManager;
+import android.os.SystemClock;
+import android.provider.DeviceConfig;
+
+import androidx.test.runner.AndroidJUnit4;
+
+import com.android.server.AppSchedulingModuleThread;
+import com.android.server.LocalServices;
+import com.android.server.job.JobSchedulerService;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.InOrder;
+import org.mockito.Mock;
+import org.mockito.MockitoSession;
+import org.mockito.quality.Strictness;
+
+import java.time.Clock;
+import java.time.Duration;
+import java.time.ZoneOffset;
+
+@RunWith(AndroidJUnit4.class)
+public class DeviceIdlenessTrackerTest {
+    private DeviceIdlenessTracker mDeviceIdlenessTracker;
+    private JobSchedulerService.Constants mConstants = new JobSchedulerService.Constants();
+    private BroadcastReceiver mBroadcastReceiver;
+    private DeviceConfig.Properties.Builder mDeviceConfigPropertiesBuilder =
+            new DeviceConfig.Properties.Builder(DeviceConfig.NAMESPACE_JOB_SCHEDULER);;
+
+    private MockitoSession mMockingSession;
+    @Mock
+    private AlarmManager mAlarmManager;
+    @Mock
+    private Context mContext;
+    @Mock
+    private JobSchedulerService mJobSchedulerService;
+    @Mock
+    private PowerManager mPowerManager;
+    @Mock
+    private Resources mResources;
+
+    @Before
+    public void setUp() {
+        mMockingSession = mockitoSession()
+                .initMocks(this)
+                .strictness(Strictness.LENIENT)
+                .spyStatic(DeviceConfig.class)
+                .mockStatic(LocalServices.class)
+                .startMocking();
+
+        // Called in StateController constructor.
+        when(mJobSchedulerService.getTestableContext()).thenReturn(mContext);
+        when(mJobSchedulerService.getLock()).thenReturn(mJobSchedulerService);
+        when(mJobSchedulerService.getConstants()).thenReturn(mConstants);
+        // Called in DeviceIdlenessTracker.startTracking.
+        when(mContext.getSystemService(Context.ALARM_SERVICE)).thenReturn(mAlarmManager);
+        when(mContext.getSystemService(UiModeManager.class)).thenReturn(mock(UiModeManager.class));
+        when(mContext.getResources()).thenReturn(mResources);
+        doReturn((int) (31 * MINUTE_IN_MILLIS)).when(mResources).getInteger(
+                com.android.internal.R.integer.config_jobSchedulerInactivityIdleThreshold);
+        doReturn((int) (17 * MINUTE_IN_MILLIS)).when(mResources).getInteger(
+                com.android.internal.R.integer
+                        .config_jobSchedulerInactivityIdleThresholdOnStablePower);
+        doReturn(mPowerManager).when(() -> LocalServices.getService(PowerManager.class));
+
+        // Freeze the clocks at 24 hours after this moment in time. Several tests create sessions
+        // in the past, and QuotaController sometimes floors values at 0, so if the test time
+        // causes sessions with negative timestamps, they will fail.
+        JobSchedulerService.sSystemClock =
+                getAdvancedClock(Clock.fixed(Clock.systemUTC().instant(), ZoneOffset.UTC),
+                        24 * HOUR_IN_MILLIS);
+        JobSchedulerService.sUptimeMillisClock = getAdvancedClock(
+                Clock.fixed(SystemClock.uptimeClock().instant(), ZoneOffset.UTC),
+                24 * HOUR_IN_MILLIS);
+        JobSchedulerService.sElapsedRealtimeClock = getAdvancedClock(
+                Clock.fixed(SystemClock.elapsedRealtimeClock().instant(), ZoneOffset.UTC),
+                24 * HOUR_IN_MILLIS);
+
+        // Initialize real objects.
+        // Capture the listeners.
+        ArgumentCaptor<BroadcastReceiver> broadcastReceiverCaptor =
+                ArgumentCaptor.forClass(BroadcastReceiver.class);
+        mDeviceIdlenessTracker = new DeviceIdlenessTracker();
+        mDeviceIdlenessTracker.startTracking(mContext,
+                mJobSchedulerService, mock(IdlenessListener.class));
+
+        verify(mContext).registerReceiver(broadcastReceiverCaptor.capture(), any(), any(), any());
+        mBroadcastReceiver = broadcastReceiverCaptor.getValue();
+    }
+
+    @After
+    public void tearDown() {
+        if (mMockingSession != null) {
+            mMockingSession.finishMocking();
+        }
+    }
+
+    private Clock getAdvancedClock(Clock clock, long incrementMs) {
+        return Clock.offset(clock, Duration.ofMillis(incrementMs));
+    }
+
+    private void advanceElapsedClock(long incrementMs) {
+        JobSchedulerService.sElapsedRealtimeClock = getAdvancedClock(
+                JobSchedulerService.sElapsedRealtimeClock, incrementMs);
+    }
+
+    private void setBatteryState(boolean isCharging, boolean isBatteryNotLow) {
+        doReturn(isCharging).when(mJobSchedulerService).isBatteryCharging();
+        doReturn(isBatteryNotLow).when(mJobSchedulerService).isBatteryNotLow();
+        mDeviceIdlenessTracker.onBatteryStateChanged(isCharging, isBatteryNotLow);
+    }
+
+    private void setDeviceConfigLong(String key, long val) {
+        mDeviceConfigPropertiesBuilder.setLong(key, val);
+        mDeviceIdlenessTracker.processConstant(mDeviceConfigPropertiesBuilder.build(), key);
+    }
+
+    @Test
+    public void testThresholdChangeWithStablePowerChange() {
+        setDeviceConfigLong(KEY_INACTIVITY_IDLE_THRESHOLD_MS, 10 * MINUTE_IN_MILLIS);
+        setDeviceConfigLong(KEY_INACTIVITY_STABLE_POWER_IDLE_THRESHOLD_MS, 5 * MINUTE_IN_MILLIS);
+        setBatteryState(false, false);
+
+        Intent screenOffIntent = new Intent(Intent.ACTION_SCREEN_OFF);
+        mBroadcastReceiver.onReceive(mContext, screenOffIntent);
+
+        final long nowElapsed = sElapsedRealtimeClock.millis();
+        long expectedUnstableAlarmElapsed = nowElapsed + 10 * MINUTE_IN_MILLIS;
+        long expectedStableAlarmElapsed = nowElapsed + 5 * MINUTE_IN_MILLIS;
+
+        InOrder inOrder = inOrder(mAlarmManager);
+        inOrder.verify(mAlarmManager)
+                .setWindow(anyInt(), eq(expectedUnstableAlarmElapsed), anyLong(), anyString(),
+                        eq(AppSchedulingModuleThread.getExecutor()), any());
+
+        // Advanced the clock a little to make sure the tracker continues to use the original time.
+        advanceElapsedClock(MINUTE_IN_MILLIS);
+
+        // Charging isn't enough for stable power.
+        setBatteryState(true, false);
+        inOrder.verify(mAlarmManager, never())
+                .setWindow(anyInt(), anyLong(), anyLong(), anyString(),
+                        eq(AppSchedulingModuleThread.getExecutor()), any());
+
+        // Now on stable power.
+        setBatteryState(true, true);
+        inOrder.verify(mAlarmManager)
+                .setWindow(anyInt(), eq(expectedStableAlarmElapsed), anyLong(), anyString(),
+                        eq(AppSchedulingModuleThread.getExecutor()), any());
+
+        // Battery-not-low isn't enough for stable power. Go back to unstable timing.
+        setBatteryState(false, true);
+        inOrder.verify(mAlarmManager)
+                .setWindow(anyInt(), eq(expectedUnstableAlarmElapsed), anyLong(), anyString(),
+                        eq(AppSchedulingModuleThread.getExecutor()), any());
+
+        // Still not on stable power.
+        setBatteryState(false, false);
+        inOrder.verify(mAlarmManager, never())
+                .setWindow(anyInt(), anyLong(), anyLong(), anyString(),
+                        eq(AppSchedulingModuleThread.getExecutor()), any());
+    }
+}
diff --git a/tests/FlickerTests/AndroidTestTemplate.xml b/tests/FlickerTests/AndroidTestTemplate.xml
index 85709c9..ed71531 100644
--- a/tests/FlickerTests/AndroidTestTemplate.xml
+++ b/tests/FlickerTests/AndroidTestTemplate.xml
@@ -28,7 +28,13 @@
         <option name="run-command" value="su root service call SurfaceFlinger 1029 i32 81920"/>
         <!-- b/307664397 - Ensure camera has the correct permissions and doesn't show a dialog -->
         <option name="run-command"
+                value="pm grant com.google.android.GoogleCamera android.permission.CAMERA"/>
+        <option name="run-command"
+                value="pm grant com.google.android.GoogleCamera android.permission.RECORD_AUDIO"/>
+        <option name="run-command"
                 value="pm grant com.google.android.GoogleCamera android.permission.ACCESS_FINE_LOCATION"/>
+        <option name="run-command"
+                value="pm grant com.google.android.GoogleCamera android.permission.ACCESS_COARSE_LOCATION"/>
     </target_preparer>
     <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
         <option name="test-user-token" value="%TEST_USER%"/>