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 
17 package com.android.eventlib.events.deviceadminreceivers;
18 
19 import android.app.admin.DeviceAdminReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.UserHandle;
23 
24 import androidx.annotation.CheckResult;
25 
26 import com.android.eventlib.Event;
27 import com.android.eventlib.EventLogger;
28 import com.android.eventlib.EventLogsQuery;
29 import com.android.queryable.info.DeviceAdminReceiverInfo;
30 import com.android.queryable.queries.DeviceAdminReceiverQuery;
31 import com.android.queryable.queries.DeviceAdminReceiverQueryHelper;
32 import com.android.queryable.queries.IntentQueryHelper;
33 import com.android.queryable.queries.UserHandleQuery;
34 import com.android.queryable.queries.UserHandleQueryHelper;
35 import com.android.queryable.util.SerializableParcelWrapper;
36 
37 import com.google.errorprone.annotations.CanIgnoreReturnValue;
38 
39 /**
40  * Event logged when
41  * {@link DeviceAdminReceiver#onUserSwitched(Context, Intent, UserHandle)} is called.
42  */
43 public final class DeviceAdminUserSwitchedEvent extends Event {
44 
45     private static final long serialVersionUID = 1;
46 
47     /** Begins a query for {@link DeviceAdminUserSwitchedEvent} events. */
queryPackage(String packageName)48     public static DeviceAdminUserSwitchedEventQuery queryPackage(String packageName) {
49         return new DeviceAdminUserSwitchedEventQuery(packageName);
50     }
51 
52     /** {@link EventLogsQuery} for {@link DeviceAdminUserSwitchedEvent}. */
53     public static final class DeviceAdminUserSwitchedEventQuery
54             extends EventLogsQuery<DeviceAdminUserSwitchedEvent,
55             DeviceAdminUserSwitchedEventQuery> {
56 
57         private static final long serialVersionUID = 1;
58 
59         DeviceAdminReceiverQueryHelper<DeviceAdminUserSwitchedEventQuery> mDeviceAdminReceiver =
60                 new DeviceAdminReceiverQueryHelper<>(this);
61         IntentQueryHelper<DeviceAdminUserSwitchedEventQuery> mIntent =
62                 new IntentQueryHelper<>(this);
63         UserHandleQueryHelper<DeviceAdminUserSwitchedEventQuery> mSwitchedUser =
64                 new UserHandleQueryHelper<>(this);
65 
DeviceAdminUserSwitchedEventQuery(String packageName)66         private DeviceAdminUserSwitchedEventQuery(String packageName) {
67             super(DeviceAdminUserSwitchedEvent.class, packageName);
68         }
69 
70         /**
71          * Queries {@link Intent} passed into
72          * {@link DeviceAdminReceiver#onUserSwitched(Context, Intent, UserHandle)}.
73          */
74         @CheckResult
whereIntent()75         public IntentQueryHelper<DeviceAdminUserSwitchedEventQuery> whereIntent() {
76             return mIntent;
77         }
78 
79         /** Queries {@link DeviceAdminReceiver}. */
80         @CheckResult
whereDeviceAdminReceiver()81         public DeviceAdminReceiverQuery<DeviceAdminUserSwitchedEventQuery> whereDeviceAdminReceiver() {
82             return mDeviceAdminReceiver;
83         }
84 
85         /** Queries {@link UserHandle} passed into
86          * {@link DeviceAdminReceiver#onUserSwitched(Context, Intent, UserHandle)}.
87          */
88         @CheckResult
whereSwitchedUser()89         public UserHandleQuery<DeviceAdminUserSwitchedEventQuery> whereSwitchedUser() {
90             return mSwitchedUser;
91         }
92 
93         @Override
filter(DeviceAdminUserSwitchedEvent event)94         protected boolean filter(DeviceAdminUserSwitchedEvent event) {
95             if (!mIntent.matches(event.mIntent)) {
96                 return false;
97             }
98             if (!mDeviceAdminReceiver.matches(event.mDeviceAdminReceiver)) {
99                 return false;
100             }
101             if (!mSwitchedUser.matches(event.mSwitchedUser)) {
102                 return false;
103             }
104             return true;
105         }
106 
107         @Override
describeQuery(String fieldName)108         public String describeQuery(String fieldName) {
109             return toStringBuilder(DeviceAdminUserSwitchedEvent.class, this)
110                     .field("intent", mIntent)
111                     .field("deviceAdminReceiver", mDeviceAdminReceiver)
112                     .field("switchedUser", mSwitchedUser)
113                     .toString();
114         }
115     }
116 
117     /** Begins logging a {@link DeviceAdminUserSwitchedEvent}. */
logger( DeviceAdminReceiver deviceAdminReceiver, Context context, Intent intent, UserHandle switchedUser)118     public static DeviceAdminUserSwitchedEventLogger logger(
119             DeviceAdminReceiver deviceAdminReceiver, Context context,
120             Intent intent, UserHandle switchedUser) {
121         return new DeviceAdminUserSwitchedEventLogger(
122                 deviceAdminReceiver, context, intent, switchedUser);
123     }
124 
125     /** {@link EventLogger} for {@link DeviceAdminUserSwitchedEvent}. */
126     public static final class DeviceAdminUserSwitchedEventLogger
127             extends EventLogger<DeviceAdminUserSwitchedEvent> {
DeviceAdminUserSwitchedEventLogger( DeviceAdminReceiver deviceAdminReceiver, Context context, Intent intent, UserHandle switchedUser)128         private DeviceAdminUserSwitchedEventLogger(
129                 DeviceAdminReceiver deviceAdminReceiver, Context context,
130                 Intent intent, UserHandle switchedUser) {
131             super(context, new DeviceAdminUserSwitchedEvent());
132             mEvent.mIntent = new SerializableParcelWrapper<>(intent);
133             mEvent.mSwitchedUser = new SerializableParcelWrapper<>(switchedUser);
134             setDeviceAdminReceiver(deviceAdminReceiver);
135         }
136 
137         /** Sets the {@link DeviceAdminReceiver} which received this event. */
138         @CanIgnoreReturnValue
setDeviceAdminReceiver( DeviceAdminReceiver deviceAdminReceiver)139         public DeviceAdminUserSwitchedEventLogger setDeviceAdminReceiver(
140                 DeviceAdminReceiver deviceAdminReceiver) {
141             mEvent.mDeviceAdminReceiver = new DeviceAdminReceiverInfo(deviceAdminReceiver);
142             return this;
143         }
144 
145         /** Sets the {@link DeviceAdminReceiver} which received this event. */
setDeviceAdminReceiver( Class<? extends DeviceAdminReceiver> deviceAdminReceiverClass)146         public DeviceAdminUserSwitchedEventLogger setDeviceAdminReceiver(
147                 Class<? extends DeviceAdminReceiver> deviceAdminReceiverClass) {
148             mEvent.mDeviceAdminReceiver = new DeviceAdminReceiverInfo(deviceAdminReceiverClass);
149             return this;
150         }
151 
152         /** Sets the {@link DeviceAdminReceiver} which received this event. */
153         @CanIgnoreReturnValue
setDeviceAdminReceiver( String deviceAdminReceiverClassName)154         public DeviceAdminUserSwitchedEventLogger setDeviceAdminReceiver(
155                 String deviceAdminReceiverClassName) {
156             mEvent.mDeviceAdminReceiver = new DeviceAdminReceiverInfo(deviceAdminReceiverClassName);
157             return this;
158         }
159 
160         /** Sets the {@link Intent} which was received. */
setIntent(Intent intent)161         public DeviceAdminUserSwitchedEventLogger setIntent(Intent intent) {
162             mEvent.mIntent = new SerializableParcelWrapper<>(intent);
163             return this;
164         }
165 
166         /** Sets the {@link UserHandle}. */
setSwitchedUser(UserHandle switchedUser)167         public DeviceAdminUserSwitchedEventLogger setSwitchedUser(UserHandle switchedUser) {
168             mEvent.mSwitchedUser = new SerializableParcelWrapper<>(switchedUser);
169             return this;
170         }
171     }
172 
173     protected SerializableParcelWrapper<Intent> mIntent;
174     protected SerializableParcelWrapper<UserHandle> mSwitchedUser;
175     protected DeviceAdminReceiverInfo mDeviceAdminReceiver;
176 
177     /**
178      * The {@link Intent} passed into
179      * {@link DeviceAdminReceiver#onUserSwitched(Context, Intent, UserHandle)}.
180      */
intent()181     public Intent intent() {
182         if (mIntent == null) {
183             return null;
184         }
185         return mIntent.get();
186     }
187 
188     /**
189      * The {@link UserHandle} passed into
190      * {@link DeviceAdminReceiver#onUserSwitched(Context, Intent, UserHandle)}.
191      */
switchedUser()192     public UserHandle switchedUser() {
193         if (mSwitchedUser == null) {
194             return null;
195         }
196         return mSwitchedUser.get();
197     }
198 
199     /** Information about the {@link DeviceAdminReceiver} which received the intent. */
deviceAdminReceiver()200     public DeviceAdminReceiverInfo deviceAdminReceiver() {
201         return mDeviceAdminReceiver;
202     }
203 
204     @Override
toString()205     public String toString() {
206         return "DeviceAdminUserSwitchedEvent{"
207                 + " intent=" + intent()
208                 + ", switchedUser=" + switchedUser()
209                 + ", deviceAdminReceiver=" + mDeviceAdminReceiver
210                 + ", packageName='" + mPackageName + "'"
211                 + ", timestamp=" + mTimestamp
212                 + "}";
213     }
214 }
215