1 /*
2  * Copyright (C) 2021 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 package com.android.server.devicepolicy;
17 
18 import static android.os.UserHandle.USER_NULL;
19 
20 import android.annotation.UserIdInt;
21 import android.content.ComponentName;
22 
23 import com.android.internal.util.Preconditions;
24 
25 import java.util.Objects;
26 
27 /**
28  * Data-transfer object used by {@link DevicePolicyManagerServiceShellCommand}.
29  */
30 final class OwnerShellData {
31 
32     public final @UserIdInt int userId;
33     public final @UserIdInt int parentUserId;
34     public final ComponentName admin;
35     public final boolean isDeviceOwner;
36     public final boolean isProfileOwner;
37     public final boolean isManagedProfileOwner;
38     public boolean isAffiliated;
39 
40     // NOTE: class is too simple to require a Builder (not to mention isAffiliated is mutable)
OwnerShellData(@serIdInt int userId, @UserIdInt int parentUserId, ComponentName admin, boolean isDeviceOwner, boolean isProfileOwner, boolean isManagedProfileOwner)41     private OwnerShellData(@UserIdInt int userId, @UserIdInt int parentUserId, ComponentName admin,
42             boolean isDeviceOwner, boolean isProfileOwner, boolean isManagedProfileOwner) {
43         Preconditions.checkArgument(userId != USER_NULL, "userId cannot be USER_NULL");
44         this.userId = userId;
45         this.parentUserId = parentUserId;
46         this.admin = Objects.requireNonNull(admin, "admin must not be null");
47         this.isDeviceOwner = isDeviceOwner;
48         this.isProfileOwner = isProfileOwner;
49         this.isManagedProfileOwner = isManagedProfileOwner;
50         if (isManagedProfileOwner) {
51             Preconditions.checkArgument(parentUserId != USER_NULL,
52                     "parentUserId cannot be USER_NULL for managed profile owner");
53             Preconditions.checkArgument(parentUserId != userId,
54                     "cannot be parent of itself (%d)", userId);
55         }
56     }
57 
58     @Override
toString()59     public String toString() {
60         StringBuilder sb = new StringBuilder(getClass().getSimpleName())
61                 .append("[userId=").append(userId)
62                 .append(",admin=").append(admin.flattenToShortString());
63         if (isDeviceOwner) {
64             sb.append(",deviceOwner");
65         }
66         if (isProfileOwner) {
67             sb.append(",isProfileOwner");
68         }
69         if (isManagedProfileOwner) {
70             sb.append(",isManagedProfileOwner");
71         }
72         if (parentUserId != USER_NULL) {
73             sb.append(",parentUserId=").append(parentUserId);
74         }
75         if (isAffiliated) {
76             sb.append(",isAffiliated");
77         }
78         return sb.append(']').toString();
79     }
80 
forDeviceOwner(@serIdInt int userId, ComponentName admin)81     static OwnerShellData forDeviceOwner(@UserIdInt int userId, ComponentName admin) {
82         return new OwnerShellData(userId, /* parentUserId= */ USER_NULL, admin,
83                 /* isDeviceOwner= */ true, /* isProfileOwner= */ false,
84                 /* isManagedProfileOwner= */ false);
85     }
86 
forUserProfileOwner(@serIdInt int userId, ComponentName admin)87     static OwnerShellData forUserProfileOwner(@UserIdInt int userId, ComponentName admin) {
88         return new OwnerShellData(userId, /* parentUserId= */ USER_NULL, admin,
89                 /* isDeviceOwner= */ false, /* isProfileOwner= */ true,
90                 /* isManagedProfileOwner= */ false);
91     }
92 
forManagedProfileOwner(@serIdInt int userId, @UserIdInt int parentUserId, ComponentName admin)93     static OwnerShellData forManagedProfileOwner(@UserIdInt int userId, @UserIdInt int parentUserId,
94             ComponentName admin) {
95         return new OwnerShellData(userId, parentUserId, admin, /* isDeviceOwner= */ false,
96                 /* isProfileOwner= */ false, /* isManagedProfileOwner= */ true);
97     }
98 }
99