blob: 5f082572db58d767273c3743df79b5077dfc2778 [file] [log] [blame]
Svet Ganov37e43272016-09-09 16:01:32 -07001/*
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.os;
18
19import android.Manifest;
20import android.annotation.NonNull;
21import android.annotation.Nullable;
22import android.content.Context;
Svetoslav Ganov31c2bba2016-10-05 18:24:31 -070023import android.content.pm.PackageManager;
Svet Ganov37e43272016-09-09 16:01:32 -070024import android.os.Binder;
25import android.os.Build;
26import android.os.IDeviceIdentifiersPolicyService;
27import android.os.Process;
28import android.os.RemoteException;
29import android.os.SystemProperties;
30import android.os.UserHandle;
31import com.android.server.SystemService;
32
33/**
34 * This service defines the policy for accessing device identifiers.
35 */
36public final class DeviceIdentifiersPolicyService extends SystemService {
37 public DeviceIdentifiersPolicyService(Context context) {
38 super(context);
39 }
40
41 @Override
42 public void onStart() {
43 publishBinderService(Context.DEVICE_IDENTIFIERS_SERVICE,
44 new DeviceIdentifiersPolicy(getContext()));
45 }
46
47 private static final class DeviceIdentifiersPolicy
48 extends IDeviceIdentifiersPolicyService.Stub {
49 private final @NonNull Context mContext;
50
51 public DeviceIdentifiersPolicy(Context context) {
52 mContext = context;
53 }
54
55 @Override
56 public @Nullable String getSerial() throws RemoteException {
Svetoslav Ganov31c2bba2016-10-05 18:24:31 -070057 if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID
58 && mContext.checkCallingOrSelfPermission(
59 Manifest.permission.READ_PHONE_STATE)
60 != PackageManager.PERMISSION_GRANTED
61 && mContext.checkCallingOrSelfPermission(
62 Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
63 != PackageManager.PERMISSION_GRANTED) {
64 throw new SecurityException("getSerial requires READ_PHONE_STATE"
65 + " or READ_PRIVILEGED_PHONE_STATE permission");
Svet Ganov37e43272016-09-09 16:01:32 -070066 }
67 return SystemProperties.get("ro.serialno", Build.UNKNOWN);
68 }
69 }
70}