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

Commit 24dd30d8 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "StatsCompanionService alarms more efficient"

parents 7f297c0f 75158ac5
Loading
Loading
Loading
Loading
+0 −15
Original line number Original line Diff line number Diff line
@@ -4287,21 +4287,6 @@
            </intent-filter>
            </intent-filter>
        </receiver>
        </receiver>


        <receiver android:name="com.android.server.stats.StatsCompanionService$AnomalyAlarmReceiver"
                  android:permission="android.permission.STATSCOMPANION"
                  android:exported="false">
        </receiver>

        <receiver android:name="com.android.server.stats.StatsCompanionService$PullingAlarmReceiver"
                  android:permission="android.permission.STATSCOMPANION"
                  android:exported="false">
        </receiver>

        <receiver android:name="com.android.server.stats.StatsCompanionService$PeriodicAlarmReceiver"
                  android:permission="android.permission.STATSCOMPANION"
                  android:exported="false">
        </receiver>

        <service android:name="android.hardware.location.GeofenceHardwareService"
        <service android:name="android.hardware.location.GeofenceHardwareService"
            android:permission="android.permission.LOCATION_HARDWARE"
            android:permission="android.permission.LOCATION_HARDWARE"
            android:exported="false" />
            android:exported="false" />
+3 −3
Original line number Original line Diff line number Diff line
@@ -3272,11 +3272,11 @@ Lcom/android/server/soundtrigger/SoundTriggerService$LocalSoundTriggerService;
Lcom/android/server/soundtrigger/SoundTriggerService$SoundTriggerServiceStub;
Lcom/android/server/soundtrigger/SoundTriggerService$SoundTriggerServiceStub;
Lcom/android/server/soundtrigger/SoundTriggerService;
Lcom/android/server/soundtrigger/SoundTriggerService;
Lcom/android/server/stats/StatsCompanionService$1;
Lcom/android/server/stats/StatsCompanionService$1;
Lcom/android/server/stats/StatsCompanionService$AnomalyAlarmReceiver;
Lcom/android/server/stats/StatsCompanionService$AnomalyAlarmListener;
Lcom/android/server/stats/StatsCompanionService$AppUpdateReceiver;
Lcom/android/server/stats/StatsCompanionService$AppUpdateReceiver;
Lcom/android/server/stats/StatsCompanionService$Lifecycle;
Lcom/android/server/stats/StatsCompanionService$Lifecycle;
Lcom/android/server/stats/StatsCompanionService$PeriodicAlarmReceiver;
Lcom/android/server/stats/StatsCompanionService$PeriodicAlarmListener;
Lcom/android/server/stats/StatsCompanionService$PullingAlarmReceiver;
Lcom/android/server/stats/StatsCompanionService$PullingAlarmListener;
Lcom/android/server/stats/StatsCompanionService$ShutdownEventReceiver;
Lcom/android/server/stats/StatsCompanionService$ShutdownEventReceiver;
Lcom/android/server/stats/StatsCompanionService$StatsdDeathRecipient;
Lcom/android/server/stats/StatsCompanionService$StatsdDeathRecipient;
Lcom/android/server/stats/StatsCompanionService;
Lcom/android/server/stats/StatsCompanionService;
+40 −42
Original line number Original line Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.server.stats;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.app.ActivityManagerInternal;
import android.app.ActivityManagerInternal;
import android.app.AlarmManager;
import android.app.AlarmManager;
import android.app.AlarmManager.OnAlarmListener;
import android.app.PendingIntent;
import android.app.PendingIntent;
import android.app.ProcessMemoryState;
import android.app.ProcessMemoryState;
import android.app.StatsManager;
import android.app.StatsManager;
@@ -124,9 +125,9 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
    private static IStatsManager sStatsd;
    private static IStatsManager sStatsd;
    private static final Object sStatsdLock = new Object();
    private static final Object sStatsdLock = new Object();


    private final PendingIntent mAnomalyAlarmIntent;
    private final OnAlarmListener mAnomalyAlarmListener = new AnomalyAlarmListener();
    private final PendingIntent mPullingAlarmIntent;
    private final OnAlarmListener mPullingAlarmListener = new PullingAlarmListener();
    private final PendingIntent mPeriodicAlarmIntent;
    private final OnAlarmListener mPeriodicAlarmListener = new PeriodicAlarmListener();
    private final BroadcastReceiver mAppUpdateReceiver;
    private final BroadcastReceiver mAppUpdateReceiver;
    private final BroadcastReceiver mUserUpdateReceiver;
    private final BroadcastReceiver mUserUpdateReceiver;
    private final ShutdownEventReceiver mShutdownEventReceiver;
    private final ShutdownEventReceiver mShutdownEventReceiver;
