diff --git a/Android/doric/src/main/java/pub/doric/DoricPanelFragment.java b/Android/doric/src/main/java/pub/doric/DoricPanelFragment.java new file mode 100644 index 00000000..2acd181e --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/DoricPanelFragment.java @@ -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); + } +} diff --git a/Android/doric/src/main/java/pub/doric/DoricRegistry.java b/Android/doric/src/main/java/pub/doric/DoricRegistry.java index e39d156c..3eb87e6c 100644 --- a/Android/doric/src/main/java/pub/doric/DoricRegistry.java +++ b/Android/doric/src/main/java/pub/doric/DoricRegistry.java @@ -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 bundles = new ConcurrentHashMap<>(); + private static Set doricLibraries = new HashSet<>(); + private static Set jsLoaders = new HashSet<>(); + + static { + addJSLoader(new DoricAssetJSLoader()); + addJSLoader(new DoricHttpJSLoader()); + } + private Map> pluginInfoMap = new HashMap<>(); private Map> nodeInfoMap = new HashMap<>(); - private static Set 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 getJSLoaders() { + return jsLoaders; + } } diff --git a/Android/doric/src/main/java/pub/doric/async/AsyncResult.java b/Android/doric/src/main/java/pub/doric/async/AsyncResult.java index 0b06f9ee..2f6af37a 100644 --- a/Android/doric/src/main/java/pub/doric/async/AsyncResult.java +++ b/Android/doric/src/main/java/pub/doric/async/AsyncResult.java @@ -26,6 +26,13 @@ public class AsyncResult { private Callback callback = null; + public AsyncResult() { + } + + public AsyncResult(R r) { + this.result = r; + } + public void setResult(R result) { this.result = result; if (this.callback != null) { diff --git a/Android/doric/src/main/java/pub/doric/loader/DoricAssetJSLoader.java b/Android/doric/src/main/java/pub/doric/loader/DoricAssetJSLoader.java new file mode 100644 index 00000000..2352d971 --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/loader/DoricAssetJSLoader.java @@ -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 request(String scheme) { + return new AsyncResult<>(DoricUtils.readAssetFile(scheme.substring("assets://".length()))); + } +} diff --git a/Android/doric/src/main/java/pub/doric/loader/DoricHttpJSLoader.java b/Android/doric/src/main/java/pub/doric/loader/DoricHttpJSLoader.java new file mode 100644 index 00000000..ca3732a6 --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/loader/DoricHttpJSLoader.java @@ -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 request(String scheme) { + final AsyncResult 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; + } +} diff --git a/Android/doric/src/main/java/pub/doric/loader/DoricJSLoaderManager.java b/Android/doric/src/main/java/pub/doric/loader/DoricJSLoaderManager.java new file mode 100644 index 00000000..f3c01898 --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/loader/DoricJSLoaderManager.java @@ -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 loadJSBundle(String scheme) { + Collection jsLoaders = DoricRegistry.getJSLoaders(); + for (IDoricJSLoader jsLoader : jsLoaders) { + if (jsLoader.filter(scheme)) { + return jsLoader.request(scheme); + } + } + return new AsyncResult<>(""); + } + +} diff --git a/Android/doric/src/main/java/pub/doric/loader/IDoricJSLoader.java b/Android/doric/src/main/java/pub/doric/loader/IDoricJSLoader.java new file mode 100644 index 00000000..c26f453f --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/loader/IDoricJSLoader.java @@ -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 request(String scheme); +}