Skip to content

Commit

Permalink
Add: Volume Changing to stop scripts, Fix: Automator ANR
Browse files Browse the repository at this point in the history
  • Loading branch information
hyb1996 committed Mar 20, 2017
1 parent c9927a3 commit 1f05bcc
Show file tree
Hide file tree
Showing 47 changed files with 494 additions and 138 deletions.
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 免Root脚本机器人
## 简介
一个**不需要Root权限**的类似按键精灵的自动操作软件,可以实现自动点击、滑动、输入文字、打开应用等。
> 注意:这里的不需要Root权限指的是一般软件,**游戏的自动点击等是需要Root权限的**
A automator
一个**不需要Root权限**的类似按键精灵的自动操作软件,可以实现自动点击、滑动、输入文字、打开应用等。

特性:
* 简单易用的自动操作函数
Expand Down
Binary file removed app-release-2.0.0内测.apk
Binary file not shown.
Binary file removed app-release-2.0.1 Beta.apk
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.stardust.scriptdroid"
minSdkVersion 19
targetSdkVersion 23
versionCode 103
versionName "2.0.1 Beta"
versionCode 106
versionName "2.0.3 Beta"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true

Expand Down
10 changes: 10 additions & 0 deletions app/src/main/assets/help/documentation/一般常用函数.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ launchApp("微信");
### currentActivity()
返回最近一次监测到的正在运行的Activity的名称,一般可以认为就是当前正在运行的Activity的名称。

### getPackageName(appName)
* appName \<String\> 应用名称

获取应用的包名。例如getPackageName("QQ")为"com.tencent.mobileqq"。如果有相同名称的应用,只返回其中某一个的包名。如果不存在这个名称的应用,会返回空字符串。

### openAppSetting(packageName)
* packageName \<String\> 应用包名

打开某个应用的应用详情页,也就是管理应用权限和可以停止其运行的页面。如果应用包名不存在,则返回false;否则返回true。

### setClip(text)
* text \<String\> 文本

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/assets/help/documentation/自动操作函数.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
**自动操作的函数都需要开启"自动操作服务"才能执行,否则执行到相应函数时脚本会停止运行。**
### auto模式
如果代码的第一行是
```
"auto";
```
那么脚本运行前会先确保自动操作服务已开启。

您也可以在设置中开启"使用Root权限自动开启自动操作服务",从而在运行到相应语句时可以自动开启。
### click(text[, i])
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/assets/javasccript_engine_init.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ var setClip = function(text){
droid.setClip(text);
}

var getPackageName = function(appName){
return droid.getPackageName(appName);
}

var openAppSetting = function(packageName){
return droid.openAppSetting(packageName);
}

var Tap = function(x, y){
return shell("input tap " + x + " " + y, true).code == 1;
}
Expand Down Expand Up @@ -202,7 +210,6 @@ var Text = function(text){
return shell("input text " + text, true).code == 1;
}


var __selector__ = droid.selector();
var __obj__ = new java.lang.Object();

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/assets/sample/界面/QQ强制聊天.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ container.addView(btnOk);
activity.setContentView(container);

