1713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato/* 2713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Copyright (C) 2016 The Android Open Source Project 3713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * 4713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Licensed under the Apache License, Version 2.0 (the "License"); 5713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * you may not use this file except in compliance with the License. 6713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * You may obtain a copy of the License at 7713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * 8713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * http://www.apache.org/licenses/LICENSE-2.0 9713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * 10713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Unless required by applicable law or agreed to in writing, software 11713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * distributed under the License is distributed on an "AS IS" BASIS, 12713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * See the License for the specific language governing permissions and 14713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * limitations under the License. 15713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 16713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 17713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onoratopackage android.os.health; 18713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 19713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onoratoimport android.content.Context; 20713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onoratoimport android.os.BatteryStats; 21713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onoratoimport android.os.Process; 22713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onoratoimport android.os.RemoteException; 23713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onoratoimport android.os.ServiceManager; 24713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 25713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onoratoimport com.android.internal.app.IBatteryStats; 26713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 27713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato/** 28713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Provides access to data about how various system resources are used by applications. 29713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * @more 30181cada18a9feab90627ab27070bc00c29ec337aJoe Onorato * <p> 31181cada18a9feab90627ab27070bc00c29ec337aJoe Onorato * If you are going to be using this class to log your application's resource usage, 32181cada18a9feab90627ab27070bc00c29ec337aJoe Onorato * please consider the amount of resources (battery, network, etc) that will be used 33181cada18a9feab90627ab27070bc00c29ec337aJoe Onorato * by the logging itself. It can be substantial. 34181cada18a9feab90627ab27070bc00c29ec337aJoe Onorato * <p> 35713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * <b>Battery Usage</b><br> 36713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * The statistics related to power (battery) usage are recorded since the device 37713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * was last unplugged. It is expected that applications schedule more work to do 38713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * while the device is plugged in (e.g. using {@link android.app.job.JobScheduler 39713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * JobScheduler}), and while that can affect charging rates, it is still preferable 40713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * to actually draining the battery. 41713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 42713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onoratopublic class SystemHealthManager { 43713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato private final IBatteryStats mBatteryStats; 44713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 45713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato /** 46713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Construct a new SystemHealthManager object. 47713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * @hide 48713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 49713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato public SystemHealthManager() { 50713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato mBatteryStats = IBatteryStats.Stub.asInterface( 51713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato ServiceManager.getService(BatteryStats.SERVICE_NAME)); 52713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 53713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 54713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato /** 55713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Obtain a SystemHealthManager object for the supplied context. 5622cfef381d437bd26d23909f733ddc537a44530fJoe Onorato * 5722cfef381d437bd26d23909f733ddc537a44530fJoe Onorato * @hide 58713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 59713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato public static SystemHealthManager from(Context context) { 60713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato return (SystemHealthManager)context.getSystemService(Context.SYSTEM_HEALTH_SERVICE); 61713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 62713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 63713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato /** 64713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Return a {@link HealthStats} object containing a snapshot of system health 65713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * metrics for the given uid (user-id, which in usually corresponds to application). 66713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * @more 67713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * 68713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * An application must hold the {@link android.Manifest.permission#BATTERY_STATS 69713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * android.permission.BATTERY_STATS} permission in order to retrieve any HealthStats 70713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * other than its own. 71713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * 72713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * @param uid User ID for a given application. 73713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * @return A {@link HealthStats} object containing the metrics for the requested 74713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * application. The keys for this HealthStats object will be from the {@link UidHealthStats} 75713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * class. 76168638c5949fcff8d28aa4d4dd918cbeaf95f301Joe Onorato * @see Process#myUid() Process.myUid() 77713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 78713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato public HealthStats takeUidSnapshot(int uid) { 79713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato try { 80713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato final HealthStatsParceler parceler = mBatteryStats.takeUidSnapshot(uid); 81713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato return parceler.getHealthStats(); 82713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } catch (RemoteException ex) { 83713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato throw new RuntimeException(ex); 84713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 85713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 86713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 87713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato /** 88713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Return a {@link HealthStats} object containing a snapshot of system health 89713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * metrics for the application calling this API. This method is the same as calling 90713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * {@code takeUidSnapshot(Process.myUid())}. 91713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * 92713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * @return A {@link HealthStats} object containing the metrics for this application. The keys 93713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * for this HealthStats object will be from the {@link UidHealthStats} class. 94713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 95713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato public HealthStats takeMyUidSnapshot() { 96713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato return takeUidSnapshot(Process.myUid()); 97713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 98713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 99713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato /** 100713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Return a {@link HealthStats} object containing a snapshot of system health 101713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * metrics for the given uids (user-id, which in usually corresponds to application). 102713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * @more 103713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * 104713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * An application must hold the {@link android.Manifest.permission#BATTERY_STATS 105713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * android.permission.BATTERY_STATS} permission in order to retrieve any HealthStats 106713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * other than its own. 107713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * 108713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * @param uids An array of User IDs to retrieve. 109713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * @return An array of {@link HealthStats} objects containing the metrics for each of 110713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * the requested uids. The keys for this HealthStats object will be from the 111713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * {@link UidHealthStats} class. 112713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 113713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato public HealthStats[] takeUidSnapshots(int[] uids) { 114713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato try { 115713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato final HealthStatsParceler[] parcelers = mBatteryStats.takeUidSnapshots(uids); 116713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato final HealthStats[] results = new HealthStats[uids.length]; 117713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato final int N = uids.length; 118713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato for (int i=0; i<N; i++) { 119713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato results[i] = parcelers[i].getHealthStats(); 120713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 121713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato return results; 122713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } catch (RemoteException ex) { 123713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato throw new RuntimeException(ex); 124713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 125713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 126713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 127713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato} 128713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 129