add: module storages

This commit is contained in:
hyb1996
2017-12-04 17:20:40 +08:00
parent 8c52a892c4
commit 6e617d83e8
14 changed files with 201 additions and 31 deletions

View File

@@ -6,6 +6,7 @@
<w>loopers</w>
<w>prefill</w>
<w>scriptable</w>
<w>storages</w>
<w>tasker</w>
<w>uncheck</w>
</words>

View File

@@ -1 +1 @@
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":230},"path":"release-3.0.0 Alpha30.apk","properties":{"packageId":"com.stardust.scriptdroid","split":"","minSdkVersion":"17"}}]
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":1},"path":"inrt-release.apk","properties":{"packageId":"com.stardust.auojs.inrt","split":"","minSdkVersion":"17"}}]

View File

@@ -0,0 +1,14 @@
var storage = storages.create("Auto.js例子:复杂数据");
var arr = [1, 4, 2, 5];
var obj = {
name: "Auto.js",
url: "www.autojs.org"
};
//保存
storage.put("arr", arr);
storage.put("obj", obj);
console.show();
//取出
log("arr = ", storage.get("arr"));
log("obj = ", storage.get("obj"));

View File

@@ -0,0 +1,14 @@
var storage = storages.create("Auto.js例子:简单数据");
var a = 1234;
var b = true;
var str = "hello";
//保存
storage.put("a", a);
storage.put("b", b);
storage.put("str", str);
console.show();
//取出
log("a = " + storage.get("a"));
log("b = " + storage.get("b"));
log("str = " + storage.get("str"));

View File

@@ -0,0 +1,21 @@
"ui";
ui.layout(
<vertical padding="16">
<horizontal>
<text textColor="black" textSize="18sp" layout_weight="1">随手记</text>
<button id="save" text="保存" w="auto" style="Widget.AppCompat.Button.Borderless.Colored"/>
</horizontal>
<input id="content" h="*" gravity="top"/>
</vertical>
);
var storage = storages.create("Auto.js例子:随手记");
var content = storage.get("content");
if(content != null){
ui.content.setText(content);
}
ui.save.click(()=>{
storage.put("content", ui.content.getText());
});

View File

