Skip to content

Commit 4053832

Browse files
Inhishonorluca020400
authored andcommitted
ExactCalculator: Clean up unused and redundant code
Change-Id: I68dbd5945db5a525eeddbead53d4c315c2199b6c
1 parent a9ce2ab commit 4053832

13 files changed

+89
-253
lines changed

src/com/android/calculator2/BoundedRational.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,6 @@ public String toNiceString() {
127127
return result;
128128
}
129129

130-
public static String toString(BoundedRational r) {
131-
if (r == null) {
132-
return "not a small rational";
133-
}
134-
return r.toString();
135-
}
136-
137130
/**
138131
* Returns a truncated (rounded towards 0) representation of the result.
139132
* Includes n digits to the right of the decimal point.
@@ -334,10 +327,6 @@ public static BoundedRational negate(BoundedRational r) {
334327
return new BoundedRational(r.mNum.negate(), r.mDen);
335328
}
336329

337-
public static BoundedRational subtract(BoundedRational r1, BoundedRational r2) {
338-
return add(r1, negate(r2));
339-
}
340-
341330
/**
342331
* Return product of r1 and r2 without reducing the result.
343332
*/
@@ -418,14 +407,6 @@ public static BoundedRational sqrt(BoundedRational r) {
418407
public final static BoundedRational MINUS_TWO = new BoundedRational(-2);
419408
public final static BoundedRational TEN = new BoundedRational(10);
420409
public final static BoundedRational TWELVE = new BoundedRational(12);
421-
public final static BoundedRational THIRTY = new BoundedRational(30);
422-
public final static BoundedRational MINUS_THIRTY = new BoundedRational(-30);
423-
public final static BoundedRational FORTY_FIVE = new BoundedRational(45);
424-
public final static BoundedRational MINUS_FORTY_FIVE = new BoundedRational(-45);
425-
public final static BoundedRational NINETY = new BoundedRational(90);
426-
public final static BoundedRational MINUS_NINETY = new BoundedRational(-90);
427-
428-
private static final BigInteger BIG_TWO = BigInteger.valueOf(2);
429410
private static final BigInteger BIG_MINUS_ONE = BigInteger.valueOf(-1);
430411

431412
/**

src/com/android/calculator2/Calculator.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import androidx.annotation.NonNull;
4949
import androidx.annotation.StringRes;
5050
import androidx.appcompat.app.AppCompatActivity;
51-
import androidx.appcompat.widget.Toolbar;
5251
import androidx.constraintlayout.motion.widget.MotionLayout;
5352
import androidx.core.content.ContextCompat;
5453
import androidx.core.graphics.Insets;
@@ -309,17 +308,17 @@ protected void onCreate(Bundle savedInstanceState) {
309308

310309
setContentView(R.layout.activity_calculator);
311310
setupEdgeToEdge();
312-
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
311+
setSupportActionBar(findViewById(R.id.toolbar));
313312

314313
// Hide all default options in the ActionBar.
315314
getSupportActionBar().setDisplayOptions(0);
316315

317316
mMainCalculator = findViewById(R.id.main_calculator);
318-
mModeView = (TextView) findViewById(R.id.mode);
319-
mFormulaText = (CalculatorFormula) findViewById(R.id.formula);
320-
mDeleteButton = (HapticButton) findViewById(R.id.del);
321-
mResultText = (CalculatorResult) findViewById(R.id.result);
322-
mFormulaContainer = (HorizontalScrollView) findViewById(R.id.formula_scroll_view);
317+
mModeView = findViewById(R.id.mode);
318+
mFormulaText = findViewById(R.id.formula);
319+
mDeleteButton = findViewById(R.id.del);
320+
mResultText = findViewById(R.id.result);
321+
mFormulaContainer = findViewById(R.id.formula_scroll_view);
323322
mEvaluator = Evaluator.getInstance(this);
324323
mEvaluator.setCallback(mEvaluatorCallback);
325324
mResultText.setEvaluator(mEvaluator, Evaluator.MAIN_INDEX);
@@ -328,8 +327,8 @@ protected void onCreate(Bundle savedInstanceState) {
328327
final TextView dpButton = findViewById(R.id.input_pad).findViewById(R.id.dec_point);
329328
dpButton.setText(getDecimalSeparator());
330329

331-
mInverseToggle = (TextView) findViewById(R.id.toggle_inv);
332-
mModeToggle = (TextView) findViewById(R.id.toggle_mode);
330+
mInverseToggle = findViewById(R.id.toggle_inv);
331+
mModeToggle = findViewById(R.id.toggle_mode);
333332

334333
mIsOneLine = mResultText.getVisibility() == View.INVISIBLE;
335334

src/com/android/calculator2/CalculatorDisplay.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import android.content.Context;
99
import android.util.AttributeSet;
10-
import android.view.View;
1110
import android.widget.LinearLayout;
1211

1312
public class CalculatorDisplay extends LinearLayout {

src/com/android/calculator2/CalculatorExpr.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,8 @@ public interface ExprResolver {
6666
// as a list of tokens. Constant
6767
// tokens are always nonempty.
6868

69-
private static enum TokenKind { CONSTANT, OPERATOR, PRE_EVAL };
69+
private enum TokenKind { CONSTANT, OPERATOR, PRE_EVAL };
7070
private static TokenKind[] tokenKindValues = TokenKind.values();
71-
private final static BigInteger BIG_MILLION = BigInteger.valueOf(1000000);
72-
private final static BigInteger BIG_BILLION = BigInteger.valueOf(1000000000);
7371

7472
private static abstract class Token {
7573
abstract TokenKind kind();
@@ -137,9 +135,7 @@ private static class Constant extends Token implements Cloneable {
137135
Constant() {
138136
mWhole = "";
139137
mFraction = "";
140-
// mSawDecimal = false;
141-
// mExponent = 0;
142-
};
138+
}
143139

144140
Constant(DataInput in) throws IOException {
145141
mWhole = in.readUTF();
@@ -226,7 +222,7 @@ public void delete() {
226222
}
227223

228224
public boolean isEmpty() {
229-
return (mSawDecimal == false && mWhole.isEmpty());
225+
return (!mSawDecimal && mWhole.isEmpty());
230226
}
231227

232228
/**
@@ -379,7 +375,7 @@ public static Token newToken(DataInput in) throws IOException {
379375
}
380376

381377
CalculatorExpr() {
382-
mExpr = new ArrayList<Token>();
378+
mExpr = new ArrayList<>();
383379
}
384380

385381
private CalculatorExpr(ArrayList<Token> expr) {
@@ -390,7 +386,7 @@ private CalculatorExpr(ArrayList<Token> expr) {
390386
* Construct CalculatorExpr, by reading it from in.
391387
*/
392388
CalculatorExpr(DataInput in) throws IOException {
393-
mExpr = new ArrayList<Token>();
389+
mExpr = new ArrayList<>();
394390
int size = in.readInt();
395391
for (int i = 0; i < size; ++i) {
396392
mExpr.add(newToken(in));
@@ -653,9 +649,6 @@ private static class EvalContext {
653649
mPrefixLength = len;
654650
mExprResolver = er;
655651
}
656-
void write(DataOutput out) throws IOException {
657-
out.writeBoolean(mDegreeMode);
658-
}
659652
}
660653

661654
private UnifiedReal toRadians(UnifiedReal x, EvalContext ec) {
@@ -854,10 +847,7 @@ private boolean canStartFactor(int i) {
854847
if (!(t instanceof Operator)) return true;
855848
int id = ((Operator)(t)).id;
856849
if (KeyMaps.isBinary(id)) return false;
857-
if (id == R.id.op_fact || id == R.id.rparen) {
858-
return false;
859-
}
860-
return true;
850+
return id != R.id.op_fact && id != R.id.rparen;
861851
}
862852

863853
private EvalRet evalTerm(int i, EvalContext ec) throws SyntaxException {
@@ -877,7 +867,6 @@ private EvalRet evalTerm(int i, EvalContext ec) throws SyntaxException {
877867
val = val.multiply(tmp.val);
878868
}
879869
cpos = tmp.pos;
880-
is_mul = is_div = false;
881870
}
882871
return new EvalRet(cpos, val);
883872
}
@@ -1029,7 +1018,7 @@ public ArrayList<Long> getTransitivelyReferencedExprs(ExprResolver er) {
10291018
// for positive and negative indices separately, but not their union. Currently we
10301019
// just settle for reverse breadth-first-search order, which handles the common case
10311020
// of simple dependency chains well.
1032-
ArrayList<Long> list = new ArrayList<Long>();
1021+
ArrayList<Long> list = new ArrayList<>();
10331022
int scanned = 0; // We've added expressions referenced by [0, scanned) to the list
10341023
addReferencedExprs(list, er);
10351024
while (scanned != list.size()) {

src/com/android/calculator2/CalculatorFormula.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -272,37 +272,25 @@ public void onGetContentRect(ActionMode mode, View view, Rect outRect) {
272272
outRect.left = (int) (outRect.right * 0.9f);
273273
}
274274
};
275-
setOnLongClickListener(new View.OnLongClickListener() {
276-
@Override
277-
public boolean onLongClick(View v) {
278-
mActionMode = startActionMode(mPasteActionModeCallback, ActionMode.TYPE_FLOATING);
279-
return true;
280-
}
275+
setOnLongClickListener(v -> {
276+
mActionMode = startActionMode(mPasteActionModeCallback, ActionMode.TYPE_FLOATING);
277+
return true;
281278
});
282279
}
283280

284281
/**
285282
* Use ContextMenu for paste support on L and lower.
286283
*/
287284
private void setupContextMenu() {
288-
setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
289-
@Override
290-
public void onCreateContextMenu(ContextMenu contextMenu, View view,
291-
ContextMenu.ContextMenuInfo contextMenuInfo) {
292-
final MenuInflater inflater = new MenuInflater(getContext());
293-
createContextMenu(inflater, contextMenu);
294-
mContextMenu = contextMenu;
295-
for (int i = 0; i < contextMenu.size(); i++) {
296-
contextMenu.getItem(i).setOnMenuItemClickListener(CalculatorFormula.this);
297-
}
298-
}
299-
});
300-
setOnLongClickListener(new View.OnLongClickListener() {
301-
@Override
302-
public boolean onLongClick(View v) {
303-
return showContextMenu();
285+
setOnCreateContextMenuListener((contextMenu, view, contextMenuInfo) -> {
286+
final MenuInflater inflater = new MenuInflater(getContext());
287+
createContextMenu(inflater, contextMenu);
288+
mContextMenu = contextMenu;
289+
for (int i = 0; i < contextMenu.size(); i++) {
290+
contextMenu.getItem(i).setOnMenuItemClickListener(CalculatorFormula.this);
304291
}
305292
});
293+
setOnLongClickListener(v -> showContextMenu());
306294
}
307295

308296
private boolean createContextMenu(MenuInflater inflater, Menu menu) {

src/com/android/calculator2/CalculatorResult.java

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
191191
}
192192
int duration = (int)(e2.getEventTime() - e1.getEventTime());
193193
if (duration < 1 || duration > 100) duration = 10;
194-
mScroller.startScroll(mCurrentPos, 0, distance, 0, (int)duration);
194+
mScroller.startScroll(mCurrentPos, 0, distance, 0, duration);
195195
postInvalidateOnAnimation();
196196
return true;
197197
}
@@ -396,7 +396,7 @@ public float getDecimalCredit() {
396396

397397
// Return the length of the exponent representation for the given exponent, in
398398
// characters.
399-
private final int expLen(int exp) {
399+
private int expLen(int exp) {
400400
if (exp == 0) return 0;
401401
final int abs_exp_digits = (int) Math.ceil(Math.log10(Math.abs((double)exp))
402402
+ 0.0000000001d /* Round whole numbers to next integer */);
@@ -481,13 +481,13 @@ private void initPositions(int initPrecOffset, int msdIndex, int lsdOffset,
481481
mLsdOffset = lsdOffset;
482482
mAppendExponent = false;
483483
// Prevent scrolling past initial position, which is calculated to show leading digits.
484-
mCurrentPos = mMinPos = (int) Math.round(initPrecOffset * mCharWidth);
484+
mCurrentPos = mMinPos = Math.round(initPrecOffset * mCharWidth);
485485
if (msdIndex == Evaluator.INVALID_MSD) {
486486
// Possible zero value
487487
if (lsdOffset == Integer.MIN_VALUE) {
488488
// Definite zero value.
489489
mMaxPos = mMinPos;
490-
mMaxCharOffset = (int) Math.round(mMaxPos/mCharWidth);
490+
mMaxCharOffset = Math.round(mMaxPos/mCharWidth);
491491
mScrollable = false;
492492
} else {
493493
// May be very small nonzero value. Allow user to find out.
@@ -542,7 +542,7 @@ private void initPositions(int initPrecOffset, int msdIndex, int lsdOffset,
542542
} else {
543543
mMaxCharOffset = Math.min(newMaxCharOffset, MAX_RIGHT_SCROLL);
544544
}
545-
mMaxPos = Math.min((int) Math.round(mMaxCharOffset * mCharWidth),
545+
mMaxPos = Math.min(Math.round(mMaxCharOffset * mCharWidth),
546546
MAX_RIGHT_SCROLL);
547547
} else if (!mWholePartFits && !mScrollable) {
548548
// Corner case in which entire number fits, but not with grouping separators. We
@@ -563,7 +563,7 @@ private void initPositions(int initPrecOffset, int msdIndex, int lsdOffset,
563563
mAppendExponent = true;
564564
}
565565
} else {
566-
mMaxPos = Math.min((int) Math.round(mMaxCharOffset * mCharWidth),
566+
mMaxPos = Math.min(Math.round(mMaxCharOffset * mCharWidth),
567567
MAX_RIGHT_SCROLL);
568568
}
569569
if (!mScrollable) {
@@ -762,8 +762,6 @@ private String formatResult(String in, int precOffset, int maxDigs, boolean trun
762762
}
763763
final float len = orig_length + nCommaChars;
764764
int deletedChars = 0;
765-
final float ellipsisCredit = getNoEllipsisCredit();
766-
final float decimalCredit = getDecimalCredit();
767765
final float effectiveLen = len - (decIndex == -1 ? 0 : getDecimalCredit());
768766
final float ellipsisAdjustment =
769767
needEllipsis ? mNoExponentCredit : getNoEllipsisCredit();
@@ -867,7 +865,6 @@ public String getFullCopyText() {
867865
*/
868866
@Override
869867
public int getMaxChars() {
870-
int result;
871868
synchronized(mWidthLock) {
872869
return (int) Math.floor(mWidthConstraint / mCharWidth);
873870
}
@@ -885,7 +882,7 @@ public boolean isScrollable() {
885882
* UI thread only.
886883
*/
887884
int getCharOffset(int pos) {
888-
return (int) Math.round(pos / mCharWidth); // Lock not needed.
885+
return Math.round(pos / mCharWidth); // Lock not needed.
889886
}
890887

891888
void clear() {
@@ -1042,43 +1039,33 @@ public void onGetContentRect(ActionMode mode, View view, Rect outRect) {
10421039
}
10431040
}
10441041
};
1045-
setOnLongClickListener(new View.OnLongClickListener() {
1046-
@Override
1047-
public boolean onLongClick(View v) {
1048-
if (mValid) {
1049-
mActionMode = startActionMode(mCopyActionModeCallback,
1050-
ActionMode.TYPE_FLOATING);
1051-
return true;
1052-
}
1053-
return false;
1042+
setOnLongClickListener(v -> {
1043+
if (mValid) {
1044+
mActionMode = startActionMode(mCopyActionModeCallback,
1045+
ActionMode.TYPE_FLOATING);
1046+
return true;
10541047
}
1048+
return false;
10551049
});
10561050
}
10571051

10581052
/**
10591053
* Use ContextMenu for copy/memory support on L and lower.
10601054
*/
10611055
private void setupContextMenu() {
1062-
setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
1063-
@Override
1064-
public void onCreateContextMenu(ContextMenu contextMenu, View view,
1065-
ContextMenu.ContextMenuInfo contextMenuInfo) {
1066-
final MenuInflater inflater = new MenuInflater(getContext());
1067-
createContextMenu(inflater, contextMenu);
1068-
mContextMenu = contextMenu;
1069-
for (int i = 0; i < contextMenu.size(); i ++) {
1070-
contextMenu.getItem(i).setOnMenuItemClickListener(CalculatorResult.this);
1071-
}
1056+
setOnCreateContextMenuListener((contextMenu, view, contextMenuInfo) -> {
1057+
final MenuInflater inflater = new MenuInflater(getContext());
1058+
createContextMenu(inflater, contextMenu);
1059+
mContextMenu = contextMenu;
1060+
for (int i = 0; i < contextMenu.size(); i ++) {
1061+
contextMenu.getItem(i).setOnMenuItemClickListener(CalculatorResult.this);
10721062
}
10731063
});
1074-
setOnLongClickListener(new View.OnLongClickListener() {
1075-
@Override
1076-
public boolean onLongClick(View v) {
1077-
if (mValid) {
1078-
return showContextMenu();
1079-
}
1080-
return false;
1064+
setOnLongClickListener(v -> {
1065+
if (mValid) {
1066+
return showContextMenu();
10811067
}
1068+
return false;
10821069
});
10831070
}
10841071

@@ -1116,12 +1103,6 @@ private void unhighlightResult() {
11161103
text.removeSpan(mHighlightSpan);
11171104
}
11181105

1119-
private void setPrimaryClip(ClipData clip) {
1120-
ClipboardManager clipboard = (ClipboardManager) getContext().
1121-
getSystemService(Context.CLIPBOARD_SERVICE);
1122-
clipboard.setPrimaryClip(clip);
1123-
}
1124-
11251106
private void copyContent() {
11261107
final CharSequence text = getFullCopyText();
11271108
ClipboardManager clipboard =

0 commit comments

Comments
 (0)