1 /* 2 * Copyright (C) 2022 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.adservices.service.measurement.inputverification; 18 19 import android.content.Context; 20 import android.hardware.input.InputManager; 21 import android.view.InputEvent; 22 23 import com.android.adservices.service.Flags; 24 import com.android.adservices.service.FlagsFactory; 25 import com.android.internal.annotations.VisibleForTesting; 26 27 /** Class for handling navigation event verification. */ 28 public class ClickVerifier { 29 private final InputManager mInputManager; 30 private final Flags mFlags; 31 ClickVerifier(Context context)32 public ClickVerifier(Context context) { 33 mInputManager = context.getSystemService(InputManager.class); 34 mFlags = FlagsFactory.getFlags(); 35 } 36 37 @VisibleForTesting ClickVerifier(InputManager inputManager, Flags flags)38 ClickVerifier(InputManager inputManager, Flags flags) { 39 mInputManager = inputManager; 40 mFlags = flags; 41 } 42 43 /** 44 * Checks if the {@link InputEvent} passed with a click registration can be verified. In order 45 * for an InputEvent to be verified, the event time of the InputEvent has to be within {@link 46 * com.android.adservices.service.PhFlags#MEASUREMENT_REGISTRATION_INPUT_EVENT_VALID_WINDOW_MS } 47 * of the API call. 48 * 49 * @param event The InputEvent passed with the registration call. 50 * @param registerTimestamp The time of the registration call. 51 * @return Whether the InputEvent can be verified. 52 */ isInputEventVerifiable(InputEvent event, long registerTimestamp)53 public boolean isInputEventVerifiable(InputEvent event, long registerTimestamp) { 54 return isInputEventVerifiableBySystem(event) 55 && isInputEventWithinValidTimeRange(registerTimestamp, event); 56 } 57 58 /** Checks whether the InputEvent can be verified by the system. */ 59 @VisibleForTesting isInputEventVerifiableBySystem(InputEvent event)60 boolean isInputEventVerifiableBySystem(InputEvent event) { 61 return !mFlags.getMeasurementIsClickVerifiedByInputEvent() 62 || mInputManager.verifyInputEvent(event) != null; 63 } 64 65 /** 66 * Checks whether the timestamp on the InputEvent and the time of the API call are within the 67 * accepted range defined at {@link 68 * com.android.adservices.service.PhFlags#MEASUREMENT_REGISTRATION_INPUT_EVENT_VALID_WINDOW_MS} 69 */ 70 @VisibleForTesting isInputEventWithinValidTimeRange(long registerTimestamp, InputEvent event)71 boolean isInputEventWithinValidTimeRange(long registerTimestamp, InputEvent event) { 72 return registerTimestamp - event.getEventTime() 73 <= mFlags.getMeasurementRegistrationInputEventValidWindowMs(); 74 } 75 } 76