1  /*
2   * Copyright (C) 2022 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 android.app.admin;
18  
19  import android.annotation.NonNull;
20  import android.annotation.Nullable;
21  import android.annotation.TestApi;
22  
23  import java.util.Objects;
24  
25  /**
26   * Class representing the target user of a policy set by an admin
27   * (set from {@link DevicePolicyManager}), this is passed in to
28   * {@link PolicyUpdateReceiver#onPolicySetResult} and
29   * {@link PolicyUpdateReceiver#onPolicyChanged}.
30   */
31  public final class TargetUser {
32      /**
33       * Indicates that the policy relates to the user the admin is installed on.
34       *
35       * @hide
36       */
37      @TestApi
38      public static final int LOCAL_USER_ID = -1;
39  
40      /**
41       * For admins of profiles, this indicates that the policy relates to the parent profile.
42       *
43       * @hide
44       */
45      @TestApi
46      public static final int PARENT_USER_ID = -2;
47  
48      /**
49       * This indicates the policy is a global policy.
50       *
51       * @hide
52       */
53      @TestApi
54      public static final int GLOBAL_USER_ID = -3;
55  
56      /**
57       * Indicates that the policy relates to some unknown user on the device.
58       *
59       * @hide
60       */
61      @TestApi
62      public static final int UNKNOWN_USER_ID = -3;
63  
64      /**
65       * Indicates that the policy relates to the user the admin is installed on.
66       */
67      @NonNull
68      public static final TargetUser LOCAL_USER = new TargetUser(LOCAL_USER_ID);
69  
70      /**
71       * For admins of profiles, this indicates that the policy relates to the parent profile.
72       */
73      @NonNull
74      public static final TargetUser PARENT_USER = new TargetUser(PARENT_USER_ID);
75  
76      /**
77       * This indicates the policy is a global policy.
78       */
79      @NonNull
80      public static final TargetUser GLOBAL = new TargetUser(GLOBAL_USER_ID);
81  
82      /**
83       * Indicates that the policy relates to some unknown user on the device. For example, if Admin1
84       * has set a global policy on a device and Admin2 has set a conflicting local
85       * policy on some other secondary user, Admin1 will get a policy update callback with
86       * {@code UNKNOWN_USER} as the target user.
87       */
88      @NonNull
89      public static final TargetUser UNKNOWN_USER = new TargetUser(UNKNOWN_USER_ID);
90  
91      private final int mUserId;
92  
93      /**
94       * @hide
95       */
TargetUser(int userId)96      public TargetUser(int userId) {
97          mUserId = userId;
98      }
99  
100      @Override
equals(@ullable Object o)101      public boolean equals(@Nullable Object o) {
102          if (this == o) return true;
103          if (o == null || getClass() != o.getClass()) return false;
104          TargetUser other = (TargetUser) o;
105          return mUserId == other.mUserId;
106      }
107  
108      @Override
hashCode()109      public int hashCode() {
110          return Objects.hash(mUserId);
111      }
112  }
113