优化 脚本管理器增删文件后的更新

新增 支持插件
优化 多点找色的内存泄漏
修复 文件重命名时的后缀问题
修复 脚本抛出Throwable不能被捕捉的问题
This commit is contained in:
hyb1996
2018-10-23 15:00:34 +08:00
parent 2b71f5e856
commit 148784656d
38 changed files with 468 additions and 57 deletions

View File

@@ -1,7 +1,7 @@
"ui";
var ColoredButton = (function() {
//继承ui.Widget
//继承ui.Widget
util.extend(ColoredButton, ui.Widget);
function ColoredButton() {

View File

@@ -34,7 +34,7 @@ public class ScriptExecutionGlobalListener implements ScriptExecutionListener {
}
@Override
public void onException(ScriptExecution execution, Exception e) {
public void onException(ScriptExecution execution, Throwable e) {
onFinish(execution);
}

View File

@@ -87,14 +87,14 @@ public class Explorer {
if (cachedParent != null) {
cachedParent.addChild(item);
}
mEventBus.post(new ExplorerChangeEvent(parent, CREATE, item));
mEventBus.post(new ExplorerChangeEvent(parent, CREATE, item, item));
}
@SuppressWarnings("unchecked")
public void refreshAll() {
if (mExplorerPageLruCache != null)
mExplorerPageLruCache.evictAll();
mEventBus.post(new ExplorerChangeEvent(ALL));
mEventBus.post(ExplorerChangeEvent.EVENT_ALL);
}

View File

@@ -1,13 +1,18 @@
package org.autojs.autojs.model.explorer;
import com.stardust.util.ObjectHelper;
public class ExplorerChangeEvent {
public static final int REMOVE = 0;
public static final int CREATE = 1;
public static final int CHANGE = 2;
public static final int ALL = 3;
public static final int CHILDREN_CHANGE = 4;
public static final ExplorerChangeEvent EVENT_ALL = new ExplorerChangeEvent(ALL);
private final int mAction;
private final ExplorerItem mItem;
private final ExplorerItem mNewItem;
@@ -21,10 +26,10 @@ public class ExplorerChangeEvent {
}
public ExplorerChangeEvent(ExplorerPage parent, int action, ExplorerItem item) {
this(parent, action, item, null);
this(parent, action, item, null);
}
public ExplorerChangeEvent(int action) {
private ExplorerChangeEvent(int action) {
this(null, action, null, null);
}
@@ -43,4 +48,30 @@ public class ExplorerChangeEvent {
public ExplorerPage getPage() {
return mPage;
}
@Override
public String toString() {
return "ExplorerChangeEvent{" +
"mAction=" + nameOfAction(mAction) +
", mPage=" + mPage +
", mItem=" + mItem +
", mNewItem=" + mNewItem +
'}';
}
private static String nameOfAction(int action) {
switch (action) {
case ALL:
return "ALL";
case CHANGE:
return "CHANGE";
case CREATE:
return "CREATE";
case REMOVE:
return "REMOVE";
case CHILDREN_CHANGE:
return "CHILDREN_CHANGE";
}
throw new IllegalArgumentException("action = " + action);
}
}

View File

@@ -44,7 +44,7 @@ public class ExplorerDirPage extends ExplorerFileItem implements ExplorerPage {
@Override
public ExplorerFileItem rename(String newName) {
return new ExplorerDirPage(getFile().renameAndReturnNewFile(newName), getParent());
return new ExplorerDirPage(getFile().renameTo(newName), getParent());
}
protected int indexOf(ExplorerItem child){

View File

@@ -2,6 +2,7 @@ package org.autojs.autojs.model.explorer;
import com.stardust.pio.PFile;
import com.stardust.util.ObjectHelper;
import com.stardust.util.Objects;
import org.autojs.autojs.model.script.ScriptFile;
@@ -71,7 +72,7 @@ public class ExplorerFileItem implements ExplorerItem {
}
public ExplorerFileItem rename(String newName) {
return new ExplorerFileItem(mFile.renameAndReturnNewFile(newName), getParent());
return new ExplorerFileItem(mFile.renameTo(newName), getParent());
}
@Override
@@ -102,4 +103,24 @@ public class ExplorerFileItem implements ExplorerItem {
String type = getType();
return type.equals("js") || type.equals("auto");
}
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"mFile=" + mFile + "}";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ExplorerFileItem that = (ExplorerFileItem) o;
return Objects.equals(mFile, that.mFile);
}
@Override
public int hashCode() {
return Objects.hashCode(mFile);
}
}

View File

@@ -30,6 +30,6 @@ public class ExplorerProjectPage extends ExplorerDirPage {
@Override
public ExplorerFileItem rename(String newName) {
return new ExplorerProjectPage(getFile().renameAndReturnNewFile(newName), getParent(), mProjectConfig);
return new ExplorerProjectPage(getFile().renameTo(newName), getParent(), mProjectConfig);
}
}

View File

@@ -52,7 +52,7 @@ public class Scripts {
}
@Override
public void onException(ScriptExecution execution, Exception e) {
public void onException(ScriptExecution execution, Throwable e) {
RhinoException rhinoException = getRhinoException(e);
int line = -1, col = 0;
if (rhinoException != null) {

View File

@@ -0,0 +1,61 @@
package org.autojs.autojs.ui.common;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.widget.EditText;
import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import org.autojs.autojs.R;
import java.io.File;
public class FileNameInputDialog implements MaterialDialog.InputCallback {
private String mExcluded;
private boolean mIsFirstTextChanged = true;
private String mExtension;
private Context mContext;
private File mDir;
private void validateInput(MaterialDialog dialog, String extension) {
EditText editText = dialog.getInputEditText();
if (editText == null)
return;
Editable input = editText.getText();
int errorResId = 0;
if (input == null || input.length() == 0) {
dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false);
return;
}
if (new File(mDir, extension == null ? input.toString() : input.toString() + extension).exists()) {
errorResId = R.string.text_file_exists;
}
if (errorResId == 0) {
editText.setError(null);
dialog.getActionButton(DialogAction.POSITIVE).setEnabled(true);
} else {
editText.setError(mContext.getString(errorResId));
dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false);
}
}
@Override
public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {
if (mIsFirstTextChanged) {
mIsFirstTextChanged = false;
return;
}
EditText editText = dialog.getInputEditText();
if (editText == null)
return;
if (input.equals(mExcluded)) {
editText.setError(null);
dialog.getActionButton(DialogAction.POSITIVE).setEnabled(true);
return;
}
validateInput(dialog, mExtension);
}
}

View File

@@ -269,10 +269,8 @@ public class ScriptOperations {
}
public Observable<ExplorerFileItem> rename(final ExplorerFileItem item) {
final ScriptFile oldFile = new ScriptFile(item.getPath());
String originalName = item.getName();
return showNameInputDialog(originalName, new InputCallback(oldFile.isDirectory() ? null : PFiles.getExtension(item.getName()),
originalName))
return showNameInputDialog(originalName, new InputCallback(null, originalName))
.map(newName -> {
ExplorerFileItem newItem = item.rename(newName);
if (ObjectHelper.equals(newItem.toScriptFile(), item.toScriptFile())) {
@@ -307,18 +305,23 @@ public class ScriptOperations {
@SuppressLint("CheckResult")
public void deleteWithoutConfirm(final ScriptFile scriptFile) {
boolean isDir = scriptFile.isDirectory();
Observable.fromPublisher((Publisher<Boolean>) s -> s.onNext(PFiles.deleteRecursively(scriptFile)))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(deleted -> {
showMessage(deleted ? R.string.text_already_delete : R.string.text_delete_failed);
if (deleted)
notifyFileRemoved(mCurrentDirectory, scriptFile);
notifyFileRemoved(isDir, scriptFile);
});
}
private void notifyFileRemoved(ScriptFile directory, ScriptFile scriptFile) {
mExplorer.notifyItemRemoved(new ExplorerFileItem(scriptFile, mExplorerPage));
private void notifyFileRemoved(boolean isDir, ScriptFile scriptFile) {
if (isDir) {
mExplorer.notifyItemRemoved(new ExplorerDirPage(scriptFile, mExplorerPage));
} else {
mExplorer.notifyItemRemoved(new ExplorerFileItem(scriptFile, mExplorerPage));
}
}

View File

@@ -10,6 +10,7 @@ import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
@@ -243,9 +244,40 @@ public class ExplorerView extends ThemeColorSwipeRefreshLayout implements SwipeR
@Subscribe
public void onExplorerChange(ExplorerChangeEvent event) {
if ((event.getAction() == ExplorerChangeEvent.ALL)
|| mCurrentPageState.page.getPath().equals(event.getPage().getPath())) {
Log.d(LOG_TAG, "on explorer change: " + event);
if ((event.getAction() == ExplorerChangeEvent.ALL)) {
loadItemList();
return;
}
String currentDirPath = mCurrentPageState.page.getPath();
String changedDirPath = event.getPage().getPath();
ExplorerItem item = event.getItem();
String changedItemPath = item == null ? null : item.getPath();
if (currentDirPath.equals(changedItemPath) || (currentDirPath.equals(changedDirPath) &&
event.getAction() == ExplorerChangeEvent.CHILDREN_CHANGE)) {
loadItemList();
return;
}
if (currentDirPath.equals(changedDirPath)) {
int i;
switch (event.getAction()) {
case ExplorerChangeEvent.CHANGE:
i = mExplorerItemList.update(item, event.getNewItem());
if (i >= 0) {
mExplorerAdapter.notifyItemChanged(item, i);
}
break;
case ExplorerChangeEvent.CREATE:
mExplorerItemList.insertAtFront(event.getNewItem());
mExplorerAdapter.notifyItemInserted(event.getNewItem(), 0);
break;
case ExplorerChangeEvent.REMOVE:
i = mExplorerItemList.remove(item);
if (i >= 0) {
mExplorerAdapter.notifyItemRemoved(item, i);
}
break;
}
}
}
@@ -417,6 +449,25 @@ public class ExplorerView extends ThemeColorSwipeRefreshLayout implements SwipeR
}
}
int getItemPosition(ExplorerItem item, int i) {
if (item instanceof ExplorerPage) {
return i + positionOfCategoryDir + 1;
}
return i + positionOfCategoryFile() + 1;
}
public void notifyItemChanged(ExplorerItem item, int i) {
notifyItemChanged(getItemPosition(item, i));
}
public void notifyItemRemoved(ExplorerItem item, int i) {
notifyItemRemoved(getItemPosition(item, i));
}
public void notifyItemInserted(ExplorerItem item, int i) {
notifyItemInserted(getItemPosition(item, i));
}
@Override
public int getItemCount() {
int count = 0;

View File

@@ -71,7 +71,7 @@ public class TaskListRecyclerView extends ThemeColorRecyclerView {
}
@Override
public void onException(ScriptExecution execution, Exception e) {
public void onException(ScriptExecution execution, Throwable e) {
onFinish(execution);
}

View File

@@ -2,6 +2,7 @@ package org.autojs.autojs.ui.viewmodel;
import android.content.SharedPreferences;
import org.autojs.autojs.model.explorer.ExplorerDirPage;
import org.autojs.autojs.model.explorer.ExplorerItem;
import org.autojs.autojs.model.explorer.ExplorerPage;
import org.autojs.autojs.model.explorer.ExplorerSorter;
@@ -145,6 +146,47 @@ public class ExplorerItemList {
}
}
public void insertAtFront(ExplorerItem item) {
if (item instanceof ExplorerPage) {
mItemGroups.add(0, (ExplorerPage) item);
} else {
mItems.add(0, item);
}
}
public int remove(ExplorerItem item) {
if (item instanceof ExplorerPage) {
return remove(mItemGroups, item);
} else {
return remove(mItems, item);
}
}
public int update(ExplorerItem oldItem, ExplorerItem newItem) {
if (oldItem instanceof ExplorerPage) {
return update(mItemGroups, (ExplorerPage) oldItem, (ExplorerPage) newItem);
} else {
return update(mItems, oldItem, newItem);
}
}
private <T> int update(ArrayList<T> list, T oldItem, T newItem) {
int i = list.indexOf(oldItem);
if (i >= 0) {
list.set(i, newItem);
}
return i;
}
private <T> int remove(ArrayList<?> list, T o) {
int i = list.indexOf(o);
if (i >= 0) {
list.remove(i);
}
return i;
}
public ExplorerPage getItemGroup(int i) {
return mItemGroups.get(i);
}