Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [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 com.android.server; |
| 18 | |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 19 | import android.Manifest; |
| 20 | import android.app.ActivityManager; |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 21 | import android.app.admin.DevicePolicyManager; |
| 22 | import android.content.Context; |
| 23 | import android.content.pm.PackageManager; |
| 24 | import android.os.Binder; |
| 25 | import android.os.CpuUsageInfo; |
| 26 | import android.os.IHardwarePropertiesManager; |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 27 | import android.os.Process; |
Ruben Brunk | 8f1d5cb | 2016-03-22 18:08:41 -0700 | [diff] [blame] | 28 | import android.os.UserHandle; |
| 29 | import com.android.server.vr.VrManagerInternal; |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 30 | |
| 31 | import java.util.Arrays; |
| 32 | |
| 33 | /** |
| 34 | * Service for {@link HardwarePropertiesManager} |
| 35 | */ |
| 36 | public class HardwarePropertiesManagerService extends IHardwarePropertiesManager.Stub { |
| 37 | |
| 38 | private static native void nativeInit(); |
| 39 | |
| 40 | private static native float[] nativeGetFanSpeeds(); |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 41 | private static native float[] nativeGetDeviceTemperatures(int type, int source); |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 42 | private static native CpuUsageInfo[] nativeGetCpuUsages(); |
| 43 | |
| 44 | private final Context mContext; |
| 45 | private final Object mLock = new Object(); |
| 46 | |
| 47 | public HardwarePropertiesManagerService(Context context) { |
| 48 | mContext = context; |
| 49 | synchronized (mLock) { |
| 50 | nativeInit(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | @Override |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 55 | public float[] getDeviceTemperatures(String callingPackage, int type, int source) |
| 56 | throws SecurityException { |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 57 | enforceHardwarePropertiesRetrievalAllowed(callingPackage); |
| 58 | synchronized (mLock) { |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 59 | return nativeGetDeviceTemperatures(type, source); |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public CpuUsageInfo[] getCpuUsages(String callingPackage) throws SecurityException { |
| 65 | enforceHardwarePropertiesRetrievalAllowed(callingPackage); |
| 66 | synchronized (mLock) { |
| 67 | return nativeGetCpuUsages(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public float[] getFanSpeeds(String callingPackage) throws SecurityException { |
| 73 | enforceHardwarePropertiesRetrievalAllowed(callingPackage); |
| 74 | synchronized (mLock) { |
| 75 | return nativeGetFanSpeeds(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Throws SecurityException if the calling package is not allowed to retrieve information |
| 81 | * provided by the service. |
| 82 | * |
| 83 | * @param callingPackage The calling package name. |
| 84 | * |
Polina Bondarenko | 8503fd5 | 2016-12-20 12:44:38 +0100 | [diff] [blame] | 85 | * @throws SecurityException if something other than the device owner, the current VR service, |
| 86 | * or a caller holding the {@link Manifest.permission#DEVICE_POWER} permission tries to |
| 87 | * retrieve information provided by this service. |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 88 | */ |
| 89 | private void enforceHardwarePropertiesRetrievalAllowed(String callingPackage) |
| 90 | throws SecurityException { |
| 91 | final PackageManager pm = mContext.getPackageManager(); |
Ruben Brunk | 8f1d5cb | 2016-03-22 18:08:41 -0700 | [diff] [blame] | 92 | int uid = 0; |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 93 | try { |
Ruben Brunk | 8f1d5cb | 2016-03-22 18:08:41 -0700 | [diff] [blame] | 94 | uid = pm.getPackageUid(callingPackage, 0); |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 95 | if (Binder.getCallingUid() != uid) { |
| 96 | throw new SecurityException("The caller has faked the package name."); |
| 97 | } |
| 98 | } catch (PackageManager.NameNotFoundException e) { |
| 99 | throw new SecurityException("The caller has faked the package name."); |
| 100 | } |
| 101 | |
Ruben Brunk | 8f1d5cb | 2016-03-22 18:08:41 -0700 | [diff] [blame] | 102 | final int userId = UserHandle.getUserId(uid); |
| 103 | final VrManagerInternal vrService = LocalServices.getService(VrManagerInternal.class); |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 104 | final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class); |
Polina Bondarenko | 8503fd5 | 2016-12-20 12:44:38 +0100 | [diff] [blame] | 105 | if (!dpm.isDeviceOwnerApp(callingPackage) |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 106 | && !vrService.isCurrentVrListener(callingPackage, userId) |
| 107 | && mContext.checkCallingOrSelfPermission(Manifest.permission.DEVICE_POWER) |
| 108 | != PackageManager.PERMISSION_GRANTED) { |
Polina Bondarenko | 8503fd5 | 2016-12-20 12:44:38 +0100 | [diff] [blame] | 109 | throw new SecurityException("The caller is not a device owner, bound VrListenerService" |
| 110 | + ", or holding the DEVICE_POWER permission."); |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | } |