新增 支持Content Uri编辑脚本

This commit is contained in:
hyb1996
2018-10-24 17:55:19 +08:00
parent cd71a7e1dd
commit dbcb0257a2
8 changed files with 89 additions and 24 deletions

View File

@@ -1,21 +1,30 @@
package org.autojs.autojs.external.open;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Toast;
import com.stardust.pio.PFiles;
import org.autojs.autojs.ui.BaseActivity;
import org.autojs.autojs.ui.edit.EditActivity;
import org.autojs.autojs.R;
import java.io.File;
/**
* Created by Stardust on 2017/2/2.
*/
public class EditIntentActivity extends BaseActivity {
private static final String EXTERNAL_FILES = "external_files";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -30,8 +39,26 @@ public class EditIntentActivity extends BaseActivity {
private void handleIntent() {
Intent intent = getIntent();
String path = intent.getData().getPath();
if (!TextUtils.isEmpty(path))
Uri uri = intent.getData();
String path = null;
if (uri.getScheme().equals(ContentResolver.SCHEME_FILE)) {
path = uri.getPath();
} else {
int i = uri.getPath().indexOf(EXTERNAL_FILES);
if (i >= 0) {
path = uri.getPath().substring(i + EXTERNAL_FILES.length());
if (!PFiles.exists(path)) {
path = new File(Environment.getExternalStorageDirectory(), path).getPath();
if (!PFiles.exists(path)) {
path = null;
}
}
}
}
if (!TextUtils.isEmpty(path)) {
EditActivity.editFile(this, path);
} else {
EditActivity.editFile(this, uri);
}
}
}

View File

@@ -74,12 +74,12 @@ public class Scripts {
};
public static void openByOtherApps(String path) {
IntentUtil.viewFile(GlobalAppContext.get(), path, "text/plain", AppFileProvider.AUTHORITY);
public static void openByOtherApps(Uri uri) {
IntentUtil.viewFile(GlobalAppContext.get(), uri, "text/plain", AppFileProvider.AUTHORITY);
}
public static void openByOtherApps(File file) {
openByOtherApps(file.getPath());
openByOtherApps(Uri.fromFile(file));
}
public static void createShortcut(ScriptFile scriptFile) {

View File

@@ -3,6 +3,7 @@ package org.autojs.autojs.ui.edit;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@@ -61,6 +62,12 @@ EditActivity extends BaseActivity implements OnActivityResultDelegate.DelegateHo
editFile(context, null, path);
}
public static void editFile(Context context, Uri uri) {
context.startActivity(new Intent(context, EditActivity_.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setData(uri));
}
public static void editFile(Context context, String name, String path) {
context.startActivity(new Intent(context, EditActivity_.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
@@ -166,13 +173,13 @@ EditActivity extends BaseActivity implements OnActivityResultDelegate.DelegateHo
@Override
public ActionMode startActionMode(ActionMode.Callback callback, int type) {
Log.d(LOG_TAG, "startActionMode: callback = " + callback + ", type = " + type);
Log.d(LOG_TAG, "startActionMode: callback = " + callback + ", type = " + type);
return super.startActionMode(callback, type);
}
@Override
public ActionMode startActionMode(ActionMode.Callback callback) {
Log.d(LOG_TAG, "startActionMode: callback = " + callback );
Log.d(LOG_TAG, "startActionMode: callback = " + callback);
return super.startActionMode(callback);
}

View File

@@ -190,7 +190,7 @@ public class EditorMenu {
private void startBuildApkActivity() {
BuildActivity_.intent(mContext)
.extra(BuildActivity.EXTRA_SOURCE, mEditorView.getFile().getPath())
.extra(BuildActivity.EXTRA_SOURCE, mEditorView.getUri().getPath())
.start();
}

View File

@@ -7,6 +7,7 @@ import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
@@ -31,6 +32,7 @@ import com.stardust.autojs.execution.ScriptExecution;
import com.stardust.pio.PFiles;
import com.stardust.util.BackPressedHandler;
import com.stardust.util.Callback;
import com.stardust.util.IntentUtil;
import com.stardust.util.ViewUtils;
import org.androidannotations.annotations.AfterViews;
@@ -116,7 +118,7 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
DrawerLayout mDrawerLayout;
private String mName;
private File mFile;
private Uri mUri;
private boolean mReadOnly = false;
private int mScriptExecutionId;
private AutoCompletion mAutoCompletion;
@@ -179,8 +181,8 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
}
}
public File getFile() {
return mFile;
public Uri getUri() {
return mUri;
}
public Observable<String> handleIntent(Intent intent) {
@@ -215,21 +217,26 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
return Observable.just(content);
} else {
if (path == null) {
return Observable.error(new IllegalArgumentException("path and content is empty"));
if (intent.getData() == null) {
return Observable.error(new IllegalArgumentException("path and content is empty"));
} else {
mUri = intent.getData();
}
} else {
mUri = Uri.fromFile(new File(path));
}
mFile = new File(path);
if (mName == null) {
mName = mFile.getName();
mName = PFiles.getNameWithoutExtension(mUri.getPath());
}
return loadFile(mFile);
return loadUri(mUri);
}
}
@SuppressLint("CheckResult")
private Observable<String> loadFile(final File file) {
private Observable<String> loadUri(final Uri uri) {
mEditor.setProgress(true);
return Observable.fromCallable(() -> PFiles.read(file))
return Observable.fromCallable(() -> PFiles.read(getContext().getContentResolver().openInputStream(uri)))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(s -> {
@@ -393,7 +400,8 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
if (showMessage) {
Snackbar.make(this, R.string.text_start_running, Snackbar.LENGTH_SHORT).show();
}
ScriptExecution execution = Scripts.runWithBroadcastSender(mFile);
// TODO: 2018/10/24
ScriptExecution execution = Scripts.runWithBroadcastSender(new File(mUri.getPath()));
if (execution == null) {
return null;
}
@@ -414,7 +422,7 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
public Observable<String> save() {
return Observable.just(mEditor.getText())
.observeOn(Schedulers.io())
.doOnNext(s -> PFiles.write(mFile, s))
.doOnNext(s -> PFiles.write(getContext().getContentResolver().openOutputStream(mUri), s))
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(s -> {
mEditor.markTextAsSaved();
@@ -489,8 +497,9 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
}
public void openByOtherApps() {
if (mFile != null)
Scripts.openByOtherApps(mFile);
if (mUri != null) {
Scripts.openByOtherApps(mUri);
}
}
public void beautifyCode() {

View File

@@ -75,7 +75,7 @@ public class DebugToolbarFragment extends ToolbarFragment implements DebugCallba
mDebugger = DebuggerSingleton.get();
mDebugger.setWeakDebugCallback(new WeakReference<>(this));
setInterrupted(false);
mCurrentEditorSourceUrl = mInitialEditorSourceUrl = mEditorView.getFile().toString();
mCurrentEditorSourceUrl = mInitialEditorSourceUrl = mEditorView.getUri().toString();
mInitialEditorSource = mEditorView.getEditor().getText();
setupEditor();
ScriptExecution execution = mEditorView.run(false);