This commit is contained in:
hyb1996
2017-05-27 14:49:25 +08:00
parent 302879333a
commit 7c88e578fe
16 changed files with 143 additions and 75 deletions

View File

@@ -9,8 +9,8 @@ android {
applicationId "com.stardust.scriptdroid" applicationId "com.stardust.scriptdroid"
minSdkVersion 19 minSdkVersion 19
targetSdkVersion 23 targetSdkVersion 23
versionCode 134 versionCode 136
versionName "2.0.12 Alpha" versionName "2.0.12 Alpha3日志版"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true multiDexEnabled true
ndk { ndk {

View File

@@ -9,6 +9,7 @@
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_LOGS"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

View File

@@ -13,6 +13,7 @@ import com.stardust.scriptdroid.autojs.AutoJs;
import com.stardust.scriptdroid.statics.ScriptStatics; import com.stardust.scriptdroid.statics.ScriptStatics;
import com.stardust.scriptdroid.tool.CrashHandler; import com.stardust.scriptdroid.tool.CrashHandler;
import com.stardust.scriptdroid.tool.JsBeautifierFactory; import com.stardust.scriptdroid.tool.JsBeautifierFactory;
import com.stardust.scriptdroid.tool.Logcat;
import com.stardust.scriptdroid.ui.error.ErrorReportActivity; import com.stardust.scriptdroid.ui.error.ErrorReportActivity;
import com.stardust.theme.ThemeColor; import com.stardust.theme.ThemeColor;
import com.stardust.theme.ThemeColorManager; import com.stardust.theme.ThemeColorManager;
@@ -43,6 +44,8 @@ public class App extends MultiDexApplication {
setUpDebugEnvironment(); setUpDebugEnvironment();
init(); init();
registerActivityLifecycleCallback(); registerActivityLifecycleCallback();
Logcat.deleteLogFile();
Logcat.startLogSavingIfNeeded();
} }
private void setUpStaticsTool() { private void setUpStaticsTool() {

View File

@@ -105,6 +105,7 @@ public class AutoJs implements AccessibilityBridge {
.setScreenCaptureRequester(mScreenCaptureRequester) .setScreenCaptureRequester(mScreenCaptureRequester)
.setAccessibilityBridge(AutoJs.this) .setAccessibilityBridge(AutoJs.this)
.setUiHandler(mUiHandler) .setUiHandler(mUiHandler)
.setAppUtils(mAppUtils)
.setShellSupplier(new Supplier<AbstractShell>() { .setShellSupplier(new Supplier<AbstractShell>() {
@Override @Override
public AbstractShell get() { public AbstractShell get() {

View File

@@ -53,10 +53,8 @@ public class AccessibilityWatchDogService extends AccessibilityService {
@Override @Override
public void onAccessibilityEvent(final AccessibilityEvent event) { public void onAccessibilityEvent(final AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
}
Log.v(TAG, "onAccessibilityEvent: " + event); Log.v(TAG, "onAccessibilityEvent: " + event);
super.onAccessibilityEvent(event);
if (!containsAllEventTypes && !eventTypes.contains(event.getEventType())) if (!containsAllEventTypes && !eventTypes.contains(event.getEventType()))
return; return;
for (Map.Entry<Integer, AccessibilityDelegate> entry : mDelegates.entrySet()) { for (Map.Entry<Integer, AccessibilityDelegate> entry : mDelegates.entrySet()) {

View File

@@ -9,7 +9,6 @@ import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteType;
*/ */
@StorIOSQLiteType(table = SQLiteStaticsStorage.TABLE_NAME) @StorIOSQLiteType(table = SQLiteStaticsStorage.TABLE_NAME)
public class ScriptStaticsRecord { public class ScriptStaticsRecord {
@StorIOSQLiteColumn(name = "name", key = true) @StorIOSQLiteColumn(name = "name", key = true)

View File

@@ -0,0 +1,33 @@
package com.stardust.scriptdroid.tool;
import com.stardust.scriptdroid.script.StorageScriptProvider;
import java.io.File;
import java.io.IOException;
/**
* Created by Stardust on 2017/5/22.
*/
public class Logcat {
private static final String LOG_PATH = StorageScriptProvider.DEFAULT_DIRECTORY_PATH + "log.txt";
private static final String DISABLE_PATH = StorageScriptProvider.DEFAULT_DIRECTORY_PATH + "disable_log.txt";
private static Process sLogSavingProcess;
public static void startLogSavingIfNeeded() {
if (sLogSavingProcess != null)
return;
if (new File(DISABLE_PATH).exists())
return;
try {
sLogSavingProcess = Runtime.getRuntime().exec("logcat -f " + LOG_PATH);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void deleteLogFile() {
new File(LOG_PATH).delete();
}
}

View File

@@ -11,6 +11,13 @@ module.exports = function(__runtime__, scope){
images.saveImage = __runtime__.images.saveImage.bind(__runtime__.images); images.saveImage = __runtime__.images.saveImage.bind(__runtime__.images);
images.findColor = function(img, color, options){ images.findColor = function(img, color, options){
if(typeof(color) == 'string'){
if(color.startsWith('#')){
color = parseInt('0x' + color.substring(1));
}else{
color = parseInt('0x' + color);
}
}
options = options || {}; options = options || {};
var region = options.region || []; var region = options.region || [];
x = region[0] || 0; x = region[0] || 0;

View File

@@ -39,8 +39,8 @@ public abstract class AbstractScriptRuntime {
@ScriptVariable @ScriptVariable
public Images images; public Images images;
public AbstractScriptRuntime(Context context, Console console, AccessibilityBridge bridge, ScreenCaptureRequester screenCaptureRequester) { public AbstractScriptRuntime(Context context, Console console, AccessibilityBridge bridge, AppUtils appUtils, ScreenCaptureRequester screenCaptureRequester) {
this.app = new AppUtils(context); this.app = appUtils;
this.console = console; this.console = console;
this.automator = new SimpleActionAutomator(bridge, this); this.automator = new SimpleActionAutomator(bridge, this);
this.info = bridge.getInfoProvider(); this.info = bridge.getInfoProvider();

View File

@@ -41,6 +41,7 @@ public class ScriptRuntime extends AbstractScriptRuntime {
private AccessibilityBridge mAccessibilityBridge; private AccessibilityBridge mAccessibilityBridge;
private Supplier<AbstractShell> mShellSupplier; private Supplier<AbstractShell> mShellSupplier;
private ScreenCaptureRequester mScreenCaptureRequester; private ScreenCaptureRequester mScreenCaptureRequester;
private AppUtils mAppUtils;
public Builder() { public Builder() {
@@ -71,6 +72,11 @@ public class ScriptRuntime extends AbstractScriptRuntime {
return this; return this;
} }
public Builder setAppUtils(AppUtils appUtils) {
mAppUtils = appUtils;
return this;
}
public ScriptRuntime build() { public ScriptRuntime build() {
return new ScriptRuntime(this); return new ScriptRuntime(this);
} }
@@ -86,7 +92,7 @@ public class ScriptRuntime extends AbstractScriptRuntime {
protected ScriptRuntime(Builder builder) { protected ScriptRuntime(Builder builder) {
super(builder.mUiHandler.getContext(), builder.mConsole, builder.mAccessibilityBridge, builder.mScreenCaptureRequester); super(builder.mUiHandler.getContext(), builder.mConsole, builder.mAccessibilityBridge, builder.mAppUtils, builder.mScreenCaptureRequester);
mAccessibilityBridge = builder.mAccessibilityBridge; mAccessibilityBridge = builder.mAccessibilityBridge;
mUiHandler = builder.mUiHandler; mUiHandler = builder.mUiHandler;
mShellSupplier = builder.mShellSupplier; mShellSupplier = builder.mShellSupplier;

View File

@@ -7,6 +7,7 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.Log;
import com.stardust.autojs.runtime.ScriptInterface; import com.stardust.autojs.runtime.ScriptInterface;
import com.stardust.util.IntentUtil; import com.stardust.util.IntentUtil;
@@ -21,7 +22,7 @@ import java.util.List;
public class AppUtils { public class AppUtils {
private Context mContext; private Context mContext;
private WeakReference<Activity> mCurrentActivity; private volatile WeakReference<Activity> mCurrentActivity = new WeakReference<>(null);
public AppUtils(Context context) { public AppUtils(Context context) {
mContext = context; mContext = context;
@@ -64,6 +65,7 @@ public class AppUtils {
@Nullable @Nullable
public Activity getCurrentActivity() { public Activity getCurrentActivity() {
Log.d("App", "getCurrentActivity: " + mCurrentActivity.get());
return mCurrentActivity.get(); return mCurrentActivity.get();
} }
@@ -75,5 +77,6 @@ public class AppUtils {
public void setCurrentActivity(Activity currentActivity) { public void setCurrentActivity(Activity currentActivity) {
mCurrentActivity = new WeakReference<>(currentActivity); mCurrentActivity = new WeakReference<>(currentActivity);
Log.d("App", "setCurrentActivity: " + currentActivity);
} }
} }

View File

@@ -118,13 +118,18 @@ public class UiSelector extends UiGlobalSelector {
@NonNull @NonNull
public UiObjectCollection untilFind() { public UiObjectCollection untilFind() {
UiObjectCollection uiObjectCollection; UiObjectCollection uiObjectCollection;
do { uiObjectCollection = find();
while (uiObjectCollection.empty()) {
if (Thread.currentThread().isInterrupted()) { if (Thread.currentThread().isInterrupted()) {
Log.d(TAG, "Thread isInterrupted"); throw new ScriptInterruptedException();
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
throw new ScriptInterruptedException(); throw new ScriptInterruptedException();
} }
uiObjectCollection = find(); uiObjectCollection = find();
} while (uiObjectCollection.empty()); }
return uiObjectCollection; return uiObjectCollection;
} }

View File

@@ -1,65 +1,72 @@
package com.stardust.autojs.runtime.api.image; package com.stardust.autojs.runtime.api.image;
import android.graphics.Color;
import java.util.Comparator;
/** /**
* Created by Stardust on 2017/5/20. * Created by Stardust on 2017/5/20.
*/ */
public interface ColorDetector { public interface ColorDetector {
boolean detectsColor(int color); boolean detectsColor(int red, int green, int blue);
abstract class AbstractColorDetector implements ColorDetector { abstract class AbstractColorDetector implements ColorDetector {
protected final int mColor; protected final int mColor;
protected final int mR, mG, mB;
public AbstractColorDetector(int color) { public AbstractColorDetector(int color) {
mColor = color; mColor = color;
mR = Color.red(color);
mG = Color.green(color);
mB = Color.blue(color);
} }
} }
class EqualityDetector implements ColorDetector { class EqualityDetector extends AbstractColorDetector {
private final int mColor;
public EqualityDetector(int color) { public EqualityDetector(int color) {
mColor = color & 0xffffff; super(color);
} }
@Override @Override
public boolean detectsColor(int color) { public boolean detectsColor(int red, int green, int blue) {
return mColor == (color & 0xffffff); return mR == red && mG == green && mB == blue;
} }
} }
class DifferenceDetector extends AbstractColorDetector { class DifferenceDetector extends AbstractColorDetector {
private final int mThreshold; private final int mRThreshold, mGThreshold, mBThreshold;
public DifferenceDetector(int color, int threshold) { public DifferenceDetector(int color, int threshold) {
super(color); super(color);
mThreshold = threshold; mRThreshold = Color.red(threshold);
mGThreshold = Color.green(threshold);
mBThreshold = Color.blue(threshold);
} }
@Override @Override
public boolean detectsColor(int color) { public boolean detectsColor(int R, int G, int B) {
return Math.abs(mColor - color) <= mThreshold; return Math.abs(R - mR) <= mRThreshold && Math.abs(G - mG) <= mGThreshold &&
Math.abs(B - mB) <= mBThreshold;
} }
} }
class RDistanceDetector extends AbstractColorDetector { class RDistanceDetector extends AbstractColorDetector {
private final int mR;
private final int mThreshold; private final int mThreshold;
public RDistanceDetector(int color, int threshold) { public RDistanceDetector(int color, int threshold) {
super(color); super(color);
mThreshold = threshold; mThreshold = threshold;
mR = (color & 0xff0000) >> 16;
} }
@Override @Override
public boolean detectsColor(int color) { public boolean detectsColor(int R, int G, int B) {
int R = (color & 0xff0000) >> 16;
return Math.abs(mR - R) <= mThreshold; return Math.abs(mR - R) <= mThreshold;
} }
} }
@@ -67,21 +74,17 @@ public interface ColorDetector {
class RGBDistanceDetector extends AbstractColorDetector { class RGBDistanceDetector extends AbstractColorDetector {
private final int mThreshold; private final int mThreshold;
private final int mR, mG, mB;
public RGBDistanceDetector(int color, int threshold) { public RGBDistanceDetector(int color, int threshold) {
super(color); super(color);
mR = (color & 0xff0000) >> 16;
mG = (color & 0x00ff00) >> 8;
mB = color & 0xff;
mThreshold = threshold * threshold; mThreshold = threshold * threshold;
} }
@Override @Override
public boolean detectsColor(int color) { public boolean detectsColor(int R, int G, int B) {
int dR = ((color & 0xff0000) >> 16) - mR; int dR = R - mR;
int dG = ((color & 0x00ff00) >> 8) - mG; int dG = G - mG;
int dB = (color & 0xff) - mB; int dB = B - mB;
int d = dR * dR + dG * dG + dB * dB; int d = dR * dR + dG * dG + dB * dB;
return d <= mThreshold; return d <= mThreshold;
} }
@@ -101,11 +104,10 @@ public interface ColorDetector {
} }
@Override @Override
public boolean detectsColor(int color) { public boolean detectsColor(int R, int G, int B) {
int R = (color & 0xff0000) >> 16;
int dR = R - mR; int dR = R - mR;
int dG = ((color & 0x00ff00) >> 8) - mG; int dG = G - mG;
int dB = (color & 0xff) - mB; int dB = B - mB;
double meanR = (mR + R) / 2; double meanR = (mR + R) / 2;
double weightR = 2 + meanR / 256; double weightR = 2 + meanR / 256;
double weightG = 4.0; double weightG = 4.0;
@@ -121,19 +123,16 @@ public interface ColorDetector {
public HDistanceDetector(int color, int threshold) { public HDistanceDetector(int color, int threshold) {
super(color); super(color);
mH = getH(color); mH = getH(mR, mG, mB);
mThreshold = threshold; mThreshold = threshold;
} }
@Override @Override
public boolean detectsColor(int color) { public boolean detectsColor(int R, int G, int B) {
return Math.abs(mH - getH(color)) <= mThreshold; return Math.abs(mH - getH(R, G, B)) <= mThreshold;
} }
private static int getH(int color) { private static int getH(int R, int G, int B) {
int R = (color & 0xff0000) >> 16;
int G = (color & 0x00ff00) >> 8;
int B = color & 0xff;
int max, min, H; int max, min, H;
if (R > G) { if (R > G) {
min = Math.min(G, B); min = Math.min(G, B);
@@ -161,24 +160,21 @@ public interface ColorDetector {
public HSDistanceDetector(int color, int threshold) { public HSDistanceDetector(int color, int threshold) {
super(color); super(color);
long HS = getHS(color); long HS = getHS(mR, mG, mB);
mH = (int) (HS & 0xffffffffL); mH = (int) (HS & 0xffffffffL);
mS = (int) ((HS >> 32) & 0xffffffffL); mS = (int) ((HS >> 32) & 0xffffffffL);
mThreshold = threshold * threshold; mThreshold = threshold * threshold;
} }
@Override @Override
public boolean detectsColor(int color) { public boolean detectsColor(int R, int G, int B) {
long hs = getHS(color); long hs = getHS(R, G, B);
int dH = (int) (hs & 0xffffffffL) - mH; int dH = (int) (hs & 0xffffffffL) - mH;
int dS = (int) ((hs >> 32) & 0xffffffffL) - mS; int dS = (int) ((hs >> 32) & 0xffffffffL) - mS;
return dH * dH + dS * dS <= mThreshold; return dH * dH + dS * dS <= mThreshold;
} }
private static long getHS(int color) { private static long getHS(int R, int G, int B) {
int R = (color & 0xff0000) >> 16;
int G = (color & 0x00ff00) >> 8;
int B = color & 0xff;
int max, min, H; int max, min, H;
if (R > G) { if (R > G) {
min = Math.min(G, B); min = Math.min(G, B);

View File

@@ -151,9 +151,11 @@ public class ColorFinder {
public static Point findColor(ColorIterator iterator, ColorDetector detector) { public static Point findColor(ColorIterator iterator, ColorDetector detector) {
Thread thread = Thread.currentThread(); Thread thread = Thread.currentThread();
while (!thread.isInterrupted() && iterator.hasNext()) { ColorIterator.Pixel pixel = new ColorIterator.Pixel();
int c = iterator.nextColor(); while (iterator.hasNext() && !thread.isInterrupted()) {
if (detector.detectsColor(c)) { iterator.nextColor(pixel);
iterator.nextColor(pixel);
if (detector.detectsColor(pixel.red, pixel.green, pixel.blue)) {
return new Point(iterator.getX(), iterator.getY()); return new Point(iterator.getX(), iterator.getY());
} }
} }
@@ -206,9 +208,11 @@ public class ColorFinder {
@Override @Override
public void run() { public void run() {
Thread thread = Thread.currentThread(); Thread thread = Thread.currentThread();
ColorIterator.Pixel pixel = new ColorIterator.Pixel();
while (mResultBox.isNull() && mColorIterator.hasNext() && !thread.isInterrupted()) { while (mResultBox.isNull() && mColorIterator.hasNext() && !thread.isInterrupted()) {
int c = mColorIterator.nextColor(); mColorIterator.nextColor(pixel);
if (mColorDetector.detectsColor(c)) { mColorIterator.nextColor(pixel);
if (mColorDetector.detectsColor(pixel.red, pixel.green, pixel.blue)) {
mResultBox.set(new Point(mColorIterator.getX(), mColorIterator.getY())); mResultBox.set(new Point(mColorIterator.getX(), mColorIterator.getY()));
break; break;
} }
@@ -234,9 +238,10 @@ public class ColorFinder {
@Override @Override
public void run() { public void run() {
Thread thread = Thread.currentThread(); Thread thread = Thread.currentThread();
ColorIterator.Pixel pixel = new ColorIterator.Pixel();
while (mColorIterator.hasNext() && !thread.isInterrupted()) { while (mColorIterator.hasNext() && !thread.isInterrupted()) {
int c = mColorIterator.nextColor(); mColorIterator.nextColor(pixel);
if (mColorDetector.detectsColor(c)) { if (mColorDetector.detectsColor(pixel.red, pixel.green, pixel.blue)) {
mResult.add(new Point(mColorIterator.getX(), mColorIterator.getY())); mResult.add(new Point(mColorIterator.getX(), mColorIterator.getY()));
} }
} }

View File

@@ -12,9 +12,15 @@ import java.nio.ByteBuffer;
public interface ColorIterator { public interface ColorIterator {
class Pixel {
int red;
int green;
int blue;
}
boolean hasNext(); boolean hasNext();
int nextColor(); void nextColor(Pixel pixel);
int getX(); int getX();
@@ -94,7 +100,7 @@ public interface ColorIterator {
} }
@Override @Override
public int nextColor() { public void nextColor(Pixel pixel) {
if (mX == mWidth - 1) { if (mX == mWidth - 1) {
skip(mSkipPerRow); skip(mSkipPerRow);
mX = 0; mX = 0;
@@ -102,11 +108,9 @@ public interface ColorIterator {
} else { } else {
mX++; mX++;
} }
int c = (mByteBuffer.get() & 0xff) << 16; pixel.red = mByteBuffer.get() & 0xff;
c |= (mByteBuffer.get() & 0xff) << 8; pixel.green = mByteBuffer.get() & 0xff;
c |= (mByteBuffer.get() & 0xff); pixel.blue = mByteBuffer.get() & 0xff;
c |= (mByteBuffer.get() & 0xff) << 24;
return c;
} }
} }
@@ -145,7 +149,7 @@ public interface ColorIterator {
} }
@Override @Override
public int nextColor() { public void nextColor(Pixel pixel) {
int c = mByteBuffer.getInt(); int c = mByteBuffer.getInt();
skip(mNextStepSkip); skip(mNextStepSkip);
mStepCount++; mStepCount++;
@@ -168,7 +172,6 @@ public interface ColorIterator {
break; break;
} }
} }
return c;
} }
@Override @Override

View File

@@ -1,5 +1,7 @@
package com.stardust.view.accessibility; package com.stardust.view.accessibility;
import android.support.annotation.CallSuper;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo; import android.view.accessibility.AccessibilityNodeInfo;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
@@ -16,15 +18,21 @@ public abstract class AccessibilityService extends android.accessibilityservice.
} }
private CopyOnWriteArrayList<NotificationCallback> mNotificationCallbacks = new CopyOnWriteArrayList<>(); private CopyOnWriteArrayList<NotificationCallback> mNotificationCallbacks = new CopyOnWriteArrayList<>();
private AccessibilityNodeInfo mRootInActiveWindow;
@CallSuper
@Override @Override
public void onAccessibilityEvent(AccessibilityEvent event) {
public AccessibilityNodeInfo getRootInActiveWindow() { if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
try { || event.getEventType() == AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
return super.getRootInActiveWindow(); || event.getEventType() == AccessibilityEvent.TYPE_VIEW_HOVER_EXIT) {
} catch (Exception ignored) { mRootInActiveWindow = super.getRootInActiveWindow();
return null;
} }
} }
@Override
public AccessibilityNodeInfo getRootInActiveWindow() {
return mRootInActiveWindow;
}
} }