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.compat.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 28 import com.android.internal.app.IBatteryStats; 29 30 /** 31 * Provides access to data about how various system resources are used by applications. 32 * @more 33 * <p> 34 * If you are going to be using this class to log your application's resource usage, 35 * please consider the amount of resources (battery, network, etc) that will be used 36 * by the logging itself. It can be substantial. 37 * <p> 38 * <b>Battery Usage</b><br> 39 * Since Android version {@link android.os.Build.VERSION_CODES#Q}, the statistics related to power 40 * (battery) usage are recorded since the device was last considered fully charged (for previous 41 * versions, it is instead since the device was last unplugged). 42 * It is expected that applications schedule more work to do while the device is 43 * plugged in (e.g. using {@link android.app.job.JobScheduler JobScheduler}), and 44 * while that can affect charging rates, it is still preferable to actually draining 45 * the battery. 46 */ 47 @SystemService(Context.SYSTEM_HEALTH_SERVICE) 48 public class SystemHealthManager { 49 private final IBatteryStats mBatteryStats; 50 51 /** 52 * Construct a new SystemHealthManager object. 53 * @hide 54 */ 55 @UnsupportedAppUsage SystemHealthManager()56 public SystemHealthManager() { 57 this(IBatteryStats.Stub.asInterface(ServiceManager.getService(BatteryStats.SERVICE_NAME))); 58 } 59 60 /** {@hide} */ SystemHealthManager(IBatteryStats batteryStats)61 public SystemHealthManager(IBatteryStats batteryStats) { 62 mBatteryStats = batteryStats; 63 } 64 65 /** 66 * Obtain a SystemHealthManager object for the supplied context. 67 * 68 * @hide 69 */ 70 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) from(Context context)71 public static SystemHealthManager from(Context context) { 72 return (SystemHealthManager)context.getSystemService(Context.SYSTEM_HEALTH_SERVICE); 73 } 74 75 /** 76 * Return a {@link HealthStats} object containing a snapshot of system health 77 * metrics for the given uid (user-id, which in usually corresponds to application). 78 * @more 79 * 80 * An application must hold the {@link android.Manifest.permission#BATTERY_STATS 81 * android.permission.BATTERY_STATS} permission in order to retrieve any HealthStats 82 * other than its own. 83 * 84 * @param uid User ID for a given application. 85 * @return A {@link HealthStats} object containing the metrics for the requested 86 * application. The keys for this HealthStats object will be from the {@link UidHealthStats} 87 * class. 88 * @see Process#myUid() Process.myUid() 89 */ takeUidSnapshot(int uid)90 public HealthStats takeUidSnapshot(int uid) { 91 try { 92 final HealthStatsParceler parceler = mBatteryStats.takeUidSnapshot(uid); 93 return parceler.getHealthStats(); 94 } catch (RemoteException ex) { 95 throw new RuntimeException(ex); 96 } 97 } 98 99 /** 100 * Return a {@link HealthStats} object containing a snapshot of system health 101 * metrics for the application calling this API. This method is the same as calling 102 * {@code takeUidSnapshot(Process.myUid())}. 103 * 104 * @return A {@link HealthStats} object containing the metrics for this application. The keys 105 * for this HealthStats object will be from the {@link UidHealthStats} class. 106 */ takeMyUidSnapshot()107 public HealthStats takeMyUidSnapshot() { 108 return takeUidSnapshot(Process.myUid()); 109 } 110 111 /** 112 * Return a {@link HealthStats} object containing a snapshot of system health 113 * metrics for the given uids (user-id, which in usually corresponds to application). 114 * @more 115 * 116 * An application must hold the {@link android.Manifest.permission#BATTERY_STATS 117 * android.permission.BATTERY_STATS} permission in order to retrieve any HealthStats 118 * other than its own. 119 * 120 * @param uids An array of User IDs to retrieve. 121 * @return An array of {@link HealthStats} objects containing the metrics for each of 122 * the requested uids. The keys for this HealthStats object will be from the 123 * {@link UidHealthStats} class. 124 */ takeUidSnapshots(int[] uids)125 public HealthStats[] takeUidSnapshots(int[] uids) { 126 try { 127 final HealthStatsParceler[] parcelers = mBatteryStats.takeUidSnapshots(uids); 128 final HealthStats[] results = new HealthStats[uids.length]; 129 final int N = uids.length; 130 for (int i=0; i<N; i++) { 131 results[i] = parcelers[i].getHealthStats(); 132 } 133 return results; 134 } catch (RemoteException ex) { 135 throw new RuntimeException(ex); 136 } 137 } 138 139 } 140 141