fix(ui): app crash when editor cannot open file
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
package org.autojs.autojs.external.tasker;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.autojs.autojs.R;
|
||||
import org.autojs.autojs.tool.EmptyObservers;
|
||||
import org.autojs.autojs.ui.BaseActivity;
|
||||
import org.autojs.autojs.ui.edit.EditorView;
|
||||
|
||||
@@ -11,6 +14,7 @@ import org.androidannotations.annotations.AfterViews;
|
||||
import org.androidannotations.annotations.EActivity;
|
||||
import org.androidannotations.annotations.ViewById;
|
||||
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.annotations.NonNull;
|
||||
import io.reactivex.functions.Consumer;
|
||||
|
||||
@@ -37,11 +41,18 @@ public class TaskerScriptEditActivity extends BaseActivity {
|
||||
@ViewById(R.id.editor_view)
|
||||
EditorView mEditorView;
|
||||
|
||||
@SuppressLint("CheckResult")
|
||||
@AfterViews
|
||||
void setUpViews() {
|
||||
mEditorView.handleIntent(getIntent()
|
||||
.putExtra(EXTRA_RUN_ENABLED, false)
|
||||
.putExtra(EXTRA_SAVE_ENABLED, false));
|
||||
.putExtra(EXTRA_SAVE_ENABLED, false))
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(EmptyObservers.consumer(),
|
||||
ex -> {
|
||||
Toast.makeText(TaskerScriptEditActivity.this, ex.getMessage(), Toast.LENGTH_LONG).show();
|
||||
finish();
|
||||
});
|
||||
BaseActivity.setToolbarAsBack(this, R.id.toolbar, mEditorView.getName());
|
||||
}
|
||||
|
||||
|
||||
@@ -226,12 +226,7 @@ public class ScriptOperations {
|
||||
|
||||
@SuppressLint("CheckResult")
|
||||
public void delete(final ScriptFile scriptFile) {
|
||||
Observable.fromPublisher(new Publisher<Boolean>() {
|
||||
@Override
|
||||
public void subscribe(Subscriber<? super Boolean> s) {
|
||||
s.onNext(PFiles.deleteRecursively(scriptFile));
|
||||
}
|
||||
}).subscribeOn(Schedulers.io())
|
||||
Observable.fromPublisher((Publisher<Boolean>) s -> s.onNext(PFiles.deleteRecursively(scriptFile))).subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(deleted -> {
|
||||
showMessage(deleted ? R.string.text_already_delete : R.string.text_delete_failed);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.autojs.autojs.ui.edit;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
@@ -7,6 +8,7 @@ import android.support.annotation.NonNull;
|
||||
import android.view.ActionMode;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.stardust.app.OnActivityResultDelegate;
|
||||
import com.stardust.autojs.core.permission.OnRequestPermissionsResultCallback;
|
||||
@@ -17,6 +19,7 @@ import com.stardust.pio.PFiles;
|
||||
|
||||
import org.autojs.autojs.R;
|
||||
import org.autojs.autojs.storage.file.TmpScriptFiles;
|
||||
import org.autojs.autojs.tool.EmptyObservers;
|
||||
import org.autojs.autojs.ui.BaseActivity;
|
||||
import org.autojs.autojs.theme.dialog.ThemeColorMaterialDialogBuilder;
|
||||
|
||||
@@ -70,13 +73,27 @@ public class EditActivity extends BaseActivity implements OnActivityResultDelega
|
||||
.putExtra(EXTRA_READ_ONLY, true));
|
||||
}
|
||||
|
||||
@SuppressLint("CheckResult")
|
||||
@AfterViews
|
||||
void setUpViews() {
|
||||
mEditorView.handleIntent(getIntent());
|
||||
mEditorView.handleIntent(getIntent())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(EmptyObservers.consumer(),
|
||||
ex -> onLoadFileError(ex.getMessage()));
|
||||
mEditorMenu = new EditorMenu(mEditorView);
|
||||
setUpToolbar();
|
||||
}
|
||||
|
||||
private void onLoadFileError(String message) {
|
||||
new ThemeColorMaterialDialogBuilder(this)
|
||||
.title(getString(R.string.text_cannot_read_file))
|
||||
.content(message)
|
||||
.positiveText(R.string.text_exit)
|
||||
.cancelable(false)
|
||||
.onPositive((dialog, which) -> finish())
|
||||
.show();
|
||||
}
|
||||
|
||||
private void setUpToolbar() {
|
||||
BaseActivity.setToolbarAsBack(this, R.id.toolbar, mEditorView.getName());
|
||||
}
|
||||
|
||||
@@ -22,13 +22,11 @@ import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.stardust.autojs.engine.JavaScriptEngine;
|
||||
import com.stardust.autojs.engine.ScriptEngine;
|
||||
import com.stardust.autojs.execution.ScriptExecution;
|
||||
import com.stardust.autojs.rhino.debug.Debugger;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.util.BackPressedHandler;
|
||||
import com.stardust.util.Callback;
|
||||
@@ -183,21 +181,23 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
||||
return mFile;
|
||||
}
|
||||
|
||||
public void handleIntent(Intent intent) {
|
||||
public Observable<String> handleIntent(Intent intent) {
|
||||
mName = intent.getStringExtra(EXTRA_NAME);
|
||||
handleText(intent);
|
||||
mReadOnly = intent.getBooleanExtra(EXTRA_READ_ONLY, false);
|
||||
boolean saveEnabled = intent.getBooleanExtra(EXTRA_SAVE_ENABLED, true);
|
||||
if (mReadOnly || !saveEnabled) {
|
||||
findViewById(R.id.save).setVisibility(View.GONE);
|
||||
}
|
||||
if (!intent.getBooleanExtra(EXTRA_RUN_ENABLED, true)) {
|
||||
findViewById(R.id.run).setVisibility(GONE);
|
||||
}
|
||||
if (mReadOnly) {
|
||||
mEditor.setReadOnly(true);
|
||||
}
|
||||
|
||||
return handleText(intent)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.doOnNext(str -> {
|
||||
mReadOnly = intent.getBooleanExtra(EXTRA_READ_ONLY, false);
|
||||
boolean saveEnabled = intent.getBooleanExtra(EXTRA_SAVE_ENABLED, true);
|
||||
if (mReadOnly || !saveEnabled) {
|
||||
findViewById(R.id.save).setVisibility(View.GONE);
|
||||
}
|
||||
if (!intent.getBooleanExtra(EXTRA_RUN_ENABLED, true)) {
|
||||
findViewById(R.id.run).setVisibility(GONE);
|
||||
}
|
||||
if (mReadOnly) {
|
||||
mEditor.setReadOnly(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void setRestoredText(String text) {
|
||||
@@ -205,37 +205,34 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
||||
mEditor.setText(text);
|
||||
}
|
||||
|
||||
private void handleText(Intent intent) {
|
||||
private Observable<String> handleText(Intent intent) {
|
||||
String path = intent.getStringExtra(EXTRA_PATH);
|
||||
String content = intent.getStringExtra(EXTRA_CONTENT);
|
||||
if (content != null) {
|
||||
setInitialText(content);
|
||||
return Observable.just(content);
|
||||
} else {
|
||||
if (path == null) {
|
||||
return;
|
||||
return Observable.error(new IllegalArgumentException("path and content is empty"));
|
||||
}
|
||||
mFile = new File(path);
|
||||
if (mName == null) {
|
||||
mName = mFile.getName();
|
||||
}
|
||||
loadFile(mFile);
|
||||
return loadFile(mFile);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressLint("CheckResult")
|
||||
private void loadFile(final File file) {
|
||||
private Observable<String> loadFile(final File file) {
|
||||
mEditor.setProgress(true);
|
||||
Observable.fromCallable(() -> PFiles.read(file))
|
||||
return Observable.fromCallable(() -> PFiles.read(file))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(s -> {
|
||||
.doOnNext(s -> {
|
||||
setInitialText(s);
|
||||
mEditor.setProgress(false);
|
||||
}, err -> {
|
||||
err.printStackTrace();
|
||||
Toast.makeText(getContext(), getContext().getString(R.string.text_cannot_read_file, file.getPath()),
|
||||
Toast.LENGTH_SHORT).show();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -218,12 +218,12 @@ public class ScriptListView extends ThemeColorSwipeRefreshLayout implements Swip
|
||||
public boolean onMenuItemClick(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.rename:
|
||||
new ScriptOperations(getContext(), this)
|
||||
new ScriptOperations(getContext(), this, getCurrentDirectory())
|
||||
.rename(mSelectedScriptFile)
|
||||
.subscribe();
|
||||
break;
|
||||
case R.id.delete:
|
||||
new ScriptOperations(getContext(), this)
|
||||
new ScriptOperations(getContext(), this, getCurrentDirectory())
|
||||
.delete(mSelectedScriptFile);
|
||||
break;
|
||||
case R.id.run_repeatedly:
|
||||
@@ -232,7 +232,7 @@ public class ScriptListView extends ThemeColorSwipeRefreshLayout implements Swip
|
||||
notifyOperated();
|
||||
break;
|
||||
case R.id.create_shortcut:
|
||||
new ScriptOperations(getContext(), this)
|
||||
new ScriptOperations(getContext(), this, getCurrentDirectory())
|
||||
.createShortcut(mSelectedScriptFile);
|
||||
break;
|
||||
case R.id.open_by_other_apps:
|
||||
@@ -244,7 +244,7 @@ public class ScriptListView extends ThemeColorSwipeRefreshLayout implements Swip
|
||||
notifyOperated();
|
||||
break;
|
||||
case R.id.timed_task:
|
||||
new ScriptOperations(getContext(), this)
|
||||
new ScriptOperations(getContext(), this, getCurrentDirectory())
|
||||
.timedTask(mSelectedScriptFile);
|
||||
notifyOperated();
|
||||
break;
|
||||
|
||||
@@ -271,7 +271,7 @@
|
||||
<string name="text_reset_to_initial_content">重置为初始内容</string>
|
||||
<string name="text_reset_fail">重置失败</string>
|
||||
<string name="text_reset_succeed">重置成功</string>
|
||||
<string name="text_cannot_read_file" formatted="true">无法读取文件: %s</string>
|
||||
<string name="text_cannot_read_file">无法读取文件</string>
|
||||
<string name="text_service">服务</string>
|
||||
<string name="text_notification_permission">通知权限</string>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user