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