@@ -27,6 +27,7 @@ import com.stardust.scriptdroid.ui.edit.completion.CodeCompletions;
import com.stardust.scriptdroid.ui.edit.completion.CodeCompletionBar;
import com.stardust.scriptdroid.ui.edit.completion.InputMethodEnhancedBarColors;
import com.stardust.scriptdroid.ui.edit.completion.Symbols;
import com.stardust.scriptdroid.ui.log.LogActivity_;
import com.stardust.scriptdroid.ui.widget.EWebView;
import com.stardust.scriptdroid.ui.widget.ToolbarMenuItem;
import com.stardust.widget.ViewSwitcher;
@@ -106,7 +107,7 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
mEditor.jumpTo(line - 1, col);
}
if (msg != null) {
Snackbar.make(EditorView.this, getResources().getString(R.string.text_error) + ": " + msg, Snackbar.LENGTH_LONG).show();
showErrorMessage(msg);
}
}
}
@@ -151,7 +152,7 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
if (!intent.getBooleanExtra(EXTRA_RUN_ENABLED, true)) {
findViewById(R.id.run).setVisibility(GONE);
}
if(mReadOnly){
if (mReadOnly) {
mEditor.setReadOnly(true);
}
@@ -357,6 +358,13 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
mEditor.replaceAll(keywords, replacement, usingRegex);
}
private void showErrorMessage(String msg) {
Snackbar.make(EditorView.this, getResources().getString(R.string.text_error) + ": " + msg, Snackbar.LENGTH_LONG)
.setAction(R.string.text_detail, v -> LogActivity_.intent(getContext()).start())
.show();
}
@Override
public void onHintClick(CodeCompletions completions, int pos) {
if (completions.shouldBeInserted()) {

View File

@@ -332,4 +332,5 @@
<string name="text_weekly_task_should_check_day_of_week">至少选择一周中的一天</string>
<string name="no_apk_builder_plugin">没有安装打包插件,是否立即下载?</string>
<string name="text_apk_builder_plugin_unavailable">打包插件不可用</string>
<string name="text_detail">详情</string>
</resources>

View File

@@ -65,7 +65,7 @@ require("__general__")(__runtime__, this);
(function(scope){
var modules = ['app', 'automator', 'console', 'dialogs', 'io', 'selector', 'shell', 'web', 'ui',
"images", "timers", "events", "engines", "RootAutomator", "http"];
"images", "timers", "events", "engines", "RootAutomator", "http", "storages"];
var len = modules.length;
for(var i = 0; i < len; i++) {
var m = modules[i];

View File

@@ -230,7 +230,7 @@ JSON = {};
var partial;
var value = holder[key];
if(value.getClass){
if(value && value.getClass){
return gson.toJson(value);
}

View File

@@ -0,0 +1,40 @@
module.exports = function(__runtime__, scope){
var storages = {};
storages.create = function(name){
return new LocalStorage(name);
}
storages.remove = function(name){
this.create(name).clear();
}
return storages;
function LocalStorage(name){
this._storage = new com.stardust.autojs.core.storage.LocalStorage(context, name);
this.put = function(key, value){
if(typeof(value) == 'undefined'){
throw new TypeError('value cannot be undefined');
}
this._storage.put(key, JSON.stringify(value));
}
this.get = function(key, defaultValue){
var value = this._storage.getString(key, null);
if(!value){
return defaultValue;
}
return JSON.parse(value);
}
this.remove = function(key){
this._storage.remove(key);
}
this.contains = function(key){
return this._storage.contains(key);
}
this.clear = function(key){
this._storage.clear();
}
}
}

View File

@@ -0,0 +1,75 @@
package com.stardust.autojs.core.storage;
import android.content.Context;
import android.content.SharedPreferences;
/**
* Created by Stardust on 2017/12/3.
*/
public class LocalStorage {
private static final String NAME_PREFIX = "autojs.localstorage.";
private SharedPreferences mSharedPreferences;
public LocalStorage(Context context, String name) {
mSharedPreferences = context.getSharedPreferences(NAME_PREFIX + name, Context.MODE_PRIVATE);
}
public LocalStorage put(String key, String value) {
mSharedPreferences.edit()
.putString(key, value)
.apply();
return this;
}
public LocalStorage put(String key, long value) {
mSharedPreferences.edit()
.putLong(key, value)
.apply();
return this;
}
public LocalStorage put(String key, boolean value) {
mSharedPreferences.edit()
.putBoolean(key, value)
.apply();
return this;
}
public long getNumber(String key, long defaultValue) {
return mSharedPreferences.getLong(key, defaultValue);
}
public boolean getBoolean(String key, boolean defaultValue) {
return mSharedPreferences.getBoolean(key, defaultValue);
}
public String getString(String key, String defaultValue) {
return mSharedPreferences.getString(key, defaultValue);
}
public long getNumber(String key) {
return getNumber(key, 0);
}
public boolean getBoolean(String key) {
return getBoolean(key, false);
}
public String getString(String key) {
return getString(key, null);
}
public void remove(String key) {
mSharedPreferences.edit().remove(key).apply();
}
public boolean contains(String key) {
return mSharedPreferences.contains(key);
}
public void clear() {
mSharedPreferences.edit().clear().apply();
}
}

View File

@@ -110,7 +110,7 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
private String readInitScript() {
try {
return PFiles.read(mAndroidContext.getAssets().open("javascript_engine_init.js"));
return PFiles.read(mAndroidContext.getAssets().open("init.js"));
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -164,8 +164,8 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
private class WrapFactory extends org.mozilla.javascript.WrapFactory {
@Override
public Object wrap(Context cx, Scriptable scope, Object obj, Class<?> staticType) {
if (staticType == String.class) {
return getRuntime().bridges.toString(obj);
if (obj instanceof CharSequence) {
return getRuntime().bridges.toString(obj.toString());
}
if (staticType == UiObjectCollection.class) {
return getRuntime().bridges.toArray(obj);

View File

@@ -1,7 +1 @@
auto();
console.log("Hello, Auto.js");
toast("Hello");
launch("com.tencent.mm");
sleep(500);
while(!click("发现"));
while(!click("扫一扫"));
toast(files.read("/sdcard/1.txt"));

View File

@@ -2,6 +2,7 @@ package com.stardust.auojs.inrt;
import android.Manifest;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
@@ -29,8 +30,7 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupView();
checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
runScript();
checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
private void setupView() {
@@ -41,31 +41,33 @@ public class MainActivity extends AppCompatActivity {
private void runScript() {
new Thread(new Runnable() {
@Override
public void run() {
try {
String js = PFiles.read(getAssets().open("script.js"));
StringScriptSource source = new StringScriptSource("main", js);
AutoJs.getInstance().getScriptEngineService().execute(source);
} catch (Exception e) {
AutoJs.getInstance().getGlobalConsole().log(e);
}
new Thread(() -> {
try {
String js = PFiles.read(getAssets().open("script.js"));
StringScriptSource source = new StringScriptSource("main", js);
AutoJs.getInstance().getScriptEngineService().execute(source);
} catch (Exception e) {
AutoJs.getInstance().getGlobalConsole().log(e);
}
}).start();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
runScript();
}
protected void checkPermission(String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String[] requestPermissions = getRequestPermissions(permissions);
if (requestPermissions.length > 0) {
requestPermissions(requestPermissions, PERMISSION_REQUEST_CODE);
return;
}
} else {
int[] grantResults = new int[permissions.length];
Arrays.fill(grantResults, PERMISSION_GRANTED);
onRequestPermissionsResult(PERMISSION_REQUEST_CODE, permissions, grantResults);
}
int[] grantResults = new int[permissions.length];
Arrays.fill(grantResults, PERMISSION_GRANTED);
onRequestPermissionsResult(PERMISSION_REQUEST_CODE, permissions, grantResults);
}