refactor: remove SimpleActionPerformHost and AccessibilityEventCommandHost
This commit is contained in:
@@ -1,120 +0,0 @@
|
||||
package com.stardust.automator;
|
||||
|
||||
import android.accessibilityservice.AccessibilityService;
|
||||
import android.util.Log;
|
||||
import android.view.accessibility.AccessibilityEvent;
|
||||
import android.view.accessibility.AccessibilityNodeInfo;
|
||||
|
||||
import com.stardust.view.accessibility.AccessibilityDelegate;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/3/9.
|
||||
*/
|
||||
|
||||
public class AccessibilityEventCommandHost implements AccessibilityDelegate {
|
||||
|
||||
|
||||
public interface Command {
|
||||
|
||||
void execute(AccessibilityService service, AccessibilityEvent event);
|
||||
|
||||
boolean isValid();
|
||||
|
||||
void setValid(boolean valid);
|
||||
}
|
||||
|
||||
public abstract static class AbstractCommand implements Command {
|
||||
|
||||
private boolean mValid;
|
||||
|
||||
public synchronized boolean isValid() {
|
||||
return mValid;
|
||||
}
|
||||
|
||||
public synchronized void setValid(boolean valid) {
|
||||
mValid = valid;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static final String TAG = "CommandHostDelegate";
|
||||
|
||||
public static final int RUN_MODE_SINGLE_THREAD = 0;
|
||||
public static final int RUN_MODE_THREAD_POOL = 1;
|
||||
public static final int RUN_MODE_NEW_THREAD_EVERY_TIME = 2;
|
||||
|
||||
|
||||
private final Queue<Command> mCommands = new LinkedList<>();
|
||||
private Executor mExecutor = Executors.newSingleThreadExecutor();
|
||||
private int mRunMode = RUN_MODE_THREAD_POOL;
|
||||
|
||||
@Override
|
||||
public boolean onAccessibilityEvent(final AccessibilityService service, final AccessibilityEvent event) {
|
||||
synchronized (mCommands) {
|
||||
if (!mCommands.isEmpty()) {
|
||||
Log.v(TAG, "will execute " + mCommands.size() + " commands");
|
||||
}
|
||||
while (!mCommands.isEmpty()) {
|
||||
final Command command = mCommands.poll();
|
||||
if (command.isValid()) {
|
||||
executeCommand(command, service, event);
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Integer> getEventTypes() {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void executeCommand(final Command command, final AccessibilityService service, final AccessibilityEvent event) {
|
||||
final Runnable r = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (!command.isValid()) {
|
||||
return;
|
||||
}
|
||||
Log.v(TAG, "executing " + command);
|
||||
command.execute(service, event);
|
||||
synchronized (command) {
|
||||
command.notify();
|
||||
}
|
||||
}
|
||||
};
|
||||
if (mRunMode == RUN_MODE_SINGLE_THREAD) {
|
||||
r.run();
|
||||
} else if (mRunMode == RUN_MODE_NEW_THREAD_EVERY_TIME) {
|
||||
new Thread(r).start();
|
||||
} else {
|
||||
mExecutor.execute(r);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void executeAndWaitForEvent(Command command) {
|
||||
synchronized (mCommands) {
|
||||
mCommands.offer(command);
|
||||
}
|
||||
synchronized (command) {
|
||||
try {
|
||||
command.wait();
|
||||
} catch (InterruptedException e) {
|
||||
command.setValid(false);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setRunMode(int mode) {
|
||||
mRunMode = mode;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
package com.stardust.automator.simple_action;
|
||||
|
||||
import android.accessibilityservice.AccessibilityService;
|
||||
import android.util.Log;
|
||||
import android.view.accessibility.AccessibilityEvent;
|
||||
import android.view.accessibility.AccessibilityNodeInfo;
|
||||
|
||||
import com.stardust.automator.UiObject;
|
||||
import com.stardust.view.accessibility.AccessibilityDelegate;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/1/21.
|
||||
*/
|
||||
|
||||
public class SimpleActionPerformHost implements AccessibilityDelegate {
|
||||
|
||||
private static final String TAG = "ActionPerformDelegate";
|
||||
|
||||
private BlockingQueue<SimpleAction> mSimpleAction = new ArrayBlockingQueue<>(1);
|
||||
private Executor mExecutor = Executors.newSingleThreadExecutor();
|
||||
|
||||
public void addToActionQueueAndWait(SimpleAction simpleAction) throws InterruptedException {
|
||||
mSimpleAction.put(simpleAction);
|
||||
synchronized (simpleAction) {
|
||||
simpleAction.wait();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onAccessibilityEvent(AccessibilityService service, AccessibilityEvent event) {
|
||||
SimpleAction action = mSimpleAction.poll();
|
||||
if (action == null)
|
||||
return false;
|
||||
AccessibilityNodeInfo root = service.getRootInActiveWindow();
|
||||
if (root == null) {
|
||||
return false;
|
||||
}
|
||||
performAction(root, action);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Integer> getEventTypes() {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void performAction(AccessibilityNodeInfo root, SimpleAction simpleAction) {
|
||||
mExecutor.execute(new SimpleActionPerformRunnable(root, simpleAction));
|
||||
}
|
||||
|
||||
|
||||
private synchronized void onActionPerformed(SimpleAction simpleAction) {
|
||||
synchronized (simpleAction) {
|
||||
simpleAction.notify();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class SimpleActionPerformRunnable implements Runnable {
|
||||
|
||||
private final SimpleAction mSimpleAction;
|
||||
private final AccessibilityNodeInfo mRoot;
|
||||
|
||||
SimpleActionPerformRunnable(AccessibilityNodeInfo root, SimpleAction simpleAction) {
|
||||
mRoot = root;
|
||||
mSimpleAction = simpleAction;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (!mSimpleAction.isValid()) {
|
||||
return;
|
||||
}
|
||||
Log.i(TAG, "perform simpleAction: " + mSimpleAction);
|
||||
if (mSimpleAction.perform(UiObject.createRoot(mRoot))) {
|
||||
mSimpleAction.setResult(true);
|
||||
onActionPerformed(mSimpleAction);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user