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