Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 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 | |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 19 | import android.annotation.SystemService; |
Mathew Inwood | 98e9ad1 | 2018-08-30 13:11:50 +0100 | [diff] [blame] | 20 | import android.annotation.UnsupportedAppUsage; |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.os.BatteryStats; |
Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 23 | import android.os.Build; |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 24 | import android.os.Process; |
| 25 | import android.os.RemoteException; |
| 26 | import android.os.ServiceManager; |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 27 | import android.os.ServiceManager.ServiceNotFoundException; |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 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 |
Joe Onorato | 181cada | 2016-05-09 10:31:41 -0700 | [diff] [blame] | 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> |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 39 | * <b>Battery Usage</b><br> |
Mike Ma | 99e57c3 | 2018-03-15 14:40:37 -0700 | [diff] [blame] | 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. |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 47 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 48 | @SystemService(Context.SYSTEM_HEALTH_SERVICE) |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 49 | public class SystemHealthManager { |
| 50 | private final IBatteryStats mBatteryStats; |
| 51 | |
| 52 | /** |
| 53 | * Construct a new SystemHealthManager object. |
| 54 | * @hide |
| 55 | */ |
Mathew Inwood | 98e9ad1 | 2018-08-30 13:11:50 +0100 | [diff] [blame] | 56 | @UnsupportedAppUsage |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 57 | public SystemHealthManager() { |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 58 | this(IBatteryStats.Stub.asInterface(ServiceManager.getService(BatteryStats.SERVICE_NAME))); |
| 59 | } |
| 60 | |
| 61 | /** {@hide} */ |
| 62 | public SystemHealthManager(IBatteryStats batteryStats) { |
| 63 | mBatteryStats = batteryStats; |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Obtain a SystemHealthManager object for the supplied context. |
Joe Onorato | 22cfef3 | 2016-05-17 15:29:51 -0700 | [diff] [blame] | 68 | * |
| 69 | * @hide |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 70 | */ |
Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 71 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 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. |
Joe Onorato | 168638c | 2016-05-09 13:31:00 -0700 | [diff] [blame] | 89 | * @see Process#myUid() Process.myUid() |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 90 | */ |
| 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 | |