1 /* 2 * Copyright (C) 2017 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.car.internal; 18 19 import android.content.Context; 20 import android.content.pm.PackageManager; 21 import android.os.Binder; 22 import android.os.Process; 23 24 /** 25 * Represent an Android permission. 26 */ 27 public class CarPermission { 28 private final Context mContext; 29 private final String mName; 30 CarPermission(Context context, String name)31 public CarPermission(Context context, String name) { 32 mContext = context; 33 mName = name; 34 } 35 checkGranted()36 public boolean checkGranted() { 37 if (mName != null) { 38 if (Binder.getCallingUid() != Process.myUid()) { 39 return PackageManager.PERMISSION_GRANTED 40 == mContext.checkCallingOrSelfPermission(mName); 41 } 42 } 43 return true; 44 } 45 assertGranted()46 public void assertGranted() { 47 if (checkGranted()) return; 48 throw new SecurityException( 49 "client does not have permission:" 50 + mName 51 + " pid:" 52 + Binder.getCallingPid() 53 + " uid:" 54 + Binder.getCallingUid()); 55 } 56 57 @Override toString()58 public String toString() { 59 return mName; 60 } 61 } 62