blob: a92e28a47660c914a9e4e77902993ad650288894 [file] [log] [blame]
Joe Onorato713fec82016-03-04 10:34:02 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os.health;
18
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060019import android.annotation.SystemService;
Mathew Inwood98e9ad12018-08-30 13:11:50 +010020import android.annotation.UnsupportedAppUsage;
Joe Onorato713fec82016-03-04 10:34:02 -080021import android.content.Context;
22import android.os.BatteryStats;
Mathew Inwood8c854f82018-09-14 12:35:36 +010023import android.os.Build;
Joe Onorato713fec82016-03-04 10:34:02 -080024import android.os.Process;
25import android.os.RemoteException;
26import android.os.ServiceManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060027import android.os.ServiceManager.ServiceNotFoundException;
Joe Onorato713fec82016-03-04 10:34:02 -080028
29import com.android.internal.app.IBatteryStats;
30
31/**
32 * Provides access to data about how various system resources are used by applications.
33 * @more
Joe Onorato181cada2016-05-09 10:31:41 -070034 * <p>
35 * If you are going to be using this class to log your application's resource usage,
36 * please consider the amount of resources (battery, network, etc) that will be used
37 * by the logging itself. It can be substantial.
38 * <p>
Joe Onorato713fec82016-03-04 10:34:02 -080039 * <b>Battery Usage</b><br>
Mike Ma99e57c32018-03-15 14:40:37 -070040 * Since Android version {@link android.os.Build.VERSION_CODES#Q}, the statistics related to power
41 * (battery) usage are recorded since the device was last considered fully charged (for previous
42 * versions, it is instead since the device was last unplugged).
43 * It is expected that applications schedule more work to do while the device is
44 * plugged in (e.g. using {@link android.app.job.JobScheduler JobScheduler}), and
45 * while that can affect charging rates, it is still preferable to actually draining
46 * the battery.
Joe Onorato713fec82016-03-04 10:34:02 -080047 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060048@SystemService(Context.SYSTEM_HEALTH_SERVICE)
Joe Onorato713fec82016-03-04 10:34:02 -080049public class SystemHealthManager {
50 private final IBatteryStats mBatteryStats;
51
52 /**
53 * Construct a new SystemHealthManager object.
54 * @hide
55 */
Mathew Inwood98e9ad12018-08-30 13:11:50 +010056 @UnsupportedAppUsage
Joe Onorato713fec82016-03-04 10:34:02 -080057 public SystemHealthManager() {
Jeff Sharkey49ca5292016-05-10 12:54:45 -060058 this(IBatteryStats.Stub.asInterface(ServiceManager.getService(BatteryStats.SERVICE_NAME)));
59 }
60
61 /** {@hide} */
62 public SystemHealthManager(IBatteryStats batteryStats) {
63 mBatteryStats = batteryStats;
Joe Onorato713fec82016-03-04 10:34:02 -080064 }
65
66 /**
67 * Obtain a SystemHealthManager object for the supplied context.
Joe Onorato22cfef32016-05-17 15:29:51 -070068 *
69 * @hide
Joe Onorato713fec82016-03-04 10:34:02 -080070 */
Mathew Inwood8c854f82018-09-14 12:35:36 +010071 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Joe Onorato713fec82016-03-04 10:34:02 -080072 public static SystemHealthManager from(Context context) {
73 return (SystemHealthManager)context.getSystemService(Context.SYSTEM_HEALTH_SERVICE);
74 }
75
76 /**
77 * Return a {@link HealthStats} object containing a snapshot of system health
78 * metrics for the given uid (user-id, which in usually corresponds to application).
79 * @more
80 *
81 * An application must hold the {@link android.Manifest.permission#BATTERY_STATS
82 * android.permission.BATTERY_STATS} permission in order to retrieve any HealthStats
83 * other than its own.
84 *
85 * @param uid User ID for a given application.
86 * @return A {@link HealthStats} object containing the metrics for the requested
87 * application. The keys for this HealthStats object will be from the {@link UidHealthStats}
88 * class.
Joe Onorato168638c2016-05-09 13:31:00 -070089 * @see Process#myUid() Process.myUid()
Joe Onorato713fec82016-03-04 10:34:02 -080090 */
91 public HealthStats takeUidSnapshot(int uid) {
92 try {
93 final HealthStatsParceler parceler = mBatteryStats.takeUidSnapshot(uid);
94 return parceler.getHealthStats();
95 } catch (RemoteException ex) {
96 throw new RuntimeException(ex);
97 }
98 }
99
100 /**
101 * Return a {@link HealthStats} object containing a snapshot of system health
102 * metrics for the application calling this API. This method is the same as calling
103 * {@code takeUidSnapshot(Process.myUid())}.
104 *
105 * @return A {@link HealthStats} object containing the metrics for this application. The keys
106 * for this HealthStats object will be from the {@link UidHealthStats} class.
107 */
108 public HealthStats takeMyUidSnapshot() {
109 return takeUidSnapshot(Process.myUid());
110 }
111
112 /**
113 * Return a {@link HealthStats} object containing a snapshot of system health
114 * metrics for the given uids (user-id, which in usually corresponds to application).
115 * @more
116 *
117 * An application must hold the {@link android.Manifest.permission#BATTERY_STATS
118 * android.permission.BATTERY_STATS} permission in order to retrieve any HealthStats
119 * other than its own.
120 *
121 * @param uids An array of User IDs to retrieve.
122 * @return An array of {@link HealthStats} objects containing the metrics for each of
123 * the requested uids. The keys for this HealthStats object will be from the
124 * {@link UidHealthStats} class.
125 */
126 public HealthStats[] takeUidSnapshots(int[] uids) {
127 try {
128 final HealthStatsParceler[] parcelers = mBatteryStats.takeUidSnapshots(uids);
129 final HealthStats[] results = new HealthStats[uids.length];
130 final int N = uids.length;
131 for (int i=0; i<N; i++) {
132 results[i] = parcelers[i].getHealthStats();
133 }
134 return results;
135 } catch (RemoteException ex) {
136 throw new RuntimeException(ex);
137 }
138 }
139
140}
141