@@ -158,12 +159,6 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        mContext = context;
        mContext = context;
        mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);


        mAnomalyAlarmIntent = PendingIntent.getBroadcast(mContext, 0,
                new Intent(mContext, AnomalyAlarmReceiver.class), 0);
        mPullingAlarmIntent = PendingIntent.getBroadcast(
                mContext, 0, new Intent(mContext, PullingAlarmReceiver.class), 0);
        mPeriodicAlarmIntent = PendingIntent.getBroadcast(
                mContext, 0, new Intent(mContext, PeriodicAlarmReceiver.class), 0);
        mAppUpdateReceiver = new AppUpdateReceiver();
        mAppUpdateReceiver = new AppUpdateReceiver();
        mUserUpdateReceiver = new BroadcastReceiver() {
        mUserUpdateReceiver = new BroadcastReceiver() {
            @Override
            @Override
@@ -345,9 +340,9 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        }
        }
    }
    }


    public final static class AnomalyAlarmReceiver extends BroadcastReceiver {
    public final static class AnomalyAlarmListener implements OnAlarmListener {
        @Override
        @Override
        public void onReceive(Context context, Intent intent) {
        public void onAlarm() {
            Slog.i(TAG, "StatsCompanionService believes an anomaly has occurred at time "
            Slog.i(TAG, "StatsCompanionService believes an anomaly has occurred at time "
                    + System.currentTimeMillis() + "ms.");
                    + System.currentTimeMillis() + "ms.");
            synchronized (sStatsdLock) {
            synchronized (sStatsdLock) {
@@ -366,9 +361,9 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        }
        }
    }
    }


    public final static class PullingAlarmReceiver extends BroadcastReceiver {
    public final static class PullingAlarmListener implements OnAlarmListener {
        @Override
        @Override
        public void onReceive(Context context, Intent intent) {
        public void onAlarm() {
            if (DEBUG)
            if (DEBUG)
                Slog.d(TAG, "Time to poll something.");
                Slog.d(TAG, "Time to poll something.");
            synchronized (sStatsdLock) {
            synchronized (sStatsdLock) {
@@ -386,9 +381,9 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        }
        }
    }
    }


    public final static class PeriodicAlarmReceiver extends BroadcastReceiver {
    public final static class PeriodicAlarmListener implements OnAlarmListener {
        @Override
        @Override
        public void onReceive(Context context, Intent intent) {
        public void onAlarm() {
            if (DEBUG)
            if (DEBUG)
                Slog.d(TAG, "Time to trigger periodic alarm.");
                Slog.d(TAG, "Time to trigger periodic alarm.");
            synchronized (sStatsdLock) {
            synchronized (sStatsdLock) {
@@ -442,9 +437,9 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        try {
        try {
            // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will
            // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will
            // only fire when it awakens.
            // only fire when it awakens.
            // This alarm is inexact, leaving its exactness completely up to the OS optimizations.
            // AlarmManager will automatically cancel any previous mAnomalyAlarmListener alarm.
            // AlarmManager will automatically cancel any previous mAnomalyAlarmIntent alarm.
            mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, timestampMs, TAG + ".anomaly",
            mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, timestampMs, mAnomalyAlarmIntent);
                    mAnomalyAlarmListener, null);
        } finally {
        } finally {
            Binder.restoreCallingIdentity(callingToken);
            Binder.restoreCallingIdentity(callingToken);
        }
        }
@@ -456,7 +451,7 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        if (DEBUG) Slog.d(TAG, "Cancelling anomaly alarm");
        if (DEBUG) Slog.d(TAG, "Cancelling anomaly alarm");
        final long callingToken = Binder.clearCallingIdentity();
        final long callingToken = Binder.clearCallingIdentity();
        try {
        try {
            mAlarmManager.cancel(mAnomalyAlarmIntent);
            mAlarmManager.cancel(mAnomalyAlarmListener);
        } finally {
        } finally {
            Binder.restoreCallingIdentity(callingToken);
            Binder.restoreCallingIdentity(callingToken);
        }
        }
@@ -472,7 +467,8 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        try {
        try {
            // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will
            // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will
            // only fire when it awakens.
            // only fire when it awakens.
            mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, timestampMs, mPeriodicAlarmIntent);
            mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, timestampMs, TAG + ".periodic",
                    mPeriodicAlarmListener, null);
        } finally {
        } finally {
            Binder.restoreCallingIdentity(callingToken);
            Binder.restoreCallingIdentity(callingToken);
        }
        }
@@ -485,7 +481,7 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
            Slog.d(TAG, "Cancelling periodic alarm");
            Slog.d(TAG, "Cancelling periodic alarm");
        final long callingToken = Binder.clearCallingIdentity();
        final long callingToken = Binder.clearCallingIdentity();
        try {
        try {
            mAlarmManager.cancel(mPeriodicAlarmIntent);
            mAlarmManager.cancel(mPeriodicAlarmListener);
        } finally {
        } finally {
            Binder.restoreCallingIdentity(callingToken);
            Binder.restoreCallingIdentity(callingToken);
        }
        }
@@ -494,14 +490,16 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
    @Override // Binder call
    @Override // Binder call
    public void setPullingAlarm(long nextPullTimeMs) {
    public void setPullingAlarm(long nextPullTimeMs) {
        enforceCallingPermission();
        enforceCallingPermission();
      if (DEBUG)
        if (DEBUG) {
        Slog.d(TAG,
            Slog.d(TAG, "Setting pulling alarm in about "
            "Setting pulling alarm in about " + (nextPullTimeMs - SystemClock.elapsedRealtime()));
                    + (nextPullTimeMs - SystemClock.elapsedRealtime()));
        }
        final long callingToken = Binder.clearCallingIdentity();
        final long callingToken = Binder.clearCallingIdentity();
        try {
        try {
            // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will
            // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will
            // only fire when it awakens.
            // only fire when it awakens.
        mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, nextPullTimeMs, mPullingAlarmIntent);
            mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, nextPullTimeMs, TAG + ".pull",
                    mPullingAlarmListener, null);
        } finally {
        } finally {
            Binder.restoreCallingIdentity(callingToken);
            Binder.restoreCallingIdentity(callingToken);
        }
        }
@@ -514,7 +512,7 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
            Slog.d(TAG, "Cancelling pulling alarm");
            Slog.d(TAG, "Cancelling pulling alarm");
        final long callingToken = Binder.clearCallingIdentity();
        final long callingToken = Binder.clearCallingIdentity();
        try {
        try {
        mAlarmManager.cancel(mPullingAlarmIntent);
            mAlarmManager.cancel(mPullingAlarmListener);
        } finally {
        } finally {
            Binder.restoreCallingIdentity(callingToken);
            Binder.restoreCallingIdentity(callingToken);
        }
        }