add: root record generates js file

fix: delay of tap, swipe by adding RootAutomator
This commit is contained in:
hyb1996
2017-08-13 11:12:14 +08:00
parent e96b6082bb
commit 9c5f876a8f
14 changed files with 267 additions and 123 deletions

View File

@@ -128,4 +128,9 @@ public class Pref {
public static boolean isRecordToastEnabled() {
return def().getBoolean(getString(R.string.key_record_toast), true);
}
public static boolean rootRecordGeneratesBinary() {
return def().getString(getString(R.string.key_root_record_out_file_type), "binary")
.equals("binary");
}
}

View File

@@ -15,6 +15,9 @@ import com.stardust.autojs.core.inputevent.InputEventCodes;
import com.stardust.autojs.core.inputevent.ShellKeyObserver;
import com.stardust.autojs.core.record.Recorder;
import com.stardust.autojs.core.record.accessibility.AccessibilityActionRecorder;
import com.stardust.autojs.core.record.inputevent.InputEventRecorder;
import com.stardust.autojs.core.record.inputevent.InputEventToAutoFileRecorder;
import com.stardust.autojs.core.record.inputevent.InputEventToRootAutomatorRecorder;
import com.stardust.autojs.core.record.inputevent.TouchRecorder;
import com.stardust.autojs.runtime.api.Shell;
import com.stardust.scriptdroid.App;
@@ -45,8 +48,6 @@ public class GlobalRecorder implements Recorder.OnStateChangedListener {
private TouchRecorder mTouchRecorder;
private Context mContext;
private boolean mDiscard = false;
private long mLastVolumeDownEventTime;
private ShellKeyObserver mShellKeyObserver;
public static GlobalRecorder getSingleton(Context context) {
if (sSingleton == null) {
@@ -55,61 +56,25 @@ public class GlobalRecorder implements Recorder.OnStateChangedListener {
return sSingleton;
}
public static void initSingleton(Context context) {
getSingleton(context);
}
public GlobalRecorder(Context context) {
mContext = new ContextThemeWrapper(context.getApplicationContext(), R.style.AppTheme);
mTouchRecorder = new TouchRecorder(context);
addKeyListeners();
mTouchRecorder = new TouchRecorder(context) {
@Override
protected InputEventRecorder createInputEventRecorder() {
if (Pref.rootRecordGeneratesBinary())
return new InputEventToAutoFileRecorder(mContext);
else
return new InputEventToRootAutomatorRecorder();
}
};
EventBus.getDefault().register(this);
}
private void addKeyListeners() {
AccessibilityService.getStickOnKeyObserver().addListener(new OnKeyListener() {
@Override
public void onKeyEvent(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN &&
(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
onVolumeDown();
}
}
});
mShellKeyObserver = new ShellKeyObserver();
mShellKeyObserver.setKeyListener(new ShellKeyObserver.KeyListener() {
@Override
public void onKeyDown(String keyName) {
if ("KEY_VOLUMEDOWN".equals(keyName)) {
onVolumeDown();
}
}
@Override
public void onKeyUp(String keyName) {
}
});
}
private void onVolumeDown() {
if (!Pref.isRecordVolumeControlEnable()) {
return;
}
if (System.currentTimeMillis() - mLastVolumeDownEventTime < 300) {
return;
}
mLastVolumeDownEventTime = System.currentTimeMillis();
int state = getState();
if (state == Recorder.STATE_RECORDING || state == Recorder.STATE_PAUSED) {
stop();
} else {
start();
}
}
public void start() {
if (Pref.isRecordWithRootEnabled()) {
@@ -139,6 +104,10 @@ public class GlobalRecorder implements Recorder.OnStateChangedListener {
return mRecorder.getCode();
}
public String getPath() {
return mRecorder.getPath();
}
public int getState() {
if (mRecorder == null)
return Recorder.STATE_NOT_START;
@@ -166,7 +135,11 @@ public class GlobalRecorder implements Recorder.OnStateChangedListener {
@Override
public void onStop() {
if (!mDiscard) {
handleRecordedScript(getCode());
String code = getCode();
if (code != null)
handleRecordedScript(code);
else
handleRecordedFile(getPath());
}
for (Recorder.OnStateChangedListener listener : mOnStateChangedListeners) {
listener.onStop();
@@ -212,6 +185,23 @@ public class GlobalRecorder implements Recorder.OnStateChangedListener {
}
}
private void handleRecordedFile(final String path) {
if (Looper.myLooper() != Looper.getMainLooper()) {
App.getApp().getUiHandler().post(new Runnable() {
@Override
public void run() {
handleRecordedFile(path);
}
});
return;
}
new ScriptOperations(mContext, null)
.importFile(path)
.subscribe();
}
private void showRecordHandleDialog(final String script) {
DialogUtils.showDialog(new ThemeColorMaterialDialogBuilder(mContext)
.title(R.string.text_recorded)

View File

@@ -48,7 +48,7 @@ import io.mattcarroll.hover.NavigatorContent;
* Created by Stardust on 2017/3/12.
*/
public class RecordNavigatorContent implements NavigatorContent, Recorder.OnStateChangedListener {
public class RecordNavigatorContent implements NavigatorContent, Recorder.OnStateChangedListener, OnKeyListener {
private View mView;
@BindView(R.id.sw_recorded_by_root)
@@ -68,18 +68,36 @@ public class RecordNavigatorContent implements NavigatorContent, Recorder.OnStat
private GlobalRecorder mRecorder;
private Context mContext;
private long mLastVolumeDownEventTime;
public RecordNavigatorContent(Context context) {
mContext = new ContextThemeWrapper(context, R.style.AppTheme);
mView = View.inflate(context, R.layout.floating_window_record, null);
mView = View.inflate(mContext, R.layout.floating_window_record, null);
ButterKnife.bind(this, mView);
HoverMenuService.getEventBus().register(this);
mRecorder = GlobalRecorder.getSingleton(context);
mRecorder.addOnStateChangedListener(this);
setState(mRecorder.getState());
AccessibilityService.getStickOnKeyObserver().addListener(this);
}
private void onVolumeDown() {
if (!Pref.isRecordVolumeControlEnable()) {
return;
}
if (System.currentTimeMillis() - mLastVolumeDownEventTime < 300) {
return;
}
mLastVolumeDownEventTime = System.currentTimeMillis();
int state = mRecorder.getState();
if (state == Recorder.STATE_RECORDING || state == Recorder.STATE_PAUSED) {
mRecorder.stop();
} else {
mRecorder.start();
}
}
@NonNull
@Override
public View getView() {
@@ -161,6 +179,7 @@ public class RecordNavigatorContent implements NavigatorContent, Recorder.OnStat
public void onMenuExit() {
HoverMenuService.getEventBus().unregister(this);
mRecorder.removeOnStateChangedListener(this);
AccessibilityService.getStickOnKeyObserver().removeListener(this);
}
@Override
@@ -182,4 +201,13 @@ public class RecordNavigatorContent implements NavigatorContent, Recorder.OnStat
public void onResume() {
setState(Recorder.STATE_RECORDING);
}
@Override
public void onKeyEvent(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN &&
(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
onVolumeDown();
}
}
}

View File

@@ -8,6 +8,7 @@ import android.text.TextUtils;
import android.webkit.MimeTypeMap;
import android.widget.Toast;
import com.stardust.pio.PFile;
import com.stardust.scriptdroid.ui.BaseActivity;
import com.stardust.scriptdroid.ui.common.ScriptOperations;
import com.stardust.scriptdroid.ui.main.MainActivity;
@@ -45,9 +46,13 @@ public class ImportIntentActivity extends BaseActivity {
private void handleIntent(Intent intent) throws FileNotFoundException {
Uri uri = intent.getData();
if (uri != null && "content".equals(uri.getScheme())) {
String ext = PFile.getExtension(uri.getScheme());
if(TextUtils.isEmpty(ext)){
ext = "js";
}
InputStream stream = getContentResolver().openInputStream(uri);
new ScriptOperations(this, null)
.importFile("", stream)
.importFile("", stream, ext)
.subscribe(new Consumer<String>() {
@Override
public void accept(@NonNull String s) throws Exception {

View File

@@ -96,7 +96,13 @@ public class ScriptOperations {
}
public void newScriptFile() {
newScriptFileForScript(null);
showFileNameInputDialog("", "js")
.subscribe(new Consumer<String>() {
@Override
public void accept(@io.reactivex.annotations.NonNull String input) throws Exception {
createScriptFile(getCurrentDirectoryPath() + input + ".js", null, true);
}
});
}
public Observable<String> importFile(final String pathFrom) {
@@ -117,13 +123,13 @@ public class ScriptOperations {
});
}
public Observable<String> importFile(String prefix, final InputStream inputStream) {
return showFileNameInputDialog(PFile.getNameWithoutExtension(prefix), "js")
public Observable<String> importFile(String prefix, final InputStream inputStream, final String ext) {
return showFileNameInputDialog(PFile.getNameWithoutExtension(prefix), ext)
.observeOn(Schedulers.io())
.map(new Function<String, String>() {
@Override
public String apply(@io.reactivex.annotations.NonNull String s) throws Exception {
final String pathTo = getCurrentDirectoryPath() + s + ".js";
final String pathTo = getCurrentDirectoryPath() + s + "." + ext;
if (PFile.copyStream(inputStream, pathTo)) {
showMessage(R.string.text_import_succeed);
} else {
@@ -198,14 +204,13 @@ public class ScriptOperations {
}
private CharSequence getString(int resId) {
return mContext.getString(resId);
}
public Observable<String> importSample(Sample sample) {
try {
return importFile(sample.name, mContext.getAssets().open(sample.path));
return importFile(sample.name, mContext.getAssets().open(sample.path), PFile.getExtension(sample.path));
} catch (IOException e) {
e.printStackTrace();
showMessage(R.string.text_import_fail);