// Copyright (c) 2013 Intel Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.xwalk.runtime;

import android.app.Activity;
import android.content.res.AssetManager;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;

/**
 * This internal class parses manifest.json of current app.
 */
public class XWalkManifestReader {
    private final static String TAG = "XWalkManifestReader";
    private final static String ASSETS_FILE_PATH = "file:///android_asset/";

    private Activity mActivity;

    public XWalkManifestReader(Activity activity) {
        mActivity = activity;
    }

    public String read(String manifestPath) {
        manifestPath = deletePrefix(manifestPath, ASSETS_FILE_PATH);

        String manifestString = null;
        try {
            manifestString = getAssetsFileContent(mActivity.getAssets(), manifestPath);
        } catch (IOException e) {
            throw new RuntimeException("Failed to read manifest.json", e);
        }
        return manifestString;
    }

    private String getAssetsFileContent(AssetManager assetManager, String fileName) throws IOException {
        String result = "";
        InputStream inputStream = null;
        try {
            inputStream = assetManager.open(fileName);
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            inputStream.read(buffer);
            result = new String(buffer);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
        return result;
    }

    private String deletePrefix(String str, String prefix) {
        String result = str;
        if (str != null && prefix != null && str.startsWith(prefix)) {
            result = str.substring(prefix.length());
        }
        return result;
    }
}

@@ -339,16 +339,73 @@
 tStream;
+%0Aimport java.net.URI;%0Aimport java.net.URISyntaxException;
 %0A%0A/**%0A *
@@ -638,16 +638,120 @@
 asset/%22;
+%0A    private final static String WWW_FOLDER = %22www%22;%0A    private final static String APP_SCHEME = %22app%22;
 %0A%0A    pr
@@ -939,51 +939,34 @@
 h = 
-deletePrefix(manifestPath, ASSETS_FILE_PATH
+getAssetsPath(manifestPath
 );%0A%0A
@@ -1848,212 +1848,614 @@
 ing 
-deletePrefix(String str, String prefix) %7B%0A        String result = str;%0A        if (str != null && prefix != null && str.startsWith(prefix)) %7B%0A            result = str.substring(prefix.length());%0A        %7D
+getAssetsPath(String path) %7B%0A        if (path == null %7C%7C path.isEmpty()) %7B%0A            return null;%0A        %7D%0A%0A        String assetsPath;%0A        URI uri = null;%0A        try %7B%0A            uri = new URI(path);%0A        %7D catch (URISyntaxException e) %7B%0A            Log.e(TAG, %22Invalid manifest URI: %22 + path, e);%0A        %7D%0A%0A        if (uri.getScheme().equals(APP_SCHEME)) %7B%0A            assetsPath = WWW_FOLDER + uri.getPath();%0A        %7D else if (path.startsWith(ASSETS_FILE_PATH)) %7B%0A            assetsPath = path.substring(ASSETS_FILE_PATH.length());%0A        %7D else %7B%0A            assetsPath = null;%0A        %7D%0A
 %0A   
@@ -2458,32 +2458,36 @@
 %0A        return 
-result
+assetsPath
 ;%0A    %7D%0A%7D%0A

Fix crash issue of loading app from manifest with app scheme.