Chromium Code Reviews| 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..e8bbc19f7fe5699cdc53f7982708b0bb9e5890d7 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,34 @@ public class ResourceExtractor { |
| } |
| } |
| - private final Context mContext; |
| private ExtractTask mExtractTask; |
| + private String[] mAssetsToExtract; |
| private static ResourceExtractor sInstance; |
| - public static ResourceExtractor get(Context context) { |
| + public static ResourceExtractor get() { |
| if (sInstance == null) { |
| - sInstance = new ResourceExtractor(context); |
| + sInstance = new ResourceExtractor(); |
| + sInstance.mAssetsToExtract = detectFilesToExtract(); |
|
nyquist
2016/10/03 04:53:59
Nit: Could you add a comment as to why is this ini
agrieve
2016/10/03 20:10:17
Made it final.
|
| } |
| 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) { |
| + return pakFileName.startsWith(language) |
| + && ".-".indexOf(pakFileName.charAt(language.length())) != -1; |
|
Ted C
2016/09/30 17:03:15
puts on the ol' paranoid hat, should we do pakFile
nyquist
2016/10/03 04:53:59
I agree with Ted here. It's not as readable as it
agrieve
2016/10/03 20:10:17
Done.
|
| } |
| - private ResourceExtractor(Context context) { |
| - mContext = context.getApplicationContext(); |
| + private static String[] detectFilesToExtract() { |
| + String language = LocaleUtils.getLanguage(Locale.getDefault()); |
| + ArrayList<String> activeLocalePakFiles = new ArrayList<String>(5); |
|
nyquist
2016/10/03 04:53:59
Nit: Care to add a tiny comment in case future rea
agrieve
2016/10/03 20:10:16
Done.
|
| + for (String pakFileName : BuildConfig.COMPRESSED_ASSETS) { |
|
michaelbai
2016/09/29 20:47:05
Do we still have pak file compressed?
agrieve
2016/09/30 00:04:37
For Chrome.apk, yes. Not for Monochrome nor webvie
michaelbai
2016/09/30 00:10:39
Oh, right, Chrome still uses compressed pak.
|
| + if (isPakFileForLanguage(pakFileName, language) |
| + || isPakFileForLanguage(pakFileName, FALLBACK_LOCALE)) { |
| + activeLocalePakFiles.add(pakFileName); |
| + } |
| + } |
| + return activeLocalePakFiles.toArray(new String[0]); |
|
nyquist
2016/10/03 04:53:59
Tiny nit: Since this happens early, and we already
agrieve
2016/10/03 20:10:17
Done.
|
| } |
| /** |
| @@ -329,6 +315,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; |
| } |
| } |