Home | History | Annotate | Download | only in os
      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.os;
     18 
     19 import android.Manifest;
     20 import android.annotation.NonNull;
     21 import android.annotation.Nullable;
     22 import android.content.Context;
     23 import android.content.pm.PackageManager;
     24 import android.os.Binder;
     25 import android.os.Build;
     26 import android.os.IDeviceIdentifiersPolicyService;
     27 import android.os.Process;
     28 import android.os.RemoteException;
     29 import android.os.SystemProperties;
     30 import android.os.UserHandle;
     31 import com.android.server.SystemService;
     32 
     33 /**
     34  * This service defines the policy for accessing device identifiers.
     35  */
     36 public 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 {
     57             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");
     66             }
     67             return SystemProperties.get("ro.serialno", Build.UNKNOWN);
     68         }
     69     }
     70 }
     71