diff --git a/apex/jobscheduler/framework/java/android/app/tare/IEconomyManager.aidl b/apex/jobscheduler/framework/java/android/app/tare/IEconomyManager.aidl new file mode 100644 index 0000000000000..bb150118ebb1b --- /dev/null +++ b/apex/jobscheduler/framework/java/android/app/tare/IEconomyManager.aidl @@ -0,0 +1,24 @@ +/** + * Copyright (C) 2021 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 android.app.tare; + + /** + * IPC interface that supports the app-facing {@link #EconomyManager} api. + * {@hide} + */ +interface IEconomyManager { +} diff --git a/apex/jobscheduler/service/java/com/android/server/tare/InternalResourceService.java b/apex/jobscheduler/service/java/com/android/server/tare/InternalResourceService.java index cfc6645d9015a..0bbb8029b380f 100644 --- a/apex/jobscheduler/service/java/com/android/server/tare/InternalResourceService.java +++ b/apex/jobscheduler/service/java/com/android/server/tare/InternalResourceService.java @@ -22,6 +22,7 @@ import static android.text.format.DateUtils.MINUTE_IN_MILLIS; import android.annotation.NonNull; import android.annotation.Nullable; import android.app.AlarmManager; +import android.app.tare.IEconomyManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -30,6 +31,7 @@ import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.net.Uri; import android.os.BatteryManagerInternal; +import android.os.Binder; import android.os.Bundle; import android.os.Handler; import android.os.Looper; @@ -37,14 +39,19 @@ import android.os.Message; import android.os.SystemClock; import android.os.UserHandle; import android.util.ArraySet; +import android.util.IndentingPrintWriter; import android.util.Log; import android.util.Slog; import android.util.SparseSetArray; import com.android.internal.annotations.GuardedBy; +import com.android.internal.util.ArrayUtils; +import com.android.internal.util.DumpUtils; import com.android.server.LocalServices; import com.android.server.SystemService; +import java.io.FileDescriptor; +import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; @@ -72,8 +79,9 @@ public class InternalResourceService extends SystemService { private final BatteryManagerInternal mBatteryManagerInternal; private final PackageManager mPackageManager; - private final CompleteEconomicPolicy mCompleteEconomicPolicy; private final Agent mAgent; + private final CompleteEconomicPolicy mCompleteEconomicPolicy; + private final EconomyManagerStub mEconomyManagerStub; @NonNull @GuardedBy("mLock") @@ -173,6 +181,7 @@ public class InternalResourceService extends SystemService { mHandler = new IrsHandler(TareHandlerThread.get().getLooper()); mBatteryManagerInternal = LocalServices.getService(BatteryManagerInternal.class); mPackageManager = context.getPackageManager(); + mEconomyManagerStub = new EconomyManagerStub(); mCompleteEconomicPolicy = new CompleteEconomicPolicy(this); mAgent = new Agent(this, mCompleteEconomicPolicy); @@ -194,7 +203,7 @@ public class InternalResourceService extends SystemService { @Override public void onStart() { - + publishBinderService(Context.RESOURCE_ECONOMY_SERVICE, mEconomyManagerStub); } @Override @@ -428,6 +437,37 @@ public class InternalResourceService extends SystemService { } } + /** + * Binder stub trampoline implementation + */ + final class EconomyManagerStub extends IEconomyManager.Stub { + /** + * "dumpsys" infrastructure + */ + @Override + public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { + if (!DumpUtils.checkDumpAndUsageStatsPermission(getContext(), TAG, pw)) return; + + if (!ArrayUtils.isEmpty(args)) { + String arg = args[0]; + if ("-h".equals(arg) || "--help".equals(arg)) { + dumpHelp(pw); + return; + } else if (arg.length() > 0 && arg.charAt(0) == '-') { + pw.println("Unknown option: " + arg); + return; + } + } + + final long identityToken = Binder.clearCallingIdentity(); + try { + dumpInternal(new IndentingPrintWriter(pw, " ")); + } finally { + Binder.restoreCallingIdentity(identityToken); + } + } + } + private final class LocalService implements EconomyManagerInternal { @Override public void registerAffordabilityChangeListener(int userId, @NonNull String pkgName, @@ -490,4 +530,23 @@ public class InternalResourceService extends SystemService { } } } + + private static void dumpHelp(PrintWriter pw) { + pw.println("Resource Economy (economy) dump options:"); + pw.println(" [-h|--help] [package] ..."); + pw.println(" -h | --help: print this help"); + pw.println(" [package] is an optional package name to limit the output to."); + } + + private void dumpInternal(final IndentingPrintWriter pw) { + synchronized (mLock) { + pw.print("Current battery level: "); + pw.println(mCurrentBatteryLevel); + + mCompleteEconomicPolicy.dump(pw); + pw.println(); + + mAgent.dumpLocked(pw); + } + } } diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java index c02dcfd3d6815..ac88c7bdcb184 100644 --- a/core/java/android/content/Context.java +++ b/core/java/android/content/Context.java @@ -5228,6 +5228,15 @@ public abstract class Context { */ public static final String JOB_SCHEDULER_SERVICE = "jobscheduler"; + /** + * Use with {@link #getSystemService(String)} to retrieve a + * {@link android.app.tare.EconomyManager} instance for understanding economic standing. + * @see #getSystemService(String) + * @hide + * @see android.app.tare.EconomyManager + */ + public static final String RESOURCE_ECONOMY_SERVICE = "tare"; + /** * Use with {@link #getSystemService(String)} to retrieve a {@link * android.service.persistentdata.PersistentDataBlockManager} instance