Home | History | Annotate | Download | only in testback
      1 package foo.bar.testback;
      2 
      3 import android.accessibilityservice.AccessibilityService;
      4 import android.accessibilityservice.AccessibilityServiceInfo;
      5 import android.os.Handler;
      6 import android.os.Message;
      7 import android.util.Log;
      8 import android.view.KeyEvent;
      9 import android.view.accessibility.AccessibilityEvent;
     10 import android.view.accessibility.AccessibilityManager;
     11 import android.view.accessibility.AccessibilityNodeInfo;
     12 
     13 public class TestBackService extends AccessibilityService {
     14 
     15 	private static final String LOG_TAG = TestBackService.class.getSimpleName();
     16 
     17 	private final Handler mHandler = new Handler() {
     18         @Override
     19         public void handleMessage(Message msg) {
     20             AccessibilityManager am = (AccessibilityManager)
     21                     getSystemService(ACCESSIBILITY_SERVICE);
     22             Log.i(LOG_TAG, "accessibilityEnabled: " + am.isEnabled()
     23                     + " touchExplorationEnabled: " + am.isTouchExplorationEnabled());
     24 //            AccessibilityServiceInfo info = getServiceInfo();
     25 //            if ((info.flags & AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE) == 0) {
     26 //                info.flags |= AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE;
     27 //            } else {
     28 //                info.flags &= ~AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE;
     29 //            }
     30 //            setServiceInfo(info);
     31             sendEmptyMessageDelayed(0, 20000);
     32         }
     33 	};
     34 
     35     @Override
     36 	public void onAccessibilityEvent(AccessibilityEvent event) {
     37 //		Log.i(LOG_TAG, AccessibilityEvent.eventTypeToString(event.getEventType()));
     38 	}
     39 
     40 	@Override
     41     protected boolean onGesture(int gestureId) {
     42 	    switch (gestureId) {
     43 	        case AccessibilityService.GESTURE_SWIPE_DOWN: {
     44 	            dumpIdResNames(getRootInActiveWindow());
     45 	        } break;
     46 	        // COPY
     47 	        case AccessibilityService.GESTURE_SWIPE_UP_AND_LEFT: {
     48 	            AccessibilityNodeInfo root = getRootInActiveWindow();
     49 	            if (root != null) {
     50 	                AccessibilityNodeInfo focus = root.findFocus(
     51 	                        AccessibilityNodeInfo.FOCUS_INPUT);
     52 	                if (focus != null) {
     53 	                    if ((focus.getActions() & 0x00004000) /* COPY*/ != 0) {
     54 	                        final boolean performed = focus.performAction(0x00004000);
     55 	                        Log.i(LOG_TAG, "Performed: " + performed);
     56 	                    }
     57 	                    focus.recycle();
     58 	                }
     59 	                root.recycle();
     60 	            }
     61 	        } break;
     62 	        // PASTE
     63             case AccessibilityService.GESTURE_SWIPE_UP: {
     64                 AccessibilityNodeInfo root = getRootInActiveWindow();
     65                 if (root != null) {
     66                     AccessibilityNodeInfo focus = root.findFocus(
     67                             AccessibilityNodeInfo.FOCUS_INPUT);
     68                     if (focus != null) {
     69                         if ((focus.getActions() & 0x00008000) /* PASTE*/ != 0) {
     70                             final boolean performed = focus.performAction(0x00008000);
     71                             Log.i(LOG_TAG, "Performed: " + performed);
     72                         }
     73                         focus.recycle();
     74                     }
     75                     root.recycle();
     76                 }
     77             } break;
     78             // CUT
     79             case AccessibilityService.GESTURE_SWIPE_UP_AND_RIGHT: {
     80                 AccessibilityNodeInfo root = getRootInActiveWindow();
     81                 if (root != null) {
     82                     AccessibilityNodeInfo focus = root.findFocus(
     83                             AccessibilityNodeInfo.FOCUS_INPUT);
     84                     if (focus != null) {
     85                         if ((focus.getActions() & 0x00010000) /* CUT*/ != 0) {
     86                             final boolean performed = focus.performAction(0x00010000);
     87                             Log.i(LOG_TAG, "Performed: " + performed);
     88                         }
     89                         focus.recycle();
     90                     }
     91                     root.recycle();
     92                 }
     93             } break;
     94             // SELECT_ALL
     95             case AccessibilityService.GESTURE_SWIPE_RIGHT_AND_UP: {
     96                 AccessibilityNodeInfo root = getRootInActiveWindow();
     97                 if (root != null) {
     98                     AccessibilityNodeInfo focus = root.findFocus(
     99                             AccessibilityNodeInfo.FOCUS_INPUT);
    100                     if (focus != null) {
    101                         if ((focus.getActions() & 0x00020000) /* SELECT ALL*/ != 0) {
    102                             final boolean performed = focus.performAction(0x00020000);
    103                             Log.i(LOG_TAG, "Performed: " + performed);
    104                         }
    105                         focus.recycle();
    106                     }
    107                     root.recycle();
    108                 }
    109             } break;
    110 	    }
    111         return super.onGesture(gestureId);
    112     }
    113 
    114 	@Override
    115     public boolean onKeyEvent(KeyEvent event) {
    116         Log.i(LOG_TAG, "onKeyEvent: " + event);
    117         boolean result = false;
    118         return result;
    119     }
    120 
    121     private void dumpIdResNames(AccessibilityNodeInfo root) {
    122 	    if (root == null) {
    123 	        return;
    124 	    }
    125 	    if (root.getViewIdResourceName() != null) {
    126 	        Log.i(LOG_TAG, root.getViewIdResourceName().toString());
    127 	    }
    128 	    final int childCount = root.getChildCount();
    129 	    for (int i = 0; i < childCount; i++) {
    130 	        dumpIdResNames(root.getChild(i));
    131 	    }
    132 	}
    133 
    134     @Override
    135 	public void onInterrupt() {
    136         /* ignore */
    137 	}
    138 
    139     @Override
    140     public void onServiceConnected() {
    141 //        AccessibilityServiceInfo info = getServiceInfo();
    142 //        info.flags |= AccessibilityServiceInfo.FLAG_REPORT_VIEW_IDS;
    143 //        setServiceInfo(info);
    144     }
    145 }
    146