Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | package com.android.calculator2; |
| 18 | |
| 19 | import android.content.res.Resources; |
| 20 | import android.content.Context; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 21 | import android.app.Activity; |
| 22 | import android.util.Log; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 23 | import android.view.View; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 24 | import android.widget.Button; |
| 25 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 26 | import java.text.DecimalFormatSymbols; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 27 | import java.util.HashMap; |
| 28 | import java.util.Locale; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 29 | |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Collection of mapping functions between key ids, characters, internationalized |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 32 | * and non-internationalized characters, etc. |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 33 | * <p> |
| 34 | * KeyMap instances are not meaningful; everything here is static. |
| 35 | * All functions are either pure, or are assumed to be called only from a single UI thread. |
| 36 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 37 | public class KeyMaps { |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Map key id to corresponding (internationalized) display string. |
| 40 | * Pure function. |
| 41 | */ |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 42 | public static String toString(Context context, int id) { |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 43 | switch(id) { |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 44 | case R.id.const_pi: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 45 | return context.getString(R.string.const_pi); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 46 | case R.id.const_e: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 47 | return context.getString(R.string.const_e); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 48 | case R.id.op_sqrt: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 49 | return context.getString(R.string.op_sqrt); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 50 | case R.id.op_fact: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 51 | return context.getString(R.string.op_fact); |
| 52 | case R.id.op_pct: |
| 53 | return context.getString(R.string.op_pct); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 54 | case R.id.fun_sin: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 55 | return context.getString(R.string.fun_sin) + context.getString(R.string.lparen); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 56 | case R.id.fun_cos: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 57 | return context.getString(R.string.fun_cos) + context.getString(R.string.lparen); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 58 | case R.id.fun_tan: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 59 | return context.getString(R.string.fun_tan) + context.getString(R.string.lparen); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 60 | case R.id.fun_arcsin: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 61 | return context.getString(R.string.fun_arcsin) + context.getString(R.string.lparen); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 62 | case R.id.fun_arccos: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 63 | return context.getString(R.string.fun_arccos) + context.getString(R.string.lparen); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 64 | case R.id.fun_arctan: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 65 | return context.getString(R.string.fun_arctan) + context.getString(R.string.lparen); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 66 | case R.id.fun_ln: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 67 | return context.getString(R.string.fun_ln) + context.getString(R.string.lparen); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 68 | case R.id.fun_log: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 69 | return context.getString(R.string.fun_log) + context.getString(R.string.lparen); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 70 | case R.id.fun_exp: |
| 71 | // Button label doesn't work. |
| 72 | return context.getString(R.string.exponential) + context.getString(R.string.lparen); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 73 | case R.id.lparen: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 74 | return context.getString(R.string.lparen); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 75 | case R.id.rparen: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 76 | return context.getString(R.string.rparen); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 77 | case R.id.op_pow: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 78 | return context.getString(R.string.op_pow); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 79 | case R.id.op_mul: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 80 | return context.getString(R.string.op_mul); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 81 | case R.id.op_div: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 82 | return context.getString(R.string.op_div); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 83 | case R.id.op_add: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 84 | return context.getString(R.string.op_add); |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 85 | case R.id.op_sqr: |
| 86 | // Button label doesn't work. |
| 87 | return context.getString(R.string.squared); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 88 | case R.id.op_sub: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 89 | return context.getString(R.string.op_sub); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 90 | case R.id.dec_point: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 91 | return context.getString(R.string.dec_point); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 92 | case R.id.digit_0: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 93 | return context.getString(R.string.digit_0); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 94 | case R.id.digit_1: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 95 | return context.getString(R.string.digit_1); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 96 | case R.id.digit_2: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 97 | return context.getString(R.string.digit_2); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 98 | case R.id.digit_3: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 99 | return context.getString(R.string.digit_3); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 100 | case R.id.digit_4: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 101 | return context.getString(R.string.digit_4); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 102 | case R.id.digit_5: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 103 | return context.getString(R.string.digit_5); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 104 | case R.id.digit_6: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 105 | return context.getString(R.string.digit_6); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 106 | case R.id.digit_7: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 107 | return context.getString(R.string.digit_7); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 108 | case R.id.digit_8: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 109 | return context.getString(R.string.digit_8); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 110 | case R.id.digit_9: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 111 | return context.getString(R.string.digit_9); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 112 | default: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 113 | return ""; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 117 | /** |
Hans Boehm | 8a4f81c | 2015-07-09 10:41:25 -0700 | [diff] [blame] | 118 | * Map key id to corresponding (internationalized) descriptive string that can be used |
| 119 | * to correctly read back a formula. |
| 120 | * Only used for operators and individual characters; not used inside constants. |
| 121 | * Returns null when we don't need a descriptive string. |
| 122 | * Pure function. |
| 123 | */ |
| 124 | public static String toDescriptiveString(Context context, int id) { |
| 125 | switch(id) { |
| 126 | case R.id.op_fact: |
| 127 | return context.getString(R.string.desc_op_fact); |
| 128 | case R.id.fun_sin: |
| 129 | return context.getString(R.string.desc_fun_sin) |
| 130 | + " " + context.getString(R.string.desc_lparen); |
| 131 | case R.id.fun_cos: |
| 132 | return context.getString(R.string.desc_fun_cos) |
| 133 | + " " + context.getString(R.string.desc_lparen); |
| 134 | case R.id.fun_tan: |
| 135 | return context.getString(R.string.desc_fun_tan) |
| 136 | + " " + context.getString(R.string.desc_lparen); |
| 137 | case R.id.fun_arcsin: |
| 138 | return context.getString(R.string.desc_fun_arcsin) |
| 139 | + " " + context.getString(R.string.desc_lparen); |
| 140 | case R.id.fun_arccos: |
| 141 | return context.getString(R.string.desc_fun_arccos) |
| 142 | + " " + context.getString(R.string.desc_lparen); |
| 143 | case R.id.fun_arctan: |
| 144 | return context.getString(R.string.desc_fun_arctan) |
| 145 | + " " + context.getString(R.string.desc_lparen); |
| 146 | case R.id.fun_ln: |
| 147 | return context.getString(R.string.desc_fun_ln) |
| 148 | + " " + context.getString(R.string.desc_lparen); |
| 149 | case R.id.fun_log: |
| 150 | return context.getString(R.string.desc_fun_log) |
| 151 | + " " + context.getString(R.string.desc_lparen); |
| 152 | case R.id.fun_exp: |
| 153 | return context.getString(R.string.desc_fun_exp) |
| 154 | + " " + context.getString(R.string.desc_lparen); |
| 155 | case R.id.lparen: |
| 156 | return context.getString(R.string.desc_lparen); |
| 157 | case R.id.rparen: |
| 158 | return context.getString(R.string.desc_rparen); |
| 159 | case R.id.op_pow: |
| 160 | return context.getString(R.string.desc_op_pow); |
| 161 | case R.id.dec_point: |
| 162 | return context.getString(R.string.desc_dec_point); |
| 163 | default: |
| 164 | return null; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /** |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 169 | * Does a button id correspond to a binary operator? |
| 170 | * Pure function. |
| 171 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 172 | public static boolean isBinary(int id) { |
| 173 | switch(id) { |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 174 | case R.id.op_pow: |
| 175 | case R.id.op_mul: |
| 176 | case R.id.op_div: |
| 177 | case R.id.op_add: |
| 178 | case R.id.op_sub: |
| 179 | return true; |
| 180 | default: |
| 181 | return false; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 185 | /** |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 186 | * Does a button id correspond to a trig function? |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 187 | * Pure function. |
| 188 | */ |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 189 | public static boolean isTrigFunc(int id) { |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 190 | switch(id) { |
| 191 | case R.id.fun_sin: |
| 192 | case R.id.fun_cos: |
| 193 | case R.id.fun_tan: |
| 194 | case R.id.fun_arcsin: |
| 195 | case R.id.fun_arccos: |
| 196 | case R.id.fun_arctan: |
Hans Boehm | 52d477a | 2016-04-01 17:42:50 -0700 | [diff] [blame] | 197 | return true; |
| 198 | default: |
| 199 | return false; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Does a button id correspond to a function that introduces an implicit lparen? |
| 205 | * Pure function. |
| 206 | */ |
| 207 | public static boolean isFunc(int id) { |
| 208 | if (isTrigFunc(id)) { |
| 209 | return true; |
| 210 | } |
| 211 | switch(id) { |
Hans Boehm | 017de98 | 2015-06-10 17:46:03 -0700 | [diff] [blame] | 212 | case R.id.fun_ln: |
| 213 | case R.id.fun_log: |
| 214 | case R.id.fun_exp: |
| 215 | return true; |
| 216 | default: |
| 217 | return false; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Does a button id correspond to a prefix operator? |
| 223 | * Pure function. |
| 224 | */ |
| 225 | public static boolean isPrefix(int id) { |
| 226 | switch(id) { |
| 227 | case R.id.op_sqrt: |
| 228 | case R.id.op_sub: |
| 229 | return true; |
| 230 | default: |
| 231 | return false; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 236 | * Does a button id correspond to a suffix operator? |
| 237 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 238 | public static boolean isSuffix(int id) { |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 239 | switch (id) { |
| 240 | case R.id.op_fact: |
| 241 | case R.id.op_pct: |
Hans Boehm | 4db31b4 | 2015-05-31 12:19:05 -0700 | [diff] [blame] | 242 | case R.id.op_sqr: |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 243 | return true; |
| 244 | default: |
| 245 | return false; |
| 246 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | public static final int NOT_DIGIT = 10; |
| 250 | |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 251 | public static final String ELLIPSIS = "\u2026"; |
| 252 | |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 253 | public static final char MINUS_SIGN = '\u2212'; |
| 254 | |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 255 | /** |
| 256 | * Map key id to digit or NOT_DIGIT |
| 257 | * Pure function. |
| 258 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 259 | public static int digVal(int id) { |
| 260 | switch (id) { |
| 261 | case R.id.digit_0: |
| 262 | return 0; |
| 263 | case R.id.digit_1: |
| 264 | return 1; |
| 265 | case R.id.digit_2: |
| 266 | return 2; |
| 267 | case R.id.digit_3: |
| 268 | return 3; |
| 269 | case R.id.digit_4: |
| 270 | return 4; |
| 271 | case R.id.digit_5: |
| 272 | return 5; |
| 273 | case R.id.digit_6: |
| 274 | return 6; |
| 275 | case R.id.digit_7: |
| 276 | return 7; |
| 277 | case R.id.digit_8: |
| 278 | return 8; |
| 279 | case R.id.digit_9: |
| 280 | return 9; |
| 281 | default: |
| 282 | return NOT_DIGIT; |
| 283 | } |
| 284 | } |
| 285 | |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 286 | /** |
| 287 | * Map digit to corresponding key. Inverse of above. |
| 288 | * Pure function. |
| 289 | */ |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 290 | public static int keyForDigVal(int v) { |
| 291 | switch(v) { |
| 292 | case 0: |
| 293 | return R.id.digit_0; |
| 294 | case 1: |
| 295 | return R.id.digit_1; |
| 296 | case 2: |
| 297 | return R.id.digit_2; |
| 298 | case 3: |
| 299 | return R.id.digit_3; |
| 300 | case 4: |
| 301 | return R.id.digit_4; |
| 302 | case 5: |
| 303 | return R.id.digit_5; |
| 304 | case 6: |
| 305 | return R.id.digit_6; |
| 306 | case 7: |
| 307 | return R.id.digit_7; |
| 308 | case 8: |
| 309 | return R.id.digit_8; |
| 310 | case 9: |
| 311 | return R.id.digit_9; |
| 312 | default: |
| 313 | return View.NO_ID; |
| 314 | } |
| 315 | } |
| 316 | |
Hans Boehm | 425ed0a | 2015-05-19 18:27:31 -0700 | [diff] [blame] | 317 | // The following two are only used for recognizing additional |
| 318 | // input characters from a physical keyboard. They are not used |
| 319 | // for output internationalization. |
| 320 | private static char mDecimalPt; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 321 | |
Hans Boehm | ffda528 | 2015-05-18 15:00:12 -0700 | [diff] [blame] | 322 | private static char mPiChar; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 323 | |
Hans Boehm | ffda528 | 2015-05-18 15:00:12 -0700 | [diff] [blame] | 324 | /** |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 325 | * Character used as a placeholder for digits that are currently unknown in a result that |
| 326 | * is being computed. We initially generate blanks, and then use this as a replacement |
| 327 | * during final translation. |
Hans Boehm | ffda528 | 2015-05-18 15:00:12 -0700 | [diff] [blame] | 328 | * <p/> |
| 329 | * Note: the character must correspond closely to the width of a digit, |
| 330 | * otherwise the UI will visibly shift once the computation is finished. |
| 331 | */ |
| 332 | private static final char CHAR_DIGIT_UNKNOWN = '\u2007'; |
| 333 | |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 334 | /** |
| 335 | * Map typed function name strings to corresponding button ids. |
| 336 | * We (now redundantly?) include both localized and English names. |
| 337 | */ |
Hans Boehm | ffda528 | 2015-05-18 15:00:12 -0700 | [diff] [blame] | 338 | private static HashMap<String, Integer> sKeyValForFun; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 339 | |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 340 | /** |
| 341 | * Result string corresponding to a character in the calculator result. |
| 342 | * The string values in the map are expected to be one character long. |
| 343 | */ |
Hans Boehm | ffda528 | 2015-05-18 15:00:12 -0700 | [diff] [blame] | 344 | private static HashMap<Character, String> sOutputForResultChar; |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 345 | |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 346 | /** |
| 347 | * Locale string corresponding to preceding map and character constants. |
| 348 | * We recompute the map if this is not the current locale. |
| 349 | */ |
Hans Boehm | ffda528 | 2015-05-18 15:00:12 -0700 | [diff] [blame] | 350 | private static String sLocaleForMaps = "none"; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 351 | |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 352 | /** |
| 353 | * Activity to use for looking up buttons. |
| 354 | */ |
| 355 | private static Activity mActivity; |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 356 | |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 357 | /** |
| 358 | * Set acttivity used for looking up button labels. |
| 359 | * Call only from UI thread. |
| 360 | */ |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 361 | public static void setActivity(Activity a) { |
| 362 | mActivity = a; |
| 363 | } |
| 364 | |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 365 | /** |
| 366 | * Return the button id corresponding to the supplied character or return NO_ID. |
| 367 | * Called only by UI thread. |
| 368 | */ |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 369 | public static int keyForChar(char c) { |
| 370 | validateMaps(); |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 371 | if (Character.isDigit(c)) { |
| 372 | int i = Character.digit(c, 10); |
| 373 | return KeyMaps.keyForDigVal(i); |
| 374 | } |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 375 | switch (c) { |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 376 | case '.': |
| 377 | case ',': |
| 378 | return R.id.dec_point; |
| 379 | case '-': |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 380 | case MINUS_SIGN: |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 381 | return R.id.op_sub; |
| 382 | case '+': |
| 383 | return R.id.op_add; |
| 384 | case '*': |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 385 | case '\u00D7': // MULTIPLICATION SIGN |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 386 | return R.id.op_mul; |
| 387 | case '/': |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 388 | case '\u00F7': // DIVISION SIGN |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 389 | return R.id.op_div; |
| 390 | // We no longer localize function names, so they can't start with an 'e' or 'p'. |
| 391 | case 'e': |
| 392 | case 'E': |
| 393 | return R.id.const_e; |
| 394 | case 'p': |
| 395 | case 'P': |
| 396 | return R.id.const_pi; |
| 397 | case '^': |
| 398 | return R.id.op_pow; |
| 399 | case '!': |
| 400 | return R.id.op_fact; |
Justin Klaassen | e2711cb | 2015-05-28 11:13:17 -0700 | [diff] [blame] | 401 | case '%': |
| 402 | return R.id.op_pct; |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 403 | case '(': |
| 404 | return R.id.lparen; |
| 405 | case ')': |
| 406 | return R.id.rparen; |
| 407 | default: |
| 408 | if (c == mDecimalPt) return R.id.dec_point; |
| 409 | if (c == mPiChar) return R.id.const_pi; |
| 410 | // pi is not translated, but it might be typable on a Greek keyboard, |
Hans Boehm | 0b9806f | 2015-06-29 16:07:15 -0700 | [diff] [blame] | 411 | // or pasted in, so we check ... |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 412 | return View.NO_ID; |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 413 | } |
| 414 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 415 | |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 416 | /** |
| 417 | * Add information corresponding to the given button id to sKeyValForFun, to be used |
| 418 | * when mapping keyboard input to button ids. |
| 419 | */ |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 420 | static void addButtonToFunMap(int button_id) { |
| 421 | Button button = (Button)mActivity.findViewById(button_id); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 422 | sKeyValForFun.put(button.getText().toString(), button_id); |
| 423 | } |
| 424 | |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 425 | /** |
| 426 | * Add information corresponding to the given button to sOutputForResultChar, to be used |
| 427 | * when translating numbers on output. |
| 428 | */ |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 429 | static void addButtonToOutputMap(char c, int button_id) { |
| 430 | Button button = (Button)mActivity.findViewById(button_id); |
| 431 | sOutputForResultChar.put(c, button.getText().toString()); |
| 432 | } |
| 433 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 434 | // Ensure that the preceding map and character constants are |
| 435 | // initialized and correspond to the current locale. |
| 436 | // Called only by a single thread, namely the UI thread. |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 437 | static void validateMaps() { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 438 | Locale locale = Locale.getDefault(); |
| 439 | String lname = locale.toString(); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 440 | if (lname != sLocaleForMaps) { |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 441 | Log.v ("Calculator", "Setting local to: " + lname); |
| 442 | sKeyValForFun = new HashMap<String, Integer>(); |
| 443 | sKeyValForFun.put("sin", R.id.fun_sin); |
| 444 | sKeyValForFun.put("cos", R.id.fun_cos); |
| 445 | sKeyValForFun.put("tan", R.id.fun_tan); |
| 446 | sKeyValForFun.put("arcsin", R.id.fun_arcsin); |
| 447 | sKeyValForFun.put("arccos", R.id.fun_arccos); |
| 448 | sKeyValForFun.put("arctan", R.id.fun_arctan); |
| 449 | sKeyValForFun.put("asin", R.id.fun_arcsin); |
| 450 | sKeyValForFun.put("acos", R.id.fun_arccos); |
| 451 | sKeyValForFun.put("atan", R.id.fun_arctan); |
| 452 | sKeyValForFun.put("ln", R.id.fun_ln); |
| 453 | sKeyValForFun.put("log", R.id.fun_log); |
| 454 | sKeyValForFun.put("sqrt", R.id.op_sqrt); // special treatment |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 455 | addButtonToFunMap(R.id.fun_sin); |
| 456 | addButtonToFunMap(R.id.fun_cos); |
| 457 | addButtonToFunMap(R.id.fun_tan); |
| 458 | addButtonToFunMap(R.id.fun_arcsin); |
| 459 | addButtonToFunMap(R.id.fun_arccos); |
| 460 | addButtonToFunMap(R.id.fun_arctan); |
| 461 | addButtonToFunMap(R.id.fun_ln); |
| 462 | addButtonToFunMap(R.id.fun_log); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 463 | |
| 464 | // Set locale-dependent character "constants" |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 465 | mDecimalPt = |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 466 | DecimalFormatSymbols.getInstance().getDecimalSeparator(); |
Hans Boehm | 425ed0a | 2015-05-19 18:27:31 -0700 | [diff] [blame] | 467 | // We recognize this in keyboard input, even if we use |
| 468 | // a different character. |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 469 | Resources res = mActivity.getResources(); |
Hans Boehm | 425ed0a | 2015-05-19 18:27:31 -0700 | [diff] [blame] | 470 | mPiChar = 0; |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 471 | String piString = res.getString(R.string.const_pi); |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 472 | if (piString.length() == 1) { |
| 473 | mPiChar = piString.charAt(0); |
| 474 | } |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 475 | |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 476 | sOutputForResultChar = new HashMap<Character, String>(); |
| 477 | sOutputForResultChar.put('e', "E"); |
| 478 | sOutputForResultChar.put('E', "E"); |
Hans Boehm | ffda528 | 2015-05-18 15:00:12 -0700 | [diff] [blame] | 479 | sOutputForResultChar.put(' ', String.valueOf(CHAR_DIGIT_UNKNOWN)); |
Hans Boehm | 08e8f32 | 2015-04-21 13:18:38 -0700 | [diff] [blame] | 480 | sOutputForResultChar.put(ELLIPSIS.charAt(0), ELLIPSIS); |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 481 | // Translate numbers for fraction display, but not the separating slash, which appears |
| 482 | // to be universal. We also do not translate the ln, sqrt, pi |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 483 | sOutputForResultChar.put('/', "/"); |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 484 | sOutputForResultChar.put('(', "("); |
| 485 | sOutputForResultChar.put(')', ")"); |
| 486 | sOutputForResultChar.put('l', "l"); |
| 487 | sOutputForResultChar.put('n', "n"); |
Hans Boehm | 24c91ed | 2016-06-30 18:53:44 -0700 | [diff] [blame^] | 488 | sOutputForResultChar.put(',', |
| 489 | String.valueOf(DecimalFormatSymbols.getInstance().getGroupingSeparator())); |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 490 | sOutputForResultChar.put('\u221A', "\u221A"); // SQUARE ROOT |
| 491 | sOutputForResultChar.put('\u03C0', "\u03C0"); // GREEK SMALL LETTER PI |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 492 | addButtonToOutputMap('-', R.id.op_sub); |
Hans Boehm | 425ed0a | 2015-05-19 18:27:31 -0700 | [diff] [blame] | 493 | addButtonToOutputMap('.', R.id.dec_point); |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 494 | for (int i = 0; i <= 9; ++i) { |
| 495 | addButtonToOutputMap((char)('0' + i), keyForDigVal(i)); |
| 496 | } |
| 497 | |
| 498 | sLocaleForMaps = lname; |
| 499 | |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 503 | /** |
| 504 | * Return function button id for the substring of s starting at pos and ending with |
| 505 | * the next "(". Return NO_ID if there is none. |
| 506 | * We currently check for both (possibly localized) button labels, and standard |
| 507 | * English names. (They should currently be the same, and hence this is currently redundant.) |
| 508 | * Callable only from UI thread. |
| 509 | */ |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 510 | public static int funForString(String s, int pos) { |
| 511 | validateMaps(); |
Hans Boehm | 4a6b7cb | 2015-04-03 18:41:52 -0700 | [diff] [blame] | 512 | int parenPos = s.indexOf('(', pos); |
| 513 | if (parenPos != -1) { |
| 514 | String funString = s.substring(pos, parenPos); |
| 515 | Integer keyValue = sKeyValForFun.get(funString); |
| 516 | if (keyValue == null) return View.NO_ID; |
| 517 | return keyValue; |
| 518 | } |
| 519 | return View.NO_ID; |
| 520 | } |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 521 | |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 522 | /** |
| 523 | * Return the localization of the string s representing a numeric answer. |
| 524 | * Callable only from UI thread. |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 525 | * A trailing e is treated as the mathematical constant, not an exponent. |
Hans Boehm | 0b3a9fd | 2015-05-22 11:36:26 -0700 | [diff] [blame] | 526 | */ |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 527 | public static String translateResult(String s) { |
| 528 | StringBuilder result = new StringBuilder(); |
| 529 | int len = s.length(); |
| 530 | validateMaps(); |
| 531 | for (int i = 0; i < len; ++i) { |
| 532 | char c = s.charAt(i); |
Hans Boehm | 995e5eb | 2016-02-08 11:03:01 -0800 | [diff] [blame] | 533 | if (i < len - 1 || c != 'e') { |
| 534 | String translation = sOutputForResultChar.get(c); |
| 535 | if (translation == null) { |
| 536 | // Should not get here. Report if we do. |
| 537 | Log.v("Calculator", "Bad character:" + c); |
| 538 | result.append(String.valueOf(c)); |
| 539 | } else { |
| 540 | result.append(translation); |
| 541 | } |
Hans Boehm | 013969e | 2015-04-13 20:29:47 -0700 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | return result.toString(); |
| 545 | } |
| 546 | |
Hans Boehm | 8461495 | 2014-11-25 18:46:17 -0800 | [diff] [blame] | 547 | } |