weex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sospar...@apache.org
Subject [6/7] incubator-weex git commit: load weex js with file scheme (#2743)
Date Wed, 01 Mar 2017 02:21:03 GMT
load weex js with file scheme (#2743)

* load weex js with file scheme

* rename method name


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/91e884e0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/91e884e0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/91e884e0

Branch: refs/heads/0.11-dev
Commit: 91e884e03d779e2af42c98180d054fe587b8292b
Parents: 64b0edd
Author: dreaming <ilybeen@gmail.com>
Authored: Wed Mar 1 10:09:39 2017 +0800
Committer: sospartan zheng <sospartan@apache.org>
Committed: Wed Mar 1 10:09:39 2017 +0800

----------------------------------------------------------------------
 .../java/com/taobao/weex/WXSDKInstance.java     |  2 +-
 .../java/com/taobao/weex/utils/WXFileUtils.java | 37 +++++++++++++++++++-
 2 files changed, 37 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/91e884e0/android/sdk/src/main/java/com/taobao/weex/WXSDKInstance.java
----------------------------------------------------------------------
diff --git a/android/sdk/src/main/java/com/taobao/weex/WXSDKInstance.java b/android/sdk/src/main/java/com/taobao/weex/WXSDKInstance.java
index 70c6802..7c0ba9b 100755
--- a/android/sdk/src/main/java/com/taobao/weex/WXSDKInstance.java
+++ b/android/sdk/src/main/java/com/taobao/weex/WXSDKInstance.java
@@ -570,7 +570,7 @@ public class WXSDKInstance implements IWXActivityStateListener,DomContext,
View.
 
     Uri uri = Uri.parse(url);
     if (uri != null && TextUtils.equals(uri.getScheme(), "file")) {
-      render(pageName, WXFileUtils.loadAsset(assembleFilePath(uri), mContext), renderOptions,
jsonInitData, flag);
+      render(pageName, WXFileUtils.loadFileOrAsset(assembleFilePath(uri), mContext), renderOptions,
jsonInitData, flag);
       return;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/91e884e0/android/sdk/src/main/java/com/taobao/weex/utils/WXFileUtils.java
----------------------------------------------------------------------
diff --git a/android/sdk/src/main/java/com/taobao/weex/utils/WXFileUtils.java b/android/sdk/src/main/java/com/taobao/weex/utils/WXFileUtils.java
index d4269fd..ebbe87b 100755
--- a/android/sdk/src/main/java/com/taobao/weex/utils/WXFileUtils.java
+++ b/android/sdk/src/main/java/com/taobao/weex/utils/WXFileUtils.java
@@ -208,6 +208,9 @@ import android.content.Context;
 import android.text.TextUtils;
 
 import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -216,6 +219,29 @@ import java.io.InputStreamReader;
 public class WXFileUtils {
 
   /**
+   * Load file in device directory, if not exist, load from asset directory.
+   * @param path FilePath
+   * @param context Weex Context
+   * @return the Content of the file
+   */
+  public static String loadFileOrAsset(String path, Context context) {
+    if (!TextUtils.isEmpty(path)) {
+      File file = new File(path);
+      if (file.exists()) {
+        try {
+          FileInputStream fis = new FileInputStream(file);
+          return readStreamToString(fis);
+        } catch (FileNotFoundException e) {
+          e.printStackTrace();
+        }
+      } else {
+        return loadAsset(path, context);
+      }
+    }
+    return "";
+  }
+
+  /**
    * Load file in asset directory.
    * @param path FilePath
    * @param context Weex Context
@@ -226,9 +252,18 @@ public class WXFileUtils {
       return null;
     }
     InputStream inputStream = null;
-    BufferedReader bufferedReader = null;
     try {
       inputStream = context.getAssets().open(path);
+      return readStreamToString(inputStream);
+    } catch (IOException e) {
+      e.printStackTrace();
+    }
+    return "";
+  }
+
+  private static String readStreamToString(InputStream inputStream) {
+    BufferedReader bufferedReader = null;
+    try {
       StringBuilder builder = new StringBuilder(inputStream.available() + 10);
       bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
       char[] data = new char[4096];


Mime
View raw message