| 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.LocaleUtils; |
| 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; | 8 import org.chromium.base.ThreadUtils; |
| 13 import org.chromium.base.annotations.CalledByNative; | 9 import org.chromium.base.annotations.CalledByNative; |
| 14 import org.chromium.base.annotations.JNINamespace; | 10 import org.chromium.base.annotations.JNINamespace; |
| 15 import org.chromium.base.annotations.SuppressFBWarnings; | 11 import org.chromium.base.annotations.SuppressFBWarnings; |
| 16 | 12 |
| 17 import java.io.File; | 13 import java.io.File; |
| 14 import java.util.ArrayList; |
| 18 import java.util.Locale; | 15 import java.util.Locale; |
| 19 | 16 |
| 20 /** | 17 /** |
| 21 * This class provides the resource bundle related methods for the native | 18 * This class provides the resource bundle related methods for the native |
| 22 * library. | 19 * library. |
| 23 */ | 20 */ |
| 24 @JNINamespace("ui") | 21 @JNINamespace("ui") |
| 25 public class ResourceBundle { | 22 public class ResourceBundle { |
| 26 private static ResourceExtractor.ResourceEntry[] sActiveLocaleResources; | 23 private static final String ASSET_DIR = "assets"; |
| 24 private static final String FALLBACK_LOCALE = "en-US"; |
| 25 private static String[] sActiveLocaleResources; |
| 27 | 26 |
| 28 /** | 27 /** |
| 29 * Applies the reverse mapping done by locale_pak_resources.py. | 28 * Initializes the list of locale packs for the active locale that are prese
nt within the apk. |
| 29 * |
| 30 * @param localePakFiles An array of pak filenames. |
| 30 */ | 31 */ |
| 31 private static String toChromeLocaleName(String srcFileName) { | 32 @SuppressFBWarnings("LI_LAZY_INIT_UPDATE_STATIC") // Not thread-safe. |
| 32 srcFileName = srcFileName.replace(".lpak", ".pak"); | 33 public static void initializeLocalePaks(String[] localePakFiles) { |
| 33 String[] parts = srcFileName.split("_"); | 34 ThreadUtils.assertOnUiThread(); |
| 34 if (parts.length > 1) { | 35 assert sActiveLocaleResources == null; |
| 35 int dotIdx = parts[1].indexOf('.'); | 36 String language = LocaleUtils.getLanguage(Locale.getDefault()); |
| 36 return parts[0] + "-" + parts[1].substring(0, dotIdx).toUpperCase(Lo
cale.ENGLISH) | 37 ArrayList<String> activeLocalePakFiles = new ArrayList<String>(localePak
Files.length); |
| 37 + parts[1].substring(dotIdx); | 38 for (String pakFileName : localePakFiles) { |
| 39 if (pakFileName.startsWith(language) || pakFileName.startsWith(FALLB
ACK_LOCALE)) { |
| 40 activeLocalePakFiles.add(pakFileName); |
| 41 } |
| 38 } | 42 } |
| 39 return srcFileName; | 43 sActiveLocaleResources = activeLocalePakFiles.toArray(new String[0]); |
| 40 } | 44 } |
| 41 | 45 |
| 42 /** | 46 @SuppressFBWarnings("MS_EXPOSE_REP") // Don't modify the list. |
| 43 * Initializes the list of locale packs for the active locale that are | 47 public static String[] getActiveLocaleResources() { |
| 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; | 48 return sActiveLocaleResources; |
| 75 } | 49 } |
| 76 | 50 |
| 77 @CalledByNative | 51 @CalledByNative |
| 78 private static String getLocalePakResourcePath(String locale) { | 52 private static String getLocalePakResourcePath(String locale) { |
| 79 if (sActiveLocaleResources == null) { | 53 if (sActiveLocaleResources == null) { |
| 80 return null; | 54 return null; |
| 81 } | 55 } |
| 82 String fileName = locale + ".pak"; | 56 String fileName = locale + ".pak"; |
| 83 for (ResourceExtractor.ResourceEntry entry : sActiveLocaleResources) { | 57 for (String resName : sActiveLocaleResources) { |
| 84 if (fileName.equals(entry.extractedFileName)) { | 58 if (fileName.equals(resName)) { |
| 85 return entry.pathWithinApk; | 59 return new File(ASSET_DIR, resName).toString(); |
| 86 } | 60 } |
| 87 } | 61 } |
| 88 return null; | 62 return null; |
| 89 } | 63 } |
| 90 } | 64 } |
| OLD | NEW |