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

Commit 64ac1a92 authored by Jing Ji's avatar Jing Ji
Browse files

Monitor long-running foreground services

If a certain package has foreground services running for a long time,
say the accumulated durations over last X hours are more than Y hours,
system will post a notification to remind the user.

Some type of apps are subjected to be exempted, i.e. if it's already
in the device idle allowlist. More exemption could be added in
the follow-up CLs.

Bug: 200326767
Bug: 203105544
Test: atest FrameworksMockingServicesTests:BackgroundRestrictionTest
Change-Id: I3a8f34c33e7a533240abc7cf4fa569a0956eec73
parent 0c660397
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -755,4 +755,25 @@ public abstract class ActivityManagerInternal {
     */
    public abstract void addAppBackgroundRestrictionListener(
            @NonNull AppBackgroundRestrictionListener listener);

    /**
     * A listener interface, which will be notified on foreground service state changes.
     */
    public interface ForegroundServiceStateListener {
        /**
         * Call when the given process's foreground service state changes.
         *
         * @param packageName The package name of the process.
         * @param uid The UID of the process.
         * @param pid The pid of the process.
         * @param started {@code true} if the process transits from non-FGS state to FGS state.
         */
        void onForegroundServiceStateChanged(String packageName, int uid, int pid, boolean started);
    }

    /**
     * Register the foreground service state change listener callback.
     */
    public abstract void addForegroundServiceStateListener(
            @NonNull ForegroundServiceStateListener listener);
}
+11 −0
Original line number Diff line number Diff line
@@ -5053,6 +5053,17 @@ public class Intent implements Parcelable, Cloneable {
    public static final String ACTION_PACKAGE_NEEDS_INTEGRITY_VERIFICATION =
            "android.intent.action.PACKAGE_NEEDS_INTEGRITY_VERIFICATION";

    /**
     * Broadcast Action: Start the foreground service manager.
     *
     * <p class="note">
     * This is a protected intent that can only be sent by the system.
     * </p>
     *
     * @hide
     */
    public static final String ACTION_SHOW_FOREGROUND_SERVICE_MANAGER =
            "android.intent.action.SHOW_FOREGROUND_SERVICE_MANAGER";

    // ---------------------------------------------------------------------
    // ---------------------------------------------------------------------
+1 −0
Original line number Diff line number Diff line
@@ -717,6 +717,7 @@
    <!-- Added in T -->
    <protected-broadcast android:name="android.intent.action.REFRESH_SAFETY_SOURCES" />
    <protected-broadcast android:name="android.app.action.DEVICE_POLICY_RESOURCE_UPDATED" />
    <protected-broadcast android:name="android.intent.action.SHOW_FOREGROUND_SERVICE_MANAGER" />

    <!-- ====================================================================== -->
    <!--                          RUNTIME PERMISSIONS                           -->
+4 −0
Original line number Diff line number Diff line
@@ -6208,4 +6208,8 @@ ul.</string>
    <string name="notification_content_abusive_bg_apps">
        <xliff:g id="app" example="Gmail">%1$s</xliff:g> is running in the background and draining battery. Tap to review.
    </string>
    <!-- Content of notification indicating long running foreground service. [CHAR LIMIT=NONE] -->
    <string name="notification_content_long_running_fgs">
        <xliff:g id="app" example="Gmail">%1$s</xliff:g> is running in the background for a long time. Tap to review.
    </string>
</resources>
+1 −0
Original line number Diff line number Diff line
@@ -4673,4 +4673,5 @@
  <java-symbol type="string" name="notification_channel_abusive_bg_apps"/>
  <java-symbol type="string" name="notification_title_abusive_bg_apps"/>
  <java-symbol type="string" name="notification_content_abusive_bg_apps"/>
  <java-symbol type="string" name="notification_content_long_running_fgs"/>
</resources>
Loading