blob: 7145fa816af86fbf3a72986f4c74c0a9e99c2aec [file] [log] [blame]
Justin Klaassen4b3af052014-05-27 17:53:10 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the 'License');
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an 'AS IS' BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.calculator2;
18
19import android.content.Context;
20
Filipe Gonçalvesa12b3cf2016-05-31 13:37:26 +010021import java.text.DecimalFormat;
22import java.text.DecimalFormatSymbols;
23import java.text.NumberFormat;
Justin Klaassen4b3af052014-05-27 17:53:10 -070024import java.util.HashMap;
Filipe Gonçalvesa12b3cf2016-05-31 13:37:26 +010025import java.util.Locale;
Justin Klaassen4b3af052014-05-27 17:53:10 -070026import java.util.Map;
27import java.util.Map.Entry;
28
29public class CalculatorExpressionTokenizer {
Justin Klaassen741471e2014-06-11 09:43:44 -070030
Justin Klaassen4b3af052014-05-27 17:53:10 -070031 private final Map<String, String> mReplacementMap;
32
Justin Klaassen2be4fdb2014-08-06 19:54:09 -070033 public CalculatorExpressionTokenizer(Context context) {
Justin Klaassen4b3af052014-05-27 17:53:10 -070034 mReplacementMap = new HashMap<>();
35
Filipe Gonçalvesa12b3cf2016-05-31 13:37:26 +010036 Locale locale = context.getResources().getConfiguration().locale;
37 if (!context.getResources().getBoolean(R.bool.use_localized_digits)) {
38 locale = new Locale.Builder()
39 .setLocale(locale)
40 .setUnicodeLocaleKeyword("nu", "latn")
41 .build();
42 }
Justin Klaassen4b3af052014-05-27 17:53:10 -070043
Filipe Gonçalvesa12b3cf2016-05-31 13:37:26 +010044 final DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
45 final char zeroDigit = symbols.getZeroDigit();
46
47 mReplacementMap.put(".", String.valueOf(symbols.getDecimalSeparator()));
48
49 for (int i = 0; i <= 9; ++i) {
50 mReplacementMap.put(Integer.toString(i), String.valueOf((char) (i + zeroDigit)));
51 }
Justin Klaassen4b3af052014-05-27 17:53:10 -070052
53 mReplacementMap.put("/", context.getString(R.string.op_div));
54 mReplacementMap.put("*", context.getString(R.string.op_mul));
55 mReplacementMap.put("-", context.getString(R.string.op_sub));
56
57 mReplacementMap.put("cos", context.getString(R.string.fun_cos));
58 mReplacementMap.put("ln", context.getString(R.string.fun_ln));
59 mReplacementMap.put("log", context.getString(R.string.fun_log));
60 mReplacementMap.put("sin", context.getString(R.string.fun_sin));
61 mReplacementMap.put("tan", context.getString(R.string.fun_tan));
62
63 mReplacementMap.put("Infinity", context.getString(R.string.inf));
64 }
65
66 public String getNormalizedExpression(String expr) {
67 for (Entry<String, String> replacementEntry : mReplacementMap.entrySet()) {
68 expr = expr.replace(replacementEntry.getValue(), replacementEntry.getKey());
69 }
70 return expr;
71 }
72
73 public String getLocalizedExpression(String expr) {
74 for (Entry<String, String> replacementEntry : mReplacementMap.entrySet()) {
75 expr = expr.replace(replacementEntry.getKey(), replacementEntry.getValue());
76 }
77 return expr;
78 }
79}