blob: cc21e99371e4faebfcad87c9964c08a7714e1958 [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
19import android.app.admin.DevicePolicyManager;
20import android.content.Context;
21import android.content.pm.PackageManager;
22import android.os.Binder;
23import android.os.CpuUsageInfo;
24import android.os.IHardwarePropertiesManager;
25
26import java.util.Arrays;
27
28/**
29 * Service for {@link HardwarePropertiesManager}
30 */
31public class HardwarePropertiesManagerService extends IHardwarePropertiesManager.Stub {
32
33 private static native void nativeInit();
34
35 private static native float[] nativeGetFanSpeeds();
36 private static native float[] nativeGetDeviceTemperatures(int type);
37 private static native CpuUsageInfo[] nativeGetCpuUsages();
38
39 private final Context mContext;
40 private final Object mLock = new Object();
41
42 public HardwarePropertiesManagerService(Context context) {
43 mContext = context;
44 synchronized (mLock) {
45 nativeInit();
46 }
47 }
48
49 @Override
50 public float[] getDeviceTemperatures(String callingPackage, int type) throws SecurityException {
51 enforceHardwarePropertiesRetrievalAllowed(callingPackage);
52 synchronized (mLock) {
53 return nativeGetDeviceTemperatures(type);
54 }
55 }
56
57 @Override
58 public CpuUsageInfo[] getCpuUsages(String callingPackage) throws SecurityException {
59 enforceHardwarePropertiesRetrievalAllowed(callingPackage);
60 synchronized (mLock) {
61 return nativeGetCpuUsages();
62 }
63 }
64
65 @Override
66 public float[] getFanSpeeds(String callingPackage) throws SecurityException {
67 enforceHardwarePropertiesRetrievalAllowed(callingPackage);
68 synchronized (mLock) {
69 return nativeGetFanSpeeds();
70 }
71 }
72
73 /**
74 * Throws SecurityException if the calling package is not allowed to retrieve information
75 * provided by the service.
76 *
77 * @param callingPackage The calling package name.
78 *
79 * @throws SecurityException if a non profile or device owner tries to retrieve information
80 * provided by the service.
81 */
82 private void enforceHardwarePropertiesRetrievalAllowed(String callingPackage)
83 throws SecurityException {
84 final PackageManager pm = mContext.getPackageManager();
85 try {
86 final int uid = pm.getPackageUid(callingPackage, 0);
87 if (Binder.getCallingUid() != uid) {
88 throw new SecurityException("The caller has faked the package name.");
89 }
90 } catch (PackageManager.NameNotFoundException e) {
91 throw new SecurityException("The caller has faked the package name.");
92 }
93
94 final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
95 if (!dpm.isDeviceOwnerApp(callingPackage) && !dpm.isProfileOwnerApp(callingPackage)) {
96 throw new SecurityException("The caller is not a device or profile owner.");
97 }
98 }
99}