1 /** 2 * Copyright (c) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 // InputMonitor is deprecated, but we still need to test it. 16 @file:Suppress("DEPRECATION") 17 18 package com.android.test.input 19 20 import android.app.Activity 21 import android.hardware.input.InputManager 22 import android.os.Bundle 23 import android.os.Looper 24 import android.os.Process 25 import android.util.Log 26 import android.view.InputChannel 27 import android.view.InputEvent 28 import android.view.InputEventReceiver 29 import android.view.InputMonitor 30 import android.view.MotionEvent 31 32 class UnresponsiveReceiver(channel: InputChannel, looper: Looper, val service: IAnrTestService) : 33 InputEventReceiver(channel, looper) { 34 companion object { 35 const val TAG = "UnresponsiveReceiver" 36 } 37 onInputEventnull38 override fun onInputEvent(event: InputEvent) { 39 Log.i(TAG, "Received $event") 40 // Not calling 'finishInputEvent' in order to trigger the ANR 41 service.notifyMotion(event as MotionEvent) 42 } 43 } 44 45 class UnresponsiveGestureMonitorActivity : Activity() { 46 companion object { 47 const val MONITOR_NAME = "unresponsive gesture monitor" 48 } 49 50 private lateinit var mInputEventReceiver: InputEventReceiver 51 private lateinit var mInputMonitor: InputMonitor 52 private lateinit var service: IAnrTestService 53 onCreatenull54 override fun onCreate(savedInstanceState: Bundle?) { 55 super.onCreate(savedInstanceState) 56 val bundle = intent.getBundleExtra("serviceBundle")!! 57 service = IAnrTestService.Stub.asInterface(bundle.getBinder("serviceBinder")) 58 val inputManager = checkNotNull(getSystemService(InputManager::class.java)) 59 mInputMonitor = inputManager.monitorGestureInput(MONITOR_NAME, displayId) 60 mInputEventReceiver = 61 UnresponsiveReceiver(mInputMonitor.getInputChannel(), Looper.myLooper()!!, service) 62 } 63 onAttachedToWindownull64 override fun onAttachedToWindow() { 65 super.onAttachedToWindow() 66 service.provideActivityInfo( 67 window.decorView.windowToken, 68 display.displayId, 69 Process.myPid(), 70 ) 71 } 72 } 73