Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(641)

Unified Diff: base/android/java/src/org/chromium/base/ResourceExtractor.java

Issue 2371843002: Reland of Move language pak files to assets. (Closed)
Patch Set: Fix ContentShell context setPrivateDataPrefix that was breaking tests Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 26db654f5a781154e68de96973942f9edd6ea9ab..9e7adf29d2e39f58cda629a21cf74332b120884c 100644
--- a/base/android/java/src/org/chromium/base/ResourceExtractor.java
+++ b/base/android/java/src/org/chromium/base/ResourceExtractor.java
@@ -4,7 +4,6 @@
package org.chromium.base;
-import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
@@ -12,8 +11,6 @@ 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;
@@ -21,6 +18,7 @@ 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;
@@ -35,23 +33,7 @@ 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 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 static final String FALLBACK_LOCALE = "en-US";
private class ExtractTask extends AsyncTask<Void, Void, Void> {
private static final int BUFFER_SIZE = 16 * 1024;
@@ -107,16 +89,16 @@ public class ResourceExtractor {
TraceEvent.begin("WalkAssets");
byte[] buffer = new byte[BUFFER_SIZE];
try {
- for (ResourceEntry entry : sResourcesToExtract) {
- File output = new File(outputDir, entry.extractedFileName);
+ for (String assetName : mAssetsToExtract) {
+ File output = new File(outputDir, assetName);
// 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 = mContext.getResources().openRawResource(
- entry.resourceId);
+ InputStream inputStream =
+ ContextUtils.getApplicationContext().getAssets().open(assetName);
try {
extractResourceHelper(inputStream, output, buffer);
} finally {
@@ -128,7 +110,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 resources: %s", e.getMessage());
+ Log.w(TAG, "Exception unpacking required pak asset: %s", e.getMessage());
deleteFiles();
return;
} finally {
@@ -166,10 +148,11 @@ public class ResourceExtractor {
/** Returns a number that is different each time the apk changes. */
private long getApkVersion() {
- PackageManager pm = mContext.getPackageManager();
+ PackageManager pm = ContextUtils.getApplicationContext().getPackageManager();
try {
// More appropriate would be versionCode, but it doesn't change while developing.
- PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), 0);
+ PackageInfo pi =
+ pm.getPackageInfo(ContextUtils.getApplicationContext().getPackageName(), 0);
return pi.lastUpdateTime;
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(e);
@@ -177,31 +160,38 @@ public class ResourceExtractor {
}
}
- private final Context mContext;
private ExtractTask mExtractTask;
+ private final String[] mAssetsToExtract = detectFilesToExtract();
private static ResourceExtractor sInstance;
- public static ResourceExtractor get(Context context) {
+ public static ResourceExtractor get() {
if (sInstance == null) {
- sInstance = new ResourceExtractor(context);
+ sInstance = new ResourceExtractor();
}
return sInstance;
}
- /**
- * 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 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 == '-';
}
- private ResourceExtractor(Context context) {
- mContext = context.getApplicationContext();
+ 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()]);
}
/**
@@ -329,6 +319,6 @@ public class ResourceExtractor {
* Pak extraction not necessarily required by the embedder.
*/
private static boolean shouldSkipPakExtraction() {
- return sResourcesToExtract.length == 0;
+ return get().mAssetsToExtract.length == 0;
}
}
« no previous file with comments | « base/android/java/src/org/chromium/base/LocaleUtils.java ('k') | base/android/java/templates/BuildConfig.template » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698