diff options
8 files changed, 51 insertions, 83 deletions
diff --git a/apex/jobscheduler/framework/java/android/app/DeviceIdleFrameworkInitializer.java b/apex/jobscheduler/framework/java/android/app/DeviceIdleFrameworkInitializer.java index cb1e6d9dc0a4..c264531c3947 100644 --- a/apex/jobscheduler/framework/java/android/app/DeviceIdleFrameworkInitializer.java +++ b/apex/jobscheduler/framework/java/android/app/DeviceIdleFrameworkInitializer.java @@ -19,14 +19,8 @@ package android.app; import android.content.Context; import android.os.DeviceIdleManager; import android.os.IDeviceIdleController; -import android.os.PowerManager; -import android.os.RemoteException; -import android.os.ServiceManager; /** - * This class needs to be pre-loaded by zygote. This is where the device idle manager wrapper - * is registered. - * * @hide */ public class DeviceIdleFrameworkInitializer { @@ -37,18 +31,5 @@ public class DeviceIdleFrameworkInitializer { Context.DEVICE_IDLE_CONTROLLER, DeviceIdleManager.class, (context, b) -> new DeviceIdleManager( context, IDeviceIdleController.Stub.asInterface(b))); - PowerManager.setIsIgnoringBatteryOptimizationsCallback((packageName) -> { - // No need for synchronization on sIDeviceIdleController; worst case - // we just initialize it twice. - if (sIDeviceIdleController == null) { - sIDeviceIdleController = IDeviceIdleController.Stub.asInterface( - ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER)); - } - try { - return sIDeviceIdleController.isPowerSaveWhitelistApp(packageName); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } - }); } } diff --git a/apex/jobscheduler/framework/java/android/app/job/JobParameters.java b/apex/jobscheduler/framework/java/android/app/job/JobParameters.java index 150cdbc3cacf..42cf17b1264e 100644 --- a/apex/jobscheduler/framework/java/android/app/job/JobParameters.java +++ b/apex/jobscheduler/framework/java/android/app/job/JobParameters.java @@ -68,7 +68,11 @@ public class JobParameters implements Parcelable { REASON_DEVICE_THERMAL, }; - /** @hide */ + /** + * @hide + * @deprecated use {@link #getReasonCodeDescription(int)} + */ + @Deprecated public static String getReasonName(int reason) { switch (reason) { case REASON_CANCELED: return "canceled"; @@ -81,6 +85,20 @@ public class JobParameters implements Parcelable { } } + /** @hide */ + // @SystemApi TODO make it a system api for mainline + @NonNull + public static int[] getJobStopReasonCodes() { + return JOB_STOP_REASON_CODES; + } + + /** @hide */ + // @SystemApi TODO make it a system api for mainline + @NonNull + public static String getReasonCodeDescription(int reasonCode) { + return getReasonName(reasonCode); + } + @UnsupportedAppUsage private final int jobId; private final PersistableBundle extras; diff --git a/apex/jobscheduler/framework/java/android/app/job/JobSchedulerFrameworkInitializer.java b/apex/jobscheduler/framework/java/android/app/job/JobSchedulerFrameworkInitializer.java index cc6ca3dd6cbf..175e5f23f761 100644 --- a/apex/jobscheduler/framework/java/android/app/job/JobSchedulerFrameworkInitializer.java +++ b/apex/jobscheduler/framework/java/android/app/job/JobSchedulerFrameworkInitializer.java @@ -19,12 +19,8 @@ package android.app.job; import android.app.JobSchedulerImpl; import android.app.SystemServiceRegistry; import android.content.Context; -import android.os.BatteryStats; /** - * This class needs to be pre-loaded by zygote. This is where the job scheduler service wrapper - * is registered. - * * @hide */ public class JobSchedulerFrameworkInitializer { @@ -32,8 +28,5 @@ public class JobSchedulerFrameworkInitializer { SystemServiceRegistry.registerStaticService( Context.JOB_SCHEDULER_SERVICE, JobScheduler.class, (b) -> new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b))); - - BatteryStats.setJobStopReasons(JobParameters.JOB_STOP_REASON_CODES, - JobParameters::getReasonName); } } diff --git a/apex/jobscheduler/framework/java/android/os/DeviceIdleManager.java b/apex/jobscheduler/framework/java/android/os/DeviceIdleManager.java index e27670c34fb2..0568beb34e08 100644 --- a/apex/jobscheduler/framework/java/android/os/DeviceIdleManager.java +++ b/apex/jobscheduler/framework/java/android/os/DeviceIdleManager.java @@ -84,4 +84,17 @@ public class DeviceIdleManager { return 0; } } + + /** + * Return whether a given package is in the power-save whitelist or not. + * @hide + */ + public boolean isApplicationWhitelisted(@NonNull String packageName) { + try { + return mService.isPowerSaveWhitelistApp(packageName); + } catch (RemoteException e) { + e.rethrowFromSystemServer(); + return false; + } + } } diff --git a/config/preloaded-classes b/config/preloaded-classes index 8d911443ce06..1b9898a3cd68 100644 --- a/config/preloaded-classes +++ b/config/preloaded-classes @@ -200,7 +200,6 @@ android.app.ContentProviderHolder android.app.ContextImpl$1 android.app.ContextImpl$ApplicationContentResolver android.app.ContextImpl -android.app.DeviceIdleFrameworkInitializer android.app.DexLoadReporter android.app.Dialog$ListenersHandler android.app.Dialog diff --git a/config/preloaded-classes-extra b/config/preloaded-classes-extra index 4bfa873f63f2..09f393ad4844 100644 --- a/config/preloaded-classes-extra +++ b/config/preloaded-classes-extra @@ -1,7 +1,3 @@ -# JobSchedulerFrameworkInitializer must always be preloaded because it registers the job scheduler -# service wrapper to SystemServiceRegistry. -android.app.DeviceIdleFrameworkInitializer -android.app.job.JobSchedulerFrameworkInitializer android.icu.impl.coll.CollationRoot android.icu.impl.IDNA2003 android.icu.impl.number.Parse diff --git a/core/java/android/os/BatteryStats.java b/core/java/android/os/BatteryStats.java index af0ec1188bc7..1c090bc10357 100644 --- a/core/java/android/os/BatteryStats.java +++ b/core/java/android/os/BatteryStats.java @@ -22,6 +22,7 @@ import static android.app.ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE_LOCAT import android.annotation.IntDef; import android.annotation.UnsupportedAppUsage; import android.app.ActivityManager; +import android.app.job.JobParameters; import android.content.Context; import android.content.pm.ApplicationInfo; import android.server.ServerProtoEnums; @@ -45,7 +46,6 @@ import com.android.internal.annotations.VisibleForTesting; import com.android.internal.location.gnssmetrics.GnssMetrics; import com.android.internal.os.BatterySipper; import com.android.internal.os.BatteryStatsHelper; -import com.android.internal.util.Preconditions; import java.io.FileDescriptor; import java.io.PrintWriter; @@ -59,7 +59,6 @@ import java.util.Formatter; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.function.Function; /** * A class providing access to battery usage statistics, including information on @@ -420,40 +419,6 @@ public abstract class BatteryStats implements Parcelable { }; /** - * "Job stop reason codes" from the job scheduler subsystem, which we can't refer to from this - * class, so we initialize it from the job scheduler side at runtime using - * {@link #setJobStopReasons}. - */ - private static int[] sJobStopReasonCodes = {0}; - - /** - * A function that converts the "job stop reason codes" to their names. - * - * Similarly to {@link #sJobStopReasonCodes} it's initialized by the job scheduler subsystem - * using {@link #setJobStopReasons}. - */ - private static Function<Integer, String> sJobStopReasonNameConverter = (x) -> "unknown"; - - /** - * Set by the job scheduler subsystem to "push" job stop reasons, and a function that returns - * the "name" of each code. We do it this way to remove a build time dependency from this - * class to the job scheduler framework code. - * - * Note the passed array will be used as-is, without copying. The caller must not change - * the array it passed to it. - * - * @hide - */ - public static void setJobStopReasons(int[] reasonCodes, - Function<Integer, String> jobStopReasonNameConverter) { - Preconditions.checkArgument(reasonCodes.length > 0); - Preconditions.checkArgument(jobStopReasonNameConverter != null); - - sJobStopReasonCodes = reasonCodes; - sJobStopReasonNameConverter = jobStopReasonNameConverter; - } - - /** * State for keeping track of counting information. */ public static abstract class Counter { @@ -4341,15 +4306,16 @@ public abstract class BatteryStats implements Parcelable { } } - final Object[] jobCompletionArgs = new Object[sJobStopReasonCodes.length + 1]; + final int[] jobStopReasonCodes = JobParameters.getJobStopReasonCodes(); + final Object[] jobCompletionArgs = new Object[jobStopReasonCodes.length + 1]; final ArrayMap<String, SparseIntArray> completions = u.getJobCompletionStats(); for (int ic=completions.size()-1; ic>=0; ic--) { SparseIntArray types = completions.valueAt(ic); if (types != null) { jobCompletionArgs[0] = "\"" + completions.keyAt(ic) + "\""; - for (int i = 0; i < sJobStopReasonCodes.length; i++) { - jobCompletionArgs[i + 1] = types.get(sJobStopReasonCodes[i], 0); + for (int i = 0; i < jobStopReasonCodes.length; i++) { + jobCompletionArgs[i + 1] = types.get(jobStopReasonCodes[i], 0); } dumpLine(pw, uid, category, JOB_COMPLETION_DATA, jobCompletionArgs); @@ -5969,7 +5935,7 @@ public abstract class BatteryStats implements Parcelable { pw.print(":"); for (int it=0; it<types.size(); it++) { pw.print(" "); - pw.print(sJobStopReasonNameConverter.apply(types.keyAt(it))); + pw.print(JobParameters.getReasonCodeDescription(types.keyAt(it))); pw.print("("); pw.print(types.valueAt(it)); pw.print("x)"); @@ -7567,7 +7533,7 @@ public abstract class BatteryStats implements Parcelable { proto.write(UidProto.JobCompletion.NAME, completions.keyAt(ic)); - for (int r : sJobStopReasonCodes) { + for (int r : JobParameters.getJobStopReasonCodes()) { long rToken = proto.start(UidProto.JobCompletion.REASON_COUNT); proto.write(UidProto.JobCompletion.ReasonCount.NAME, r); proto.write(UidProto.JobCompletion.ReasonCount.COUNT, types.get(r, 0)); diff --git a/core/java/android/os/PowerManager.java b/core/java/android/os/PowerManager.java index 794d62e8fd6c..dd1f8c31ebb1 100644 --- a/core/java/android/os/PowerManager.java +++ b/core/java/android/os/PowerManager.java @@ -37,7 +37,6 @@ import com.android.internal.util.Preconditions; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.concurrent.Executor; -import java.util.function.Function; /** * This class gives you control of the power state of the device. @@ -828,13 +827,13 @@ public final class PowerManager { @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) final Handler mHandler; + /** We lazily initialize it.*/ + private DeviceIdleManager mDeviceIdleManager; + IThermalService mThermalService; private final ArrayMap<OnThermalStatusChangedListener, IThermalStatusListener> mListenerMap = new ArrayMap<>(); - private static Function<String, Boolean> sIsIgnoringBatteryOptimizationsCallback = - (packageName) -> false; - /** * {@hide} */ @@ -844,6 +843,14 @@ public final class PowerManager { mHandler = handler; } + private DeviceIdleManager getDeviceIdleManager() { + if (mDeviceIdleManager == null) { + // No need for synchronization; getSystemService() will return the same object anyway. + mDeviceIdleManager = mContext.getSystemService(DeviceIdleManager.class); + } + return mDeviceIdleManager; + } + /** * Gets the minimum supported screen brightness setting. * The screen may be allowed to become dimmer than this value but @@ -1662,12 +1669,7 @@ public final class PowerManager { * {@link android.provider.Settings#ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS}. */ public boolean isIgnoringBatteryOptimizations(String packageName) { - return sIsIgnoringBatteryOptimizationsCallback.apply(packageName); - } - - /** @hide */ - public static void setIsIgnoringBatteryOptimizationsCallback(Function<String, Boolean> f) { - sIsIgnoringBatteryOptimizationsCallback = f; + return getDeviceIdleManager().isApplicationWhitelisted(packageName); } /** |