Skip to content

Instantly share code, notes, and snippets.

@viztushar
Created May 24, 2017 17:41
Show Gist options
  • Save viztushar/342681553e4c2ea207dbe463ac04fbd1 to your computer and use it in GitHub Desktop.
Save viztushar/342681553e4c2ea207dbe463ac04fbd1 to your computer and use it in GitHub Desktop.
package com.viztushar.myaccessibility;
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.os.Bundle;
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
/**
* Created by Tushar on 24-05-2017.
*/
public class MyAccessibilityService extends AccessibilityService {
private static final String TAG = MyAccessibilityService.class.getSimpleName();
AccessibilityNodeInfo info;
public MyAcc(){
}
public String getEventType(AccessibilityEvent event){
switch (event.getEventType()) {
case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
return "TYPE_NOTIFICATION_STATE_CHANGED";
case AccessibilityEvent.TYPE_VIEW_CLICKED:
return "TYPE_VIEW_CLICKED";
case AccessibilityEvent.TYPE_VIEW_FOCUSED:
return "TYPE_VIEW_FOCUSED";
case AccessibilityEvent.TYPE_VIEW_LONG_CLICKED:
return "TYPE_VIEW_LONG_CLICKED";
case AccessibilityEvent.TYPE_VIEW_SELECTED:
return "TYPE_VIEW_SELECTED";
case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
return "TYPE_WINDOW_STATE_CHANGED";
case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
return "TYPE_VIEW_TEXT_CHANGED";
}
return "default";
}
public String getEventText(AccessibilityEvent accessibilityEvent){
StringBuilder sb = new StringBuilder();
for (CharSequence s : accessibilityEvent.getText()){
sb.append(s);
}
return sb.toString();
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
String obj = event.getText().toString();
String sub = obj.substring(1,obj.length() - 1);
info = event.getSource();
Log.v(TAG, String.format("onAccessibilityEvent: Type %s Text %s",
getEventType(event), getEventText(event)));
if(info != null && info.getClassName().equals("android.widget.EditText")){
Bundle bundle = new Bundle();
bundle.putCharSequence(AccessibilityNodeInfoCompat.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,"hello");
info.performAction(AccessibilityNodeInfoCompat.ACTION_SET_TEXT,bundle);
}
}
@Override
protected void onServiceConnected() {
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.flags = AccessibilityServiceInfo.DEFAULT;
info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
setServiceInfo(info);
super.onServiceConnected();
}
@Override
public void onInterrupt() {
}
@Override
public void onCreate() {
super.onCreate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment