OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser.payments; | |
6 | |
7 import org.chromium.base.annotations.JNINamespace; | |
8 | |
9 import java.util.Locale; | |
10 | |
11 /** | |
12 * Formatter for currency amounts. | |
13 * https://w3c.github.io/browser-payment-api/specs/paymentrequest.html#currencya
mount | |
14 */ | |
15 @JNINamespace("payments") | |
16 public class CurrencyFormatter { | |
17 /** | |
18 * Pointer to the native implementation. | |
19 */ | |
20 private long mCurrencyFormatterAndroid; | |
21 | |
22 /** | |
23 * Builds the formatter for the given currency code and the current user loc
ale. | |
24 * | |
25 * @param currencyCode The currency code. Most commonly, this follows ISO 4
217 format: 3 upper | |
26 * case ASCII letters. For example, "USD". Format is no
t restricted. Should | |
27 * not be null. | |
28 * @param currencySystem URI specifying the ISO4217 currency code specificat
ion. See for | |
29 * details: https://w3c.github.io/browser-payment-api/#paymentcurre
ncyamount-dictionary | |
30 * By default, the value is "urn:iso:std:iso:4217". | |
31 * @param userLocale User's current locale. Should not be null. | |
32 */ | |
33 public CurrencyFormatter(String currencyCode, String currencySystem, Locale
userLocale) { | |
34 assert currencyCode != null : "currencyCode should not be null"; | |
35 assert currencySystem != null : "currencySystem should not be null"; | |
36 assert userLocale != null : "userLocale should not be null"; | |
37 | |
38 // Note that this pointer could leak the native object. The called must
call destroy() to | |
39 // ensure that the native object is destroyed. | |
40 mCurrencyFormatterAndroid = nativeInitCurrencyFormatterAndroid( | |
41 currencyCode, currencySystem, userLocale.toString()); | |
42 } | |
43 | |
44 /** Will destroy the native object. This class shouldn't be used afterwards.
*/ | |
45 public void destroy() { | |
46 if (mCurrencyFormatterAndroid != 0) { | |
47 nativeDestroy(mCurrencyFormatterAndroid); | |
48 mCurrencyFormatterAndroid = 0; | |
49 } | |
50 } | |
51 | |
52 /** @return The currency code formatted for display. */ | |
53 public String getFormattedCurrencyCode() { | |
54 return nativeGetFormattedCurrencyCode(mCurrencyFormatterAndroid); | |
55 } | |
56 | |
57 /** | |
58 * Formats the currency string for display. Does not parse the string into a
number, because it | |
59 * might be too large. The number is formatted for the current locale and ca
n include a | |
60 * currency symbol (e.g. $) anywhere in the string, but will not contain the
currency code | |
61 * (e.g. USD/US). All spaces in the currency are unicode non-breaking space. | |
62 * | |
63 * @param amountValue The number to format. Should be in "^-?[0-9]+(\.[0-9]+
)?$" format. Should | |
64 * not be null. | |
65 * @return The amount formatted with the specified currency. See description
for details. | |
66 */ | |
67 public String format(String amountValue) { | |
68 assert amountValue != null : "amountValue should not be null"; | |
69 | |
70 return nativeFormat(mCurrencyFormatterAndroid, amountValue); | |
71 } | |
72 | |
73 private native long nativeInitCurrencyFormatterAndroid( | |
74 String currencyCode, String currencySystem, String localeName); | |
75 private native void nativeDestroy(long nativeCurrencyFormatterAndroid); | |
76 private native String nativeFormat(long nativeCurrencyFormatterAndroid, Stri
ng amountValue); | |
77 private native String nativeGetFormattedCurrencyCode(long nativeCurrencyForm
atterAndroid); | |
78 } | |
OLD | NEW |