forked from CyanogenMod/android_packages_apps_Calculator
/
FormattedNumberEditText.java
312 lines (267 loc) · 10.8 KB
/
FormattedNumberEditText.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android2.calculator3.view;
import android.content.Context;
import android.text.Editable;
import android.text.Html;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import com.android2.calculator3.R;
import com.xlythe.math.BaseModule;
import com.xlythe.math.Constants;
import com.xlythe.math.EquationFormatter;
import com.xlythe.math.Solver;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* FormattedNumberEditText adds more advanced functionality to NumberEditText.
*
* Commas will appear as numbers are typed, exponents will be raised, and backspacing
* on sin( and log( will remove the whole word. Because of the formatting, getText() will
* no longer return the correct value. getCleanText() has been added instead.
* */
public class FormattedNumberEditText extends NumberEditText {
private boolean mDebug = false;
private final Set<TextWatcher> mTextWatchers = new HashSet<>();
private boolean mTextWatchersEnabled = true;
private final TextWatcher mTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
if (!mTextWatchersEnabled || mSolver == null || getSelectionStart() == -1) return;
mTextWatchersEnabled = false;
onFormat(s);
mTextWatchersEnabled = true;
}
};
private EquationFormatter mEquationFormatter;
private Solver mSolver;
private List<String> mKeywords;
private boolean mIsInserting;
public FormattedNumberEditText(Context context) {
super(context);
setUp(context, null);
}
public FormattedNumberEditText(Context context, AttributeSet attr) {
super(context, attr);
setUp(context, attr);
}
private void setUp(Context context, AttributeSet attrs) {
// Display ^ , and other visual cues
mEquationFormatter = new EquationFormatter();
addTextChangedListener(mTextWatcher);
mKeywords = Arrays.asList(
context.getString(R.string.fun_arcsin) + "(",
context.getString(R.string.fun_arccos) + "(",
context.getString(R.string.fun_arctan) + "(",
context.getString(R.string.fun_sin) + "(",
context.getString(R.string.fun_cos) + "(",
context.getString(R.string.fun_tan) + "(",
context.getString(R.string.fun_arccsc) + "(",
context.getString(R.string.fun_arcsec) + "(",
context.getString(R.string.fun_arccot) + "(",
context.getString(R.string.fun_csc) + "(",
context.getString(R.string.fun_sec) + "(",
context.getString(R.string.fun_cot) + "(",
context.getString(R.string.fun_log) + "(",
context.getString(R.string.mod) + "(",
context.getString(R.string.fun_ln) + "(",
context.getString(R.string.fun_det) + "(",
context.getString(R.string.fun_transpose) + "(",
context.getString(R.string.fun_inverse) + "(",
context.getString(R.string.fun_trace) + "(",
context.getString(R.string.fun_norm) + "(",
context.getString(R.string.fun_polar) + "(",
context.getString(R.string.dx),
context.getString(R.string.dy),
context.getString(R.string.cbrt) + "(");
}
protected void onFormat(Editable s) {
String text = removeFormatting(s.toString());
// Get the selection handle, since we're setting text and that'll overwrite it
MutableInteger selectionHandle = new MutableInteger(getSelectionStart());
// Adjust the handle by removing any comas or spacing to the left
String cs = s.subSequence(0, selectionHandle.intValue()).toString();
selectionHandle.subtract(TextUtil.countOccurrences(cs, mSolver.getBaseModule().getSeparator()));
// Update the text with formatted (comas, etc) text
setText(Html.fromHtml(formatText(text, selectionHandle)));
setSelection(selectionHandle.intValue());
}
@Override
public void addTextChangedListener(TextWatcher watcher) {
if (watcher.equals(mTextWatcher)) {
super.addTextChangedListener(watcher);
} else {
mTextWatchers.add(watcher);
}
}
@Override
public void setText(CharSequence text, BufferType type) {
if (mTextWatchersEnabled) {
for (TextWatcher textWatcher : mTextWatchers) {
textWatcher.beforeTextChanged(getCleanText(), 0, 0, 0);
}
}
super.setText(text, type);
if (text != null && !mIsInserting) {
setSelection(getText().length());
}
invalidateTextSize();
if (mTextWatchersEnabled) {
for (TextWatcher textWatcher : mTextWatchers) {
textWatcher.afterTextChanged(getEditableFactory().newEditable(getCleanText()));
textWatcher.onTextChanged(getCleanText(), 0, 0, 0);
}
}
}
public String getCleanText() {
return removeFormatting(getText().toString());
}
public void insert(String delta) {
String text = getText().toString();
int selectionHandle = getSelectionStart();
String textBeforeInsertionHandle = text.substring(0, selectionHandle);
String textAfterInsertionHandle = text.substring(selectionHandle, text.length());
mIsInserting = true;
setText(textBeforeInsertionHandle + delta + BaseModule.SELECTION_HANDLE + textAfterInsertionHandle);
mIsInserting = false;
}
public void clear() {
setText(null);
}
public boolean isCursorModified() {
return getSelectionStart() != getText().length();
}
public void next() {
if (getSelectionStart() == getText().length()) {
setSelection(0);
} else {
setSelection(getSelectionStart() + 1);
}
}
@Override
public void backspace() {
// Check and remove keywords
String text = getText().toString();
int selectionHandle = getSelectionStart();
String textBeforeInsertionHandle = text.substring(0, selectionHandle);
String textAfterInsertionHandle = text.substring(selectionHandle, text.length());
for(String s : mKeywords) {
if(textBeforeInsertionHandle.endsWith(s)) {
int deletionLength = s.length();
String newText = textBeforeInsertionHandle.substring(0, textBeforeInsertionHandle.length() - deletionLength) + textAfterInsertionHandle;
setText(newText);
setSelection(selectionHandle - deletionLength);
return;
}
}
// Override NumberEditText's method -- because commas might disappear, it complicates things
if (selectionHandle != 0) {
setText(textBeforeInsertionHandle.substring(0, textBeforeInsertionHandle.length() - 1)
+ textAfterInsertionHandle);
if (getText().length() == text.length() - 2) {
// 2 characters were deleted (likely a comma and a number)
selectionHandle -= 2;
} else {
--selectionHandle;
}
setSelection(selectionHandle);
}
}
@Override
public void setSelection(int index) {
super.setSelection(Math.max(0, Math.min(getText().length(), index)));
}
@Override
public int getSelectionStart() {
// When setting a movement method, selectionStart() suddenly defaults to -1 instead of 0.
return Math.max(0, super.getSelectionStart());
}
public void setSolver(Solver solver) {
mSolver = solver;
}
protected String removeFormatting(String input) {
input = input.replace(Constants.POWER_PLACEHOLDER, Constants.POWER);
if(mSolver != null) {
input = input.replace(String.valueOf(mSolver.getBaseModule().getSeparator()), "");
}
return input;
}
protected String formatText(String input, MutableInteger selectionHandle) {
int customHandle = input.indexOf(BaseModule.SELECTION_HANDLE);
if (customHandle >= 0) {
selectionHandle.set(customHandle);
input = input.replace(Character.toString(BaseModule.SELECTION_HANDLE), "");
}
if (mSolver != null) {
// Add grouping, and then split on the selection handle
// which is saved as a unique char
String grouped = mEquationFormatter.addComas(mSolver, input, selectionHandle.intValue());
if (grouped.contains(String.valueOf(BaseModule.SELECTION_HANDLE))) {
String[] temp = grouped.split(String.valueOf(BaseModule.SELECTION_HANDLE));
selectionHandle.set(temp[0].length());
input = "";
for (String s : temp) {
input += s;
}
} else {
input = grouped;
}
}
return mEquationFormatter.insertSupScripts(input);
}
protected Solver getSolver() {
return mSolver;
}
public void setDebugEnabled(boolean enabled) {
mDebug = enabled;
}
public boolean isDebuggingEnabled() {
return mDebug;
}
public class MutableInteger {
private int value;
public MutableInteger(int value) {
this.value = value;
}
public MutableInteger(MutableInteger value) {
this.value = value.intValue();
}
public void set(int value) {
this.value = value;
}
public void add(int value) {
this.value += value;
}
public void subtract(int value) {
this.value -= value;
}
public int intValue() {
return value;
}
@Override
public String toString() {
return Integer.toString(value);
}
}
}