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  * @hide
28  */
29 public class CarPermission {
30     private final Context mContext;
31     private final String mName;
32 
33     /** @hide */
CarPermission(Context context, String name)34     public CarPermission(Context context, String name) {
35         mContext = context;
36         mName = name;
37     }
38 
39     /** @hide */
checkGranted()40     public boolean checkGranted() {
41         if (mName != null) {
42             if (Binder.getCallingUid() != Process.myUid()) {
43                 return PackageManager.PERMISSION_GRANTED
44                         == mContext.checkCallingOrSelfPermission(mName);
45             }
46         }
47         return true;
48     }
49 
50     /** @hide */
assertGranted()51     public void assertGranted() {
52         if (checkGranted()) return;
53         throw new SecurityException(
54                 "client does not have permission:"
55                         + mName
56                         + " pid:"
57                         + Binder.getCallingPid()
58                         + " uid:"
59                         + Binder.getCallingUid());
60     }
61 
62     /** @hide */
63     @Override
toString()64     public String toString() {
65         return mName;
66     }
67 }
68