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