Index: base/android/java/src/org/chromium/base/ResourceExtractor.java |
diff --git a/base/android/java/src/org/chromium/base/ResourceExtractor.java b/base/android/java/src/org/chromium/base/ResourceExtractor.java |
index 9e7adf29d2e39f58cda629a21cf74332b120884c..26db654f5a781154e68de96973942f9edd6ea9ab 100644 |
--- a/base/android/java/src/org/chromium/base/ResourceExtractor.java |
+++ b/base/android/java/src/org/chromium/base/ResourceExtractor.java |
@@ -4,6 +4,7 @@ |
package org.chromium.base; |
+import android.content.Context; |
import android.content.SharedPreferences; |
import android.content.pm.PackageInfo; |
import android.content.pm.PackageManager; |
@@ -11,6 +12,8 @@ import android.os.AsyncTask; |
import android.os.Handler; |
import android.os.Looper; |
+import org.chromium.base.annotations.SuppressFBWarnings; |
+ |
import java.io.File; |
import java.io.FileOutputStream; |
import java.io.IOException; |
@@ -18,7 +21,6 @@ import java.io.InputStream; |
import java.io.OutputStream; |
import java.util.ArrayList; |
import java.util.List; |
-import java.util.Locale; |
import java.util.concurrent.CancellationException; |
import java.util.concurrent.ExecutionException; |
@@ -33,7 +35,23 @@ public class ResourceExtractor { |
private static final String V8_NATIVES_DATA_FILENAME = "natives_blob.bin"; |
private static final String V8_SNAPSHOT_DATA_FILENAME = "snapshot_blob.bin"; |
private static final String APP_VERSION_PREF = "org.chromium.base.ResourceExtractor.Version"; |
- private static final String FALLBACK_LOCALE = "en-US"; |
+ |
+ private static ResourceEntry[] sResourcesToExtract = new ResourceEntry[0]; |
+ |
+ /** |
+ * Holds information about a res/raw file (e.g. locale .pak files). |
+ */ |
+ public static final class ResourceEntry { |
+ public final int resourceId; |
+ public final String pathWithinApk; |
+ public final String extractedFileName; |
+ |
+ public ResourceEntry(int resourceId, String pathWithinApk, String extractedFileName) { |
+ this.resourceId = resourceId; |
+ this.pathWithinApk = pathWithinApk; |
+ this.extractedFileName = extractedFileName; |
+ } |
+ } |
private class ExtractTask extends AsyncTask<Void, Void, Void> { |
private static final int BUFFER_SIZE = 16 * 1024; |
@@ -89,16 +107,16 @@ public class ResourceExtractor { |
TraceEvent.begin("WalkAssets"); |
byte[] buffer = new byte[BUFFER_SIZE]; |
try { |
- for (String assetName : mAssetsToExtract) { |
- File output = new File(outputDir, assetName); |
+ for (ResourceEntry entry : sResourcesToExtract) { |
+ File output = new File(outputDir, entry.extractedFileName); |
// TODO(agrieve): It would be better to check that .length == expectedLength. |
// http://crbug.com/606413 |
if (output.length() != 0) { |
continue; |
} |
TraceEvent.begin("ExtractResource"); |
- InputStream inputStream = |
- ContextUtils.getApplicationContext().getAssets().open(assetName); |
+ InputStream inputStream = mContext.getResources().openRawResource( |
+ entry.resourceId); |
try { |
extractResourceHelper(inputStream, output, buffer); |
} finally { |
@@ -110,7 +128,7 @@ public class ResourceExtractor { |
// Try to recover here, can we try again after deleting files instead of |
// returning null? It might be useful to gather UMA here too to track if |
// this happens with regularity. |
- Log.w(TAG, "Exception unpacking required pak asset: %s", e.getMessage()); |
+ Log.w(TAG, "Exception unpacking required pak resources: %s", e.getMessage()); |
deleteFiles(); |
return; |
} finally { |
@@ -148,11 +166,10 @@ public class ResourceExtractor { |
/** Returns a number that is different each time the apk changes. */ |
private long getApkVersion() { |
- PackageManager pm = ContextUtils.getApplicationContext().getPackageManager(); |
+ PackageManager pm = mContext.getPackageManager(); |
try { |
// More appropriate would be versionCode, but it doesn't change while developing. |
- PackageInfo pi = |
- pm.getPackageInfo(ContextUtils.getApplicationContext().getPackageName(), 0); |
+ PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), 0); |
return pi.lastUpdateTime; |
} catch (PackageManager.NameNotFoundException e) { |
throw new RuntimeException(e); |
@@ -160,38 +177,31 @@ public class ResourceExtractor { |
} |
} |
+ private final Context mContext; |
private ExtractTask mExtractTask; |
- private final String[] mAssetsToExtract = detectFilesToExtract(); |
private static ResourceExtractor sInstance; |
- public static ResourceExtractor get() { |
+ public static ResourceExtractor get(Context context) { |
if (sInstance == null) { |
- sInstance = new ResourceExtractor(); |
+ sInstance = new ResourceExtractor(context); |
} |
return sInstance; |
} |
- private static boolean isPakFileForLanguage(String pakFileName, String language) { |
- if (pakFileName.length() <= language.length() || !pakFileName.startsWith(language)) { |
- return false; |
- } |
- char c = pakFileName.charAt(language.length()); |
- return c == '.' || c == '-'; |
+ /** |
+ * Specifies the files that should be extracted from the APK. |
+ * and moved to {@link #getOutputDir()}. |
+ */ |
+ @SuppressFBWarnings("EI_EXPOSE_STATIC_REP2") |
+ public static void setResourcesToExtract(ResourceEntry[] entries) { |
+ assert (sInstance == null || sInstance.mExtractTask == null) |
+ : "Must be called before startExtractingResources is called"; |
+ sResourcesToExtract = entries; |
} |
- private static String[] detectFilesToExtract() { |
- String language = LocaleUtils.getLanguage(Locale.getDefault()); |
- // Currenty (Oct 2016), this array can be as big as 4 entries, so using a capacity |
- // that allows a bit of growth, but is still in the right ballpark.. |
- ArrayList<String> activeLocalePakFiles = new ArrayList<String>(6); |
- for (String pakFileName : BuildConfig.COMPRESSED_ASSETS) { |
- if (isPakFileForLanguage(pakFileName, language) |
- || isPakFileForLanguage(pakFileName, FALLBACK_LOCALE)) { |
- activeLocalePakFiles.add(pakFileName); |
- } |
- } |
- return activeLocalePakFiles.toArray(new String[activeLocalePakFiles.size()]); |
+ private ResourceExtractor(Context context) { |
+ mContext = context.getApplicationContext(); |
} |
/** |
@@ -319,6 +329,6 @@ public class ResourceExtractor { |
* Pak extraction not necessarily required by the embedder. |
*/ |
private static boolean shouldSkipPakExtraction() { |
- return get().mAssetsToExtract.length == 0; |
+ return sResourcesToExtract.length == 0; |
} |
} |