1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.xwalk.runtime;
7 import android.app.Activity;
8 import android.content.res.AssetManager;
9 import android.util.Log;
11 import java.io.IOException;
12 import java.io.InputStream;
14 import java.net.URISyntaxException;
17 * This internal class parses manifest.json of current app.
19 public class XWalkManifestReader {
20 private final static String TAG = "XWalkManifestReader";
21 private final static String ASSETS_FILE_PATH = "file:///android_asset/";
22 private final static String WWW_FOLDER = "www";
23 private final static String APP_SCHEME = "app";
25 private Activity mActivity;
27 public XWalkManifestReader(Activity activity) {
31 public String read(String manifestPath) {
32 manifestPath = getAssetsPath(manifestPath);
34 String manifestString = null;
36 manifestString = getAssetsFileContent(mActivity.getAssets(), manifestPath);
37 } catch (IOException e) {
38 throw new RuntimeException("Failed to read manifest.json", e);
40 return manifestString;
43 private String getAssetsFileContent(AssetManager assetManager, String fileName) throws IOException {
45 InputStream inputStream = null;
47 inputStream = assetManager.open(fileName);
48 int size = inputStream.available();
49 byte[] buffer = new byte[size];
50 inputStream.read(buffer);
51 result = new String(buffer);
53 if (inputStream != null) {
60 private String getAssetsPath(String path) {
61 if (path == null || path.isEmpty()) {
69 } catch (URISyntaxException e) {
70 Log.e(TAG, "Invalid manifest URI: " + path, e);
73 if (uri.getScheme().equals(APP_SCHEME)) {
74 assetsPath = WWW_FOLDER + uri.getPath();
75 } else if (path.startsWith(ASSETS_FILE_PATH)) {
76 assetsPath = path.substring(ASSETS_FILE_PATH.length());