• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package android.os.health;
18 
19 import android.annotation.SystemService;
20 import android.annotation.UnsupportedAppUsage;
21 import android.content.Context;
22 import android.os.BatteryStats;
23 import android.os.Build;
24 import android.os.Process;
25 import android.os.RemoteException;
26 import android.os.ServiceManager;
27 import android.os.ServiceManager.ServiceNotFoundException;
28 
29 import com.android.internal.app.IBatteryStats;
30 
31 /**
32  * Provides access to data about how various system resources are used by applications.
33  * @more
34  * <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>
39  * <b>Battery Usage</b><br>
40  * 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.
47  */
48 @SystemService(Context.SYSTEM_HEALTH_SERVICE)
49 public class SystemHealthManager {
50     private final IBatteryStats mBatteryStats;
51 
52     /**
53      * Construct a new SystemHealthManager object.
54      * @hide
55      */
56     @UnsupportedAppUsage
SystemHealthManager()57     public SystemHealthManager() {
58         this(IBatteryStats.Stub.asInterface(ServiceManager.getService(BatteryStats.SERVICE_NAME)));
59     }
60 
61     /** {@hide} */
SystemHealthManager(IBatteryStats batteryStats)62     public SystemHealthManager(IBatteryStats batteryStats) {
63         mBatteryStats = batteryStats;
64     }
65 
66     /**
67      * Obtain a SystemHealthManager object for the supplied context.
68      *
69      * @hide
70      */
71     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
from(Context context)72     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.
89      * @see Process#myUid() Process.myUid()
90      */
takeUidSnapshot(int uid)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      */
takeMyUidSnapshot()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      */
takeUidSnapshots(int[] uids)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 
142