1 /* 2 * Copyright (C) 2020 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.devicepolicy; 18 19 import android.annotation.Nullable; 20 import android.content.ComponentName; 21 import android.os.UserHandle; 22 23 /** 24 * Caller identity containing the caller's UID, package name and component name. 25 * All parameters are verified on object creation unless the component name is null and the 26 * caller is a delegate. 27 */ 28 final class CallerIdentity { 29 30 private final int mUid; 31 @Nullable 32 private final String mPackageName; 33 @Nullable 34 private final ComponentName mComponentName; 35 CallerIdentity(int uid, @Nullable String packageName, @Nullable ComponentName componentName)36 CallerIdentity(int uid, @Nullable String packageName, @Nullable ComponentName componentName) { 37 mUid = uid; 38 mPackageName = packageName; 39 mComponentName = componentName; 40 } 41 getUid()42 public int getUid() { 43 return mUid; 44 } 45 getUserId()46 public int getUserId() { 47 return UserHandle.getUserId(mUid); 48 } 49 getUserHandle()50 public UserHandle getUserHandle() { 51 return UserHandle.getUserHandleForUid(mUid); 52 } 53 getPackageName()54 @Nullable public String getPackageName() { 55 return mPackageName; 56 } 57 getComponentName()58 @Nullable public ComponentName getComponentName() { 59 return mComponentName; 60 } 61 hasAdminComponent()62 public boolean hasAdminComponent() { 63 return mComponentName != null; 64 } 65 hasPackage()66 public boolean hasPackage() { 67 return mPackageName != null; 68 } 69 70 @Override toString()71 public String toString() { 72 StringBuilder builder = new StringBuilder("CallerIdentity[uid=").append(mUid); 73 if (mPackageName != null) { 74 builder.append(", pkg=").append(mPackageName); 75 } 76 if (mComponentName != null) { 77 builder.append(", cmp=").append(mComponentName.flattenToShortString()); 78 } 79 return builder.append("]").toString(); 80 } 81 } 82