| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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.ui.base; | 5 package org.chromium.ui.base; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import org.chromium.base.BuildConfig; |
| 8 import android.content.res.Resources; | |
| 9 import android.content.res.TypedArray; | |
| 10 | |
| 11 import org.chromium.base.ResourceExtractor; | |
| 12 import org.chromium.base.ThreadUtils; | |
| 13 import org.chromium.base.annotations.CalledByNative; | 8 import org.chromium.base.annotations.CalledByNative; |
| 14 import org.chromium.base.annotations.JNINamespace; | 9 import org.chromium.base.annotations.JNINamespace; |
| 15 import org.chromium.base.annotations.SuppressFBWarnings; | |
| 16 | 10 |
| 17 import java.io.File; | 11 import java.io.File; |
| 18 import java.util.Locale; | 12 import java.util.Arrays; |
| 19 | 13 |
| 20 /** | 14 /** |
| 21 * This class provides the resource bundle related methods for the native | 15 * This class provides the resource bundle related methods for the native |
| 22 * library. | 16 * library. |
| 23 */ | 17 */ |
| 24 @JNINamespace("ui") | 18 @JNINamespace("ui") |
| 25 public class ResourceBundle { | 19 public class ResourceBundle { |
| 26 private static ResourceExtractor.ResourceEntry[] sActiveLocaleResources; | 20 private static final String ASSET_DIR = "assets"; |
| 27 | |
| 28 /** | |
| 29 * Applies the reverse mapping done by locale_pak_resources.py. | |
| 30 */ | |
| 31 private static String toChromeLocaleName(String srcFileName) { | |
| 32 srcFileName = srcFileName.replace(".lpak", ".pak"); | |
| 33 String[] parts = srcFileName.split("_"); | |
| 34 if (parts.length > 1) { | |
| 35 int dotIdx = parts[1].indexOf('.'); | |
| 36 return parts[0] + "-" + parts[1].substring(0, dotIdx).toUpperCase(Lo
cale.ENGLISH) | |
| 37 + parts[1].substring(dotIdx); | |
| 38 } | |
| 39 return srcFileName; | |
| 40 } | |
| 41 | |
| 42 /** | |
| 43 * Initializes the list of locale packs for the active locale that are | |
| 44 * present within the apk. | |
| 45 * | |
| 46 * @param context Any context | |
| 47 * @param localePaksResId Resource ID locale_paks (generated by | |
| 48 * locale_pak_resources.py) | |
| 49 */ | |
| 50 @SuppressFBWarnings("LI_LAZY_INIT_UPDATE_STATIC") // Not thread-safe. | |
| 51 public static void initializeLocalePaks(Context context, int localePaksResId
) { | |
| 52 ThreadUtils.assertOnUiThread(); | |
| 53 assert sActiveLocaleResources == null; | |
| 54 Resources resources = context.getResources(); | |
| 55 TypedArray resIds = resources.obtainTypedArray(localePaksResId); | |
| 56 try { | |
| 57 int len = resIds.length(); | |
| 58 sActiveLocaleResources = new ResourceExtractor.ResourceEntry[len]; | |
| 59 for (int i = 0; i < len; ++i) { | |
| 60 int resId = resIds.getResourceId(i, 0); | |
| 61 String resPath = resources.getString(resId); | |
| 62 String srcBaseName = new File(resPath).getName(); | |
| 63 String dstBaseName = toChromeLocaleName(srcBaseName); | |
| 64 sActiveLocaleResources[i] = new ResourceExtractor.ResourceEntry(
resId, resPath, | |
| 65 dstBaseName); | |
| 66 } | |
| 67 } finally { | |
| 68 resIds.recycle(); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 @SuppressFBWarnings("MS_EXPOSE_REP") // Don't modify the array. | |
| 73 public static ResourceExtractor.ResourceEntry[] getActiveLocaleResources() { | |
| 74 return sActiveLocaleResources; | |
| 75 } | |
| 76 | 21 |
| 77 @CalledByNative | 22 @CalledByNative |
| 78 private static String getLocalePakResourcePath(String locale) { | 23 private static String getLocalePakResourcePath(String locale) { |
| 79 if (sActiveLocaleResources == null) { | |
| 80 return null; | |
| 81 } | |
| 82 String fileName = locale + ".pak"; | 24 String fileName = locale + ".pak"; |
| 83 for (ResourceExtractor.ResourceEntry entry : sActiveLocaleResources) { | 25 if (Arrays.binarySearch(BuildConfig.UNCOMPRESSED_ASSETS, fileName) >= 0)
{ |
| 84 if (fileName.equals(entry.extractedFileName)) { | 26 return new File(ASSET_DIR, fileName).toString(); |
| 85 return entry.pathWithinApk; | |
| 86 } | |
| 87 } | 27 } |
| 88 return null; | 28 return null; |
| 89 } | 29 } |
| 90 } | 30 } |
| OLD | NEW |