OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.content.browser; | 5 package org.chromium.base; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.SharedPreferences; | 8 import android.content.SharedPreferences; |
9 import android.content.pm.PackageInfo; | 9 import android.content.pm.PackageInfo; |
10 import android.content.pm.PackageManager; | 10 import android.content.pm.PackageManager; |
11 import android.content.res.AssetManager; | 11 import android.content.res.AssetManager; |
12 import android.os.AsyncTask; | 12 import android.os.AsyncTask; |
13 import android.preference.PreferenceManager; | 13 import android.preference.PreferenceManager; |
14 import android.util.Log; | 14 import android.util.Log; |
15 | 15 |
16 import org.chromium.base.PathUtils; | |
17 import org.chromium.ui.base.LocalizationUtils; | |
18 | |
19 import java.io.File; | 16 import java.io.File; |
20 import java.io.FileOutputStream; | 17 import java.io.FileOutputStream; |
21 import java.io.FilenameFilter; | 18 import java.io.FilenameFilter; |
22 import java.io.IOException; | 19 import java.io.IOException; |
23 import java.io.InputStream; | 20 import java.io.InputStream; |
24 import java.io.OutputStream; | 21 import java.io.OutputStream; |
| 22 import java.util.ArrayList; |
25 import java.util.HashSet; | 23 import java.util.HashSet; |
| 24 import java.util.List; |
26 import java.util.concurrent.CancellationException; | 25 import java.util.concurrent.CancellationException; |
27 import java.util.concurrent.ExecutionException; | 26 import java.util.concurrent.ExecutionException; |
28 import java.util.regex.Pattern; | 27 import java.util.regex.Pattern; |
29 | 28 |
30 /** | 29 /** |
31 * Handles extracting the necessary resources bundled in an APK and moving them
to a location on | 30 * Handles extracting the necessary resources bundled in an APK and moving them
to a location on |
32 * the file system accessible from the native code. | 31 * the file system accessible from the native code. |
33 */ | 32 */ |
34 public class ResourceExtractor { | 33 public class ResourceExtractor { |
35 | 34 |
(...skipping 24 matching lines...) Expand all Loading... |
60 } | 59 } |
61 | 60 |
62 String timestampFile = checkPakTimestamp(outputDir); | 61 String timestampFile = checkPakTimestamp(outputDir); |
63 if (timestampFile != null) { | 62 if (timestampFile != null) { |
64 deleteFiles(); | 63 deleteFiles(); |
65 } | 64 } |
66 | 65 |
67 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferen
ces(mContext); | 66 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferen
ces(mContext); |
68 HashSet<String> filenames = (HashSet<String>) prefs.getStringSet( | 67 HashSet<String> filenames = (HashSet<String>) prefs.getStringSet( |
69 PAK_FILENAMES, new HashSet<String>()); | 68 PAK_FILENAMES, new HashSet<String>()); |
70 String currentLocale = LocalizationUtils.getDefaultLocale(); | 69 String currentLocale = LocaleUtils.getDefaultLocale(); |
71 String currentLanguage = currentLocale.split("-", 2)[0]; | 70 String currentLanguage = currentLocale.split("-", 2)[0]; |
72 | 71 |
73 if (prefs.getString(LAST_LANGUAGE, "").equals(currentLanguage) | 72 if (prefs.getString(LAST_LANGUAGE, "").equals(currentLanguage) |
74 && filenames.size() >= sMandatoryPaks.length) { | 73 && filenames.size() >= sMandatoryPaks.length) { |
75 boolean filesPresent = true; | 74 boolean filesPresent = true; |
76 for (String file : filenames) { | 75 for (String file : filenames) { |
77 if (!new File(outputDir, file).exists()) { | 76 if (!new File(outputDir, file).exists()) { |
78 filesPresent = false; | 77 filesPresent = false; |
79 break; | 78 break; |
80 } | 79 } |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 } | 255 } |
257 | 256 |
258 /** | 257 /** |
259 * By default the ResourceExtractor will attempt to extract a pak resource f
or the users | 258 * By default the ResourceExtractor will attempt to extract a pak resource f
or the users |
260 * currently specified locale. This behavior can be changed with this functi
on and is | 259 * currently specified locale. This behavior can be changed with this functi
on and is |
261 * only needed by tests. | 260 * only needed by tests. |
262 * @param extract False if we should not attempt to extract a pak file for | 261 * @param extract False if we should not attempt to extract a pak file for |
263 * the users currently selected locale and try to extract only the | 262 * the users currently selected locale and try to extract only the |
264 * pak files specified in sMandatoryPaks. | 263 * pak files specified in sMandatoryPaks. |
265 */ | 264 */ |
| 265 @VisibleForTesting |
266 public static void setExtractImplicitLocaleForTesting(boolean extract) { | 266 public static void setExtractImplicitLocaleForTesting(boolean extract) { |
267 assert (sInstance == null || sInstance.mExtractTask == null) | 267 assert (sInstance == null || sInstance.mExtractTask == null) |
268 : "Must be called before startExtractingResources is called"; | 268 : "Must be called before startExtractingResources is called"; |
269 sExtractImplicitLocalePak = extract; | 269 sExtractImplicitLocalePak = extract; |
270 } | 270 } |
271 | 271 |
| 272 /** |
| 273 * Marks all the 'pak' resources, packaged as assets, for extraction during |
| 274 * running the tests. |
| 275 */ |
| 276 @VisibleForTesting |
| 277 public void setExtractAllPaksForTesting() { |
| 278 List<String> pakFileAssets = new ArrayList<String>(); |
| 279 AssetManager manager = mContext.getResources().getAssets(); |
| 280 try { |
| 281 String[] files = manager.list(""); |
| 282 for (String file : files) { |
| 283 if (file.endsWith(".pak")) pakFileAssets.add(file); |
| 284 } |
| 285 } catch (IOException e) { |
| 286 Log.w(LOGTAG, "Exception while accessing assets: " + e.getMessage(),
e); |
| 287 } |
| 288 setMandatoryPaksToExtract(pakFileAssets.toArray(new String[pakFileAssets
.size()])); |
| 289 } |
| 290 |
272 private ResourceExtractor(Context context) { | 291 private ResourceExtractor(Context context) { |
273 mContext = context.getApplicationContext(); | 292 mContext = context.getApplicationContext(); |
274 } | 293 } |
275 | 294 |
276 public void waitForCompletion() { | 295 public void waitForCompletion() { |
277 if (shouldSkipPakExtraction()) { | 296 if (shouldSkipPakExtraction()) { |
278 return; | 297 return; |
279 } | 298 } |
280 | 299 |
281 assert mExtractTask != null; | 300 assert mExtractTask != null; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 /** | 363 /** |
345 * Pak extraction not necessarily required by the embedder; we allow them to
skip | 364 * Pak extraction not necessarily required by the embedder; we allow them to
skip |
346 * this process if they call setMandatoryPaksToExtract with a single empty S
tring. | 365 * this process if they call setMandatoryPaksToExtract with a single empty S
tring. |
347 */ | 366 */ |
348 private static boolean shouldSkipPakExtraction() { | 367 private static boolean shouldSkipPakExtraction() { |
349 // Must call setMandatoryPaksToExtract before beginning resource extract
ion. | 368 // Must call setMandatoryPaksToExtract before beginning resource extract
ion. |
350 assert sMandatoryPaks != null; | 369 assert sMandatoryPaks != null; |
351 return sMandatoryPaks.length == 1 && "".equals(sMandatoryPaks[0]); | 370 return sMandatoryPaks.length == 1 && "".equals(sMandatoryPaks[0]); |
352 } | 371 } |
353 } | 372 } |
OLD | NEW |