btnOk.setOnClickListener(function(view){
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mqqwpa://im/chat?chat_type=wpa&uin=" + qq.getText())));
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mqqwpa://im/chat?chat_type=wpa&uin=" + qq.getText()))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
activity.finish();
});
8 changes: 6 additions & 2 deletions app/src/main/assets/sample/界面/简单计算器.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ importClass(com.afollestad.materialdialogs.MaterialDialog);
new MaterialDialog.Builder(activity)
.title("简单计算器")
.input("请输入算式", "1+1", function(dialog, input){
eval("var ans = (" + input + ")");
toast(ans);
try{
eval("var ans = (" + input + ")");
toast(ans);
}catch(e){
toast("式子错误");
}
})
.positiveText("计算")
.dismissListener(function(dialog){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"auto";
launch("com.tencent.mobileqq");
toast("请打开一个聊天窗口");
toast("出现红包时将会自动拆开并关闭");
Expand Down
9 changes: 2 additions & 7 deletions app/src/main/assets/sample/自动操作/屏蔽QQ通知.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
"auto";
importClass(android.content.Intent);
importClass(android.net.Uri);
importClass(android.provider.Settings);

function 打开App设置(packageName){
var intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
var uri = Uri.fromParts("package", packageName, null);
intent.setData(uri);
context.startActivity(intent);
}

打开App设置("com.tencent.mobileqq");
openAppSetting("com.tencent.mobileqq");
while(!currentPackage().equals("com.android.settings"));
while(!click("通知"));
while(!click("全部阻止"));
Expand Down
1 change: 1 addition & 0 deletions app/src/main/assets/sample/自动操作/微信扫一扫.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"auto";
launch("com.tencent.mm");
while(!click("发现"));
while(!click("扫一扫"));
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"auto";

function 打开正在运行的服务(){
launch("com.android.settings");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
"auto";
launchApp("支付宝");
while(!click("扫一扫"));
2 changes: 2 additions & 0 deletions app/src/main/assets/sample/选择器/QQ互赞脚本.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"auto";

function 下滑(){
className("AbsListView").scrollable().scrollForward();
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/assets/sample/选择器/QQ图片轰炸.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"auto";

launchApp("QQ");
toast("打开一个聊天窗口,并点击下面的图片按钮");
for(i = 0; i < 100;i++){
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/assets/sample/选择器/微信一键已读.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"auto";

while(true){
if(id("i4").untilFindOne().parent().parent().click()){
sleep(700);
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/assets/sample/选择器/批量取关公众号.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"auto";

function 取消关注(){
id("af9").longClickable().longClick();
click("取消关注");
click("不再关注");
}

toast("请在10秒内打开订阅号界面");
toast("10秒后如果没有反应请稍微滑动一下页面");
sleep(10 * 1000);

while(true){
取消关注();
}
2 changes: 2 additions & 0 deletions app/src/main/assets/sample/选择器/网易云音乐签到.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"auto";

launchApp("网易云音乐");
id("kp").click();
text("签到").className("TextView").click();
6 changes: 6 additions & 0 deletions app/src/main/java/com/stardust/scriptdroid/Pref.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public static boolean isRecordVolumeControlEnable() {
return def().getBoolean(getString(R.string.key_use_volume_control_record), false);
}

public static boolean isRunningVolumeControlEnable() {
return def().getBoolean(getString(R.string.key_use_volume_control_running), false);
}


private static String getString(int id) {
return App.getApp().getString(id);
}
Expand All @@ -58,4 +63,5 @@ public static int MaxTextLengthForCodeCompletion() {
public static boolean isFirstUsing() {
return getDisposableBoolean("isFirstUsing", true);
}

}
28 changes: 28 additions & 0 deletions app/src/main/java/com/stardust/scriptdroid/droid/Droid.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package com.stardust.scriptdroid.droid;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.Toast;

import com.jraska.console.timber.ConsoleTree;
import com.stardust.scriptdroid.App;
import com.stardust.scriptdroid.Pref;
import com.stardust.scriptdroid.R;
import com.stardust.scriptdroid.droid.runtime.DroidRuntime;
import com.stardust.scriptdroid.droid.runtime.ScriptStopException;
import com.stardust.scriptdroid.droid.script.JavaScriptEngine;
import com.stardust.scriptdroid.droid.script.RhinoJavaScriptEngine;
import com.stardust.scriptdroid.droid.script.ScriptExecuteActivity;
import com.stardust.scriptdroid.service.VolumeChangeObverseService;
import com.stardust.scriptdroid.tool.FileUtils;

import java.io.File;
Expand All @@ -32,6 +36,8 @@ public class Droid {

public static final String UI = "\"ui\";";

public static final String AUTO = "\"auto\";";

public interface OnRunFinishedListener extends Serializable {
void onRunFinished(Object result);

Expand Down Expand Up @@ -63,8 +69,18 @@ public void onException(@NonNull Exception e) {
}
};
private static Droid instance = new Droid();
private VolumeChangeObverseService.OnVolumeChangeListener mOnVolumeChangeListener = new VolumeChangeObverseService.OnVolumeChangeListener() {
@Override
public void onVolumeChange() {
if (Pref.isRunningVolumeControlEnable()) {
stopAllAndToast();
}
}
};


private Droid() {
VolumeChangeObverseService.addOnVolumeChangeListener(mOnVolumeChangeListener);
}

public static Droid getInstance() {
Expand Down Expand Up @@ -107,6 +123,7 @@ private void runScript(String script) {
}

public void runScript(final String script, OnRunFinishedListener listener, RunningConfig config) {
App.getApp().startService(new Intent(App.getApp(), VolumeChangeObverseService.class));
listener = listener == null ? DEFAULT_LISTENER : listener;
if (config.runInNewThread) {
if (script.startsWith(UI)) {
Expand All @@ -125,6 +142,15 @@ public int stopAll() {
return JAVA_SCRIPT_ENGINE.stopAll();
}


public void stopAllAndToast() {
int n = stopAll();
if (n > 0)
RUNTIME.toast(String.format(App.getResString(R.string.text_already_stop_n_scripts), n));
else
RUNTIME.toast(App.getResString(R.string.text_no_running_script));
}

private void checkFile(File file) {
if (file == null) {
throw new NullPointerException("file = null");
Expand All @@ -151,6 +177,8 @@ private static class RunScriptRunnable implements Runnable {
@Override
public void run() {
try {
if (mScript.startsWith(AUTO))
DroidRuntime.getRuntime().ensureAccessibilityServiceEnable();
mOnRunFinishedListener.onRunFinished(JAVA_SCRIPT_ENGINE.execute(mScript));
} catch (Exception e) {
mOnRunFinishedListener.onException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.stardust.scriptdroid.droid.runtime.action.ActionTarget;
import com.stardust.scriptdroid.tool.AccessibilityServiceTool;
import com.stardust.scriptdroid.service.AccessibilityWatchDogService;
import com.stardust.scriptdroid.tool.IntentTool;
import com.stardust.theme.dialog.ThemeColorMaterialDialogBuilder;
import com.jraska.console.Console;
import com.stardust.scriptdroid.App;
Expand Down Expand Up @@ -77,28 +78,34 @@ protected DroidRuntime() {
mUIHandler = new Handler(App.getApp().getMainLooper());
}

public void launchPackage(String packageName) {
PackageManager packageManager = App.getApp().getPackageManager();
App.getApp().startActivity(packageManager.getLaunchIntentForPackage(packageName));
public boolean launchPackage(String packageName) {
try {
PackageManager packageManager = App.getApp().getPackageManager();
App.getApp().startActivity(packageManager.getLaunchIntentForPackage(packageName));
return true;
} catch (Exception e) {
return false;
}

}

public void launch(String packageName, String className) {
App.getApp().startActivity(new Intent().setClassName(packageName, className).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
public boolean launchApp(String appName) {
return launchPackage(getPackageName(appName));
}

public void launchApp(String appName) {
public String getPackageName(String appName) {
PackageManager packageManager = App.getApp().getPackageManager();
List<ApplicationInfo> installedApplications = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo applicationInfo : installedApplications) {
if (packageManager.getApplicationLabel(applicationInfo).toString().equals(appName)) {
launchPackage(applicationInfo.packageName);
break;
return applicationInfo.processName;
}
}
return "";
}

public String[] getPackageName(String appName) {
return new String[0];
public boolean openAppSetting(String packageName){
return IntentTool.goToAppSetting(App.getApp(), packageName);
}

public ActionTarget text(String text, int i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public abstract class Action {

private boolean mPerformUtilSucceed = false;
private Object mResult;
private volatile Object mResult = false;

public Action(boolean performUtilSucceed) {
mPerformUtilSucceed = performUtilSucceed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import com.stardust.scriptdroid.droid.runtime.DroidRuntime;
import com.stardust.view.accessibility.AccessibilityDelegate;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;


/**
* Created by Stardust on 2017/1/21.
Expand All @@ -27,12 +30,14 @@ public class ActionPerformAccessibilityDelegate implements AccessibilityDelegate

private static Action action;

private Executor mExecutor = Executors.newFixedThreadPool(5);

public static void setAction(Action action) {
synchronized (ACTION_LOCK) {
ActionPerformAccessibilityDelegate.action = action;
}
}

@Override
public boolean onAccessibilityEvent(AccessibilityService service, AccessibilityEvent event) {
synchronized (ACTION_LOCK) {
Expand All @@ -43,18 +48,27 @@ public boolean onAccessibilityEvent(AccessibilityService service, AccessibilityE
Log.v(TAG, "root = null");
return false;
}
Log.i(TAG, "perform action:" + action);
if (action.perform(root)) {
action.setResult(true);
onActionPerformed();
} else if (!action.performUtilSucceed()) {
action.setResult(false);
onActionPerformed();
}
performAction(root, action);
return false;
}
}

private void performAction(final AccessibilityNodeInfo root, final Action action) {
mExecutor.execute(new Runnable() {
@Override
public void run() {
Log.i(TAG, "perform action:" + action);
if (action.perform(root)) {
action.setResult(true);
onActionPerformed();
} else if (!action.performUtilSucceed()) {
action.setResult(false);
onActionPerformed();
}
}
});
}


private void onActionPerformed() {
synchronized (ACTION_LOCK) {
Expand Down
Loading

0 comments on commit 1f05bcc

Please sign in to comment.