• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 package com.android.tests.accessibilityeventlogger;
16 
17 import android.accessibilityservice.AccessibilityService;
18 import android.accessibilityservice.AccessibilityServiceInfo;
19 import android.util.Log;
20 import android.view.accessibility.AccessibilityEvent;
21 import android.widget.Toast;
22 
23 import java.util.Locale;
24 
25 public class AELogger extends AccessibilityService {
26     private static final String TAG = AELogger.class.getSimpleName();
27 
28     private static final int TOAST_EVENT_TYPES =
29             AccessibilityEvent.TYPE_VIEW_CLICKED | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED;
30 
31     @Override
onServiceConnected()32     public void onServiceConnected() {
33       super.onServiceConnected();
34       Log.v(TAG, "Service connected.");
35     }
36 
37 
38     @Override
onInterrupt()39     public void onInterrupt() {
40         // Do nothing
41     }
42 
43     @Override
onAccessibilityEvent(AccessibilityEvent event)44     public void onAccessibilityEvent(AccessibilityEvent event) {
45         final String eventClass = event.getClassName() != null
46                 ? event.getClassName().toString() : null;
47         final String eventText = event.getText() != null
48                 ? String.valueOf(event.getText()).toLowerCase(Locale.getDefault()) : null;
49         final String eventType = AccessibilityEvent.eventTypeToString(event.getEventType());
50 
51         Log.d(TAG, String.format(
52                     "typ=%s cls=%s pkg=%s txt=%s dsc=%s",
53                     eventType,
54                     eventClass,
55                     event.getPackageName(),
56                     eventText,
57                     event.getContentDescription()
58                     ));
59 
60         // Show selected event types
61         if (0 != (TOAST_EVENT_TYPES & event.getEventType())) {
62             final Toast toast = Toast.makeText(this,
63                     eventType + ": " + eventClass, Toast.LENGTH_SHORT);
64             toast.show();
65         }
66     }
67 }
68