This source file includes following definitions.
- JNINamespace
- setResources
- setErrorPageResources
- setDefaultTextEncoding
- getDefaultTextEncoding
- getNoDomainPageContent
- getLoadErrorPageContent
- getResource
- getRawFileResourceContent
package org.chromium.android_webview;
import android.content.res.Resources;
import android.util.SparseArray;
import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.ref.SoftReference;
import java.util.NoSuchElementException;
import java.util.Scanner;
@JNINamespace("android_webview::AwResource")
public class AwResource {
private static int RAW_LOAD_ERROR;
private static int RAW_NO_DOMAIN;
private static int STRING_DEFAULT_TEXT_ENCODING;
private static Resources sResources;
private static SparseArray<SoftReference<String>> sResourceCache;
private static final int TYPE_STRING = 0;
private static final int TYPE_RAW = 1;
public static void setResources(Resources resources) {
sResources = resources;
sResourceCache = new SparseArray<SoftReference<String>>();
}
public static void setErrorPageResources(int loaderror, int nodomain) {
RAW_LOAD_ERROR = loaderror;
RAW_NO_DOMAIN = nodomain;
}
public static void setDefaultTextEncoding(int encoding) {
STRING_DEFAULT_TEXT_ENCODING = encoding;
}
@CalledByNative
public static String getDefaultTextEncoding() {
return getResource(STRING_DEFAULT_TEXT_ENCODING, TYPE_STRING);
}
@CalledByNative
public static String getNoDomainPageContent() {
return getResource(RAW_NO_DOMAIN, TYPE_RAW);
}
@CalledByNative
public static String getLoadErrorPageContent() {
return getResource(RAW_LOAD_ERROR, TYPE_RAW);
}
private static String getResource(int resid, int type) {
assert resid != 0;
assert sResources != null;
assert sResourceCache != null;
SoftReference<String> stringRef = sResourceCache.get(resid);
String result = stringRef == null ? null : stringRef.get();
if (result == null) {
switch (type) {
case TYPE_STRING:
result = sResources.getString(resid);
break;
case TYPE_RAW:
result = getRawFileResourceContent(resid);
break;
default:
throw new IllegalArgumentException("Unknown resource type");
}
sResourceCache.put(resid, new SoftReference<String>(result));
}
return result;
}
private static String getRawFileResourceContent(int resid) {
assert resid != 0;
assert sResources != null;
InputStreamReader isr = null;
String result = null;
try {
isr = new InputStreamReader(
sResources.openRawResource(resid));
result = new Scanner(isr).useDelimiter("\\A").next();
} catch (Resources.NotFoundException e) {
return "";
} catch (NoSuchElementException e) {
return "";
} finally {
try {
if (isr != null) {
isr.close();
}
} catch (IOException e) {
}
}
return result;
}
}