Skip to content

Commit

Permalink
[#595]数据存储storage增加同步调用 (#596)
Browse files Browse the repository at this point in the history
Change-Id: I5debe4731dc734bd5d7092504e7d4eb6b8840c35
  • Loading branch information
changhuii authored Oct 19, 2023
1 parent 736f3ed commit 567429e
Showing 1 changed file with 63 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/*
* Copyright (c) 2021, the hapjs-platform Project Contributors
* Copyright (c) 2021-present, the hapjs-platform Project Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.hapjs.features.storage.data;

import android.text.TextUtils;

import org.hapjs.bridge.FeatureExtension;
import org.hapjs.bridge.Request;
import org.hapjs.bridge.Response;
Expand Down Expand Up @@ -36,7 +37,12 @@
mode = FeatureExtension.Mode.SYNC,
type = FeatureExtension.Type.ATTRIBUTE,
access = FeatureExtension.Access.READ,
alias = LocalStorageFeature.ATTR_LENGTH_ALIAS)
alias = LocalStorageFeature.ATTR_LENGTH_ALIAS),
@ActionAnnotation(name = LocalStorageFeature.ACTION_SET_SYNC, mode = FeatureExtension.Mode.SYNC),
@ActionAnnotation(name = LocalStorageFeature.ACTION_GET_SYNC, mode = FeatureExtension.Mode.SYNC),
@ActionAnnotation(name = LocalStorageFeature.ACTION_DELETE_SYNC, mode = FeatureExtension.Mode.SYNC),
@ActionAnnotation(name = LocalStorageFeature.ACTION_CLEAR_SYNC, mode = FeatureExtension.Mode.SYNC),
@ActionAnnotation(name = LocalStorageFeature.ACTION_KEY_SYNC, mode = FeatureExtension.Mode.SYNC)
})
public class LocalStorageFeature extends FeatureExtension {
protected static final String FEATURE_NAME = "system.storage";
Expand All @@ -45,6 +51,12 @@ public class LocalStorageFeature extends FeatureExtension {
protected static final String ACTION_DELETE = "delete";
protected static final String ACTION_CLEAR = "clear";
protected static final String ACTION_KEY = "key";
protected static final String ACTION_SET_SYNC = "setSync";
protected static final String ACTION_GET_SYNC = "getSync";
protected static final String ACTION_DELETE_SYNC = "deleteSync";
protected static final String ACTION_CLEAR_SYNC = "clearSync";
protected static final String ACTION_KEY_SYNC = "keySync";


protected static final String ATTR_LENGTH_ALIAS = "length";
protected static final String ATTR_GET_LENGTH = "__getLength";
Expand Down Expand Up @@ -74,109 +86,111 @@ protected Response invokeInner(Request request) throws Exception {
invokeKey(request);
} else if (ATTR_GET_LENGTH.equals(action)) {
return invokeGetLength(request);
} else if (ACTION_SET_SYNC.equals(action)) {
return invokeSetSync(request);
} else if (ACTION_GET_SYNC.equals(action)) {
return invokeGetSync(request);
} else if (ACTION_DELETE_SYNC.equals(action)) {
return invokeDeleteSync(request);
} else if (ACTION_CLEAR_SYNC.equals(action)) {
return invokeClearSync(request);
} else if (ACTION_KEY_SYNC.equals(action)) {
return invokeKeySync(request);
}
return Response.SUCCESS;
}

private void invokeGet(Request request) throws JSONException {
Response response = invokeGetSync(request);
request.getCallback().callback(response);
}

private Response invokeGetSync(Request request) throws JSONException {
JSONObject jsonParams = request.getJSONParams();
String key = jsonParams.optString(PARAMS_KEY);
if (TextUtils.isEmpty(key)) {
request
.getCallback()
.callback(new Response(Response.CODE_ILLEGAL_ARGUMENT,
PARAMS_KEY + " not define"));
return;
return new Response(Response.CODE_ILLEGAL_ARGUMENT, PARAMS_KEY + " not define");
}
String value = getStorage(request).get(key);
if (value == null) {
// default param may be a null value
// so we should return empty string only when there's no default value
if (jsonParams.has(PARAMS_DEFAULT)) {
value = jsonParams.optString(PARAMS_DEFAULT, null);
value = jsonParams.optString(PARAMS_DEFAULT, "");
} else {
value = "";
}
}
Response response = new Response(value);
request.getCallback().callback(response);
return new Response(value);
}

private void invokeSet(Request request) throws JSONException {
Response response = invokeSetSync(request);
request.getCallback().callback(response);
}

private Response invokeSetSync(Request request) throws JSONException {
JSONObject jsonParams = request.getJSONParams();
String key = jsonParams.optString(PARAMS_KEY);
if (TextUtils.isEmpty(key)) {
request
.getCallback()
.callback(new Response(Response.CODE_ILLEGAL_ARGUMENT,
PARAMS_KEY + " not define"));
return;
return new Response(Response.CODE_ILLEGAL_ARGUMENT, PARAMS_KEY + " not define");
}
String value = jsonParams.optString(PARAMS_VALUE);
if (getStorage(request).set(key, value)) {
request.getCallback().callback(Response.SUCCESS);
} else {
request.getCallback().callback(Response.ERROR);
}
return (getStorage(request).set(key, value) ? Response.SUCCESS : Response.ERROR);
}

private void invokeDelete(Request request) throws JSONException {
Response response = invokeDeleteSync(request);
request.getCallback().callback(response);
}

private Response invokeDeleteSync(Request request) throws JSONException {
JSONObject jsonParams = request.getJSONParams();
String key = jsonParams.optString(PARAMS_KEY);
if (TextUtils.isEmpty(key)) {
request
.getCallback()
.callback(new Response(Response.CODE_ILLEGAL_ARGUMENT,
PARAMS_KEY + " not define"));
return;
return new Response(Response.CODE_ILLEGAL_ARGUMENT, PARAMS_KEY + " not define");
}
if (getStorage(request).delete(key)) {
request.getCallback().callback(Response.SUCCESS);
return Response.SUCCESS;
} else {
request.getCallback().callback(Response.ERROR);
return Response.ERROR;
}
}

private void invokeClear(Request request) {
Response response = invokeClearSync(request);
request.getCallback().callback(response);
}

private Response invokeClearSync(Request request) {
if (getStorage(request).clear()) {
request.getCallback().callback(Response.SUCCESS);
return Response.SUCCESS;
} else {
request.getCallback().callback(Response.ERROR);
return Response.ERROR;
}
}

private void invokeKey(Request request) throws JSONException {
Response response = invokeKeySync(request);
request.getCallback().callback(response);
}

private Response invokeKeySync(Request request) throws JSONException {
JSONObject jsonParams = request.getJSONParams();
int index = jsonParams.optInt(PARAMS_INDEX, -1);
if (index == -1) {
request
.getCallback()
.callback(new Response(Response.CODE_ILLEGAL_ARGUMENT,
PARAMS_INDEX + " not define"));
return;
return new Response(Response.CODE_ILLEGAL_ARGUMENT, PARAMS_INDEX + " not define");
}

if (index < 0) {
request
.getCallback()
.callback(new Response(Response.CODE_ILLEGAL_ARGUMENT,
"index: " + index + " must >= 0"));
return;
return new Response(Response.CODE_ILLEGAL_ARGUMENT, "index: " + index + " must >= 0");
}

String key = getStorage(request).key(index);
if (key == null) {
request
.getCallback()
.callback(
new Response(
Response.CODE_ILLEGAL_ARGUMENT,
"index: " + index + " must < storage.length"));
return;
return new Response(Response.CODE_ILLEGAL_ARGUMENT, "index: " + index + " must < storage.length");
}

Response response = new Response(key);
request.getCallback().callback(response);
return new Response(key);
}

private Response invokeGetLength(Request request) {
Expand Down

0 comments on commit 567429e

Please sign in to comment.