add editor (modified by 920 text editor)
This commit is contained in:
287
app/src/main/java/com/stardust/scriptdroid/EditActivity.java
Normal file
287
app/src/main/java/com/stardust/scriptdroid/EditActivity.java
Normal file
@@ -0,0 +1,287 @@
|
||||
package com.stardust.scriptdroid;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.util.SparseArray;
|
||||
import android.view.View;
|
||||
|
||||
import com.afollestad.materialdialogs.DialogAction;
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.afollestad.materialdialogs.folderselector.FolderChooserDialog;
|
||||
import com.jecelyin.editor.v2.Pref;
|
||||
import com.jecelyin.editor.v2.common.Command;
|
||||
import com.jecelyin.editor.v2.common.SaveListener;
|
||||
import com.jecelyin.editor.v2.ui.EditorDelegate;
|
||||
import com.jecelyin.editor.v2.ui.IActivity;
|
||||
import com.jecelyin.editor.v2.ui.TabManager;
|
||||
import com.jecelyin.editor.v2.view.EditorView;
|
||||
import com.jecelyin.editor.v2.view.menu.MenuDef;
|
||||
import com.stardust.scriptdroid.droid.Droid;
|
||||
import com.stardust.scriptdroid.widget.ToolbarMenuItem;
|
||||
import com.stardust.util.SparseArrayEntries;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/1/29.
|
||||
*/
|
||||
|
||||
public class EditActivity extends IActivity {
|
||||
|
||||
|
||||
public static void editFile(Context context, String path) {
|
||||
editFile(context, null, path);
|
||||
}
|
||||
|
||||
public static void editFile(Context context, String name, String path) {
|
||||
context.startActivity(new Intent(context, EditActivity.class)
|
||||
.putExtra("path", path)
|
||||
.putExtra("name", name));
|
||||
}
|
||||
|
||||
private String mName;
|
||||
private File mFile;
|
||||
private View mView;
|
||||
private EditorDelegate mEditorDelegate;
|
||||
private ToolbarMenuItem mSaveMenuItem, mRedoMenuItem, mUndoMenuItem, mRunMenuItem;
|
||||
private SparseArray<ToolbarMenuItem> mMenuMap;
|
||||
|
||||
public void onCreate(Bundle b) {
|
||||
super.onCreate(b);
|
||||
handleIntent();
|
||||
setUpUI();
|
||||
setUpEditor();
|
||||
}
|
||||
|
||||
private void setUpEditor() {
|
||||
if (mFile != null) {
|
||||
Pref.getInstance(this).setReadOnly(false);
|
||||
mEditorDelegate = new EditorDelegate(0, mFile, 0, null);
|
||||
EditorView editorView = (EditorView) findViewById(R.id.editor);
|
||||
mEditorDelegate.setEditorView(editorView);
|
||||
}
|
||||
}
|
||||
|
||||
private void setUpUI() {
|
||||
setTheme(R.style.EditorTheme);
|
||||
mView = View.inflate(this, R.layout.activity_edit, null);
|
||||
setContentView(mView);
|
||||
initMenuItem();
|
||||
setUpToolbar();
|
||||
setUpListener();
|
||||
}
|
||||
|
||||
private void initMenuItem() {
|
||||
mRedoMenuItem = (ToolbarMenuItem) findViewById(R.id.redo);
|
||||
mUndoMenuItem = (ToolbarMenuItem) findViewById(R.id.undo);
|
||||
mSaveMenuItem = (ToolbarMenuItem) findViewById(R.id.save);
|
||||
mRunMenuItem = (ToolbarMenuItem) findViewById(R.id.run);
|
||||
mMenuMap = new SparseArrayEntries<ToolbarMenuItem>()
|
||||
.entry(com.jecelyin.editor.v2.R.id.m_redo, mRedoMenuItem)
|
||||
.entry(com.jecelyin.editor.v2.R.id.m_undo, mUndoMenuItem)
|
||||
.entry(com.jecelyin.editor.v2.R.id.m_save, mSaveMenuItem)
|
||||
.entry(R.id.run, mRunMenuItem)
|
||||
.sparseArray();
|
||||
}
|
||||
|
||||
private void setUpListener() {
|
||||
mUndoMenuItem.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
undo();
|
||||
}
|
||||
});
|
||||
mRedoMenuItem.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
redo();
|
||||
}
|
||||
});
|
||||
mSaveMenuItem.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
saveFile(false, null);
|
||||
}
|
||||
});
|
||||
mRunMenuItem.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (mEditorDelegate.isChanged()) {
|
||||
saveFile(false, new SaveListener() {
|
||||
@Override
|
||||
public void onSaved() {
|
||||
run();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
run();
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void run() {
|
||||
Snackbar.make(mView, "开始运行", Snackbar.LENGTH_SHORT).show();
|
||||
setMenuStatus(R.id.run, MenuDef.STATUS_DISABLED);
|
||||
Droid.getInstance().runScriptFile(mFile, new Droid.OnRunFinishedListener() {
|
||||
@Override
|
||||
public void onRunFinished(Object result, final Exception e) {
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
setMenuStatus(R.id.run, MenuDef.STATUS_NORMAL);
|
||||
if (e != null)
|
||||
Snackbar.make(mView, "错误: " + e.getMessage(), Snackbar.LENGTH_INDEFINITE).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void handleIntent() {
|
||||
String path = getIntent().getStringExtra("path");
|
||||
mName = getIntent().getStringExtra("name");
|
||||
if (path == null) {
|
||||
finish();
|
||||
} else {
|
||||
mFile = new File(path);
|
||||
if (mName == null) {
|
||||
mName = mFile.getName();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setUpToolbar() {
|
||||
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
||||
toolbar.setTitle(mName);
|
||||
setSupportActionBar(toolbar);
|
||||
if (getSupportActionBar() != null) {
|
||||
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void saveFile(boolean toast, SaveListener listener) {
|
||||
Command command = new Command(Command.CommandEnum.SAVE);
|
||||
command.args = new Bundle();
|
||||
command.args.putBoolean("is_cluster", !toast);
|
||||
command.object = listener;
|
||||
mEditorDelegate.doCommand(command);
|
||||
}
|
||||
|
||||
private void undo() {
|
||||
Command command = new Command(Command.CommandEnum.UNDO);
|
||||
mEditorDelegate.doCommand(command);
|
||||
}
|
||||
|
||||
private void redo() {
|
||||
Command command = new Command(Command.CommandEnum.REDO);
|
||||
mEditorDelegate.doCommand(command);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startPickPathActivity(String s, String s1) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
if (mEditorDelegate.isChanged()) {
|
||||
showExitConfirmDialog();
|
||||
} else {
|
||||
super.finish();
|
||||
}
|
||||
}
|
||||
|
||||
private void showExitConfirmDialog() {
|
||||
new MaterialDialog.Builder(this)
|
||||
.title(R.string.text_alert)
|
||||
.content(R.string.edit_exit_without_save_warn)
|
||||
.positiveText(R.string.text_cancel)
|
||||
.negativeText(R.string.text_save_and_exit)
|
||||
.neutralText(R.string.text_exit_directly )
|
||||
.onNegative(new MaterialDialog.SingleButtonCallback() {
|
||||
@Override
|
||||
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
|
||||
saveFile(true, null);
|
||||
EditActivity.super.finish();
|
||||
}
|
||||
})
|
||||
.onNeutral(new MaterialDialog.SingleButtonCallback() {
|
||||
@Override
|
||||
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
|
||||
EditActivity.super.finish();
|
||||
}
|
||||
})
|
||||
.show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doNextCommand() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void setMenuStatus(int menuResId, int status) {
|
||||
ToolbarMenuItem menuItem = mMenuMap.get(menuResId);
|
||||
if (menuItem == null)
|
||||
return;
|
||||
boolean disabled = status == MenuDef.STATUS_DISABLED;
|
||||
menuItem.setEnabled(!disabled);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onDocumentChanged(int i) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doCommand(Command command) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void openFile(String s, String s1, int i) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFindFolderCallback(FolderChooserDialog.FolderCallback folderCallback) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public TabManager getTabManager() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertText(CharSequence charSequence) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFolderSelection(@NonNull FolderChooserDialog dialog, @NonNull File folder) {
|
||||
|
||||
}
|
||||
|
||||
protected void onDestroy() {
|
||||
try {
|
||||
super.onDestroy();
|
||||
} catch (Exception ignored) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,9 +3,12 @@ package com.stardust.scriptdroid;
|
||||
import android.Manifest;
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v4.widget.DrawerLayout;
|
||||
import android.support.v7.app.ActionBarDrawerToggle;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.text.InputType;
|
||||
import android.view.Menu;
|
||||
@@ -14,7 +17,6 @@ import android.view.View;
|
||||
|
||||
import com.afollestad.materialdialogs.DialogAction;
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.jecelyin.editor.v2.core.widget.JecEditText;
|
||||
import com.stardust.scriptdroid.droid.runtime.action.ActionPerformService;
|
||||
import com.stardust.scriptdroid.droid.script.file.ScriptFile;
|
||||
import com.stardust.scriptdroid.droid.script.file.ScriptFileList;
|
||||
@@ -38,6 +40,7 @@ public class MainActivity extends BaseActivity {
|
||||
private ScriptListRecyclerView mScriptListRecyclerView;
|
||||
private ScriptFileList mScriptFileList;
|
||||
private FileChooser mFileChooser;
|
||||
private DrawerLayout mDrawerLayout;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -48,22 +51,17 @@ public class MainActivity extends BaseActivity {
|
||||
setUpFileChooser();
|
||||
|
||||
checkPermissions();
|
||||
|
||||
}
|
||||
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
}
|
||||
|
||||
private void checkPermissions() {
|
||||
checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
|
||||
goToAccessibilityPermissionSettingIfDisabled();
|
||||
JecEditText jecEditText = new JecEditText(this);
|
||||
}
|
||||
|
||||
private void goToAccessibilityPermissionSettingIfDisabled() {
|
||||
if (!AccessibilityServiceUtils.isAccessibilityServiceEnabled(this, ActionPerformService.class)) {
|
||||
new MaterialDialog.Builder(this)
|
||||
.title(R.string.text_alert)
|
||||
.content(R.string.explain_accessibility_permission)
|
||||
.positiveText(R.string.text_go_to_setting)
|
||||
.negativeText(R.string.text_cancel)
|
||||
@@ -110,6 +108,7 @@ public class MainActivity extends BaseActivity {
|
||||
mView = View.inflate(this, R.layout.activity_main, null);
|
||||
setContentView(mView);
|
||||
mSlidingUpPanel = $(R.id.bottom_menu);
|
||||
mDrawerLayout = $(R.id.drawer_layout);
|
||||
|
||||
setUpToolbar();
|
||||
setUpScriptList();
|
||||
@@ -119,8 +118,12 @@ public class MainActivity extends BaseActivity {
|
||||
private void setUpToolbar() {
|
||||
Toolbar toolbar = $(R.id.toolbar);
|
||||
setSupportActionBar(toolbar);
|
||||
toolbar.setNavigationIcon(R.drawable.ic_script_droid_40);
|
||||
toolbar.setTitle(R.string.app_name);
|
||||
|
||||
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.text_drawer_open,
|
||||
R.string.text_drawer_close);
|
||||
drawerToggle.syncState();
|
||||
mDrawerLayout.addDrawerListener(drawerToggle);
|
||||
}
|
||||
|
||||
private void setUpScriptList() {
|
||||
@@ -204,7 +207,7 @@ public class MainActivity extends BaseActivity {
|
||||
.entry(R.id.action_test, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
startActivity(new Intent(MainActivity.this, TestActivity.class));
|
||||
startActivity(new Intent(MainActivity.this, EditActivity.class));
|
||||
}
|
||||
})
|
||||
.map();
|
||||
@@ -216,7 +219,7 @@ public class MainActivity extends BaseActivity {
|
||||
}
|
||||
|
||||
private void disableAccessibilityService() {
|
||||
if (ActionPerformService.getInstance() != null) {
|
||||
if (ActionPerformService.getInstance() != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
ActionPerformService.getInstance().disableSelf();
|
||||
}
|
||||
Snackbar.make(mView, R.string.text_service_disabled, Snackbar.LENGTH_SHORT).show();
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
package com.stardust.scriptdroid;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.jecelyin.editor.v2.Pref;
|
||||
import com.jecelyin.editor.v2.common.Command;
|
||||
import com.jecelyin.editor.v2.ui.EditorDelegate;
|
||||
import com.jecelyin.editor.v2.ui.IActivity;
|
||||
import com.jecelyin.editor.v2.ui.TabManager;
|
||||
import com.jecelyin.editor.v2.view.EditorView;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/1/29.
|
||||
*/
|
||||
|
||||
public class TestActivity extends BaseActivity implements IActivity{
|
||||
|
||||
public void onCreate(Bundle b) {
|
||||
super.onCreate(b);
|
||||
Pref.getInstance(this).setTheme(0);
|
||||
setContentView(R.layout.edit_layout);
|
||||
EditorDelegate editorDelegate = new EditorDelegate(0, "Hello 920 Editor", "Hello 920");
|
||||
EditorView editorView = $(R.id.editor);
|
||||
editorDelegate.setEditorView(editorView);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startPickPathActivity(String s, String s1) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doNextCommand() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public TabManager getTabManager() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMenuStatus(int i, int i1) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDocumentChanged(int i) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doCommand(Command command) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void openFile(String s, String s1, int i) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,20 @@ import java.io.FileNotFoundException;
|
||||
|
||||
public class Droid {
|
||||
|
||||
public interface OnRunFinishedListener {
|
||||
void onRunFinished(Object result, Exception e);
|
||||
}
|
||||
|
||||
private static final DroidRuntime RUNTIME = DroidRuntime.getRuntime();
|
||||
private static final JavaScriptEngine JAVA_SCRIPT_ENGINE = new GBJDuktapeJavaScriptEngine(RUNTIME);
|
||||
private static final OnRunFinishedListener DEFAULT_LISTENER = new OnRunFinishedListener() {
|
||||
@Override
|
||||
public void onRunFinished(Object result, Exception e) {
|
||||
if (e != null) {
|
||||
RUNTIME.toast("错误: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
};
|
||||
private static Droid instance = new Droid();
|
||||
|
||||
protected Droid() {
|
||||
@@ -31,18 +43,30 @@ public class Droid {
|
||||
runScript(FileUtils.readString(file));
|
||||
}
|
||||
|
||||
public void runScriptFile(File file, OnRunFinishedListener listener) {
|
||||
checkFile(file);
|
||||
runScript(FileUtils.readString(file), listener);
|
||||
}
|
||||
|
||||
private void runScript(String script) {
|
||||
runScript(script, null);
|
||||
}
|
||||
|
||||
public void runScriptFile(String path) {
|
||||
runScriptFile(new File(path));
|
||||
}
|
||||
|
||||
public void runScript(final String script) {
|
||||
public void runScript(final String script, OnRunFinishedListener listener) {
|
||||
if (listener == null)
|
||||
listener = DEFAULT_LISTENER;
|
||||
final OnRunFinishedListener finalListener = listener;
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
JAVA_SCRIPT_ENGINE.execute(script);
|
||||
finalListener.onRunFinished(JAVA_SCRIPT_ENGINE.execute(script), null);
|
||||
} catch (Exception e) {
|
||||
RUNTIME.toast("错误" + e.getMessage());
|
||||
finalListener.onRunFinished(null, e);
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
|
||||
@@ -9,6 +9,7 @@ import android.view.accessibility.AccessibilityNodeInfo;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.stardust.scriptdroid.App;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.droid.runtime.action.Action;
|
||||
import com.stardust.scriptdroid.droid.runtime.action.ActionFactory;
|
||||
import com.stardust.scriptdroid.droid.runtime.action.ActionPerformService;
|
||||
@@ -101,11 +102,11 @@ public class DroidRuntime implements IDroidRuntime {
|
||||
return performAction(target.createAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD));
|
||||
}
|
||||
|
||||
public boolean scrollAllUp(){
|
||||
public boolean scrollAllUp() {
|
||||
return performAction(ActionFactory.createScrollAllAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD));
|
||||
}
|
||||
|
||||
public boolean scrollAllDown(){
|
||||
public boolean scrollAllDown() {
|
||||
return performAction(ActionFactory.createScrollAllAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD));
|
||||
}
|
||||
|
||||
@@ -132,8 +133,8 @@ public class DroidRuntime implements IDroidRuntime {
|
||||
|
||||
private boolean performAction(Action action) {
|
||||
if (ActionPerformService.getInstance() == null) {
|
||||
toast("没有权限");
|
||||
return false;
|
||||
toast(App.getApp().getString(R.string.text_no_accessibility_permission));
|
||||
throw new PermissionDeniedException(App.getApp().getString(R.string.text_no_accessibility_permission));
|
||||
}
|
||||
ActionPerformService.setAction(action);
|
||||
synchronized (mLock) {
|
||||
@@ -159,7 +160,7 @@ public class DroidRuntime implements IDroidRuntime {
|
||||
|
||||
@Override
|
||||
public void sleep(long millis) {
|
||||
try{
|
||||
try {
|
||||
Thread.sleep(millis);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.stardust.scriptdroid.droid.runtime;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/1/29.
|
||||
*/
|
||||
public class PermissionDeniedException extends RuntimeException {
|
||||
public PermissionDeniedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -30,8 +30,8 @@ public class DuktapeJavaScriptEngine implements JavaScriptEngine {
|
||||
|
||||
|
||||
@Override
|
||||
public void execute(String script) {
|
||||
mDuktape.evaluate(script);
|
||||
public Object execute(String script) {
|
||||
return mDuktape.evaluate(script);
|
||||
}
|
||||
|
||||
public <T> void set(String varName, Class<T> c, T value) {
|
||||
|
||||
@@ -22,7 +22,11 @@ public class GBJDuktapeJavaScriptEngine implements JavaScriptEngine {
|
||||
public GBJDuktapeJavaScriptEngine(IDroidRuntime runtime) {
|
||||
setRuntime(runtime);
|
||||
mDuktapeEngine.put("context", App.getApp());
|
||||
execute(init);
|
||||
try {
|
||||
execute(init);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void setRuntime(IDroidRuntime runtime) {
|
||||
@@ -30,12 +34,8 @@ public class GBJDuktapeJavaScriptEngine implements JavaScriptEngine {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String script) {
|
||||
try {
|
||||
mDuktapeEngine.execute(JSTransformer.parse(new StringReader(script)));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
public Object execute(String script) throws IOException {
|
||||
return mDuktapeEngine.execute(JSTransformer.parse(new StringReader(script)));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -3,13 +3,15 @@ package com.stardust.scriptdroid.droid.script;
|
||||
import com.stardust.scriptdroid.App;
|
||||
import com.stardust.scriptdroid.droid.script.file.AssetScript;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/1/27.
|
||||
*/
|
||||
|
||||
public interface JavaScriptEngine {
|
||||
|
||||
void execute(String script);
|
||||
Object execute(String script) throws IOException;
|
||||
|
||||
<T> void set(String varName, Class<T> c, T value);
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ public class ScriptFile {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public void run(Context context) {
|
||||
public void run() {
|
||||
Droid.getInstance().runScriptFile(toFile());
|
||||
}
|
||||
|
||||
|
||||
@@ -167,7 +167,7 @@ public class ScriptListRecyclerView extends RecyclerView {
|
||||
}
|
||||
|
||||
private static void loadScriptFileOperations() {
|
||||
ClassTool.loadClasses(Run.class, Edit.class, Rename.class, CreateShortcut.class, Remove.class, Delete.class);
|
||||
ClassTool.loadClasses(Run.class, Rename.class, OpenByOtherApp.class, CreateShortcut.class, Remove.class, Delete.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.stardust.scriptdroid.widget;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.stardust.scriptdroid.R;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/1/29.
|
||||
*/
|
||||
|
||||
public class ToolbarMenuItem extends LinearLayout {
|
||||
|
||||
private static final int COLOR_DISABLED = 0X77e0e0e0;
|
||||
private ImageView mImageView;
|
||||
private TextView mTextView;
|
||||
private Drawable mEnabledDrawable, mDisabledDrawable;
|
||||
|
||||
public ToolbarMenuItem(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init(attrs);
|
||||
}
|
||||
|
||||
public ToolbarMenuItem(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init(attrs);
|
||||
}
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
||||
public ToolbarMenuItem(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
init(attrs);
|
||||
}
|
||||
|
||||
private void init(AttributeSet attrs) {
|
||||
inflate(getContext(), R.layout.toolbar_menu_item, this);
|
||||
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ToolbarMenuItem);
|
||||
String text = a.getString(R.styleable.ToolbarMenuItem_text);
|
||||
int iconResId = a.getResourceId(R.styleable.ToolbarMenuItem_icon, 0);
|
||||
int iconColor = a.getColor(R.styleable.ToolbarMenuItem_icon_color, Color.TRANSPARENT);
|
||||
a.recycle();
|
||||
mImageView = (ImageView) findViewById(R.id.icon);
|
||||
mTextView = (TextView) findViewById(R.id.text);
|
||||
mTextView.setText(text);
|
||||
mImageView.setImageResource(iconResId);
|
||||
if (iconColor != Color.TRANSPARENT) {
|
||||
mImageView.setImageDrawable(convertDrawableToGrayScale(mImageView.getDrawable(), iconColor));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnabled(boolean enabled) {
|
||||
if (enabled == isEnabled())
|
||||
return;
|
||||
super.setEnabled(enabled);
|
||||
ensureEnabledDrawable();
|
||||
ensureDisabledDrawable();
|
||||
mImageView.setImageDrawable(enabled ? mEnabledDrawable : mDisabledDrawable);
|
||||
mTextView.setTextColor(enabled ? Color.WHITE : COLOR_DISABLED);
|
||||
}
|
||||
|
||||
private void ensureDisabledDrawable() {
|
||||
if (mDisabledDrawable == null) {
|
||||
mDisabledDrawable = convertDrawableToGrayScale(mEnabledDrawable, COLOR_DISABLED);
|
||||
}
|
||||
}
|
||||
|
||||
private void ensureEnabledDrawable() {
|
||||
if (mEnabledDrawable == null) {
|
||||
mEnabledDrawable = mImageView.getDrawable();
|
||||
}
|
||||
}
|
||||
|
||||
public static Drawable convertDrawableToGrayScale(Drawable drawable, int color) {
|
||||
if (drawable == null || drawable.getConstantState() == null)
|
||||
return null;
|
||||
Drawable res = drawable.getConstantState().newDrawable().mutate();
|
||||
res.setColorFilter(color, PorterDuff.Mode.SRC_IN);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user