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.os.PersistableBundle;
22 
23 import androidx.annotation.CheckResult;
24 
25 import com.android.eventlib.Event;
26 import com.android.eventlib.EventLogger;
27 import com.android.eventlib.EventLogsQuery;
28 import com.android.queryable.info.DeviceAdminReceiverInfo;
29 import com.android.queryable.queries.DeviceAdminReceiverQuery;
30 import com.android.queryable.queries.DeviceAdminReceiverQueryHelper;
31 import com.android.queryable.queries.PersistableBundleQuery;
32 import com.android.queryable.queries.PersistableBundleQueryHelper;
33 import com.android.queryable.util.SerializableParcelWrapper;
34 
35 import com.google.errorprone.annotations.CanIgnoreReturnValue;
36 
37 /**
38  * Event logged when
39  * {@link DeviceAdminReceiver#onTransferOwnershipComplete(Context, PersistableBundle)} is called.
40  */
41 public final class DeviceAdminTransferOwnershipCompleteEvent extends Event {
42 
43     private static final long serialVersionUID = 1;
44 
45     /** Begins a query for {@link DeviceAdminTransferOwnershipCompleteEvent} events. */
queryPackage(String packageName)46     public static DeviceAdminTransferOwnershipCompleteEventQuery queryPackage(String packageName) {
47         return new DeviceAdminTransferOwnershipCompleteEventQuery(packageName);
48     }
49 
50     /** {@link EventLogsQuery} for {@link DeviceAdminTransferOwnershipCompleteEvent}. */
51     public static final class DeviceAdminTransferOwnershipCompleteEventQuery
52             extends EventLogsQuery<DeviceAdminTransferOwnershipCompleteEvent,
53             DeviceAdminTransferOwnershipCompleteEventQuery> {
54 
55         private static final long serialVersionUID = 1;
56 
57         DeviceAdminReceiverQueryHelper<DeviceAdminTransferOwnershipCompleteEventQuery> mDeviceAdminReceiver =
58                 new DeviceAdminReceiverQueryHelper<>(this);
59         PersistableBundleQueryHelper<DeviceAdminTransferOwnershipCompleteEventQuery> mBundle =
60                 new PersistableBundleQueryHelper<>(this);
61 
DeviceAdminTransferOwnershipCompleteEventQuery(String packageName)62         private DeviceAdminTransferOwnershipCompleteEventQuery(String packageName) {
63             super(DeviceAdminTransferOwnershipCompleteEvent.class, packageName);
64         }
65 
66         /** Queries {@link DeviceAdminReceiver}. */
67         @CheckResult
whereDeviceAdminReceiver()68         public DeviceAdminReceiverQuery<DeviceAdminTransferOwnershipCompleteEventQuery> whereDeviceAdminReceiver() {
69             return mDeviceAdminReceiver;
70         }
71 
72         /**
73          * Queries {@link PersistableBundle} passed into
74          * {@link DeviceAdminReceiver#onTransferOwnershipComplete(Context, PersistableBundle)}.
75          */
76         @CheckResult
whereBundle()77         public PersistableBundleQuery<DeviceAdminTransferOwnershipCompleteEventQuery> whereBundle() {
78             return mBundle;
79         }
80 
81         @Override
filter(DeviceAdminTransferOwnershipCompleteEvent event)82         protected boolean filter(DeviceAdminTransferOwnershipCompleteEvent event) {
83             if (!mDeviceAdminReceiver.matches(event.mDeviceAdminReceiver)) {
84                 return false;
85             }
86             if (!mBundle.matches(event.mBundle)) {
87                 return false;
88             }
89             return true;
90         }
91 
92         @Override
describeQuery(String fieldName)93         public String describeQuery(String fieldName) {
94             return toStringBuilder(DeviceAdminTransferOwnershipCompleteEvent.class, this)
95                     .field("deviceAdminReceiver", mDeviceAdminReceiver)
96                     .field("bundle", mBundle)
97                     .toString();
98         }
99     }
100 
101     /** Begins logging a {@link DeviceAdminTransferOwnershipCompleteEvent}. */
logger( DeviceAdminReceiver deviceAdminReceiver, Context context, PersistableBundle bundle)102     public static DeviceAdminTransferOwnershipCompleteEventLogger logger(
103             DeviceAdminReceiver deviceAdminReceiver,
104             Context context, PersistableBundle bundle) {
105         return new DeviceAdminTransferOwnershipCompleteEventLogger(
106                 deviceAdminReceiver, context, bundle);
107     }
108 
109     /** {@link EventLogger} for {@link DeviceAdminTransferOwnershipCompleteEvent}. */
110     public static final class DeviceAdminTransferOwnershipCompleteEventLogger
111             extends EventLogger<DeviceAdminTransferOwnershipCompleteEvent> {
DeviceAdminTransferOwnershipCompleteEventLogger( DeviceAdminReceiver deviceAdminReceiver, Context context, PersistableBundle bundle)112         private DeviceAdminTransferOwnershipCompleteEventLogger(
113                 DeviceAdminReceiver deviceAdminReceiver,
114                 Context context, PersistableBundle bundle) {
115             super(context, new DeviceAdminTransferOwnershipCompleteEvent());
116             mEvent.mBundle = new SerializableParcelWrapper<>(bundle);
117             setDeviceAdminReceiver(deviceAdminReceiver);
118         }
119 
120         /** Sets the {@link DeviceAdminReceiver} which received this event. */
121         @CanIgnoreReturnValue
setDeviceAdminReceiver( DeviceAdminReceiver deviceAdminReceiver)122         public DeviceAdminTransferOwnershipCompleteEventLogger setDeviceAdminReceiver(
123                 DeviceAdminReceiver deviceAdminReceiver) {
124             mEvent.mDeviceAdminReceiver = new DeviceAdminReceiverInfo(deviceAdminReceiver);
125             return this;
126         }
127 
128         /** Sets the {@link DeviceAdminReceiver} which received this event. */
setDeviceAdminReceiver( Class<? extends DeviceAdminReceiver> deviceAdminReceiverClass)129         public DeviceAdminTransferOwnershipCompleteEventLogger setDeviceAdminReceiver(
130                 Class<? extends DeviceAdminReceiver> deviceAdminReceiverClass) {
131             mEvent.mDeviceAdminReceiver = new DeviceAdminReceiverInfo(deviceAdminReceiverClass);
132             return this;
133         }
134 
135         /** Sets the {@link DeviceAdminReceiver} which received this event. */
136         @CanIgnoreReturnValue
setDeviceAdminReceiver( String deviceAdminReceiverClassName)137         public DeviceAdminTransferOwnershipCompleteEventLogger setDeviceAdminReceiver(
138                 String deviceAdminReceiverClassName) {
139             mEvent.mDeviceAdminReceiver = new DeviceAdminReceiverInfo(deviceAdminReceiverClassName);
140             return this;
141         }
142 
143         /** Sets the {@link PersistableBundle} which was received. */
setBundle(PersistableBundle bundle)144         public DeviceAdminTransferOwnershipCompleteEventLogger setBundle(PersistableBundle bundle) {
145             mEvent.mBundle = new SerializableParcelWrapper<>(bundle);
146             return this;
147         }
148     }
149 
150     protected DeviceAdminReceiverInfo mDeviceAdminReceiver;
151     protected SerializableParcelWrapper<PersistableBundle> mBundle;
152 
153     /** Information about the {@link DeviceAdminReceiver} which received the intent. */
deviceAdminReceiver()154     public DeviceAdminReceiverInfo deviceAdminReceiver() {
155         return mDeviceAdminReceiver;
156     }
157 
158     /**
159      * The {@link PersistableBundle} passed into
160      * {@link DeviceAdminReceiver#onTransferOwnershipComplete(Context, PersistableBundle)}.
161      */
bundle()162     public PersistableBundle bundle() {
163         if (mBundle == null) {
164             return null;
165         }
166         return mBundle.get();
167     }
168 
169     @Override
toString()170     public String toString() {
171         return "DeviceAdminTransferOwnershipCompleteEvent{"
172                 + ", bundle=" + bundle()
173                 + ", deviceAdminReceiver=" + mDeviceAdminReceiver
174                 + ", packageName='" + mPackageName + "'"
175                 + ", timestamp=" + mTimestamp
176                 + "}";
177     }
178 }
179