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

Commit ced7d76e authored by Sudheer Shanka's avatar Sudheer Shanka
Browse files

Apply "most-recent" delivery policy to certain broadcasts.

Applying the policy to DEVICE_IDLE_MODE_CHANGED and
LIGHT_DEVICE_IDLE_MODE_CHANGED broadcasts so that when dispatching
these broadcasts, we can discard any older broadcasts waiting
to be delivered and deliver only the most recent to the receivers.

Bug: 256597171
Bug: 256597397
Test: atest ./tests/tests/batterysaving/src/android/os/cts/deviceidle/DeviceIdleTest.java
Change-Id: Icfade7cd207d525d29856bb050ca4377085dbbc1
parent 90cef8f4
Loading
Loading
Loading
Loading
+16 −6
Original line number Original line Diff line number Diff line
@@ -29,6 +29,7 @@ import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager;
import android.app.ActivityManagerInternal;
import android.app.ActivityManagerInternal;
import android.app.AlarmManager;
import android.app.AlarmManager;
import android.app.BroadcastOptions;
import android.content.BroadcastReceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Context;
import android.content.IIntentReceiver;
import android.content.IIntentReceiver;
@@ -314,7 +315,9 @@ public class DeviceIdleController extends SystemService
    private Sensor mMotionSensor;
    private Sensor mMotionSensor;
    private LocationRequest mLocationRequest;
    private LocationRequest mLocationRequest;
    private Intent mIdleIntent;
    private Intent mIdleIntent;
    private Bundle mIdleIntentOptions;
    private Intent mLightIdleIntent;
    private Intent mLightIdleIntent;
    private Bundle mLightIdleIntentOptions;
    private AnyMotionDetector mAnyMotionDetector;
    private AnyMotionDetector mAnyMotionDetector;
    private final AppStateTrackerImpl mAppStateTracker;
    private final AppStateTrackerImpl mAppStateTracker;
    @GuardedBy("this")
    @GuardedBy("this")
@@ -1798,10 +1801,12 @@ public class DeviceIdleController extends SystemService
                    } catch (RemoteException e) {
                    } catch (RemoteException e) {
                    }
                    }
                    if (deepChanged) {
                    if (deepChanged) {
                        getContext().sendBroadcastAsUser(mIdleIntent, UserHandle.ALL);
                        getContext().sendBroadcastAsUser(mIdleIntent, UserHandle.ALL,
                                null /* receiverPermission */, mIdleIntentOptions);
                    }
                    }
                    if (lightChanged) {
                    if (lightChanged) {
                        getContext().sendBroadcastAsUser(mLightIdleIntent, UserHandle.ALL);
                        getContext().sendBroadcastAsUser(mLightIdleIntent, UserHandle.ALL,
                                null /* receiverPermission */, mLightIdleIntentOptions);
                    }
                    }
                    EventLogTags.writeDeviceIdleOnComplete();
                    EventLogTags.writeDeviceIdleOnComplete();
                    mGoingIdleWakeLock.release();
                    mGoingIdleWakeLock.release();
@@ -1821,13 +1826,13 @@ public class DeviceIdleController extends SystemService
                        incActiveIdleOps();
                        incActiveIdleOps();
                        mLocalActivityManager.broadcastIntentWithCallback(mIdleIntent,
                        mLocalActivityManager.broadcastIntentWithCallback(mIdleIntent,
                                mIdleStartedDoneReceiver, null, UserHandle.USER_ALL,
                                mIdleStartedDoneReceiver, null, UserHandle.USER_ALL,
                                null, null, null);
                                null, null, mIdleIntentOptions);
                    }
                    }
                    if (lightChanged) {
                    if (lightChanged) {
                        incActiveIdleOps();
                        incActiveIdleOps();
                        mLocalActivityManager.broadcastIntentWithCallback(mLightIdleIntent,
                        mLocalActivityManager.broadcastIntentWithCallback(mLightIdleIntent,
                                mIdleStartedDoneReceiver, null, UserHandle.USER_ALL,
                                mIdleStartedDoneReceiver, null, UserHandle.USER_ALL,
                                null, null, null);
                                null, null, mLightIdleIntentOptions);
                    }
                    }
                    // Always start with one active op for the message being sent here.
                    // Always start with one active op for the message being sent here.
                    // Now we are done!
                    // Now we are done!
@@ -1849,10 +1854,12 @@ public class DeviceIdleController extends SystemService
                    } catch (RemoteException e) {
                    } catch (RemoteException e) {
                    }
                    }
                    if (deepChanged) {
                    if (deepChanged) {
                        getContext().sendBroadcastAsUser(mIdleIntent, UserHandle.ALL);
                        getContext().sendBroadcastAsUser(mIdleIntent, UserHandle.ALL,
                                null /* receiverPermission */, mIdleIntentOptions);
                    }
                    }
                    if (lightChanged) {
                    if (lightChanged) {
                        getContext().sendBroadcastAsUser(mLightIdleIntent, UserHandle.ALL);
                        getContext().sendBroadcastAsUser(mLightIdleIntent, UserHandle.ALL,
                                null /* receiverPermission */, mLightIdleIntentOptions);
                    }
                    }
                    EventLogTags.writeDeviceIdleOffComplete();
                    EventLogTags.writeDeviceIdleOffComplete();
                } break;
                } break;
@@ -2531,6 +2538,9 @@ public class DeviceIdleController extends SystemService
                mLightIdleIntent = new Intent(PowerManager.ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED);
                mLightIdleIntent = new Intent(PowerManager.ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED);
                mLightIdleIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
                mLightIdleIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
                        | Intent.FLAG_RECEIVER_FOREGROUND);
                        | Intent.FLAG_RECEIVER_FOREGROUND);
                final BroadcastOptions options = BroadcastOptions.makeBasic();
                options.setDeliveryGroupPolicy(BroadcastOptions.DELIVERY_GROUP_POLICY_MOST_RECENT);
                mIdleIntentOptions = mLightIdleIntentOptions = options.toBundle();


                IntentFilter filter = new IntentFilter();
                IntentFilter filter = new IntentFilter();
                filter.addAction(Intent.ACTION_BATTERY_CHANGED);
                filter.addAction(Intent.ACTION_BATTERY_CHANGED);