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

@@ -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);