diff options
Diffstat (limited to 'src/com/android/calculator2/KeyMaps.java')
-rw-r--r-- | src/com/android/calculator2/KeyMaps.java | 158 |
1 files changed, 147 insertions, 11 deletions
diff --git a/src/com/android/calculator2/KeyMaps.java b/src/com/android/calculator2/KeyMaps.java index e82f35d..cdfe4e4 100644 --- a/src/com/android/calculator2/KeyMaps.java +++ b/src/com/android/calculator2/KeyMaps.java @@ -82,11 +82,11 @@ public class KeyMaps { return context.getString(R.string.op_div); case R.id.op_add: return context.getString(R.string.op_add); + case R.id.op_sub: + return context.getString(R.string.op_sub); case R.id.op_sqr: // Button label doesn't work. return context.getString(R.string.squared); - case R.id.op_sub: - return context.getString(R.string.op_sub); case R.id.dec_point: return context.getString(R.string.dec_point); case R.id.digit_0: @@ -115,6 +115,142 @@ public class KeyMaps { } /** + * Map key id to a single byte, somewhat human readable, description. + * Used to serialize expressions in the database. + * The result is in the range 0x20-0x7f. + */ + public static byte toByte(int id) { + char result; + // We only use characters with single-byte UTF8 encodings in the range 0x20-0x7F. + switch(id) { + case R.id.const_pi: + result = 'p'; + break; + case R.id.const_e: + result = 'e'; + break; + case R.id.op_sqrt: + result = 'r'; + break; + case R.id.op_fact: + result = '!'; + break; + case R.id.op_pct: + result = '%'; + break; + case R.id.fun_sin: + result = 's'; + break; + case R.id.fun_cos: + result = 'c'; + break; + case R.id.fun_tan: + result = 't'; + break; + case R.id.fun_arcsin: + result = 'S'; + break; + case R.id.fun_arccos: + result = 'C'; + break; + case R.id.fun_arctan: + result = 'T'; + break; + case R.id.fun_ln: + result = 'l'; + break; + case R.id.fun_log: + result = 'L'; + break; + case R.id.fun_exp: + result = 'E'; + break; + case R.id.lparen: + result = '('; + break; + case R.id.rparen: + result = ')'; + break; + case R.id.op_pow: + result = '^'; + break; + case R.id.op_mul: + result = '*'; + break; + case R.id.op_div: + result = '/'; + break; + case R.id.op_add: + result = '+'; + break; + case R.id.op_sub: + result = '-'; + break; + case R.id.op_sqr: + result = '2'; + break; + default: + throw new AssertionError("Unexpected key id"); + } + return (byte)result; + } + + /** + * Map single byte encoding generated by key id generated by toByte back to + * key id. + */ + public static int fromByte(byte b) { + switch((char)b) { + case 'p': + return R.id.const_pi; + case 'e': + return R.id.const_e; + case 'r': + return R.id.op_sqrt; + case '!': + return R.id.op_fact; + case '%': + return R.id.op_pct; + case 's': + return R.id.fun_sin; + case 'c': + return R.id.fun_cos; + case 't': + return R.id.fun_tan; + case 'S': + return R.id.fun_arcsin; + case 'C': + return R.id.fun_arccos; + case 'T': + return R.id.fun_arctan; + case 'l': + return R.id.fun_ln; + case 'L': + return R.id.fun_log; + case 'E': + return R.id.fun_exp; + case '(': + return R.id.lparen; + case ')': + return R.id.rparen; + case '^': + return R.id.op_pow; + case '*': + return R.id.op_mul; + case '/': + return R.id.op_div; + case '+': + return R.id.op_add; + case '-': + return R.id.op_sub; + case '2': + return R.id.op_sqr; + default: + throw new AssertionError("Unexpected single byte operator encoding"); + } + } + + /** * Map key id to corresponding (internationalized) descriptive string that can be used * to correctly read back a formula. * Only used for operators and individual characters; not used inside constants. @@ -344,10 +480,10 @@ public class KeyMaps { private static HashMap<Character, String> sOutputForResultChar; /** - * Locale string corresponding to preceding map and character constants. + * Locale corresponding to preceding map and character constants. * We recompute the map if this is not the current locale. */ - private static String sLocaleForMaps = "none"; + private static Locale sLocaleForMaps = null; /** * Activity to use for looking up buttons. @@ -431,14 +567,14 @@ public class KeyMaps { sOutputForResultChar.put(c, button.getText().toString()); } - // Ensure that the preceding map and character constants are - // initialized and correspond to the current locale. - // Called only by a single thread, namely the UI thread. + /** + * Ensure that the preceding map and character constants correspond to the current locale. + * Called only by UI thread. + */ static void validateMaps() { Locale locale = Locale.getDefault(); - String lname = locale.toString(); - if (lname != sLocaleForMaps) { - Log.v ("Calculator", "Setting local to: " + lname); + if (!locale.equals(sLocaleForMaps)) { + Log.v ("Calculator", "Setting locale to: " + locale.toLanguageTag()); sKeyValForFun = new HashMap<String, Integer>(); sKeyValForFun.put("sin", R.id.fun_sin); sKeyValForFun.put("cos", R.id.fun_cos); @@ -495,7 +631,7 @@ public class KeyMaps { addButtonToOutputMap((char)('0' + i), keyForDigVal(i)); } - sLocaleForMaps = lname; + sLocaleForMaps = locale; } } |