apply plugin retrolambda
This commit is contained in:
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -49,7 +49,7 @@
|
||||
<ConfirmationsSetting value="0" id="Add" />
|
||||
<ConfirmationsSetting value="0" id="Remove" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||
</component>
|
||||
<component name="ProjectType">
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
apply plugin: 'com.android.application'
|
||||
apply plugin: 'com.getkeepsafe.dexcount'
|
||||
apply plugin: 'me.tatarka.retrolambda'
|
||||
def AAVersion = '4.2.0'
|
||||
|
||||
android {
|
||||
@@ -30,8 +31,8 @@ android {
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_7
|
||||
targetCompatibility JavaVersion.VERSION_1_7
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
encoding = 'utf-8'
|
||||
}
|
||||
dataBinding {
|
||||
|
||||
2
app/proguard-rules.pro
vendored
2
app/proguard-rules.pro
vendored
@@ -25,6 +25,8 @@
|
||||
-dontwarn com.iwebpp.nodeandroid.**
|
||||
-dontwarn org.msgpack.core.**
|
||||
-dontwarn com.pushtorefresh.storio.**
|
||||
-dontwarn java.lang.invoke.*
|
||||
-dontwarn **$$Lambda$*
|
||||
|
||||
-keep class org.mozilla.javascript.** { *; }
|
||||
-keep class com.jecelyin.editor.** { *; }
|
||||
|
||||
@@ -8,8 +8,8 @@ import com.stardust.autojs.script.JavaScriptFileSource;
|
||||
import com.stardust.autojs.script.ScriptSource;
|
||||
import com.stardust.autojs.script.SequenceScriptSource;
|
||||
import com.stardust.autojs.script.StringScriptSource;
|
||||
import com.stardust.scriptdroid.script.PathChecker;
|
||||
import com.stardust.scriptdroid.script.Scripts;
|
||||
import com.stardust.scriptdroid.model.script.PathChecker;
|
||||
import com.stardust.scriptdroid.model.script.Scripts;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
|
||||
@@ -5,13 +5,11 @@ import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.text.TextUtils;
|
||||
import android.webkit.MimeTypeMap;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.scriptdroid.ui.BaseActivity;
|
||||
import com.stardust.scriptdroid.ui.common.ScriptOperations;
|
||||
import com.stardust.scriptdroid.ui.main.MainActivity;
|
||||
import com.stardust.scriptdroid.R;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
@@ -46,7 +44,7 @@ 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());
|
||||
String ext = PFiles.getExtension(uri.getScheme());
|
||||
if(TextUtils.isEmpty(ext)){
|
||||
ext = "js";
|
||||
}
|
||||
|
||||
@@ -4,10 +4,10 @@ import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.stardust.scriptdroid.script.PathChecker;
|
||||
import com.stardust.scriptdroid.model.script.PathChecker;
|
||||
import com.stardust.scriptdroid.external.CommonUtils;
|
||||
import com.stardust.scriptdroid.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.script.Scripts;
|
||||
import com.stardust.scriptdroid.model.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.model.script.Scripts;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/1/23.
|
||||
|
||||
@@ -11,8 +11,8 @@ import android.view.View;
|
||||
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.external.CommonUtils;
|
||||
import com.stardust.scriptdroid.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.script.StorageFileProvider;
|
||||
import com.stardust.scriptdroid.model.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.model.script.StorageFileProvider;
|
||||
import com.stardust.scriptdroid.ui.BaseActivity;
|
||||
import com.stardust.scriptdroid.ui.main.scripts.ScriptListView;
|
||||
import com.twofortyfouram.locale.sdk.client.ui.activity.AbstractAppCompatPluginActivity;
|
||||
@@ -46,12 +46,9 @@ public class TaskPrefEditActivity extends AbstractAppCompatPluginActivity {
|
||||
ScriptListView scriptList = (ScriptListView) findViewById(R.id.script_list);
|
||||
scriptList.setStorageFileProvider(mStorageFileProvider);
|
||||
scriptList.setCurrentDirectory(StorageFileProvider.DEFAULT_DIRECTORY);
|
||||
scriptList.setOnScriptFileClickListener(new ScriptListView.OnScriptFileClickListener() {
|
||||
@Override
|
||||
public void onScriptFileClick(View view, ScriptFile file) {
|
||||
mSelectedScriptFilePath = file.getPath();
|
||||
finish();
|
||||
}
|
||||
scriptList.setOnScriptFileClickListener((view, file) -> {
|
||||
mSelectedScriptFilePath = file.getPath();
|
||||
finish();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.external.CommonUtils;
|
||||
import com.stardust.scriptdroid.external.open.RunIntentActivity;
|
||||
@@ -41,7 +41,7 @@ public class ScriptWidget extends AppWidgetProvider {
|
||||
if (TextUtils.isEmpty(path) || widgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
|
||||
return false;
|
||||
}
|
||||
String name = PFile.getNameWithoutExtension(path);
|
||||
String name = PFiles.getNameWithoutExtension(path);
|
||||
int requestCode = ScriptWidgets.getRequestCodeForAppWidgetId(widgetId);
|
||||
Log.d(LOG_TAG, "updateWidget: id = " + widgetId + ", requestCode = " + requestCode + ", path = " + path);
|
||||
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
|
||||
|
||||
@@ -9,8 +9,8 @@ import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.script.StorageFileProvider;
|
||||
import com.stardust.scriptdroid.model.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.model.script.StorageFileProvider;
|
||||
import com.stardust.scriptdroid.ui.BaseActivity;
|
||||
import com.stardust.scriptdroid.ui.main.scripts.ScriptListView;
|
||||
|
||||
@@ -46,12 +46,9 @@ public class ScriptWidgetSettingsActivity extends BaseActivity {
|
||||
ScriptListView scriptList = (ScriptListView) findViewById(R.id.script_list);
|
||||
scriptList.setStorageFileProvider(mStorageFileProvider);
|
||||
scriptList.setCurrentDirectory(StorageFileProvider.DEFAULT_DIRECTORY);
|
||||
scriptList.setOnScriptFileClickListener(new ScriptListView.OnScriptFileClickListener() {
|
||||
@Override
|
||||
public void onScriptFileClick(View view, ScriptFile file) {
|
||||
mSelectedScriptFilePath = file.getPath();
|
||||
finish();
|
||||
}
|
||||
scriptList.setOnScriptFileClickListener((view, file) -> {
|
||||
mSelectedScriptFilePath = file.getPath();
|
||||
finish();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
package com.stardust.scriptdroid.script.sample;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.stardust.autojs.script.StringScriptSource;
|
||||
import com.stardust.scriptdroid.autojs.AutoJs;
|
||||
import com.stardust.util.AssetsCache;
|
||||
package com.stardust.scriptdroid.model.sample;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.stardust.scriptdroid.script.sample;
|
||||
package com.stardust.scriptdroid.model.sample;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetManager;
|
||||
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.scriptdroid.App;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -43,7 +43,7 @@ public class SampleFileManager {
|
||||
try {
|
||||
String[] samples = assets.list(path);
|
||||
for (String sample : samples) {
|
||||
group.add(new Sample(PFile.getNameWithoutExtension(sample), path + "/" + sample));
|
||||
group.add(new Sample(PFiles.getNameWithoutExtension(sample), path + "/" + sample));
|
||||
}
|
||||
return group;
|
||||
} catch (IOException e) {
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.stardust.scriptdroid.script.sample;
|
||||
package com.stardust.scriptdroid.model.sample;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.stardust.scriptdroid.script;
|
||||
package com.stardust.scriptdroid.model.script;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
@@ -1,13 +1,13 @@
|
||||
package com.stardust.scriptdroid.script;
|
||||
package com.stardust.scriptdroid.model.script;
|
||||
|
||||
import android.os.Environment;
|
||||
import android.renderscript.Script;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import com.stardust.autojs.script.AutoFileSource;
|
||||
import com.stardust.autojs.script.JavaScriptFileSource;
|
||||
import com.stardust.autojs.script.ScriptSource;
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
@@ -18,7 +18,7 @@ import java.util.ArrayList;
|
||||
* Created by Stardust on 2017/1/23.
|
||||
*/
|
||||
|
||||
public class ScriptFile extends File {
|
||||
public class ScriptFile extends PFile {
|
||||
|
||||
public static final int TYPE_UNKNOWN = 0;
|
||||
public static final int TYPE_AUTO = 1;
|
||||
@@ -26,57 +26,20 @@ public class ScriptFile extends File {
|
||||
|
||||
private int mType = -1;
|
||||
|
||||
private String mSimplifyPath;
|
||||
private String mSimplifiedName;
|
||||
|
||||
|
||||
public ScriptFile(String path) {
|
||||
super(path);
|
||||
init();
|
||||
}
|
||||
|
||||
public ScriptFile(String parent, String name) {
|
||||
super(parent, name);
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
mSimplifiedName = PFile.getNameWithoutExtension(getName());
|
||||
mSimplifyPath = getPath();
|
||||
if (mSimplifyPath.startsWith(Environment.getExternalStorageDirectory().getPath())) {
|
||||
mSimplifyPath = mSimplifyPath.substring(Environment.getExternalStorageDirectory().getPath().length());
|
||||
}
|
||||
}
|
||||
|
||||
public ScriptFile(ScriptFile parent, String child) {
|
||||
super(parent, child);
|
||||
init();
|
||||
}
|
||||
|
||||
public boolean renameTo(String newName) {
|
||||
if (isDirectory())
|
||||
return renameTo(new File(getParent(), newName));
|
||||
else
|
||||
return renameTo(new File(getParent(), newName + "." + getExtension()));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public ScriptFile renameAndReturnNewFile(String newName) {
|
||||
ScriptFile newFile = isDirectory() ? new ScriptFile(getParent(), newName) :
|
||||
new ScriptFile(getParent(), newName + "." + getExtension());
|
||||
if (renameTo(newFile)) {
|
||||
return newFile;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String getExtension() {
|
||||
return PFile.getExtension(getName());
|
||||
}
|
||||
|
||||
public String getSimplifiedPath() {
|
||||
return mSimplifyPath;
|
||||
public ScriptFile(File file) {
|
||||
super(file.getPath());
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
@@ -98,13 +61,8 @@ public class ScriptFile extends File {
|
||||
|
||||
@Override
|
||||
public ScriptFile[] listFiles() {
|
||||
return listFiles(new FileFilter() {
|
||||
@Override
|
||||
public boolean accept(File file) {
|
||||
return (file.getName().endsWith(".js") || file.getName().endsWith(".auto") ||
|
||||
file.isDirectory()) && !file.getName().startsWith(".");
|
||||
}
|
||||
});
|
||||
return listFiles(file -> (file.getName().endsWith(".js") || file.getName().endsWith(".auto") ||
|
||||
file.isDirectory()) && !file.getName().startsWith("."));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -132,14 +90,6 @@ public class ScriptFile extends File {
|
||||
|
||||
}
|
||||
|
||||
public String getSimplifiedName() {
|
||||
return mSimplifiedName;
|
||||
}
|
||||
|
||||
public boolean moveTo(ScriptFile to) {
|
||||
return renameTo(new File(to, getName()));
|
||||
}
|
||||
|
||||
public ScriptSource toSource() {
|
||||
if (getType() == TYPE_JAVA_SCRIPT) {
|
||||
return new JavaScriptFileSource(this);
|
||||
@@ -1,11 +1,10 @@
|
||||
package com.stardust.scriptdroid.script;
|
||||
package com.stardust.scriptdroid.model.script;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import com.stardust.autojs.engine.RhinoJavaScriptEngine;
|
||||
import com.stardust.autojs.execution.ExecutionConfig;
|
||||
import com.stardust.autojs.execution.ScriptExecution;
|
||||
import com.stardust.autojs.execution.ScriptExecutionListener;
|
||||
@@ -19,7 +18,7 @@ import com.stardust.scriptdroid.autojs.AutoJs;
|
||||
import com.stardust.scriptdroid.external.CommonUtils;
|
||||
import com.stardust.scriptdroid.external.shortcut.Shortcut;
|
||||
import com.stardust.scriptdroid.external.shortcut.ShortcutActivity;
|
||||
import com.stardust.scriptdroid.script.sample.Sample;
|
||||
import com.stardust.scriptdroid.model.sample.Sample;
|
||||
import com.stardust.scriptdroid.ui.edit.EditActivity;
|
||||
import com.stardust.util.AssetsCache;
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
package com.stardust.scriptdroid.script;
|
||||
package com.stardust.scriptdroid.model.script;
|
||||
|
||||
import android.os.Environment;
|
||||
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.scriptdroid.App;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.util.LimitedHashMap;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
|
||||
import java.io.FileFilter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -28,31 +30,33 @@ public class StorageFileProvider {
|
||||
public static final int CREATE = 1;
|
||||
public static final int CHANGE = 2;
|
||||
public static final int ALL = 3;
|
||||
public static final String DEFAULT_DIRECTORY_PATH = Environment.getExternalStorageDirectory() + App.getApp().getString(R.string.folder_name);
|
||||
public static final ScriptFile DEFAULT_DIRECTORY = new ScriptFile(DEFAULT_DIRECTORY_PATH);
|
||||
|
||||
public static class DirectoryChangeEvent {
|
||||
|
||||
|
||||
private final ScriptFile mDir;
|
||||
private final PFile mDir;
|
||||
private final int mChange;
|
||||
private final ScriptFile mFile;
|
||||
private final ScriptFile mNewFile;
|
||||
private final PFile mFile;
|
||||
private final PFile mNewFile;
|
||||
|
||||
public DirectoryChangeEvent(ScriptFile dir, int change, ScriptFile file) {
|
||||
public DirectoryChangeEvent(PFile dir, int change, PFile file) {
|
||||
this(dir, change, file, file);
|
||||
}
|
||||
|
||||
public DirectoryChangeEvent(ScriptFile directory) {
|
||||
public DirectoryChangeEvent(PFile directory) {
|
||||
this(directory, ALL, null);
|
||||
}
|
||||
|
||||
public DirectoryChangeEvent(ScriptFile dir, int change, ScriptFile oldFile, ScriptFile newFile) {
|
||||
public DirectoryChangeEvent(PFile dir, int change, PFile oldFile, PFile newFile) {
|
||||
mDir = dir;
|
||||
mChange = change;
|
||||
mFile = oldFile;
|
||||
mNewFile = newFile;
|
||||
}
|
||||
|
||||
public ScriptFile getDir() {
|
||||
public PFile getDir() {
|
||||
return mDir;
|
||||
}
|
||||
|
||||
@@ -60,26 +64,49 @@ public class StorageFileProvider {
|
||||
return mChange;
|
||||
}
|
||||
|
||||
public ScriptFile getFile() {
|
||||
public PFile getFile() {
|
||||
return mFile;
|
||||
}
|
||||
|
||||
public ScriptFile getNewFile() {
|
||||
public PFile getNewFile() {
|
||||
return mNewFile;
|
||||
}
|
||||
}
|
||||
|
||||
public static final String DEFAULT_DIRECTORY_PATH = Environment.getExternalStorageDirectory() + App.getApp().getString(R.string.folder_name);
|
||||
public static final ScriptFile DEFAULT_DIRECTORY = new ScriptFile(DEFAULT_DIRECTORY_PATH);
|
||||
|
||||
|
||||
private static StorageFileProvider defaultProvider = new StorageFileProvider();
|
||||
|
||||
public static StorageFileProvider getDefault() {
|
||||
return defaultProvider;
|
||||
}
|
||||
|
||||
private static StorageFileProvider externalStorageProvider;
|
||||
private static final FileFilter SCRIPT_FILTER = file ->
|
||||
file.isDirectory() || file.getName().endsWith(".js") || file.getName().endsWith(".auto");
|
||||
private static final StorageFileProvider DEFAULT_PROVIDER = new StorageFileProvider(DEFAULT_DIRECTORY, 10, SCRIPT_FILTER);
|
||||
|
||||
private EventBus mDirectoryEventBus = new EventBus();
|
||||
private LimitedHashMap<String, List<PFile>> mPFileCache;
|
||||
private PFile mInitialDirectory;
|
||||
|
||||
public StorageFileProvider(PFile initialDirectory, int cacheSize, FileFilter fileFilter) {
|
||||
mInitialDirectory = initialDirectory;
|
||||
mFileFilter = fileFilter;
|
||||
mPFileCache = new LimitedHashMap<>(cacheSize);
|
||||
}
|
||||
|
||||
private FileFilter mFileFilter;
|
||||
|
||||
public StorageFileProvider(String path, int cacheSize) {
|
||||
this(new PFile(path), cacheSize);
|
||||
}
|
||||
|
||||
public StorageFileProvider(PFile initialDirectory, int cacheSize) {
|
||||
mInitialDirectory = initialDirectory;
|
||||
mPFileCache = new LimitedHashMap<>(cacheSize);
|
||||
}
|
||||
|
||||
public StorageFileProvider() {
|
||||
this(DEFAULT_DIRECTORY, 10);
|
||||
}
|
||||
|
||||
public static StorageFileProvider getDefault() {
|
||||
return DEFAULT_PROVIDER;
|
||||
}
|
||||
|
||||
public static StorageFileProvider getExternalStorageProvider() {
|
||||
if (externalStorageProvider == null) {
|
||||
@@ -88,31 +115,13 @@ public class StorageFileProvider {
|
||||
return externalStorageProvider;
|
||||
}
|
||||
|
||||
private EventBus mDirectoryEventBus = new EventBus();
|
||||
private LimitedHashMap<String, List<ScriptFile>> mScriptFileCache;
|
||||
private ScriptFile mInitialDirectory;
|
||||
private ScriptFile[] mInitialDirectoryScriptFiles;
|
||||
|
||||
public StorageFileProvider(String path, int cacheSize) {
|
||||
this(new ScriptFile(path), cacheSize);
|
||||
}
|
||||
|
||||
public StorageFileProvider(ScriptFile initialDirectory, int cacheSize) {
|
||||
mInitialDirectory = initialDirectory;
|
||||
mScriptFileCache = new LimitedHashMap<>(cacheSize);
|
||||
}
|
||||
|
||||
public StorageFileProvider() {
|
||||
this(DEFAULT_DIRECTORY, 10);
|
||||
}
|
||||
|
||||
public void notifyDirectoryChanged(ScriptFile directory) {
|
||||
public void notifyDirectoryChanged(PFile directory) {
|
||||
clearCache(directory);
|
||||
mDirectoryEventBus.post(new DirectoryChangeEvent(directory));
|
||||
}
|
||||
|
||||
public void notifyFileChanged(ScriptFile dir, ScriptFile oldFile, ScriptFile newFile) {
|
||||
List<ScriptFile> files = getScriptFilesFromCache(dir);
|
||||
public void notifyFileChanged(PFile dir, PFile oldFile, PFile newFile) {
|
||||
List<PFile> files = getFilesFromCache(dir);
|
||||
if (files == null)
|
||||
return;
|
||||
int i = files.indexOf(oldFile);
|
||||
@@ -123,8 +132,8 @@ public class StorageFileProvider {
|
||||
}
|
||||
|
||||
|
||||
public void notifyFileRemoved(ScriptFile dir, ScriptFile file) {
|
||||
List<ScriptFile> files = getScriptFilesFromCache(dir);
|
||||
public void notifyFileRemoved(PFile dir, PFile file) {
|
||||
List<PFile> files = getFilesFromCache(dir);
|
||||
if (files == null)
|
||||
return;
|
||||
if (files.remove(file)) {
|
||||
@@ -132,8 +141,8 @@ public class StorageFileProvider {
|
||||
}
|
||||
}
|
||||
|
||||
public void notifyFileCreated(ScriptFile dir, ScriptFile file) {
|
||||
List<ScriptFile> files = getScriptFilesFromCache(dir);
|
||||
public void notifyFileCreated(PFile dir, PFile file) {
|
||||
List<PFile> files = getFilesFromCache(dir);
|
||||
if (files == null)
|
||||
return;
|
||||
files.add(0, file);
|
||||
@@ -141,60 +150,63 @@ public class StorageFileProvider {
|
||||
}
|
||||
|
||||
public void notifyStoragePermissionGranted() {
|
||||
mScriptFileCache.clear();
|
||||
mInitialDirectoryScriptFiles = null;
|
||||
mPFileCache.clear();
|
||||
mDirectoryEventBus.post(new DirectoryChangeEvent(mInitialDirectory));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void refreshAll() {
|
||||
Map<String, ScriptFile> files = (Map<String, ScriptFile>) mScriptFileCache.clone();
|
||||
mScriptFileCache.clear();
|
||||
mInitialDirectoryScriptFiles = null;
|
||||
Map<String, PFile> files = (Map<String, PFile>) mPFileCache.clone();
|
||||
mPFileCache.clear();
|
||||
mDirectoryEventBus.post(new DirectoryChangeEvent(mInitialDirectory));
|
||||
for (Map.Entry<String, ScriptFile> file : files.entrySet()) {
|
||||
mDirectoryEventBus.post(new DirectoryChangeEvent(new ScriptFile(file.getKey())));
|
||||
for (Map.Entry<String, PFile> file : files.entrySet()) {
|
||||
mDirectoryEventBus.post(new DirectoryChangeEvent(new PFile(file.getKey())));
|
||||
}
|
||||
}
|
||||
|
||||
public ScriptFile getInitialDirectory() {
|
||||
public PFile getInitialDirectory() {
|
||||
return mInitialDirectory;
|
||||
}
|
||||
|
||||
public Observable<ScriptFile> getInitialDirectoryScriptFiles() {
|
||||
return getDirectoryScriptFiles(mInitialDirectory);
|
||||
public Observable<PFile> getInitialDirectoryFiles() {
|
||||
return getDirectoryFiles(mInitialDirectory);
|
||||
}
|
||||
|
||||
public Observable<ScriptFile> getDirectoryScriptFiles(ScriptFile directory) {
|
||||
List<ScriptFile> scriptFiles = getScriptFilesFromCache(directory);
|
||||
if (scriptFiles == null) {
|
||||
public Observable<PFile> getDirectoryFiles(PFile directory) {
|
||||
List<PFile> PFiles = getFilesFromCache(directory);
|
||||
if (PFiles == null) {
|
||||
return listFiles(directory);
|
||||
}
|
||||
return Observable.fromIterable(scriptFiles);
|
||||
return Observable.fromIterable(PFiles);
|
||||
}
|
||||
|
||||
private void clearCache(ScriptFile directory) {
|
||||
mScriptFileCache.remove(directory.getPath());
|
||||
private void clearCache(PFile directory) {
|
||||
mPFileCache.remove(directory.getPath());
|
||||
}
|
||||
|
||||
|
||||
private Observable<ScriptFile> listFiles(ScriptFile directory) {
|
||||
private Observable<PFile> listFiles(PFile directory) {
|
||||
return Observable.just(directory)
|
||||
.flatMap(new Function<ScriptFile, ObservableSource<ScriptFile>>() {
|
||||
.flatMap(new Function<PFile, ObservableSource<PFile>>() {
|
||||
@Override
|
||||
public ObservableSource<ScriptFile> apply(@NonNull ScriptFile dir) throws Exception {
|
||||
ScriptFile[] scriptFiles = dir.listFiles();
|
||||
if (scriptFiles == null) {
|
||||
public ObservableSource<PFile> apply(@NonNull PFile dir) throws Exception {
|
||||
PFile[] files;
|
||||
if (mFileFilter == null) {
|
||||
files = dir.listFiles();
|
||||
} else {
|
||||
files = dir.listFiles(mFileFilter);
|
||||
}
|
||||
if (files == null) {
|
||||
return Observable.empty();
|
||||
}
|
||||
mScriptFileCache.put(dir.getPath(), new ArrayList<>(Arrays.asList(scriptFiles)));
|
||||
return Observable.fromArray(scriptFiles);
|
||||
mPFileCache.put(dir.getPath(), new ArrayList<>(Arrays.asList(files)));
|
||||
return Observable.fromArray(files);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private List<ScriptFile> getScriptFilesFromCache(ScriptFile directory) {
|
||||
return mScriptFileCache.get(directory.getPath());
|
||||
private List<PFile> getFilesFromCache(PFile directory) {
|
||||
return mPFileCache.get(directory.getPath());
|
||||
}
|
||||
|
||||
|
||||
@@ -8,12 +8,12 @@ import com.google.gson.JsonNull;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.stardust.autojs.execution.ScriptExecution;
|
||||
import com.stardust.autojs.script.StringScriptSource;
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.scriptdroid.App;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.autojs.AutoJs;
|
||||
import com.stardust.scriptdroid.script.Scripts;
|
||||
import com.stardust.scriptdroid.script.StorageFileProvider;
|
||||
import com.stardust.scriptdroid.model.script.Scripts;
|
||||
import com.stardust.scriptdroid.model.script.StorageFileProvider;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/5/11.
|
||||
@@ -83,7 +83,7 @@ public class SublimeResponseHandler implements Handler {
|
||||
if (TextUtils.isEmpty(name)) {
|
||||
name = "[" + viewId + "]";
|
||||
} else {
|
||||
name = PFile.getNameWithoutExtension(name);
|
||||
name = PFiles.getNameWithoutExtension(name);
|
||||
}
|
||||
mScriptExecutions.put(viewId, Scripts.run(new StringScriptSource("<remote>:" + name, script)));
|
||||
}
|
||||
@@ -108,13 +108,13 @@ public class SublimeResponseHandler implements Handler {
|
||||
if (TextUtils.isEmpty(name)) {
|
||||
name = "untitled";
|
||||
}
|
||||
name = PFile.getNameWithoutExtension(name);
|
||||
name = PFiles.getNameWithoutExtension(name);
|
||||
if (!name.endsWith(".js")) {
|
||||
name = "<remote>" + name + ".js";
|
||||
} else {
|
||||
name = "<remote>" + name;
|
||||
}
|
||||
PFile.write(StorageFileProvider.DEFAULT_DIRECTORY_PATH + name, script);
|
||||
PFiles.write(StorageFileProvider.DEFAULT_DIRECTORY_PATH + name, script);
|
||||
App.getApp().getUiHandler().toast(R.string.text_script_save_successfully);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.stardust.app.OnActivityResultDelegate;
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
@@ -64,7 +64,7 @@ public abstract class DrawableSaver {
|
||||
public void run() {
|
||||
try {
|
||||
OutputStream outputStream = mContext.openFileOutput(mName, Context.MODE_PRIVATE);
|
||||
PFile.write(inputStream, outputStream);
|
||||
PFiles.write(inputStream, outputStream);
|
||||
outputStream.close();
|
||||
inputStream.close();
|
||||
callback.run();
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
package com.stardust.scriptdroid.tool;
|
||||
|
||||
import com.stardust.scriptdroid.script.StorageFileProvider;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/5/22.
|
||||
*/
|
||||
|
||||
public class Logcat {
|
||||
|
||||
private static final String LOG_PATH = StorageFileProvider.DEFAULT_DIRECTORY_PATH + "log.txt";
|
||||
private static final String DISABLE_PATH = StorageFileProvider.DEFAULT_DIRECTORY_PATH + "disable_log.txt";
|
||||
private static Process sLogSavingProcess;
|
||||
|
||||
public static void startLogSavingIfNeeded() {
|
||||
if (sLogSavingProcess != null)
|
||||
return;
|
||||
if (new File(DISABLE_PATH).exists())
|
||||
return;
|
||||
try {
|
||||
sLogSavingProcess = Runtime.getRuntime().exec("logcat -f " + LOG_PATH);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void deleteLogFile() {
|
||||
new File(LOG_PATH).delete();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.stardust.scriptdroid.tool;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.Observer;
|
||||
import io.reactivex.annotations.NonNull;
|
||||
import io.reactivex.disposables.Disposable;
|
||||
|
||||
@@ -11,8 +11,8 @@ import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.stardust.app.DialogUtils;
|
||||
import com.stardust.scriptdroid.App;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.script.Scripts;
|
||||
import com.stardust.scriptdroid.model.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.model.script.Scripts;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
@@ -14,13 +14,14 @@ import com.afollestad.materialdialogs.DialogAction;
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.stardust.app.DialogUtils;
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.pio.UncheckedIOException;
|
||||
import com.stardust.scriptdroid.App;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.script.Scripts;
|
||||
import com.stardust.scriptdroid.script.StorageFileProvider;
|
||||
import com.stardust.scriptdroid.script.sample.Sample;
|
||||
import com.stardust.scriptdroid.model.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.model.script.Scripts;
|
||||
import com.stardust.scriptdroid.model.script.StorageFileProvider;
|
||||
import com.stardust.scriptdroid.model.sample.Sample;
|
||||
import com.stardust.theme.dialog.ThemeColorMaterialDialogBuilder;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
@@ -61,12 +62,9 @@ public class ScriptOperations {
|
||||
|
||||
public void newScriptFileForScript(final String script) {
|
||||
showFileNameInputDialog("", "js")
|
||||
.subscribe(new Consumer<String>() {
|
||||
@Override
|
||||
public void accept(@io.reactivex.annotations.NonNull String input) throws Exception {
|
||||
createScriptFile(getCurrentDirectoryPath() + input + ".js", script, false);
|
||||
}
|
||||
});
|
||||
.subscribe(input ->
|
||||
createScriptFile(getCurrentDirectoryPath() + input + ".js", script, false)
|
||||
);
|
||||
}
|
||||
|
||||
private String getCurrentDirectoryPath() {
|
||||
@@ -78,10 +76,10 @@ public class ScriptOperations {
|
||||
}
|
||||
|
||||
public void createScriptFile(String path, String script, boolean edit) {
|
||||
if (PFile.createIfNotExists(path)) {
|
||||
if (PFiles.createIfNotExists(path)) {
|
||||
if (script != null) {
|
||||
try {
|
||||
PFile.write(path, script);
|
||||
PFiles.write(path, script);
|
||||
} catch (UncheckedIOException e) {
|
||||
showMessage(R.string.text_file_write_fail);
|
||||
return;
|
||||
@@ -106,13 +104,13 @@ public class ScriptOperations {
|
||||
}
|
||||
|
||||
public Observable<String> importFile(final String pathFrom) {
|
||||
return showFileNameInputDialog(PFile.getNameWithoutExtension(pathFrom), PFile.getExtension(pathFrom))
|
||||
return showFileNameInputDialog(PFiles.getNameWithoutExtension(pathFrom), PFiles.getExtension(pathFrom))
|
||||
.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 + "." + PFile.getExtension(pathFrom);
|
||||
if (PFile.copy(pathFrom, pathTo)) {
|
||||
final String pathTo = getCurrentDirectoryPath() + s + "." + PFiles.getExtension(pathFrom);
|
||||
if (PFiles.copy(pathFrom, pathTo)) {
|
||||
showMessage(R.string.text_import_succeed);
|
||||
} else {
|
||||
showMessage(R.string.text_import_fail);
|
||||
@@ -124,13 +122,13 @@ public class ScriptOperations {
|
||||
}
|
||||
|
||||
public Observable<String> importFile(String prefix, final InputStream inputStream, final String ext) {
|
||||
return showFileNameInputDialog(PFile.getNameWithoutExtension(prefix), ext)
|
||||
return showFileNameInputDialog(PFiles.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 + "." + ext;
|
||||
if (PFile.copyStream(inputStream, pathTo)) {
|
||||
if (PFiles.copyStream(inputStream, pathTo)) {
|
||||
showMessage(R.string.text_import_succeed);
|
||||
} else {
|
||||
showMessage(R.string.text_import_fail);
|
||||
@@ -207,7 +205,7 @@ public class ScriptOperations {
|
||||
|
||||
public Observable<String> importSample(Sample sample) {
|
||||
try {
|
||||
return importFile(sample.name, mContext.getAssets().open(sample.path), PFile.getExtension(sample.path));
|
||||
return importFile(sample.name, mContext.getAssets().open(sample.path), PFiles.getExtension(sample.path));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
showMessage(R.string.text_import_fail);
|
||||
@@ -218,17 +216,14 @@ public class ScriptOperations {
|
||||
public Observable<Boolean> rename(final ScriptFile file) {
|
||||
final ScriptFile oldFile = new ScriptFile(file.getPath());
|
||||
String originalName = file.getSimplifiedName();
|
||||
return showNameInputDialog(originalName, new InputCallback(file.isDirectory() ? null : PFile.getExtension(file.getName()),
|
||||
return showNameInputDialog(originalName, new InputCallback(file.isDirectory() ? null : PFiles.getExtension(file.getName()),
|
||||
originalName))
|
||||
.map(new Function<String, Boolean>() {
|
||||
@Override
|
||||
public Boolean apply(@io.reactivex.annotations.NonNull String newName) throws Exception {
|
||||
ScriptFile newFile = file.renameAndReturnNewFile(newName);
|
||||
if (newFile != null) {
|
||||
mStorageFileProvider.notifyFileChanged(mCurrentDirectory, oldFile, newFile);
|
||||
}
|
||||
return newFile != null;
|
||||
.map(newName -> {
|
||||
PFile newFile = file.renameAndReturnNewFile(newName);
|
||||
if (newFile != null) {
|
||||
mStorageFileProvider.notifyFileChanged(mCurrentDirectory, oldFile, newFile);
|
||||
}
|
||||
return newFile != null;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -241,7 +236,7 @@ public class ScriptOperations {
|
||||
Observable.fromPublisher(new Publisher<Boolean>() {
|
||||
@Override
|
||||
public void subscribe(Subscriber<? super Boolean> s) {
|
||||
s.onNext(PFile.deleteRecursively(scriptFile));
|
||||
s.onNext(PFiles.deleteRecursively(scriptFile));
|
||||
}
|
||||
}).subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
|
||||
@@ -20,11 +20,10 @@ import android.widget.FrameLayout;
|
||||
|
||||
import com.afollestad.materialdialogs.DialogAction;
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.theme.dialog.ThemeColorMaterialDialogBuilder;
|
||||
|
||||
import org.apache.commons.lang3.StringEscapeUtils;
|
||||
import org.jdeferred.Deferred;
|
||||
import org.jdeferred.DoneCallback;
|
||||
import org.jdeferred.impl.DeferredObject;
|
||||
@@ -43,7 +42,6 @@ import io.reactivex.functions.Consumer;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import io.reactivex.subjects.PublishSubject;
|
||||
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
|
||||
import retrofit2.http.GET;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/9/27.
|
||||
@@ -106,7 +104,7 @@ public class CodeMirrorEditor extends FrameLayout {
|
||||
try {
|
||||
sAvailableThemes = getResources().getAssets().list("editor/codemirror/theme");
|
||||
for (int i = 0; i < sAvailableThemes.length; i++) {
|
||||
sAvailableThemes[i] = PFile.getNameWithoutExtension(sAvailableThemes[i]);
|
||||
sAvailableThemes[i] = PFiles.getNameWithoutExtension(sAvailableThemes[i]);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
@@ -204,7 +202,7 @@ public class CodeMirrorEditor extends FrameLayout {
|
||||
Observable.fromCallable(new Callable<String>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
return PFile.read(file);
|
||||
return PFiles.read(file);
|
||||
}
|
||||
}).subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
|
||||
@@ -10,9 +10,8 @@ import android.view.MenuItem;
|
||||
import com.afollestad.materialdialogs.DialogAction;
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.model.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.ui.BaseActivity;
|
||||
import com.stardust.scriptdroid.ui.edit.completion.CodeCompletionBar;
|
||||
import com.stardust.theme.dialog.ThemeColorMaterialDialogBuilder;
|
||||
|
||||
import org.androidannotations.annotations.AfterViews;
|
||||
|
||||
@@ -6,7 +6,7 @@ import android.text.InputType;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.autojs.AutoJs;
|
||||
import com.stardust.theme.dialog.ThemeColorMaterialDialogBuilder;
|
||||
@@ -178,7 +178,7 @@ public class EditorMenu {
|
||||
Observable.zip(mEditor.getText(), mEditor.getLineCount(), new BiFunction<String, Integer, String>() {
|
||||
@Override
|
||||
public String apply(@NonNull String text, @NonNull Integer lineCount) throws Exception {
|
||||
String size = PFile.getHumanReadableSize(text.length());
|
||||
String size = PFiles.getHumanReadableSize(text.length());
|
||||
return String.format(Locale.getDefault(), mContext.getString(R.string.format_editor_info),
|
||||
text.length(), lineCount, size);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.res.ColorStateList;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.design.widget.Snackbar;
|
||||
@@ -17,9 +16,9 @@ import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.stardust.autojs.engine.JavaScriptEngine;
|
||||
import com.stardust.autojs.execution.ScriptExecution;
|
||||
import com.stardust.autojs.script.JavaScriptFileSource;
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.script.Scripts;
|
||||
import com.stardust.scriptdroid.model.script.Scripts;
|
||||
import com.stardust.scriptdroid.ui.edit.completion.CodeCompletions;
|
||||
import com.stardust.scriptdroid.ui.edit.completion.CodeCompletionBar;
|
||||
import com.stardust.scriptdroid.ui.edit.completion.InputMethodEnhancedBarColors;
|
||||
@@ -40,7 +39,7 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.functions.Consumer;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
import static com.stardust.scriptdroid.script.Scripts.ACTION_ON_EXECUTION_FINISHED;
|
||||
import static com.stardust.scriptdroid.model.script.Scripts.ACTION_ON_EXECUTION_FINISHED;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/9/28.
|
||||
@@ -261,7 +260,7 @@ public class EditorView extends FrameLayout {
|
||||
.doOnNext(new Consumer<String>() {
|
||||
@Override
|
||||
public void accept(@io.reactivex.annotations.NonNull String s) throws Exception {
|
||||
PFile.write(mFile, s);
|
||||
PFiles.write(mFile, s);
|
||||
}
|
||||
})
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
|
||||
@@ -17,7 +17,7 @@ import com.stardust.autojs.engine.JavaScriptEngine;
|
||||
import com.stardust.autojs.execution.ScriptExecution;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.autojs.AutoJs;
|
||||
import com.stardust.scriptdroid.script.sample.Sample;
|
||||
import com.stardust.scriptdroid.model.sample.Sample;
|
||||
import com.stardust.scriptdroid.ui.BaseActivity;
|
||||
import com.stardust.scriptdroid.ui.common.ScriptOperations;
|
||||
import com.stardust.scriptdroid.ui.help.HelpCatalogueActivity;
|
||||
@@ -32,8 +32,8 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.annotations.NonNull;
|
||||
import io.reactivex.functions.Consumer;
|
||||
|
||||
import static com.stardust.scriptdroid.script.Scripts.ACTION_ON_EXECUTION_FINISHED;
|
||||
import static com.stardust.scriptdroid.script.Scripts.EXTRA_EXCEPTION_MESSAGE;
|
||||
import static com.stardust.scriptdroid.model.script.Scripts.ACTION_ON_EXECUTION_FINISHED;
|
||||
import static com.stardust.scriptdroid.model.script.Scripts.EXTRA_EXCEPTION_MESSAGE;
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,7 +19,7 @@ import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.accessibility.AccessibilityService;
|
||||
import com.stardust.scriptdroid.autojs.AutoJs;
|
||||
import com.stardust.scriptdroid.autojs.record.GlobalRecorder;
|
||||
import com.stardust.scriptdroid.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.model.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.tool.AccessibilityServiceTool;
|
||||
import com.stardust.scriptdroid.ui.floating.layoutinspector.LayoutBoundsFloatyWindow;
|
||||
import com.stardust.scriptdroid.ui.floating.layoutinspector.LayoutHierarchyFloatyWindow;
|
||||
|
||||
@@ -10,7 +10,7 @@ import com.stardust.enhancedfloaty.FloatyService;
|
||||
import com.stardust.enhancedfloaty.ResizableFloaty;
|
||||
import com.stardust.enhancedfloaty.ResizableFloatyWindow;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.model.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.ui.edit.EditActivity_;
|
||||
import com.stardust.scriptdroid.ui.edit.EditorView;
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.webkit.WebView;
|
||||
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.scriptdroid.ui.BaseActivity;
|
||||
import com.stardust.theme.ThemeColorManagerCompat;
|
||||
import com.stardust.util.UnderuseExecutors;
|
||||
@@ -52,7 +52,7 @@ public class DocumentationActivity extends BaseActivity {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
mDocumentation = PFile.read(getAssets().open("help/" + path));
|
||||
mDocumentation = PFiles.read(getAssets().open("help/" + path));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
import android.workground.WrapContentLinearLayoutManager;
|
||||
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.scriptdroid.App;
|
||||
import com.stardust.scriptdroid.ui.BaseActivity;
|
||||
import com.google.gson.Gson;
|
||||
@@ -202,7 +202,7 @@ public class HelpCatalogueActivity extends BaseActivity {
|
||||
private static void readCatalogues(String path) {
|
||||
Map<String, List<Item>> fromJson;
|
||||
try {
|
||||
String json = PFile.read(App.getApp().getAssets().open(path));
|
||||
String json = PFiles.read(App.getApp().getAssets().open(path));
|
||||
Gson gson = new Gson();
|
||||
Type type = new TypeToken<Map<String, List<Item>>>() {
|
||||
}.getType();
|
||||
|
||||
@@ -6,7 +6,7 @@ import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.webkit.WebView;
|
||||
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.ui.BaseActivity;
|
||||
|
||||
@@ -43,7 +43,7 @@ public class LocalWebViewActivity extends BaseActivity {
|
||||
String path = intent.getStringExtra("path");
|
||||
if (path != null)
|
||||
try {
|
||||
mHtml = PFile.read(getAssets().open("help/" + path));
|
||||
mHtml = PFiles.read(getAssets().open("help/" + path));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
mHtml = getString(R.string.text_load_failed);
|
||||
|
||||
@@ -2,8 +2,11 @@ package com.stardust.scriptdroid.ui.main;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.RequiresApi;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.design.widget.TabLayout;
|
||||
import android.support.v4.app.Fragment;
|
||||
@@ -14,20 +17,18 @@ import android.support.v7.widget.Toolbar;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
|
||||
import com.afollestad.materialdialogs.DialogAction;
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.qq.e.comm.DownloadService;
|
||||
import com.stardust.app.FragmentPagerAdapterBuilder;
|
||||
import com.stardust.app.OnActivityResultDelegate;
|
||||
import com.stardust.enhancedfloaty.FloatyService;
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.scriptdroid.BuildConfig;
|
||||
import com.stardust.scriptdroid.Pref;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.autojs.AutoJs;
|
||||
import com.stardust.scriptdroid.ui.common.NotAskAgainDialog;
|
||||
import com.stardust.scriptdroid.ui.floating.FloatyWindowManger;
|
||||
import com.stardust.scriptdroid.script.StorageFileProvider;
|
||||
import com.stardust.scriptdroid.model.script.StorageFileProvider;
|
||||
import com.stardust.scriptdroid.ui.main.community.CommunityFragment_;
|
||||
import com.stardust.scriptdroid.ui.main.doc.OnlineDocsFragment_;
|
||||
import com.stardust.scriptdroid.ui.main.scripts.MyScriptListFragment_;
|
||||
@@ -47,6 +48,8 @@ import org.androidannotations.annotations.Click;
|
||||
import org.androidannotations.annotations.EActivity;
|
||||
import org.androidannotations.annotations.ViewById;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@EActivity(R.layout.activity_main)
|
||||
public class MainActivity extends BaseActivity implements OnActivityResultDelegate.DelegateHost, BackPressedHandler.HostActivity {
|
||||
|
||||
@@ -91,7 +94,7 @@ public class MainActivity extends BaseActivity implements OnActivityResultDelega
|
||||
}
|
||||
new CommonMarkdownView.DialogBuilder(this)
|
||||
.padding(36, 0, 36, 0)
|
||||
.markdown(PFile.read(getResources().openRawResource(R.raw.annunciation)))
|
||||
.markdown(PFiles.read(getResources().openRawResource(R.raw.annunciation)))
|
||||
.title(R.string.text_annunciation)
|
||||
.positiveText(R.string.ok)
|
||||
.canceledOnTouchOutside(false)
|
||||
@@ -118,12 +121,9 @@ public class MainActivity extends BaseActivity implements OnActivityResultDelega
|
||||
.content(R.string.explain_accessibility_permission)
|
||||
.positiveText(R.string.text_go_to_setting)
|
||||
.negativeText(R.string.text_cancel)
|
||||
.onPositive(new MaterialDialog.SingleButtonCallback() {
|
||||
@Override
|
||||
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
|
||||
AccessibilityServiceTool.enableAccessibilityService();
|
||||
}
|
||||
}).show();
|
||||
.onPositive((dialog, which) ->
|
||||
AccessibilityServiceTool.enableAccessibilityService()
|
||||
).show();
|
||||
}
|
||||
|
||||
private void setUpToolbar() {
|
||||
@@ -150,13 +150,10 @@ public class MainActivity extends BaseActivity implements OnActivityResultDelega
|
||||
}
|
||||
|
||||
private void setUpViewPagerFragmentBehaviors() {
|
||||
mPagerAdapter.setOnFragmentInstantiateListener(new FragmentPagerAdapterBuilder.OnFragmentInstantiateListener() {
|
||||
@Override
|
||||
public void OnInstantiate(int pos, Fragment fragment) {
|
||||
((ViewPagerFragment) fragment).setFab(mFab);
|
||||
if (pos == mViewPager.getCurrentItem()) {
|
||||
((ViewPagerFragment) fragment).onPageShow();
|
||||
}
|
||||
mPagerAdapter.setOnFragmentInstantiateListener((pos, fragment) -> {
|
||||
((ViewPagerFragment) fragment).setFab(mFab);
|
||||
if (pos == mViewPager.getCurrentItem()) {
|
||||
((ViewPagerFragment) fragment).onPageShow();
|
||||
}
|
||||
});
|
||||
mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
|
||||
@@ -208,10 +205,14 @@ public class MainActivity extends BaseActivity implements OnActivityResultDelega
|
||||
mActivityResultMediator.onActivityResult(requestCode, resultCode, data);
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
StorageFileProvider.getDefault().notifyStoragePermissionGranted();
|
||||
if (Arrays.asList(permissions).contains(Manifest.permission.READ_EXTERNAL_STORAGE)
|
||||
&& checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
|
||||
StorageFileProvider.getDefault().notifyStoragePermissionGranted();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -86,12 +86,7 @@ public class DrawerFragment extends android.support.v4.app.Fragment {
|
||||
super.onCreate(savedInstanceState);
|
||||
mConnectionStateDisposable = SublimePluginService.getConnectionState()
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.doOnNext(new Consumer<Boolean>() {
|
||||
@Override
|
||||
public void accept(@io.reactivex.annotations.NonNull Boolean connected) throws Exception {
|
||||
mConnectionItem.getSwitchCompat().setChecked(connected);
|
||||
}
|
||||
})
|
||||
.doOnNext(connected -> mConnectionItem.getSwitchCompat().setChecked(connected))
|
||||
.subscribe();
|
||||
EventBus.getDefault().register(this);
|
||||
}
|
||||
@@ -177,20 +172,12 @@ public class DrawerFragment extends android.support.v4.app.Fragment {
|
||||
}
|
||||
|
||||
private void enableAccessibilityServiceByRootIfNeeded() {
|
||||
Observable.fromCallable(new Callable<Boolean>() {
|
||||
@Override
|
||||
public Boolean call() throws Exception {
|
||||
return Pref.enableAccessibilityServiceByRoot() && !isAccessibilityServiceEnabled();
|
||||
}
|
||||
})
|
||||
Observable.fromCallable(() -> Pref.enableAccessibilityServiceByRoot() && !isAccessibilityServiceEnabled())
|
||||
.subscribeOn(Schedulers.computation())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Consumer<Boolean>() {
|
||||
@Override
|
||||
public void accept(@io.reactivex.annotations.NonNull Boolean needed) throws Exception {
|
||||
if (needed) {
|
||||
enableAccessibilityServiceByRoot();
|
||||
}
|
||||
.subscribe(needed -> {
|
||||
if (needed) {
|
||||
enableAccessibilityServiceByRoot();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -211,27 +198,16 @@ public class DrawerFragment extends android.support.v4.app.Fragment {
|
||||
String host = Pref.getServerAddressOrDefault(WifiTool.getWifiAddress(getActivity()));
|
||||
new MaterialDialog.Builder(getActivity())
|
||||
.title(R.string.text_server_address)
|
||||
.input("", host, new MaterialDialog.InputCallback() {
|
||||
@Override
|
||||
public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {
|
||||
Pref.saveServerAddress(input.toString());
|
||||
connectToRemote(input.toString());
|
||||
}
|
||||
.input("", host, (dialog, input) -> {
|
||||
Pref.saveServerAddress(input.toString());
|
||||
connectToRemote(input.toString());
|
||||
})
|
||||
.neutralText(R.string.text_help)
|
||||
.onNeutral(new MaterialDialog.SingleButtonCallback() {
|
||||
@Override
|
||||
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
|
||||
mConnectionItem.getSwitchCompat().setChecked(false, false);
|
||||
IntentUtil.browse(getActivity(), URL_SUBLIME_PLUGIN_HELP);
|
||||
}
|
||||
})
|
||||
.cancelListener(new DialogInterface.OnCancelListener() {
|
||||
@Override
|
||||
public void onCancel(DialogInterface dialog) {
|
||||
mConnectionItem.getSwitchCompat().setChecked(false, false);
|
||||
}
|
||||
.onNeutral((dialog, which) -> {
|
||||
mConnectionItem.getSwitchCompat().setChecked(false, false);
|
||||
IntentUtil.browse(getActivity(), URL_SUBLIME_PLUGIN_HELP);
|
||||
})
|
||||
.cancelListener(dialog -> mConnectionItem.getSwitchCompat().setChecked(false, false))
|
||||
.show();
|
||||
}
|
||||
|
||||
@@ -310,23 +286,15 @@ public class DrawerFragment extends android.support.v4.app.Fragment {
|
||||
|
||||
private void enableAccessibilityServiceByRoot() {
|
||||
mAccessibilityServiceItem.setProgress(true);
|
||||
Observable.fromCallable(new Callable<Boolean>() {
|
||||
@Override
|
||||
public Boolean call() throws Exception {
|
||||
return AccessibilityServiceTool.enableAccessibilityServiceByRootAndWaitFor(4000);
|
||||
}
|
||||
})
|
||||
Observable.fromCallable(() -> AccessibilityServiceTool.enableAccessibilityServiceByRootAndWaitFor(4000))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Consumer<Boolean>() {
|
||||
@Override
|
||||
public void accept(@io.reactivex.annotations.NonNull Boolean succeed) throws Exception {
|
||||
if (!succeed) {
|
||||
Toast.makeText(getContext(), R.string.text_enable_accessibitliy_service_by_root_failed, Toast.LENGTH_SHORT).show();
|
||||
AccessibilityServiceTool.goToAccessibilitySetting();
|
||||
}
|
||||
mAccessibilityServiceItem.setProgress(false);
|
||||
.subscribe(succeed -> {
|
||||
if (!succeed) {
|
||||
Toast.makeText(getContext(), R.string.text_enable_accessibitliy_service_by_root_failed, Toast.LENGTH_SHORT).show();
|
||||
AccessibilityServiceTool.goToAccessibilitySetting();
|
||||
}
|
||||
mAccessibilityServiceItem.setProgress(false);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,22 +1,14 @@
|
||||
package com.stardust.scriptdroid.ui.main.scripts;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.view.View;
|
||||
|
||||
import com.afollestad.materialdialogs.DialogAction;
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.script.Scripts;
|
||||
import com.stardust.scriptdroid.script.StorageFileProvider;
|
||||
import com.stardust.scriptdroid.model.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.model.script.Scripts;
|
||||
import com.stardust.scriptdroid.tool.SimpleObserver;
|
||||
import com.stardust.scriptdroid.ui.common.ScriptLoopDialog;
|
||||
import com.stardust.scriptdroid.ui.common.ScriptOperations;
|
||||
import com.stardust.scriptdroid.ui.floating.EditorFloaty;
|
||||
import com.stardust.scriptdroid.ui.main.FloatingActionMenu;
|
||||
import com.stardust.scriptdroid.ui.main.ViewPagerFragment;
|
||||
import com.stardust.util.BackPressedHandler;
|
||||
@@ -24,17 +16,8 @@ import com.stardust.util.BackPressedHandler;
|
||||
import org.androidannotations.annotations.AfterViews;
|
||||
import org.androidannotations.annotations.EFragment;
|
||||
import org.androidannotations.annotations.ViewById;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.reactivestreams.Subscriber;
|
||||
|
||||
import butterknife.OnClick;
|
||||
import butterknife.Optional;
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.Disposable;
|
||||
import io.reactivex.functions.Consumer;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import okhttp3.Response;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/3/13.
|
||||
|
||||
@@ -17,10 +17,11 @@ import android.widget.PopupMenu;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.script.Scripts;
|
||||
import com.stardust.scriptdroid.script.StorageFileProvider;
|
||||
import com.stardust.scriptdroid.model.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.model.script.Scripts;
|
||||
import com.stardust.scriptdroid.model.script.StorageFileProvider;
|
||||
import com.stardust.scriptdroid.ui.common.ScriptLoopDialog;
|
||||
import com.stardust.scriptdroid.ui.common.ScriptOperations;
|
||||
import com.stardust.scriptdroid.ui.viewmodel.ScriptList;
|
||||
@@ -120,7 +121,7 @@ public class ScriptListView extends SwipeRefreshLayout implements SwipeRefreshLa
|
||||
addView(mScriptListView);
|
||||
initScriptListRecyclerView();
|
||||
mStorageFileProvider = StorageFileProvider.getDefault();
|
||||
setCurrentDirectory(mStorageFileProvider.getInitialDirectory());
|
||||
setCurrentDirectory(new ScriptFile(mStorageFileProvider.getInitialDirectory()));
|
||||
mStorageFileProvider.registerDirectoryChangeListener(this);
|
||||
}
|
||||
|
||||
@@ -149,29 +150,18 @@ public class ScriptListView extends SwipeRefreshLayout implements SwipeRefreshLa
|
||||
|
||||
private void loadScriptList() {
|
||||
mScriptList.clear();
|
||||
mStorageFileProvider.getDirectoryScriptFiles(mCurrentDirectory)
|
||||
mStorageFileProvider.getDirectoryFiles(mCurrentDirectory)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.collectInto(mScriptList, new BiConsumer<ScriptList, ScriptFile>() {
|
||||
@Override
|
||||
public void accept(ScriptList list, ScriptFile file) throws Exception {
|
||||
list.add(file);
|
||||
}
|
||||
.collectInto(mScriptList, (list, file) -> {
|
||||
mScriptList.add(new ScriptFile(file));
|
||||
})
|
||||
.observeOn(Schedulers.computation())
|
||||
.doOnSuccess(new Consumer<ScriptList>() {
|
||||
@Override
|
||||
public void accept(@NonNull ScriptList list) throws Exception {
|
||||
list.sort();
|
||||
}
|
||||
})
|
||||
.doOnSuccess(ScriptList::sort)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Consumer<ScriptList>() {
|
||||
@Override
|
||||
public void accept(@NonNull ScriptList list) throws Exception {
|
||||
mScriptList = list;
|
||||
mScriptListAdapter.notifyDataSetChanged();
|
||||
setRefreshing(false);
|
||||
}
|
||||
.subscribe(list -> {
|
||||
mScriptList = list;
|
||||
mScriptListAdapter.notifyDataSetChanged();
|
||||
setRefreshing(false);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -356,7 +346,7 @@ public class ScriptListView extends SwipeRefreshLayout implements SwipeRefreshLa
|
||||
public void bind(ScriptFile file, int position) {
|
||||
mScriptFile = file;
|
||||
mName.setText(file.getSimplifiedName());
|
||||
mDesc.setText(PFile.getHumanReadableSize(file.length()));
|
||||
mDesc.setText(PFiles.getHumanReadableSize(file.length()));
|
||||
if (file.getType() == ScriptFile.TYPE_JAVA_SCRIPT) {
|
||||
mFirstChar.setText("J");
|
||||
mFirstCharBackground.setColor(ResourcesCompat.getColor(getResources(), R.color.color_j, getContext().getTheme()));
|
||||
|
||||
@@ -17,7 +17,7 @@ import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.stardust.scriptdroid.BuildConfig;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.network.entity.VersionInfo;
|
||||
import com.stardust.scriptdroid.script.StorageFileProvider;
|
||||
import com.stardust.scriptdroid.model.script.StorageFileProvider;
|
||||
import com.stardust.scriptdroid.tool.IntentTool;
|
||||
import com.stardust.util.DownloadTask;
|
||||
import com.stardust.util.IntentUtil;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.stardust.scriptdroid.ui.viewmodel;
|
||||
|
||||
import com.stardust.scriptdroid.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.model.script.ScriptFile;
|
||||
import com.stardust.util.FileSorter;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
|
||||
module.exports = function(__runtime__, scope){
|
||||
scope.files = com.stardust.pio.PFile;
|
||||
var files = com.stardust.pio.PFiles;
|
||||
scope.files = files;
|
||||
scope.open = function(path, mode, encoding, bufferSize){
|
||||
if(arguments.length == 1){
|
||||
return com.stardust.pio.PFile.open(path);
|
||||
return files.open(path);
|
||||
}else if(arguments.length == 2){
|
||||
return com.stardust.pio.PFile.open(path, mode);
|
||||
return files.open(path, mode);
|
||||
}else if(arguments.length == 3){
|
||||
return com.stardust.pio.PFile.open(path, mode, encoding);
|
||||
return files.open(path, mode, encoding);
|
||||
}else if(arguments.length == 4){
|
||||
return com.stardust.pio.PFile.open(path, mode, encoding, bufferSize);
|
||||
return files.open(path, mode, encoding, bufferSize);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
var __require__ = require;
|
||||
var __nodejs_modules__ = {
|
||||
'websocket' : com.iwebpp.wspp.WebSocket,
|
||||
'websocketserver': com.iwebpp.wspp.WebSocketServer,
|
||||
'net': com.iwebpp.node.net.TCP,
|
||||
'tcp': com.iwebpp.node.net.TCP,
|
||||
'udt': com.iwebpp.node.net.UDT,
|
||||
'readable': com.iwebpp.node.stream.Readable2,
|
||||
'writable': com.iwebpp.node.stream.Writable2,
|
||||
'duplex': com.iwebpp.node.stream.Duplex,
|
||||
'transform': com.iwebpp.node.stream.Transform,
|
||||
'passthrough': com.iwebpp.node.stream.PassThrough,
|
||||
'dns': com.iwebpp.node.Dns,
|
||||
'url': com.iwebpp.node.Url,
|
||||
}
|
||||
|
||||
var require = function(module){
|
||||
if (module === 'http'){
|
||||
return {
|
||||
get: function(url, listener){
|
||||
return com.iwebpp.node.http.http.get(NodeCurrentContext, url, listener);
|
||||
},
|
||||
request: function(url, listener) {
|
||||
return com.iwebpp.node.http.http.request(NodeCurrentContext, url, listener);
|
||||
},
|
||||
createServer: function(listener) {
|
||||
return com.iwebpp.node.http.http.createServer(NodeCurrentContext, listener);
|
||||
},
|
||||
};
|
||||
}
|
||||
if (module === 'httpp'){
|
||||
return {
|
||||
get: function(url, listener){
|
||||
return com.iwebpp.node.http.httpp.get(NodeCurrentContext, url, listener);
|
||||
},
|
||||
request: function(url, listener) {
|
||||
return com.iwebpp.node.http.httpp.request(NodeCurrentContext, url, listener);
|
||||
},
|
||||
createServer: function(listener) {
|
||||
return com.iwebpp.node.http.httpp.createServer(NodeCurrentContext, listener);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return __require__(module);
|
||||
};
|
||||
@@ -8,7 +8,7 @@ import com.stardust.autojs.rhino.RhinoAndroidHelper;
|
||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||
import com.stardust.autojs.script.JavaScriptSource;
|
||||
import com.stardust.autojs.script.StringScriptSource;
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.pio.UncheckedIOException;
|
||||
|
||||
import org.mozilla.javascript.Context;
|
||||
@@ -106,7 +106,7 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
|
||||
|
||||
private String readInitScript() {
|
||||
try {
|
||||
return PFile.read(mAndroidContext.getAssets().open("javascript_engine_init.js"));
|
||||
return PFiles.read(mAndroidContext.getAssets().open("javascript_engine_init.js"));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
@@ -7,11 +7,8 @@ import android.util.Log;
|
||||
import com.stardust.autojs.runtime.api.AbstractShell;
|
||||
import com.stardust.autojs.runtime.api.ProcessShell;
|
||||
import com.stardust.autojs.core.inputevent.InputDevices;
|
||||
import com.stardust.autojs.runtime.api.Shell;
|
||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||
import com.stardust.autojs.script.AutoFileSource;
|
||||
import com.stardust.concurrent.VolatileBox;
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@@ -71,7 +68,7 @@ public class RootAutomatorEngine extends ScriptEngine.AbstractScriptEngine<AutoF
|
||||
|
||||
public static String getExecutablePath(Context context) {
|
||||
File tmp = new File(context.getCacheDir(), "root_automator");
|
||||
PFile.copyAsset(context, ROOT_AUTOMATOR_EXECUTABLE_ASSET, tmp.getAbsolutePath());
|
||||
PFiles.copyAsset(context, ROOT_AUTOMATOR_EXECUTABLE_ASSET, tmp.getAbsolutePath());
|
||||
return tmp.getAbsolutePath();
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ import com.stardust.util.IntentUtil;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.List;
|
||||
|
||||
import static com.stardust.pio.PFile.getExtension;
|
||||
import static com.stardust.pio.PFiles.getExtension;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/2.
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
package com.stardust.autojs.script;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.Reader;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/8/2.
|
||||
@@ -17,7 +14,7 @@ public class AutoFileSource extends ScriptSource {
|
||||
private File mFile;
|
||||
|
||||
public AutoFileSource(File file) {
|
||||
super(PFile.getNameWithoutExtension(file.getAbsolutePath()));
|
||||
super(PFiles.getNameWithoutExtension(file.getAbsolutePath()));
|
||||
mFile = file;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.stardust.autojs.script;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.pio.UncheckedIOException;
|
||||
|
||||
import java.io.File;
|
||||
@@ -20,7 +20,7 @@ public class JavaScriptFileSource extends JavaScriptSource {
|
||||
private String mScript;
|
||||
|
||||
public JavaScriptFileSource(File file) {
|
||||
super(PFile.getNameWithoutExtension(file.getName()));
|
||||
super(PFiles.getNameWithoutExtension(file.getName()));
|
||||
mFile = file;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ public class JavaScriptFileSource extends JavaScriptSource {
|
||||
@Override
|
||||
public String getScript() {
|
||||
if (mScript == null)
|
||||
mScript = PFile.read(mFile);
|
||||
mScript = PFiles.read(mFile);
|
||||
return mScript;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.stardust.autojs.script;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.pio.UncheckedIOException;
|
||||
|
||||
import org.mozilla.javascript.Function;
|
||||
@@ -102,7 +102,7 @@ public class JsBeautifier {
|
||||
InputStream is = mContext.getAssets().open(mBeautifyJsPath);
|
||||
if (mScriptable == null)
|
||||
mScriptable = mScriptContext.initSafeStandardObjects();
|
||||
mJsBeautifyFunction = mScriptContext.compileFunction(mScriptable, PFile.read(is), "<js_beautify>", 1, null);
|
||||
mJsBeautifyFunction = mScriptContext.compileFunction(mScriptable, PFiles.read(is), "<js_beautify>", 1, null);
|
||||
} catch (IOException e) {
|
||||
exitContext();
|
||||
throw new UncheckedIOException(e);
|
||||
|
||||
@@ -7,9 +7,8 @@ buildscript {
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:2.3.3'
|
||||
classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.6.4'
|
||||
classpath 'me.tatarka:gradle-retrolambda:3.7.0'
|
||||
|
||||
// NOTE: Do not place your application dependencies here; they belong
|
||||
// in the individual module build.gradle files
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,358 +1,133 @@
|
||||
package com.stardust.pio;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetManager;
|
||||
import android.os.Environment;
|
||||
import android.provider.MediaStore;
|
||||
|
||||
import com.stardust.util.Func1;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Locale;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/1.
|
||||
* Created by Stardust on 2017/10/19.
|
||||
*/
|
||||
|
||||
public class PFile {
|
||||
public class PFile extends File {
|
||||
|
||||
private static final String TAG = "PFile";
|
||||
private String mSimplifyPath;
|
||||
private String mSimplifiedName;
|
||||
private String mExtension;
|
||||
|
||||
static final int DEFAULT_BUFFER_SIZE = 8192;
|
||||
static final String DEFAULT_ENCODING = Charset.defaultCharset().name();
|
||||
|
||||
public static PFile open(String path, String mode, String encoding, int bufferSize) {
|
||||
switch (mode) {
|
||||
case "r":
|
||||
return new PReadableTextFile(path, encoding, bufferSize);
|
||||
case "w":
|
||||
return new PWritableTextFile(path, encoding, bufferSize, false);
|
||||
case "a":
|
||||
return new PWritableTextFile(path, encoding, bufferSize, true);
|
||||
}
|
||||
return null;
|
||||
public PFile(@NonNull String pathname) {
|
||||
super(pathname);
|
||||
init();
|
||||
}
|
||||
|
||||
public static PFile open(String path, String mode, String encoding) {
|
||||
return open(path, mode, encoding, DEFAULT_BUFFER_SIZE);
|
||||
public PFile(String parent, @NonNull String child) {
|
||||
super(parent, child);
|
||||
init();
|
||||
}
|
||||
|
||||
public static PFile open(String path, String mode) {
|
||||
return open(path, mode, DEFAULT_ENCODING, DEFAULT_BUFFER_SIZE);
|
||||
public PFile(File parent, @NonNull String child) {
|
||||
super(parent, child);
|
||||
init();
|
||||
}
|
||||
|
||||
public static PFile open(String path) {
|
||||
return open(path, "r", DEFAULT_ENCODING, DEFAULT_BUFFER_SIZE);
|
||||
public PFile(@NonNull URI uri) {
|
||||
super(uri);
|
||||
init();
|
||||
}
|
||||
|
||||
public static boolean create(String path) {
|
||||
try {
|
||||
return new File(path).createNewFile();
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
|
||||
private void init() {
|
||||
mSimplifiedName = PFiles.getNameWithoutExtension(getName());
|
||||
mSimplifyPath = getPath();
|
||||
if (mSimplifyPath.startsWith(Environment.getExternalStorageDirectory().getPath())) {
|
||||
mSimplifyPath = mSimplifyPath.substring(Environment.getExternalStorageDirectory().getPath().length());
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean createIfNotExists(String path) {
|
||||
ensureDir(path);
|
||||
File file = new File(path);
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
return file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
public boolean renameTo(String newName) {
|
||||
if (isDirectory())
|
||||
return renameTo(new File(getParent(), newName));
|
||||
else
|
||||
return renameTo(new File(getParent(), newName + "." + getExtension()));
|
||||
}
|
||||
|
||||
public static boolean exists(String path) {
|
||||
return new File(path).exists();
|
||||
}
|
||||
|
||||
public static boolean ensureDir(String path) {
|
||||
int i = path.lastIndexOf("\\");
|
||||
if (i < 0)
|
||||
i = path.lastIndexOf("/");
|
||||
if (i >= 0) {
|
||||
String folder = path.substring(0, i);
|
||||
File file = new File(folder);
|
||||
if (file.exists())
|
||||
return true;
|
||||
return file.mkdirs();
|
||||
@Nullable
|
||||
public PFile renameAndReturnNewFile(String newName) {
|
||||
PFile newFile = isDirectory() ? new PFile(getParent(), newName) :
|
||||
new PFile(getParent(), newName + "." + getExtension());
|
||||
if (renameTo(newFile)) {
|
||||
return newFile;
|
||||
} else {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String read(String path, String encoding) {
|
||||
return read(new File(path), encoding);
|
||||
}
|
||||
|
||||
public static String read(String path) {
|
||||
return read(new File(path));
|
||||
}
|
||||
|
||||
|
||||
public static String read(File file, String encoding) {
|
||||
try {
|
||||
return read(new FileInputStream(file), encoding);
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
public String getExtension() {
|
||||
if (mExtension == null) {
|
||||
mExtension = PFiles.getExtension(getName());
|
||||
}
|
||||
return mExtension;
|
||||
}
|
||||
|
||||
public static String read(File file) {
|
||||
return read(file, "utf-8");
|
||||
public String getSimplifiedPath() {
|
||||
return mSimplifyPath;
|
||||
}
|
||||
|
||||
public static String read(InputStream is, String encoding) {
|
||||
try {
|
||||
byte[] bytes = new byte[is.available()];
|
||||
is.read(bytes);
|
||||
return new String(bytes, encoding);
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
@Override
|
||||
public PFile getParentFile() {
|
||||
String p = this.getParent();
|
||||
if (p == null)
|
||||
return null;
|
||||
return new PFile(p);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PFile[] listFiles() {
|
||||
String ss[] = list();
|
||||
if (ss == null) return null;
|
||||
PFile[] files = new PFile[ss.length];
|
||||
for (int i = 0; i < ss.length; i++) {
|
||||
files[i] = new PFile(ss[i]);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
public static String read(InputStream inputStream) {
|
||||
return read(inputStream, "utf-8");
|
||||
@Override
|
||||
public PFile[] listFiles(FilenameFilter filter) {
|
||||
String ss[] = list();
|
||||
if (ss == null) return null;
|
||||
ArrayList<PFile> files = new ArrayList<>();
|
||||
for (String s : ss)
|
||||
if ((filter == null) || filter.accept(this, s))
|
||||
files.add(new PFile(this, s));
|
||||
return files.toArray(new PFile[files.size()]);
|
||||
}
|
||||
|
||||
public static boolean copyRaw(Context context, int rawId, String path) {
|
||||
InputStream is = context.getResources().openRawResource(rawId);
|
||||
return copyStream(is, path);
|
||||
}
|
||||
|
||||
public static boolean copyStream(InputStream is, String path) {
|
||||
if (!ensureDir(path))
|
||||
return false;
|
||||
File file = new File(path);
|
||||
try {
|
||||
if (!file.exists())
|
||||
if (!file.createNewFile())
|
||||
return false;
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
write(is, fos);
|
||||
return true;
|
||||
} catch (IOException | UncheckedIOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
@Override
|
||||
public PFile[] listFiles(FileFilter filter) {
|
||||
String ss[] = list();
|
||||
if (ss == null) return null;
|
||||
ArrayList<PFile> files = new ArrayList<>();
|
||||
for (String s : ss) {
|
||||
PFile f = new PFile(this, s);
|
||||
if ((filter == null) || filter.accept(f))
|
||||
files.add(f);
|
||||
}
|
||||
return files.toArray(new PFile[files.size()]);
|
||||
|
||||
}
|
||||
|
||||
public static void write(InputStream is, OutputStream os) {
|
||||
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
|
||||
try {
|
||||
while (is.available() > 0) {
|
||||
int n = is.read(buffer);
|
||||
os.write(buffer, 0, n);
|
||||
}
|
||||
is.close();
|
||||
os.close();
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
public String getSimplifiedName() {
|
||||
return mSimplifiedName;
|
||||
}
|
||||
|
||||
|
||||
public static void write(String path, String text) {
|
||||
write(new File(path), text);
|
||||
public boolean moveTo(PFile to) {
|
||||
return renameTo(new File(to, getName()));
|
||||
}
|
||||
|
||||
public static void write(String path, String text, String encoding) {
|
||||
try {
|
||||
write(new FileOutputStream(path), text, encoding);
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void write(File file, String text) {
|
||||
try {
|
||||
write(new FileOutputStream(file), text);
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void write(FileOutputStream fileOutputStream, String text) {
|
||||
write(fileOutputStream, text, "utf-8");
|
||||
}
|
||||
|
||||
|
||||
public static void write(OutputStream outputStream, String text, String encoding) {
|
||||
try {
|
||||
outputStream.write(text.getBytes(encoding));
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean copy(String pathFrom, String pathTo) {
|
||||
try {
|
||||
return copyStream(new FileInputStream(pathFrom), pathTo);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean copyAsset(Context context, String assetFile, String path) {
|
||||
try {
|
||||
return copyStream(context.getAssets().open(assetFile), path);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static String renameWithoutExtensionAndReturnNewPath(String path, String newName) {
|
||||
File file = new File(path);
|
||||
File newFile = new File(file.getParent(), newName + "." + getExtension(file.getName()));
|
||||
file.renameTo(newFile);
|
||||
return newFile.getAbsolutePath();
|
||||
}
|
||||
|
||||
public static boolean renameWithoutExtension(String path, String newName) {
|
||||
File file = new File(path);
|
||||
File newFile = new File(file.getParent(), newName + "." + getExtension(file.getName()));
|
||||
return file.renameTo(newFile);
|
||||
}
|
||||
|
||||
public static boolean rename(String path, String newName) {
|
||||
File f = new File(path);
|
||||
return f.renameTo(new File(f.getParent(), newName));
|
||||
}
|
||||
|
||||
public static boolean move(String path, String newPath) {
|
||||
File f = new File(path);
|
||||
return f.renameTo(new File(newPath));
|
||||
}
|
||||
|
||||
public static String getExtension(String fileName) {
|
||||
int i = fileName.lastIndexOf('.');
|
||||
if (i < 0 || i + 1 >= fileName.length() - 1)
|
||||
return "";
|
||||
return fileName.substring(i + 1);
|
||||
}
|
||||
|
||||
public static String generateNotExistingPath(String path, String extension) {
|
||||
if (!new File(path + extension).exists())
|
||||
return path + extension;
|
||||
int i = 0;
|
||||
while (true) {
|
||||
String pathI = path + "(" + i + ")" + extension;
|
||||
if (!new File(pathI).exists())
|
||||
return pathI;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getName(String filePath) {
|
||||
return new File(filePath).getName();
|
||||
}
|
||||
|
||||
public static String getNameWithoutExtension(String filePath) {
|
||||
String fileName = getName(filePath);
|
||||
int b = fileName.lastIndexOf('.');
|
||||
if (b < 0)
|
||||
b = fileName.length();
|
||||
fileName = fileName.substring(0, b);
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public static File copyAssetToTmpFile(Context context, String path) {
|
||||
String extension = "." + getExtension(path);
|
||||
String name = getNameWithoutExtension(path);
|
||||
if (name.length() < 5) {
|
||||
name += name.hashCode();
|
||||
}
|
||||
try {
|
||||
File tmpFile = File.createTempFile(name, extension, context.getCacheDir());
|
||||
copyAsset(context, path, tmpFile.getPath());
|
||||
return tmpFile;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean deleteRecursively(File file) {
|
||||
if (file.isFile())
|
||||
return file.delete();
|
||||
for (File child : file.listFiles()) {
|
||||
if (!deleteRecursively(child))
|
||||
return false;
|
||||
}
|
||||
return file.delete();
|
||||
}
|
||||
|
||||
public static boolean remove(String path) {
|
||||
return new File(path).delete();
|
||||
}
|
||||
|
||||
public static boolean removeDir(String path) {
|
||||
return deleteRecursively(new File(path));
|
||||
}
|
||||
|
||||
public static String getSdcardPath() {
|
||||
return Environment.getExternalStorageDirectory().getPath();
|
||||
}
|
||||
|
||||
public static String readAsset(AssetManager assets, String path) {
|
||||
try {
|
||||
return read(assets.open(path));
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String[] listDir(String path) {
|
||||
File file = new File(path);
|
||||
return file.list();
|
||||
}
|
||||
|
||||
public static String[] listDir(String path, final Func1<String, Boolean> filter) {
|
||||
final File file = new File(path);
|
||||
return file.list(new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File dir, String name) {
|
||||
return filter.call(name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static boolean isFile(String path) {
|
||||
return new File(path).isFile();
|
||||
}
|
||||
|
||||
public static boolean isDir(String path) {
|
||||
return new File(path).isDirectory();
|
||||
}
|
||||
|
||||
public static boolean isEmptyDir(String path) {
|
||||
File file = new File(path);
|
||||
return file.isDirectory() && file.list().length == 0;
|
||||
}
|
||||
|
||||
public static String join(String parent, String child) {
|
||||
return new File(parent, child).getPath();
|
||||
}
|
||||
|
||||
public static String getHumanReadableSize(long bytes) {
|
||||
int unit = 1024;
|
||||
if (bytes < unit) return bytes + " B";
|
||||
int exp = (int) (Math.log(bytes) / Math.log(unit));
|
||||
String pre = "KMGTPE".substring(exp - 1, exp);
|
||||
return String.format(Locale.getDefault(), "%.1f %sB", bytes / Math.pow(unit, exp), pre);
|
||||
}
|
||||
}
|
||||
|
||||
357
common/src/main/java/com/stardust/pio/PFiles.java
Normal file
357
common/src/main/java/com/stardust/pio/PFiles.java
Normal file
@@ -0,0 +1,357 @@
|
||||
package com.stardust.pio;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetManager;
|
||||
import android.os.Environment;
|
||||
|
||||
import com.stardust.util.Func1;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/1.
|
||||
*/
|
||||
|
||||
public class PFiles {
|
||||
|
||||
private static final String TAG = "PFiles";
|
||||
|
||||
static final int DEFAULT_BUFFER_SIZE = 8192;
|
||||
static final String DEFAULT_ENCODING = Charset.defaultCharset().name();
|
||||
|
||||
public static Object open(String path, String mode, String encoding, int bufferSize) {
|
||||
switch (mode) {
|
||||
case "r":
|
||||
return new PReadableTextFile(path, encoding, bufferSize);
|
||||
case "w":
|
||||
return new PWritableTextFile(path, encoding, bufferSize, false);
|
||||
case "a":
|
||||
return new PWritableTextFile(path, encoding, bufferSize, true);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Object open(String path, String mode, String encoding) {
|
||||
return open(path, mode, encoding, DEFAULT_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public static Object open(String path, String mode) {
|
||||
return open(path, mode, DEFAULT_ENCODING, DEFAULT_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public static Object open(String path) {
|
||||
return open(path, "r", DEFAULT_ENCODING, DEFAULT_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public static boolean create(String path) {
|
||||
try {
|
||||
return new File(path).createNewFile();
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean createIfNotExists(String path) {
|
||||
ensureDir(path);
|
||||
File file = new File(path);
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
return file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean exists(String path) {
|
||||
return new File(path).exists();
|
||||
}
|
||||
|
||||
public static boolean ensureDir(String path) {
|
||||
int i = path.lastIndexOf("\\");
|
||||
if (i < 0)
|
||||
i = path.lastIndexOf("/");
|
||||
if (i >= 0) {
|
||||
String folder = path.substring(0, i);
|
||||
File file = new File(folder);
|
||||
if (file.exists())
|
||||
return true;
|
||||
return file.mkdirs();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static String read(String path, String encoding) {
|
||||
return read(new File(path), encoding);
|
||||
}
|
||||
|
||||
public static String read(String path) {
|
||||
return read(new File(path));
|
||||
}
|
||||
|
||||
|
||||
public static String read(File file, String encoding) {
|
||||
try {
|
||||
return read(new FileInputStream(file), encoding);
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String read(File file) {
|
||||
return read(file, "utf-8");
|
||||
}
|
||||
|
||||
public static String read(InputStream is, String encoding) {
|
||||
try {
|
||||
byte[] bytes = new byte[is.available()];
|
||||
is.read(bytes);
|
||||
return new String(bytes, encoding);
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String read(InputStream inputStream) {
|
||||
return read(inputStream, "utf-8");
|
||||
}
|
||||
|
||||
public static boolean copyRaw(Context context, int rawId, String path) {
|
||||
InputStream is = context.getResources().openRawResource(rawId);
|
||||
return copyStream(is, path);
|
||||
}
|
||||
|
||||
public static boolean copyStream(InputStream is, String path) {
|
||||
if (!ensureDir(path))
|
||||
return false;
|
||||
File file = new File(path);
|
||||
try {
|
||||
if (!file.exists())
|
||||
if (!file.createNewFile())
|
||||
return false;
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
write(is, fos);
|
||||
return true;
|
||||
} catch (IOException | UncheckedIOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void write(InputStream is, OutputStream os) {
|
||||
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
|
||||
try {
|
||||
while (is.available() > 0) {
|
||||
int n = is.read(buffer);
|
||||
os.write(buffer, 0, n);
|
||||
}
|
||||
is.close();
|
||||
os.close();
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void write(String path, String text) {
|
||||
write(new File(path), text);
|
||||
}
|
||||
|
||||
public static void write(String path, String text, String encoding) {
|
||||
try {
|
||||
write(new FileOutputStream(path), text, encoding);
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void write(File file, String text) {
|
||||
try {
|
||||
write(new FileOutputStream(file), text);
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void write(FileOutputStream fileOutputStream, String text) {
|
||||
write(fileOutputStream, text, "utf-8");
|
||||
}
|
||||
|
||||
|
||||
public static void write(OutputStream outputStream, String text, String encoding) {
|
||||
try {
|
||||
outputStream.write(text.getBytes(encoding));
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean copy(String pathFrom, String pathTo) {
|
||||
try {
|
||||
return copyStream(new FileInputStream(pathFrom), pathTo);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean copyAsset(Context context, String assetFile, String path) {
|
||||
try {
|
||||
return copyStream(context.getAssets().open(assetFile), path);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static String renameWithoutExtensionAndReturnNewPath(String path, String newName) {
|
||||
File file = new File(path);
|
||||
File newFile = new File(file.getParent(), newName + "." + getExtension(file.getName()));
|
||||
file.renameTo(newFile);
|
||||
return newFile.getAbsolutePath();
|
||||
}
|
||||
|
||||
public static boolean renameWithoutExtension(String path, String newName) {
|
||||
File file = new File(path);
|
||||
File newFile = new File(file.getParent(), newName + "." + getExtension(file.getName()));
|
||||
return file.renameTo(newFile);
|
||||
}
|
||||
|
||||
public static boolean rename(String path, String newName) {
|
||||
File f = new File(path);
|
||||
return f.renameTo(new File(f.getParent(), newName));
|
||||
}
|
||||
|
||||
public static boolean move(String path, String newPath) {
|
||||
File f = new File(path);
|
||||
return f.renameTo(new File(newPath));
|
||||
}
|
||||
|
||||
public static String getExtension(String fileName) {
|
||||
int i = fileName.lastIndexOf('.');
|
||||
if (i < 0 || i + 1 >= fileName.length() - 1)
|
||||
return "";
|
||||
return fileName.substring(i + 1);
|
||||
}
|
||||
|
||||
public static String generateNotExistingPath(String path, String extension) {
|
||||
if (!new File(path + extension).exists())
|
||||
return path + extension;
|
||||
int i = 0;
|
||||
while (true) {
|
||||
String pathI = path + "(" + i + ")" + extension;
|
||||
if (!new File(pathI).exists())
|
||||
return pathI;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getName(String filePath) {
|
||||
return new File(filePath).getName();
|
||||
}
|
||||
|
||||
public static String getNameWithoutExtension(String filePath) {
|
||||
String fileName = getName(filePath);
|
||||
int b = fileName.lastIndexOf('.');
|
||||
if (b < 0)
|
||||
b = fileName.length();
|
||||
fileName = fileName.substring(0, b);
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public static File copyAssetToTmpFile(Context context, String path) {
|
||||
String extension = "." + getExtension(path);
|
||||
String name = getNameWithoutExtension(path);
|
||||
if (name.length() < 5) {
|
||||
name += name.hashCode();
|
||||
}
|
||||
try {
|
||||
File tmpFile = File.createTempFile(name, extension, context.getCacheDir());
|
||||
copyAsset(context, path, tmpFile.getPath());
|
||||
return tmpFile;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean deleteRecursively(File file) {
|
||||
if (file.isFile())
|
||||
return file.delete();
|
||||
for (File child : file.listFiles()) {
|
||||
if (!deleteRecursively(child))
|
||||
return false;
|
||||
}
|
||||
return file.delete();
|
||||
}
|
||||
|
||||
public static boolean remove(String path) {
|
||||
return new File(path).delete();
|
||||
}
|
||||
|
||||
public static boolean removeDir(String path) {
|
||||
return deleteRecursively(new File(path));
|
||||
}
|
||||
|
||||
public static String getSdcardPath() {
|
||||
return Environment.getExternalStorageDirectory().getPath();
|
||||
}
|
||||
|
||||
public static String readAsset(AssetManager assets, String path) {
|
||||
try {
|
||||
return read(assets.open(path));
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String[] listDir(String path) {
|
||||
File file = new File(path);
|
||||
return file.list();
|
||||
}
|
||||
|
||||
public static String[] listDir(String path, final Func1<String, Boolean> filter) {
|
||||
final File file = new File(path);
|
||||
return file.list(new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File dir, String name) {
|
||||
return filter.call(name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static boolean isFile(String path) {
|
||||
return new File(path).isFile();
|
||||
}
|
||||
|
||||
public static boolean isDir(String path) {
|
||||
return new File(path).isDirectory();
|
||||
}
|
||||
|
||||
public static boolean isEmptyDir(String path) {
|
||||
File file = new File(path);
|
||||
return file.isDirectory() && file.list().length == 0;
|
||||
}
|
||||
|
||||
public static String join(String parent, String child) {
|
||||
return new File(parent, child).getPath();
|
||||
}
|
||||
|
||||
public static String getHumanReadableSize(long bytes) {
|
||||
int unit = 1024;
|
||||
if (bytes < unit) return bytes + " B";
|
||||
int exp = (int) (Math.log(bytes) / Math.log(unit));
|
||||
String pre = "KMGTPE".substring(exp - 1, exp);
|
||||
return String.format(Locale.getDefault(), "%.1f %sB", bytes / Math.pow(unit, exp), pre);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ package com.stardust.pio;
|
||||
* Created by Stardust on 2017/4/6.
|
||||
*/
|
||||
|
||||
public class PReadableBinaryFile extends PFile {
|
||||
public class PReadableBinaryFile extends PFiles {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import java.util.List;
|
||||
* Created by Stardust on 2017/4/1.
|
||||
*/
|
||||
|
||||
public class PReadableTextFile extends PFile implements Closeable {
|
||||
public class PReadableTextFile implements Closeable {
|
||||
|
||||
private BufferedReader mBufferedReader;
|
||||
private FileInputStream mFileInputStream;
|
||||
@@ -16,7 +16,7 @@ public class PReadableTextFile extends PFile implements Closeable {
|
||||
private String mEncoding;
|
||||
|
||||
public PReadableTextFile(String path) {
|
||||
this(path, PFile.DEFAULT_ENCODING);
|
||||
this(path, PFiles.DEFAULT_ENCODING);
|
||||
}
|
||||
|
||||
public PReadableTextFile(String path, String encoding) {
|
||||
|
||||
@@ -10,11 +10,13 @@ import java.io.UnsupportedEncodingException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static com.stardust.pio.PFiles.DEFAULT_BUFFER_SIZE;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/1.
|
||||
*/
|
||||
|
||||
public class PWritableTextFile extends PFile implements Closeable {
|
||||
public class PWritableTextFile implements Closeable {
|
||||
|
||||
public static PWritableTextFile open(String path, String encoding, int bufferSize) {
|
||||
return new PWritableTextFile(path, encoding, bufferSize, false);
|
||||
@@ -36,7 +38,7 @@ public class PWritableTextFile extends PFile implements Closeable {
|
||||
|
||||
public PWritableTextFile(String path, String encoding, int bufferingSize, boolean append) {
|
||||
if (bufferingSize <= 0) {
|
||||
bufferingSize = PFile.DEFAULT_BUFFER_SIZE;
|
||||
bufferingSize = DEFAULT_BUFFER_SIZE;
|
||||
}
|
||||
try {
|
||||
mBufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path, append), encoding), bufferingSize);
|
||||
@@ -46,15 +48,15 @@ public class PWritableTextFile extends PFile implements Closeable {
|
||||
}
|
||||
|
||||
public PWritableTextFile(String path) {
|
||||
this(path, PFile.DEFAULT_ENCODING, PFile.DEFAULT_BUFFER_SIZE, false);
|
||||
this(path, PFiles.DEFAULT_ENCODING, DEFAULT_BUFFER_SIZE, false);
|
||||
}
|
||||
|
||||
public PWritableTextFile(String path, boolean append) {
|
||||
this(path, PFile.DEFAULT_ENCODING, PFile.DEFAULT_BUFFER_SIZE, append);
|
||||
this(path, PFiles.DEFAULT_ENCODING, DEFAULT_BUFFER_SIZE, append);
|
||||
}
|
||||
|
||||
public PWritableTextFile(String path, int bufferSize) {
|
||||
this(path, PFile.DEFAULT_ENCODING, bufferSize, false);
|
||||
this(path, PFiles.DEFAULT_ENCODING, bufferSize, false);
|
||||
}
|
||||
|
||||
public PWritableTextFile(String path, String encoding) {
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.stardust.util;
|
||||
import android.app.Activity;
|
||||
import android.content.res.AssetManager;
|
||||
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/3/14.
|
||||
@@ -19,7 +19,7 @@ public class AssetsCache {
|
||||
return cache.get(path, new SimpleCache.Supplier<String>() {
|
||||
@Override
|
||||
public String get(String key) {
|
||||
return PFile.readAsset(assetManager, path);
|
||||
return PFiles.readAsset(assetManager, path);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
package com.stardust.util;
|
||||
|
||||
import com.stardust.pio.PFile;
|
||||
import com.stardust.pio.PFiles;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.Collator;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/3/31.
|
||||
@@ -39,7 +37,7 @@ public class FileSorter {
|
||||
public static final Comparator<File> TYPE = new Comparator<File>() {
|
||||
@Override
|
||||
public int compare(File o1, File o2) {
|
||||
return -PFile.getExtension(o1.getName()).compareTo(PFile.getExtension(o2.getName()));
|
||||
return -PFiles.getExtension(o1.getName()).compareTo(PFiles.getExtension(o2.getName()));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user