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