blob: 48557c26e8d2083b86d33251f234fb5e5b82f2dd [file] [log] [blame]
Hans Boehm84614952014-11-25 18:46:17 -08001/*
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
17package com.android.calculator2;
18
19import android.content.res.Resources;
20import android.content.Context;
21import android.view.View;
22import java.text.DecimalFormatSymbols;
23
24public class KeyMaps {
25 // Map key id to corresponding (internationalized) display string
26 public static String toString(int id, Context context) {
27 Resources res = context.getResources();
28 switch(id) {
29 case R.id.const_pi: return res.getString(R.string.const_pi);
30 case R.id.const_e: return res.getString(R.string.const_e);
31 case R.id.op_sqrt: return res.getString(R.string.op_sqrt);
32 case R.id.op_fact: return res.getString(R.string.op_fact);
33 case R.id.fun_sin: return res.getString(R.string.fun_sin)
34 + res.getString(R.string.lparen);
35 case R.id.fun_cos: return res.getString(R.string.fun_cos)
36 + res.getString(R.string.lparen);
37 case R.id.fun_tan: return res.getString(R.string.fun_tan)
38 + res.getString(R.string.lparen);
39 case R.id.fun_arcsin: return res.getString(R.string.fun_arcsin)
40 + res.getString(R.string.lparen);
41 case R.id.fun_arccos: return res.getString(R.string.fun_arccos)
42 + res.getString(R.string.lparen);
43 case R.id.fun_arctan: return res.getString(R.string.fun_arctan)
44 + res.getString(R.string.lparen);
45 case R.id.fun_ln: return res.getString(R.string.fun_ln)
46 + res.getString(R.string.lparen);
47 case R.id.fun_log: return res.getString(R.string.fun_log)
48 + res.getString(R.string.lparen);
49 case R.id.lparen: return res.getString(R.string.lparen);
50 case R.id.rparen: return res.getString(R.string.rparen);
51 case R.id.op_pow: return res.getString(R.string.op_pow);
52 case R.id.op_mul: return res.getString(R.string.op_mul);
53 case R.id.op_div: return res.getString(R.string.op_div);
54 case R.id.op_add: return res.getString(R.string.op_add);
55 case R.id.op_sub: return res.getString(R.string.op_sub);
56 case R.id.dec_point: return res.getString(R.string.dec_point);
57 case R.id.digit_0: return res.getString(R.string.digit_0);
58 case R.id.digit_1: return res.getString(R.string.digit_1);
59 case R.id.digit_2: return res.getString(R.string.digit_2);
60 case R.id.digit_3: return res.getString(R.string.digit_3);
61 case R.id.digit_4: return res.getString(R.string.digit_4);
62 case R.id.digit_5: return res.getString(R.string.digit_5);
63 case R.id.digit_6: return res.getString(R.string.digit_6);
64 case R.id.digit_7: return res.getString(R.string.digit_7);
65 case R.id.digit_8: return res.getString(R.string.digit_8);
66 case R.id.digit_9: return res.getString(R.string.digit_9);
67 default: return "?oops?";
68 }
69 }
70
71 // Does a button id correspond to a binary operator?
72 public static boolean isBinary(int id) {
73 switch(id) {
74 case R.id.op_pow:
75 case R.id.op_mul:
76 case R.id.op_div:
77 case R.id.op_add:
78 case R.id.op_sub:
79 return true;
80 default:
81 return false;
82 }
83 }
84
85 // Does a button id correspond to a suffix operator?
86 public static boolean isSuffix(int id) {
87 return id == R.id.op_fact;
88 }
89
90 public static final int NOT_DIGIT = 10;
91
92 // Map key id to digit or NOT_DIGIT
93 public static int digVal(int id) {
94 switch (id) {
95 case R.id.digit_0:
96 return 0;
97 case R.id.digit_1:
98 return 1;
99 case R.id.digit_2:
100 return 2;
101 case R.id.digit_3:
102 return 3;
103 case R.id.digit_4:
104 return 4;
105 case R.id.digit_5:
106 return 5;
107 case R.id.digit_6:
108 return 6;
109 case R.id.digit_7:
110 return 7;
111 case R.id.digit_8:
112 return 8;
113 case R.id.digit_9:
114 return 9;
115 default:
116 return NOT_DIGIT;
117 }
118 }
119
120 // Map digit to corresponding key. Inverse of above.
121 public static int keyForDigVal(int v) {
122 switch(v) {
123 case 0:
124 return R.id.digit_0;
125 case 1:
126 return R.id.digit_1;
127 case 2:
128 return R.id.digit_2;
129 case 3:
130 return R.id.digit_3;
131 case 4:
132 return R.id.digit_4;
133 case 5:
134 return R.id.digit_5;
135 case 6:
136 return R.id.digit_6;
137 case 7:
138 return R.id.digit_7;
139 case 8:
140 return R.id.digit_8;
141 case 9:
142 return R.id.digit_9;
143 default:
144 return View.NO_ID;
145 }
146 }
147
148 final static char decimalPt =
149 DecimalFormatSymbols.getInstance().getDecimalSeparator();
150
151 // Return the button id corresponding to the supplied character
152 // or NO_ID
153 // TODO: Should probably also check on characters used as button
154 // labels. But those don't really seem to be internationalized.
155 public static int keyForChar(char c) {
156 if (Character.isDigit(c)) {
157 int i = Character.digit(c, 10);
158 return KeyMaps.keyForDigVal(i);
159 }
160 if (c == decimalPt) return R.id.dec_point;
161 switch (c) {
162 case '.':
163 return R.id.dec_point;
164 case '-':
165 return R.id.op_sub;
166 case '+':
167 return R.id.op_add;
168 case '*':
169 return R.id.op_mul;
170 case '/':
171 return R.id.op_div;
172 case 'e':
173 return R.id.const_e;
174 case '^':
175 return R.id.op_pow;
176 default:
177 return View.NO_ID;
178 }
179 }
180}