新增 支持Content Uri编辑脚本
This commit is contained in:
@@ -1,21 +1,30 @@
|
|||||||
package org.autojs.autojs.external.open;
|
package org.autojs.autojs.external.open;
|
||||||
|
|
||||||
|
import android.content.ContentResolver;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Environment;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.stardust.pio.PFiles;
|
||||||
|
|
||||||
import org.autojs.autojs.ui.BaseActivity;
|
import org.autojs.autojs.ui.BaseActivity;
|
||||||
import org.autojs.autojs.ui.edit.EditActivity;
|
import org.autojs.autojs.ui.edit.EditActivity;
|
||||||
import org.autojs.autojs.R;
|
import org.autojs.autojs.R;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Stardust on 2017/2/2.
|
* Created by Stardust on 2017/2/2.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class EditIntentActivity extends BaseActivity {
|
public class EditIntentActivity extends BaseActivity {
|
||||||
|
|
||||||
|
private static final String EXTERNAL_FILES = "external_files";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@@ -30,8 +39,26 @@ public class EditIntentActivity extends BaseActivity {
|
|||||||
|
|
||||||
private void handleIntent() {
|
private void handleIntent() {
|
||||||
Intent intent = getIntent();
|
Intent intent = getIntent();
|
||||||
String path = intent.getData().getPath();
|
Uri uri = intent.getData();
|
||||||
if (!TextUtils.isEmpty(path))
|
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);
|
EditActivity.editFile(this, path);
|
||||||
|
} else {
|
||||||
|
EditActivity.editFile(this, uri);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,12 +74,12 @@ public class Scripts {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
public static void openByOtherApps(String path) {
|
public static void openByOtherApps(Uri uri) {
|
||||||
IntentUtil.viewFile(GlobalAppContext.get(), path, "text/plain", AppFileProvider.AUTHORITY);
|
IntentUtil.viewFile(GlobalAppContext.get(), uri, "text/plain", AppFileProvider.AUTHORITY);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void openByOtherApps(File file) {
|
public static void openByOtherApps(File file) {
|
||||||
openByOtherApps(file.getPath());
|
openByOtherApps(Uri.fromFile(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void createShortcut(ScriptFile scriptFile) {
|
public static void createShortcut(ScriptFile scriptFile) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package org.autojs.autojs.ui.edit;
|
|||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
@@ -61,6 +62,12 @@ EditActivity extends BaseActivity implements OnActivityResultDelegate.DelegateHo
|
|||||||
editFile(context, null, path);
|
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) {
|
public static void editFile(Context context, String name, String path) {
|
||||||
context.startActivity(new Intent(context, EditActivity_.class)
|
context.startActivity(new Intent(context, EditActivity_.class)
|
||||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||||
@@ -166,13 +173,13 @@ EditActivity extends BaseActivity implements OnActivityResultDelegate.DelegateHo
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ActionMode startActionMode(ActionMode.Callback callback, int type) {
|
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);
|
return super.startActionMode(callback, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ActionMode startActionMode(ActionMode.Callback callback) {
|
public ActionMode startActionMode(ActionMode.Callback callback) {
|
||||||
Log.d(LOG_TAG, "startActionMode: callback = " + callback );
|
Log.d(LOG_TAG, "startActionMode: callback = " + callback);
|
||||||
return super.startActionMode(callback);
|
return super.startActionMode(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -190,7 +190,7 @@ public class EditorMenu {
|
|||||||
|
|
||||||
private void startBuildApkActivity() {
|
private void startBuildApkActivity() {
|
||||||
BuildActivity_.intent(mContext)
|
BuildActivity_.intent(mContext)
|
||||||
.extra(BuildActivity.EXTRA_SOURCE, mEditorView.getFile().getPath())
|
.extra(BuildActivity.EXTRA_SOURCE, mEditorView.getUri().getPath())
|
||||||
.start();
|
.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import android.content.Context;
|
|||||||
import android.content.ContextWrapper;
|
import android.content.ContextWrapper;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
@@ -31,6 +32,7 @@ import com.stardust.autojs.execution.ScriptExecution;
|
|||||||
import com.stardust.pio.PFiles;
|
import com.stardust.pio.PFiles;
|
||||||
import com.stardust.util.BackPressedHandler;
|
import com.stardust.util.BackPressedHandler;
|
||||||
import com.stardust.util.Callback;
|
import com.stardust.util.Callback;
|
||||||
|
import com.stardust.util.IntentUtil;
|
||||||
import com.stardust.util.ViewUtils;
|
import com.stardust.util.ViewUtils;
|
||||||
|
|
||||||
import org.androidannotations.annotations.AfterViews;
|
import org.androidannotations.annotations.AfterViews;
|
||||||
@@ -116,7 +118,7 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
|||||||
DrawerLayout mDrawerLayout;
|
DrawerLayout mDrawerLayout;
|
||||||
|
|
||||||
private String mName;
|
private String mName;
|
||||||
private File mFile;
|
private Uri mUri;
|
||||||
private boolean mReadOnly = false;
|
private boolean mReadOnly = false;
|
||||||
private int mScriptExecutionId;
|
private int mScriptExecutionId;
|
||||||
private AutoCompletion mAutoCompletion;
|
private AutoCompletion mAutoCompletion;
|
||||||
@@ -179,8 +181,8 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public File getFile() {
|
public Uri getUri() {
|
||||||
return mFile;
|
return mUri;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Observable<String> handleIntent(Intent intent) {
|
public Observable<String> handleIntent(Intent intent) {
|
||||||
@@ -215,21 +217,26 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
|||||||
return Observable.just(content);
|
return Observable.just(content);
|
||||||
} else {
|
} else {
|
||||||
if (path == null) {
|
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) {
|
if (mName == null) {
|
||||||
mName = mFile.getName();
|
mName = PFiles.getNameWithoutExtension(mUri.getPath());
|
||||||
}
|
}
|
||||||
return loadFile(mFile);
|
return loadUri(mUri);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@SuppressLint("CheckResult")
|
@SuppressLint("CheckResult")
|
||||||
private Observable<String> loadFile(final File file) {
|
private Observable<String> loadUri(final Uri uri) {
|
||||||
mEditor.setProgress(true);
|
mEditor.setProgress(true);
|
||||||
return Observable.fromCallable(() -> PFiles.read(file))
|
return Observable.fromCallable(() -> PFiles.read(getContext().getContentResolver().openInputStream(uri)))
|
||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.doOnNext(s -> {
|
.doOnNext(s -> {
|
||||||
@@ -393,7 +400,8 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
|||||||
if (showMessage) {
|
if (showMessage) {
|
||||||
Snackbar.make(this, R.string.text_start_running, Snackbar.LENGTH_SHORT).show();
|
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) {
|
if (execution == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -414,7 +422,7 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
|||||||
public Observable<String> save() {
|
public Observable<String> save() {
|
||||||
return Observable.just(mEditor.getText())
|
return Observable.just(mEditor.getText())
|
||||||
.observeOn(Schedulers.io())
|
.observeOn(Schedulers.io())
|
||||||
.doOnNext(s -> PFiles.write(mFile, s))
|
.doOnNext(s -> PFiles.write(getContext().getContentResolver().openOutputStream(mUri), s))
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.doOnNext(s -> {
|
.doOnNext(s -> {
|
||||||
mEditor.markTextAsSaved();
|
mEditor.markTextAsSaved();
|
||||||
@@ -489,8 +497,9 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void openByOtherApps() {
|
public void openByOtherApps() {
|
||||||
if (mFile != null)
|
if (mUri != null) {
|
||||||
Scripts.openByOtherApps(mFile);
|
Scripts.openByOtherApps(mUri);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void beautifyCode() {
|
public void beautifyCode() {
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ public class DebugToolbarFragment extends ToolbarFragment implements DebugCallba
|
|||||||
mDebugger = DebuggerSingleton.get();
|
mDebugger = DebuggerSingleton.get();
|
||||||
mDebugger.setWeakDebugCallback(new WeakReference<>(this));
|
mDebugger.setWeakDebugCallback(new WeakReference<>(this));
|
||||||
setInterrupted(false);
|
setInterrupted(false);
|
||||||
mCurrentEditorSourceUrl = mInitialEditorSourceUrl = mEditorView.getFile().toString();
|
mCurrentEditorSourceUrl = mInitialEditorSourceUrl = mEditorView.getUri().toString();
|
||||||
mInitialEditorSource = mEditorView.getEditor().getText();
|
mInitialEditorSource = mEditorView.getEditor().getText();
|
||||||
setupEditor();
|
setupEditor();
|
||||||
ScriptExecution execution = mEditorView.run(false);
|
ScriptExecution execution = mEditorView.run(false);
|
||||||
|
|||||||
@@ -136,6 +136,8 @@ public class PFiles {
|
|||||||
return new String(bytes, encoding);
|
return new String(bytes, encoding);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new UncheckedIOException(e);
|
throw new UncheckedIOException(e);
|
||||||
|
} finally {
|
||||||
|
closeSilently(is);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -218,7 +220,7 @@ public class PFiles {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void write(FileOutputStream fileOutputStream, String text) {
|
public static void write(OutputStream fileOutputStream, String text) {
|
||||||
write(fileOutputStream, text, "utf-8");
|
write(fileOutputStream, text, "utf-8");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,9 +228,10 @@ public class PFiles {
|
|||||||
public static void write(OutputStream outputStream, String text, String encoding) {
|
public static void write(OutputStream outputStream, String text, String encoding) {
|
||||||
try {
|
try {
|
||||||
outputStream.write(text.getBytes(encoding));
|
outputStream.write(text.getBytes(encoding));
|
||||||
outputStream.close();
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new UncheckedIOException(e);
|
throw new UncheckedIOException(e);
|
||||||
|
} finally {
|
||||||
|
closeSilently(outputStream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -134,6 +134,25 @@ public class IntentUtil {
|
|||||||
return uri;
|
return uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean viewFile(Context context, Uri uri, String mimeType, String fileProviderAuthority) {
|
||||||
|
if (uri.getScheme().equals("file")) {
|
||||||
|
return viewFile(context, uri.getPath(), mimeType, fileProviderAuthority);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
context.startActivity(new Intent(Intent.ACTION_VIEW)
|
||||||
|
.setDataAndType(uri, mimeType)
|
||||||
|
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||||
|
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||||
|
.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION));
|
||||||
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean viewFile(Context context, String path, String mimeType, String fileProviderAuthority) {
|
public static boolean viewFile(Context context, String path, String mimeType, String fileProviderAuthority) {
|
||||||
try {
|
try {
|
||||||
Uri uri = getUriOfFile(context, path, fileProviderAuthority);
|
Uri uri = getUriOfFile(context, path, fileProviderAuthority);
|
||||||
|
|||||||
Reference in New Issue
Block a user