Add resource system for doric,start with android

This commit is contained in:
pengfei.zhou
2021-10-20 18:57:17 +08:00
committed by osborn
parent 7637a820e5
commit 5501dd38d9
26 changed files with 880 additions and 23 deletions

View File

@@ -41,6 +41,11 @@ import pub.doric.plugin.ShaderPlugin;
import pub.doric.plugin.StatusBarPlugin;
import pub.doric.plugin.StoragePlugin;
import pub.doric.refresh.RefreshableNode;
import pub.doric.resource.DoricAssetsLoader;
import pub.doric.resource.DoricAndroidLoader;
import pub.doric.resource.DoricLocalLoader;
import pub.doric.resource.DoricRemoteLoader;
import pub.doric.resource.DoricResourceManager;
import pub.doric.shader.DraggableNode;
import pub.doric.shader.GestureContainerNode;
import pub.doric.shader.HLayoutNode;
@@ -77,6 +82,7 @@ public class DoricRegistry {
private Drawable defaultPlaceHolderDrawable = null;
private Drawable defaultErrorDrawable = null;
private final DoricResourceManager doricResourceManager = new DoricResourceManager();
private void initRegistry(DoricRegistry doricRegistry) {
@@ -123,6 +129,11 @@ public class DoricRegistry {
this.registerViewNode(SwitchNode.class);
this.registerViewNode(FlexNode.class);
this.registerViewNode(GestureContainerNode.class);
this.getResourceManager().registerLoader(new DoricAndroidLoader("drawable"));
this.getResourceManager().registerLoader(new DoricAndroidLoader("raw"));
this.getResourceManager().registerLoader(new DoricAssetsLoader());
this.getResourceManager().registerLoader(new DoricLocalLoader());
this.getResourceManager().registerLoader(new DoricRemoteLoader());
initRegistry(this);
doricJSEngine.setEnvironmentValue(DoricSingleton.getInstance().envMap);
DoricSingleton.getInstance().registries.add(new WeakReference<>(this));
@@ -218,4 +229,8 @@ public class DoricRegistry {
public static void register(DoricLibrary doricLibrary) {
DoricSingleton.getInstance().registerLibrary(doricLibrary);
}
public DoricResourceManager getResourceManager() {
return doricResourceManager;
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.resource;
import pub.doric.DoricContext;
/**
* @Description: This loads resource from android resource such as R.drawable,R.raw and so on
* @Author: pengfei.zhou
* @CreateDate: 2021/10/20
*/
public class DoricAndroidLoader implements DoricResourceLoader {
private final String defType;
public DoricAndroidLoader(String defType) {
this.defType = defType;
}
@Override
public String resourceType() {
return this.defType;
}
@Override
public DoricResource load(DoricContext doricContext, String identifier) {
return new DoricAndroidResource(this.defType, identifier, doricContext);
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.resource;
import java.io.InputStream;
import pub.doric.DoricContext;
import pub.doric.async.AsyncResult;
/**
* @Description: This represents an android resource like a drawable from R.drawable
* @Author: pengfei.zhou
* @CreateDate: 2021/10/20
*/
public class DoricAndroidResource extends DoricResource {
private final String defType;
private final String identifier;
public DoricAndroidResource(String defType, String identifier, DoricContext doricContext) {
super(doricContext);
this.defType = defType;
this.identifier = identifier;
}
@Override
public AsyncResult<InputStream> asInputStream() {
AsyncResult<InputStream> result = new AsyncResult<>();
int resId = doricContext.getContext().getResources().getIdentifier(
identifier,
defType,
doricContext.getContext().getPackageName());
if (resId > 0) {
result.setResult(doricContext.getContext().getResources().openRawResource(resId));
} else {
result.setError(new Throwable("Cannot find resource for :" + identifier + ",type = " + defType));
}
return result;
}
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.resource;
import pub.doric.DoricContext;
/**
* @Description: This loads resource from android's assets dir
* @Author: pengfei.zhou
* @CreateDate: 2021/10/20
*/
public class DoricAssetsLoader implements DoricResourceLoader {
@Override
public String resourceType() {
return "assets";
}
@Override
public DoricResource load(DoricContext doricContext, String identifier) {
return new DoricAssetsResource(identifier, doricContext);
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.resource;
import java.io.IOException;
import java.io.InputStream;
import pub.doric.DoricContext;
import pub.doric.async.AsyncResult;
/**
* @Description: This represents a resource from assets
* @Author: pengfei.zhou
* @CreateDate: 2021/10/20
*/
public class DoricAssetsResource extends DoricResource {
private final String path;
public DoricAssetsResource(String path, DoricContext doricContext) {
super(doricContext);
this.path = path;
}
@Override
public AsyncResult<InputStream> asInputStream() {
AsyncResult<InputStream> result = new AsyncResult<>();
try {
InputStream inputStream = doricContext.getContext().getAssets().open(path);
result.setResult(inputStream);
} catch (IOException e) {
result.setError(e);
}
return result;
}
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.resource;
import pub.doric.DoricContext;
/**
* @Description: This loads resource from file system
* @Author: pengfei.zhou
* @CreateDate: 2021/10/20
*/
public class DoricLocalLoader implements DoricResourceLoader {
@Override
public String resourceType() {
return "local";
}
@Override
public DoricResource load(DoricContext doricContext, String identifier) {
return new DoricLocalResource(doricContext, identifier);
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.resource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import pub.doric.DoricContext;
import pub.doric.async.AsyncResult;
/**
* @Description: This represents a local file
* @Author: pengfei.zhou
* @CreateDate: 2021/10/20
*/
public class DoricLocalResource extends DoricResource {
private final String filePath;
public DoricLocalResource(DoricContext doricContext, String identifier) {
super(doricContext);
this.filePath = identifier;
}
@Override
public AsyncResult<InputStream> asInputStream() {
AsyncResult<InputStream> result = new AsyncResult<>();
try {
result.setResult(new FileInputStream(filePath));
} catch (FileNotFoundException e) {
result.setError(e);
}
return result;
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.resource;
import pub.doric.DoricContext;
/**
* @Description: This loads resource from network
* @Author: pengfei.zhou
* @CreateDate: 2021/10/20
*/
public class DoricRemoteLoader implements DoricResourceLoader {
@Override
public String resourceType() {
return "remote";
}
@Override
public DoricResource load(DoricContext doricContext, String identifier) {
return new DoricRemoteResource(doricContext, identifier);
}
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.resource;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import androidx.annotation.Nullable;
import pub.doric.DoricContext;
import pub.doric.async.AsyncResult;
/**
* @Description: This represents a resource from network
* @Author: pengfei.zhou
* @CreateDate: 2021/10/20
*/
public class DoricRemoteResource extends DoricResource {
private final String url;
public DoricRemoteResource(DoricContext doricContext, String identifier) {
super(doricContext);
this.url = identifier;
}
@Override
public AsyncResult<InputStream> asInputStream() {
final AsyncResult<InputStream> result = new AsyncResult<>();
Glide.with(doricContext.getContext()).download(url)
.listener(new RequestListener<File>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<File> target, boolean isFirstResource) {
result.setError(e);
return false;
}
@Override
public boolean onResourceReady(File resource, Object model, Target<File> target, DataSource dataSource, boolean isFirstResource) {
try {
result.setResult(new FileInputStream(resource));
} catch (FileNotFoundException e) {
result.setError(e);
}
return false;
}
})
.submit();
return result;
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.resource;
import java.io.InputStream;
import pub.doric.DoricContext;
import pub.doric.async.AsyncResult;
/**
* @Description: This represents a resource entity
* @Author: pengfei.zhou
* @CreateDate: 2021/10/20
*/
public abstract class DoricResource {
protected final DoricContext doricContext;
public DoricResource(DoricContext doricContext) {
this.doricContext = doricContext;
}
public abstract AsyncResult<InputStream> asInputStream();
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.resource;
import pub.doric.DoricContext;
/**
* @Description: For loading resources
* @Author: pengfei.zhou
* @CreateDate: 2021/10/20
*/
public interface DoricResourceLoader {
/**
* Determines which type's resource this loader could load
*
* @return type's name of this resource loader
*/
String resourceType();
/**
* Load resource by identifier in your way
*
* @param doricContext which context current running
* @param identifier Identifies the resource
* @return Resource's inputStream
*/
DoricResource load(DoricContext doricContext, String identifier);
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.resource;
import java.util.HashMap;
import java.util.Map;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import pub.doric.DoricContext;
/**
* @Description: This manages all resource loaders
* @Author: pengfei.zhou
* @CreateDate: 2021/10/20
*/
public class DoricResourceManager {
private final Map<String, DoricResourceLoader> mResourceLoaders = new HashMap<>();
public void registerLoader(DoricResourceLoader loader) {
mResourceLoaders.put(loader.resourceType(), loader);
}
public void unRegisterLoader(DoricResourceLoader loader) {
mResourceLoaders.remove(loader.resourceType());
}
@Nullable
public DoricResource load(@NonNull DoricContext doricContext, @NonNull String type, @NonNull String identifier) {
DoricResourceLoader loader = mResourceLoaders.get(type);
if (loader != null) {
return loader.load(doricContext, identifier);
}
return null;
}
}

View File

@@ -53,11 +53,15 @@ import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue;
import java.io.File;
import java.io.InputStream;
import jp.wasabeef.glide.transformations.BlurTransformation;
import pub.doric.DoricContext;
import pub.doric.DoricSingleton;
import pub.doric.async.AsyncResult;
import pub.doric.extension.bridge.DoricMethod;
import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.resource.DoricResource;
import pub.doric.shader.flex.FlexNode;
import pub.doric.utils.DoricLog;
import pub.doric.utils.DoricUtils;
@@ -358,6 +362,36 @@ public class ImageNode extends ViewNode<ImageView> {
@Override
protected void blend(ImageView view, String name, JSValue prop) {
switch (name) {
case "image":
if (!prop.isObject()) {
return;
}
JSObject resource = prop.asObject();
final String type = resource.getProperty("type").asString().value();
final String identifier = resource.getProperty("identifier").asString().value();
DoricResource doricResource = getDoricContext().getDriver().getRegistry().getResourceManager().load(getDoricContext(), type, identifier);
if (doricResource != null) {
doricResource.asInputStream().setCallback(new AsyncResult.Callback<InputStream>() {
@Override
public void onResult(InputStream result) {
loadIntoTarget(Glide.with(getContext()).load(result));
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
DoricLog.e("Cannot load resource type = %s, identifier = %s, %s", type, identifier, t.getLocalizedMessage());
}
@Override
public void onFinish() {
}
});
} else {
DoricLog.e("Cannot find loader for resource type = %s, identifier = %s", type, identifier);
}
break;
case "imageUrl":
if (!prop.isString()) {
return;