blob: 4016d29fd2e6f289a62935cb2b547283284a3286 [file] [log] [blame]
Polina Bondarenkof8754ac2016-02-12 20:38:23 +01001/*
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 com.android.server;
18
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070019import static android.os.HardwarePropertiesManager.DEVICE_TEMPERATURE_BATTERY;
20import static android.os.HardwarePropertiesManager.DEVICE_TEMPERATURE_CPU;
21import static android.os.HardwarePropertiesManager.DEVICE_TEMPERATURE_GPU;
22import static android.os.HardwarePropertiesManager.DEVICE_TEMPERATURE_SKIN;
23import static android.os.HardwarePropertiesManager.TEMPERATURE_CURRENT;
24import static android.os.HardwarePropertiesManager.TEMPERATURE_SHUTDOWN;
25import static android.os.HardwarePropertiesManager.TEMPERATURE_THROTTLING;
26import static android.os.HardwarePropertiesManager.TEMPERATURE_THROTTLING_BELOW_VR_MIN;
27
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -080028import android.Manifest;
29import android.app.ActivityManager;
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070030import android.app.AppOpsManager;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010031import android.app.admin.DevicePolicyManager;
32import android.content.Context;
33import android.content.pm.PackageManager;
34import android.os.Binder;
35import android.os.CpuUsageInfo;
36import android.os.IHardwarePropertiesManager;
Polina Bondarenkoeb845522016-03-01 14:39:48 +010037import android.os.Process;
Ruben Brunk8f1d5cb2016-03-22 18:08:41 -070038import android.os.UserHandle;
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070039import com.android.internal.util.DumpUtils;
Ruben Brunk8f1d5cb2016-03-22 18:08:41 -070040import com.android.server.vr.VrManagerInternal;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010041
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070042import java.io.FileDescriptor;
43import java.io.PrintWriter;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010044import java.util.Arrays;
45
46/**
47 * Service for {@link HardwarePropertiesManager}
48 */
49public class HardwarePropertiesManagerService extends IHardwarePropertiesManager.Stub {
50
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070051 private static final String TAG = "HardwarePropertiesManagerService";
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010052 private static native void nativeInit();
53
54 private static native float[] nativeGetFanSpeeds();
Polina Bondarenkoeb845522016-03-01 14:39:48 +010055 private static native float[] nativeGetDeviceTemperatures(int type, int source);
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010056 private static native CpuUsageInfo[] nativeGetCpuUsages();
57
58 private final Context mContext;
59 private final Object mLock = new Object();
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070060 private final AppOpsManager mAppOps;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010061
62 public HardwarePropertiesManagerService(Context context) {
63 mContext = context;
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070064 mAppOps = (AppOpsManager)mContext.getSystemService(Context.APP_OPS_SERVICE);
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010065 synchronized (mLock) {
66 nativeInit();
67 }
68 }
69
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070070 // TODO - Make HardwarePropertiesManager APIs require a userId to verifiy
71 // cross user permission - b/63697518
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010072 @Override
Polina Bondarenkoeb845522016-03-01 14:39:48 +010073 public float[] getDeviceTemperatures(String callingPackage, int type, int source)
74 throws SecurityException {
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010075 enforceHardwarePropertiesRetrievalAllowed(callingPackage);
76 synchronized (mLock) {
Polina Bondarenkoeb845522016-03-01 14:39:48 +010077 return nativeGetDeviceTemperatures(type, source);
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010078 }
79 }
80
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070081 // TODO - Make HardwarePropertiesManager APIs require a userId to verifiy
82 // cross user permission - b/63697518
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010083 @Override
84 public CpuUsageInfo[] getCpuUsages(String callingPackage) throws SecurityException {
85 enforceHardwarePropertiesRetrievalAllowed(callingPackage);
86 synchronized (mLock) {
87 return nativeGetCpuUsages();
88 }
89 }
90
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070091 // TODO - Make HardwarePropertiesManager APIs require a userId to verifiy
92 // cross user permission - b/63697518
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010093 @Override
94 public float[] getFanSpeeds(String callingPackage) throws SecurityException {
95 enforceHardwarePropertiesRetrievalAllowed(callingPackage);
96 synchronized (mLock) {
97 return nativeGetFanSpeeds();
98 }
99 }
100
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -0700101 private String getCallingPackageName() {
102 final String[] packages = mContext.getPackageManager().getPackagesForUid(
103 Binder.getCallingUid());
104 if (packages != null && packages.length > 0) {
105 return packages[0];
106 }
107 return "unknown";
108 }
109
110 private void dumpTempValues(String pkg, PrintWriter pw, int type,
111 String typeLabel) {
112 dumpTempValues(pkg, pw, type, typeLabel, "temperatures: ",
113 TEMPERATURE_CURRENT);
114 dumpTempValues(pkg, pw, type, typeLabel, "throttling temperatures: ",
115 TEMPERATURE_THROTTLING);
116 dumpTempValues(pkg, pw, type, typeLabel, "shutdown temperatures: ",
117 TEMPERATURE_SHUTDOWN);
118 dumpTempValues(pkg, pw, type, typeLabel, "vr throttling temperatures: ",
119 TEMPERATURE_THROTTLING_BELOW_VR_MIN);
120 }
121
122 private void dumpTempValues(String pkg, PrintWriter pw, int type,
123 String typeLabel, String subLabel, int valueType) {
124 pw.println(typeLabel + subLabel + Arrays.toString(getDeviceTemperatures(
125 pkg, type, valueType)));
126 }
127
128 @Override
129 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
130 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
131 pw.println("****** Dump of HardwarePropertiesManagerService ******");
132
133 final String PKG = getCallingPackageName();
134 dumpTempValues(PKG, pw, DEVICE_TEMPERATURE_CPU, "CPU ");
135 dumpTempValues(PKG, pw, DEVICE_TEMPERATURE_GPU, "GPU ");
136 dumpTempValues(PKG, pw, DEVICE_TEMPERATURE_BATTERY, "Battery ");
137 dumpTempValues(PKG, pw, DEVICE_TEMPERATURE_SKIN, "Skin ");
138
139 float[] fanSpeeds = getFanSpeeds(PKG);
140 pw.println("Fan speed: " + Arrays.toString(fanSpeeds) + "\n");
141
142 CpuUsageInfo[] cpuUsageInfos = getCpuUsages(PKG);
143 int core = 0;
144 for (int i = 0; i < cpuUsageInfos.length; i++) {
145 pw.println("Cpu usage of core: " + i +
146 ", active = " + cpuUsageInfos[i].getActive() +
147 ", total = " + cpuUsageInfos[i].getTotal());
148 }
149 pw.println("****** End of HardwarePropertiesManagerService dump ******");
150 }
151
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100152 /**
153 * Throws SecurityException if the calling package is not allowed to retrieve information
154 * provided by the service.
155 *
156 * @param callingPackage The calling package name.
157 *
Polina Bondarenko8503fd52016-12-20 12:44:38 +0100158 * @throws SecurityException if something other than the device owner, the current VR service,
159 * or a caller holding the {@link Manifest.permission#DEVICE_POWER} permission tries to
160 * retrieve information provided by this service.
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100161 */
162 private void enforceHardwarePropertiesRetrievalAllowed(String callingPackage)
163 throws SecurityException {
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -0700164 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
165 final int userId = UserHandle.getUserId(Binder.getCallingUid());
Ruben Brunk8f1d5cb2016-03-22 18:08:41 -0700166 final VrManagerInternal vrService = LocalServices.getService(VrManagerInternal.class);
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100167 final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
Polina Bondarenko8503fd52016-12-20 12:44:38 +0100168 if (!dpm.isDeviceOwnerApp(callingPackage)
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -0800169 && !vrService.isCurrentVrListener(callingPackage, userId)
170 && mContext.checkCallingOrSelfPermission(Manifest.permission.DEVICE_POWER)
171 != PackageManager.PERMISSION_GRANTED) {
Polina Bondarenko8503fd52016-12-20 12:44:38 +0100172 throw new SecurityException("The caller is not a device owner, bound VrListenerService"
173 + ", or holding the DEVICE_POWER permission.");
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100174 }
175 }
176}
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -0700177
178