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

Commit fb2a9989 authored by Kweku Adams's avatar Kweku Adams
Browse files

Add dumping functionality.

Add the glue to be able to dump tare state.

Bug: 158300259
Test: adb shell dumpsys tare
Change-Id: If4fd4355611b4ce697834c2124f387b288f54fd2
parent b448a897
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
/**
 * 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 {
}
+61 −2
Original line number Diff line number Diff line
@@ -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);
        }
    }
}
+9 −0
Original line number Diff line number Diff line
@@ -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