feat:add DoricJSLoader

This commit is contained in:
pengfei.zhou 2019-11-23 14:54:37 +08:00
parent 876fa98254
commit e1ac85d18a
7 changed files with 245 additions and 1 deletions

View File

@ -0,0 +1,38 @@
/*
* Copyright [2019] [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;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
/**
* @Description: pub.doric
* @Author: pengfei.zhou
* @CreateDate: 2019-11-23
*/
public class DoricPanelFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
}

View File

@ -17,6 +17,9 @@ package pub.doric;
import android.text.TextUtils;
import pub.doric.loader.DoricAssetJSLoader;
import pub.doric.loader.DoricHttpJSLoader;
import pub.doric.loader.IDoricJSLoader;
import pub.doric.plugin.NavigatorPlugin;
import pub.doric.plugin.NetworkPlugin;
import pub.doric.plugin.ShaderPlugin;
@ -37,6 +40,7 @@ import pub.doric.utils.DoricMetaInfo;
import pub.doric.plugin.DoricJavaPlugin;
import pub.doric.plugin.ModalPlugin;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@ -50,9 +54,16 @@ import java.util.concurrent.ConcurrentHashMap;
*/
public class DoricRegistry {
private static Map<String, String> bundles = new ConcurrentHashMap<>();
private static Set<DoricLibrary> doricLibraries = new HashSet<>();
private static Set<IDoricJSLoader> jsLoaders = new HashSet<>();
static {
addJSLoader(new DoricAssetJSLoader());
addJSLoader(new DoricHttpJSLoader());
}
private Map<String, DoricMetaInfo<DoricJavaPlugin>> pluginInfoMap = new HashMap<>();
private Map<String, DoricMetaInfo<ViewNode>> nodeInfoMap = new HashMap<>();
private static Set<DoricLibrary> doricLibraries = new HashSet<>();
private static void initRegistry(DoricRegistry doricRegistry) {
for (DoricLibrary library : doricLibraries) {
@ -114,4 +125,12 @@ public class DoricRegistry {
public String acquireJSBundle(String name) {
return bundles.get(name);
}
public static void addJSLoader(IDoricJSLoader jsLoader) {
jsLoaders.add(jsLoader);
}
public static Collection<IDoricJSLoader> getJSLoaders() {
return jsLoaders;
}
}

View File

@ -26,6 +26,13 @@ public class AsyncResult<R> {
private Callback<R> callback = null;
public AsyncResult() {
}
public AsyncResult(R r) {
this.result = r;
}
public void setResult(R result) {
this.result = result;
if (this.callback != null) {

View File

@ -0,0 +1,36 @@
/*
* Copyright [2019] [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.loader;
import pub.doric.async.AsyncResult;
import pub.doric.utils.DoricUtils;
/**
* @Description: handle "assets://asset-file-path"
* @Author: pengfei.zhou
* @CreateDate: 2019-11-23
*/
public class DoricAssetJSLoader implements IDoricJSLoader {
@Override
public boolean filter(String scheme) {
return scheme.startsWith("assets");
}
@Override
public AsyncResult<String> request(String scheme) {
return new AsyncResult<>(DoricUtils.readAssetFile(scheme.substring("assets://".length())));
}
}

View File

@ -0,0 +1,64 @@
/*
* Copyright [2019] [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.loader;
import com.bumptech.glide.RequestBuilder;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import pub.doric.async.AsyncResult;
/**
* @Description: handle like "https://xxxx.js"
* @Author: pengfei.zhou
* @CreateDate: 2019-11-23
*/
public class DoricHttpJSLoader implements IDoricJSLoader {
private OkHttpClient okHttpClient = new OkHttpClient();
@Override
public boolean filter(String scheme) {
return scheme.startsWith("http");
}
@Override
public AsyncResult<String> request(String scheme) {
final AsyncResult<String> ret = new AsyncResult<>();
okHttpClient.newCall(new Request.Builder().url(scheme).build()).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
ret.setError(e);
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) {
try {
ret.setResult(response.body().string());
} catch (Exception e) {
ret.setError(e);
}
}
});
return ret;
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright [2019] [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.loader;
import java.util.Collection;
import pub.doric.DoricRegistry;
import pub.doric.async.AsyncResult;
/**
* @Description: pub.doric
* @Author: pengfei.zhou
* @CreateDate: 2019-11-23
*/
public class DoricJSLoaderManager {
private DoricJSLoaderManager() {
}
private static class Inner {
private static final DoricJSLoaderManager sInstance = new DoricJSLoaderManager();
}
public static DoricJSLoaderManager getInstance() {
return Inner.sInstance;
}
public AsyncResult<String> loadJSBundle(String scheme) {
Collection<IDoricJSLoader> jsLoaders = DoricRegistry.getJSLoaders();
for (IDoricJSLoader jsLoader : jsLoaders) {
if (jsLoader.filter(scheme)) {
return jsLoader.request(scheme);
}
}
return new AsyncResult<>("");
}
}

View File

@ -0,0 +1,29 @@
/*
* Copyright [2019] [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.loader;
import pub.doric.async.AsyncResult;
/**
* @Description: pub.doric
* @Author: pengfei.zhou
* @CreateDate: 2019-11-23
*/
public interface IDoricJSLoader {
boolean filter(String scheme);
AsyncResult<String> request(String scheme);
}