api(events): events.setKeyInterceptionEnabled()
This commit is contained in:
@@ -17,7 +17,6 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
* When the execution finish successfully, the engine should be destroy in the thread that created it.
|
* When the execution finish successfully, the engine should be destroy in the thread that created it.
|
||||||
* <p>
|
* <p>
|
||||||
* If you want to stop the engine in other threads, you should call {@link ScriptEngine#forceStop()}.
|
* If you want to stop the engine in other threads, you should call {@link ScriptEngine#forceStop()}.
|
||||||
* It will throw a {@link ScriptException}.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public interface ScriptEngine<S extends ScriptSource> {
|
public interface ScriptEngine<S extends ScriptSource> {
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import android.os.Handler;
|
|||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.support.annotation.RequiresApi;
|
import android.support.annotation.RequiresApi;
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
import com.stardust.autojs.R;
|
import com.stardust.autojs.R;
|
||||||
import com.stardust.autojs.core.accessibility.AccessibilityBridge;
|
import com.stardust.autojs.core.accessibility.AccessibilityBridge;
|
||||||
@@ -24,6 +26,7 @@ import com.stardust.autojs.core.inputevent.InputEventObserver;
|
|||||||
import com.stardust.autojs.core.inputevent.TouchObserver;
|
import com.stardust.autojs.core.inputevent.TouchObserver;
|
||||||
import com.stardust.view.accessibility.AccessibilityNotificationObserver;
|
import com.stardust.view.accessibility.AccessibilityNotificationObserver;
|
||||||
import com.stardust.view.accessibility.AccessibilityService;
|
import com.stardust.view.accessibility.AccessibilityService;
|
||||||
|
import com.stardust.view.accessibility.KeyInterceptor;
|
||||||
import com.stardust.view.accessibility.NotificationListener;
|
import com.stardust.view.accessibility.NotificationListener;
|
||||||
import com.stardust.view.accessibility.OnKeyListener;
|
import com.stardust.view.accessibility.OnKeyListener;
|
||||||
|
|
||||||
@@ -47,6 +50,8 @@ public class Events extends EventEmitter implements OnKeyListener, TouchObserver
|
|||||||
private boolean mListeningNotification = false;
|
private boolean mListeningNotification = false;
|
||||||
private boolean mListeningToast = false;
|
private boolean mListeningToast = false;
|
||||||
private ScriptRuntime mScriptRuntime;
|
private ScriptRuntime mScriptRuntime;
|
||||||
|
private volatile boolean mInterceptsKey = false;
|
||||||
|
private KeyInterceptor mKeyInterceptor;
|
||||||
|
|
||||||
public Events(Context context, AccessibilityBridge accessibilityBridge, ScriptRuntime runtime) {
|
public Events(Context context, AccessibilityBridge accessibilityBridge, ScriptRuntime runtime) {
|
||||||
super(runtime.bridges);
|
super(runtime.bridges);
|
||||||
@@ -72,10 +77,7 @@ public class Events extends EventEmitter implements OnKeyListener, TouchObserver
|
|||||||
public void observeKey() {
|
public void observeKey() {
|
||||||
if (mListeningKey)
|
if (mListeningKey)
|
||||||
return;
|
return;
|
||||||
mScriptRuntime.ensureAccessibilityServiceEnabled();
|
AccessibilityService service = getAccessibilityService();
|
||||||
AccessibilityService service = mAccessibilityBridge.getService();
|
|
||||||
if (service == null)
|
|
||||||
throw new ScriptException("AccessibilityService = null");
|
|
||||||
if ((service.getServiceInfo().flags & AccessibilityServiceInfo.FLAG_REQUEST_FILTER_KEY_EVENTS) == 0) {
|
if ((service.getServiceInfo().flags & AccessibilityServiceInfo.FLAG_REQUEST_FILTER_KEY_EVENTS) == 0) {
|
||||||
throw new ScriptException(mContext.getString(R.string.text_should_enable_key_observing));
|
throw new ScriptException(mContext.getString(R.string.text_should_enable_key_observing));
|
||||||
}
|
}
|
||||||
@@ -102,6 +104,23 @@ public class Events extends EventEmitter implements OnKeyListener, TouchObserver
|
|||||||
mTouchObserver.observe();
|
mTouchObserver.observe();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setKeyInterceptionEnabled(boolean enabled) {
|
||||||
|
mInterceptsKey = enabled;
|
||||||
|
if (mInterceptsKey && mKeyInterceptor == null) {
|
||||||
|
mKeyInterceptor = event -> mInterceptsKey;
|
||||||
|
getAccessibilityService().getKeyInterrupterObserver().addKeyInterrupter(mKeyInterceptor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private AccessibilityService getAccessibilityService() {
|
||||||
|
mScriptRuntime.ensureAccessibilityServiceEnabled();
|
||||||
|
AccessibilityService service = mAccessibilityBridge.getService();
|
||||||
|
if (service == null)
|
||||||
|
throw new ScriptException("AccessibilityService = null");
|
||||||
|
return service;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public Events onKeyDown(String keyName, Object listener) {
|
public Events onKeyDown(String keyName, Object listener) {
|
||||||
on(PREFIX_KEY_DOWN + keyName, listener);
|
on(PREFIX_KEY_DOWN + keyName, listener);
|
||||||
return this;
|
return this;
|
||||||
@@ -207,6 +226,13 @@ public class Events extends EventEmitter implements OnKeyListener, TouchObserver
|
|||||||
NotificationListenerService.getInstance().removeListener(this);
|
NotificationListenerService.getInstance().removeListener(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (mKeyInterceptor != null) {
|
||||||
|
AccessibilityService service = mAccessibilityBridge.getService();
|
||||||
|
if (service != null) {
|
||||||
|
service.getKeyInterrupterObserver().removeKeyInterrupter(mKeyInterceptor);
|
||||||
|
}
|
||||||
|
mKeyInterceptor = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -243,4 +269,5 @@ public class Events extends EventEmitter implements OnKeyListener, TouchObserver
|
|||||||
public void onToast(final AccessibilityNotificationObserver.Toast toast) {
|
public void onToast(final AccessibilityNotificationObserver.Toast toast) {
|
||||||
mHandler.post(() -> emit("toast", toast));
|
mHandler.post(() -> emit("toast", toast));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package com.stardust.view.accessibility;
|
package com.stardust.view.accessibility;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Handler;
|
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
@@ -13,10 +11,7 @@ import java.util.HashSet;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.SortedMap;
|
import java.util.SortedMap;
|
||||||
import java.util.Timer;
|
|
||||||
import java.util.TimerTask;
|
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@@ -40,6 +35,7 @@ public class AccessibilityService extends android.accessibilityservice.Accessibi
|
|||||||
private static boolean containsAllEventTypes = false;
|
private static boolean containsAllEventTypes = false;
|
||||||
private static final Set<Integer> eventTypes = new HashSet<>();
|
private static final Set<Integer> eventTypes = new HashSet<>();
|
||||||
private OnKeyListener.Observer mOnKeyObserver = new OnKeyListener.Observer();
|
private OnKeyListener.Observer mOnKeyObserver = new OnKeyListener.Observer();
|
||||||
|
private KeyInterceptor.Observer mKeyInterrupterObserver = new KeyInterceptor.Observer();
|
||||||
private ExecutorService mKeyEventExecutor;
|
private ExecutorService mKeyEventExecutor;
|
||||||
private AccessibilityNodeInfo mFastRootInActiveWindow;
|
private AccessibilityNodeInfo mFastRootInActiveWindow;
|
||||||
|
|
||||||
@@ -97,7 +93,11 @@ public class AccessibilityService extends android.accessibilityservice.Accessibi
|
|||||||
stickOnKeyObserver.onKeyEvent(event.getKeyCode(), event);
|
stickOnKeyObserver.onKeyEvent(event.getKeyCode(), event);
|
||||||
mOnKeyObserver.onKeyEvent(event.getKeyCode(), event);
|
mOnKeyObserver.onKeyEvent(event.getKeyCode(), event);
|
||||||
});
|
});
|
||||||
return false;
|
return mKeyInterrupterObserver.onInterceptKeyEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
public KeyInterceptor.Observer getKeyInterrupterObserver() {
|
||||||
|
return mKeyInterrupterObserver;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OnKeyListener.Observer getOnKeyObserver() {
|
public OnKeyListener.Observer getOnKeyObserver() {
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package com.stardust.view.accessibility;
|
||||||
|
|
||||||
|
import android.view.KeyEvent;
|
||||||
|
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Stardust on 2018/2/27.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public interface KeyInterceptor {
|
||||||
|
|
||||||
|
boolean onInterceptKeyEvent(KeyEvent event);
|
||||||
|
|
||||||
|
class Observer implements KeyInterceptor {
|
||||||
|
private CopyOnWriteArrayList<KeyInterceptor> mKeyInterceptors = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
|
public void addKeyInterrupter(KeyInterceptor interrupter){
|
||||||
|
mKeyInterceptors.add(interrupter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean removeKeyInterrupter(KeyInterceptor interrupter){
|
||||||
|
return mKeyInterceptors.remove(interrupter);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onInterceptKeyEvent(KeyEvent event) {
|
||||||
|
for(KeyInterceptor interrupter : mKeyInterceptors){
|
||||||
|
try {
|
||||||
|
if(interrupter.onInterceptKeyEvent(event)){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user