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