some refactoring, no functionality change

This commit is contained in:
tibbi 2016-06-18 23:37:33 +02:00
parent 5d58152cd1
commit ea4c740316
9 changed files with 228 additions and 216 deletions

View File

@ -3,6 +3,8 @@ package com.simplemobiletools.calculator;
import android.support.test.rule.ActivityTestRule; import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4; import android.support.test.runner.AndroidJUnit4;
import com.simplemobiletools.calculator.activities.MainActivity;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;

View File

@ -9,7 +9,7 @@
android:label="@string/app_name" android:label="@string/app_name"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity <activity
android:name=".MainActivity" android:name=".activities.MainActivity"
android:screenOrientation="portrait"> android:screenOrientation="portrait">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN"/> <action android:name="android.intent.action.MAIN"/>
@ -28,12 +28,12 @@
</activity> </activity>
<activity <activity
android:name=".AboutActivity" android:name=".activities.AboutActivity"
android:label="@string/about" android:label="@string/about"
android:screenOrientation="portrait"/> android:screenOrientation="portrait"/>
<activity <activity
android:name=".LicenseActivity" android:name=".activities.LicenseActivity"
android:label="@string/third_party_licences" android:label="@string/third_party_licences"
android:screenOrientation="portrait"/> android:screenOrientation="portrait"/>

View File

@ -1,62 +1,63 @@
package com.simplemobiletools.calculator; package com.simplemobiletools.calculator;
public class CalculatorImpl { public class CalculatorImpl {
private String displayedValue; private String mDisplayedValue;
private String displayedFormula; private String mDisplayedFormula;
private double baseValue; private String mLastKey;
private double secondValue; private String mLastOperation;
private boolean resetValue; private Calculator mCallback;
private String lastKey;
private String lastOperation; private boolean mIsFirstOperation;
private Calculator callback; private boolean mResetValue;
private boolean isFirstOperation; private double mBaseValue;
private double mSecondValue;
public CalculatorImpl(Calculator calculator) { public CalculatorImpl(Calculator calculator) {
callback = calculator; mCallback = calculator;
resetValues(); resetValues();
setValue("0"); setValue("0");
setFormula(""); setFormula("");
} }
public CalculatorImpl(Calculator calculatorInterface, String value) { public CalculatorImpl(Calculator calculatorInterface, String value) {
callback = calculatorInterface; mCallback = calculatorInterface;
resetValues(); resetValues();
displayedValue = value; mDisplayedValue = value;
setFormula(""); setFormula("");
} }
private void resetValueIfNeeded() { private void resetValueIfNeeded() {
if (resetValue) if (mResetValue)
displayedValue = "0"; mDisplayedValue = "0";
resetValue = false; mResetValue = false;
} }
private void resetValues() { private void resetValues() {
baseValue = 0; mBaseValue = 0;
secondValue = 0; mSecondValue = 0;
resetValue = false; mResetValue = false;
lastKey = ""; mLastKey = "";
lastOperation = ""; mLastOperation = "";
displayedValue = ""; mDisplayedValue = "";
displayedFormula = ""; mDisplayedFormula = "";
isFirstOperation = true; mIsFirstOperation = true;
} }
public void setValue(String value) { public void setValue(String value) {
callback.setValue(value); mCallback.setValue(value);
displayedValue = value; mDisplayedValue = value;
} }
private void setFormula(String value) { private void setFormula(String value) {
callback.setFormula(value); mCallback.setFormula(value);
displayedFormula = value; mDisplayedFormula = value;
} }
private void updateFormula() { private void updateFormula() {
final String first = Formatter.doubleToString(baseValue); final String first = Formatter.doubleToString(mBaseValue);
final String second = Formatter.doubleToString(secondValue); final String second = Formatter.doubleToString(mSecondValue);
final String sign = getSign(lastOperation); final String sign = getSign(mLastOperation);
if (sign.equals("")) { if (sign.equals("")) {
setFormula(sign + first); setFormula(sign + first);
@ -65,8 +66,8 @@ public class CalculatorImpl {
} }
} }
public void setLastKey(String lastKey) { public void setLastKey(String mLastKey) {
this.lastKey = lastKey; this.mLastKey = mLastKey;
} }
public void addDigit(int number) { public void addDigit(int number) {
@ -87,11 +88,11 @@ public class CalculatorImpl {
private void updateResult(double value) { private void updateResult(double value) {
setValue(Formatter.doubleToString(value)); setValue(Formatter.doubleToString(value));
baseValue = value; mBaseValue = value;
} }
public String getDisplayedNumber() { public String getDisplayedNumber() {
return displayedValue; return mDisplayedValue;
} }
public double getDisplayedNumberAsDouble() { public double getDisplayedNumberAsDouble() {
@ -99,28 +100,28 @@ public class CalculatorImpl {
} }
public String getDisplayedFormula() { public String getDisplayedFormula() {
return displayedFormula; return mDisplayedFormula;
} }
public void handleResult() { public void handleResult() {
secondValue = getDisplayedNumberAsDouble(); mSecondValue = getDisplayedNumberAsDouble();
calculateResult(); calculateResult();
baseValue = getDisplayedNumberAsDouble(); mBaseValue = getDisplayedNumberAsDouble();
} }
public void calculateResult() { public void calculateResult() {
if (!isFirstOperation) if (!mIsFirstOperation)
updateFormula(); updateFormula();
switch (lastOperation) { switch (mLastOperation) {
case Constants.PLUS: case Constants.PLUS:
updateResult(baseValue + secondValue); updateResult(mBaseValue + mSecondValue);
break; break;
case Constants.MINUS: case Constants.MINUS:
updateResult(baseValue - secondValue); updateResult(mBaseValue - mSecondValue);
break; break;
case Constants.MULTIPLY: case Constants.MULTIPLY:
updateResult(baseValue * secondValue); updateResult(mBaseValue * mSecondValue);
break; break;
case Constants.DIVIDE: case Constants.DIVIDE:
divideNumbers(); divideNumbers();
@ -132,44 +133,44 @@ public class CalculatorImpl {
powerNumbers(); powerNumbers();
break; break;
case Constants.ROOT: case Constants.ROOT:
updateResult(Math.sqrt(baseValue)); updateResult(Math.sqrt(mBaseValue));
break; break;
default: default:
break; break;
} }
isFirstOperation = false; mIsFirstOperation = false;
} }
private void divideNumbers() { private void divideNumbers() {
double resultValue = 0; double resultValue = 0;
if (secondValue != 0) if (mSecondValue != 0)
resultValue = baseValue / secondValue; resultValue = mBaseValue / mSecondValue;
updateResult(resultValue); updateResult(resultValue);
} }
private void moduloNumbers() { private void moduloNumbers() {
double resultValue = 0; double resultValue = 0;
if (secondValue != 0) if (mSecondValue != 0)
resultValue = baseValue % secondValue; resultValue = mBaseValue % mSecondValue;
updateResult(resultValue); updateResult(resultValue);
} }
private void powerNumbers() { private void powerNumbers() {
double resultValue = Math.pow(baseValue, secondValue); double resultValue = Math.pow(mBaseValue, mSecondValue);
if (Double.isInfinite(resultValue) || Double.isNaN(resultValue)) if (Double.isInfinite(resultValue) || Double.isNaN(resultValue))
resultValue = 0; resultValue = 0;
updateResult(resultValue); updateResult(resultValue);
} }
public void handleOperation(String operation) { public void handleOperation(String operation) {
if (lastKey.equals(Constants.DIGIT)) if (mLastKey.equals(Constants.DIGIT))
handleResult(); handleResult();
resetValue = true; mResetValue = true;
lastKey = operation; mLastKey = operation;
lastOperation = operation; mLastOperation = operation;
if (operation.equals(Constants.ROOT)) if (operation.equals(Constants.ROOT))
calculateResult(); calculateResult();
@ -177,7 +178,7 @@ public class CalculatorImpl {
public void handleClear() { public void handleClear() {
final String oldValue = getDisplayedNumber(); final String oldValue = getDisplayedNumber();
String newValue; String newValue = "0";
final int len = oldValue.length(); final int len = oldValue.length();
int minLen = 1; int minLen = 1;
if (oldValue.contains("-")) if (oldValue.contains("-"))
@ -185,13 +186,11 @@ public class CalculatorImpl {
if (len > minLen) if (len > minLen)
newValue = oldValue.substring(0, len - 1); newValue = oldValue.substring(0, len - 1);
else
newValue = "0";
newValue = newValue.replaceAll("\\.$", ""); newValue = newValue.replaceAll("\\.$", "");
newValue = formatString(newValue); newValue = formatString(newValue);
setValue(newValue); setValue(newValue);
baseValue = Formatter.stringToDouble(newValue); mBaseValue = Formatter.stringToDouble(newValue);
} }
public void handleReset() { public void handleReset() {
@ -201,15 +200,15 @@ public class CalculatorImpl {
} }
public void handleEquals() { public void handleEquals() {
if (lastKey.equals(Constants.EQUALS)) if (mLastKey.equals(Constants.EQUALS))
calculateResult(); calculateResult();
if (!lastKey.equals(Constants.DIGIT)) if (!mLastKey.equals(Constants.DIGIT))
return; return;
secondValue = getDisplayedNumberAsDouble(); mSecondValue = getDisplayedNumberAsDouble();
calculateResult(); calculateResult();
lastKey = Constants.EQUALS; mLastKey = Constants.EQUALS;
} }
public void decimalClicked() { public void decimalClicked() {
@ -246,9 +245,9 @@ public class CalculatorImpl {
} }
public void numpadClicked(int id) { public void numpadClicked(int id) {
if (lastKey.equals(Constants.EQUALS)) if (mLastKey.equals(Constants.EQUALS))
lastOperation = Constants.EQUALS; mLastOperation = Constants.EQUALS;
lastKey = Constants.DIGIT; mLastKey = Constants.DIGIT;
resetValueIfNeeded(); resetValueIfNeeded();
switch (id) { switch (id) {

View File

@ -19,20 +19,20 @@ import butterknife.OnClick;
import yuku.ambilwarna.AmbilWarnaDialog; import yuku.ambilwarna.AmbilWarnaDialog;
public class MyWidgetConfigure extends AppCompatActivity { public class MyWidgetConfigure extends AppCompatActivity {
@BindView(R.id.btn_reset) View resetBtn; @BindView(R.id.btn_reset) View mResetBtn;
@BindView(R.id.config_bg_color) View bgColorPicker; @BindView(R.id.config_bg_color) View mBgColorPicker;
@BindView(R.id.config_bg_seekbar) SeekBar bgSeekBar; @BindView(R.id.config_bg_seekbar) SeekBar mBgSeekBar;
@BindView(R.id.config_text_color) View textColorPicker; @BindView(R.id.config_text_color) View mTextColorPicker;
@BindView(R.id.config_calc) View background; @BindView(R.id.config_calc) View mBackground;
@BindView(R.id.config_save) Button saveBtn; @BindView(R.id.config_save) Button mSaveBtn;
@BindView(R.id.result) TextView result; @BindView(R.id.result) TextView mResult;
@BindView(R.id.formula) TextView formula; @BindView(R.id.formula) TextView mFormula;
private int widgetId;
private int bgColor; private int mBgColor;
private int bgColorWithoutTransparency; private int mBgColorWithoutTransparency;
private float bgAlpha; private int mWidgetId;
private int textColor; private int mTextColor;
private float mBgAlpha;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@ -45,98 +45,98 @@ public class MyWidgetConfigure extends AppCompatActivity {
final Intent intent = getIntent(); final Intent intent = getIntent();
final Bundle extras = intent.getExtras(); final Bundle extras = intent.getExtras();
if (extras != null) if (extras != null)
widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); mWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
if (widgetId == AppWidgetManager.INVALID_APPWIDGET_ID) if (mWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
finish(); finish();
} }
private void initVariables() { private void initVariables() {
final SharedPreferences prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE); final SharedPreferences prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
bgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, 1); mBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, 1);
if (bgColor == 1) { if (mBgColor == 1) {
bgColor = Color.BLACK; mBgColor = Color.BLACK;
bgAlpha = .2f; mBgAlpha = .2f;
} else { } else {
bgAlpha = Color.alpha(bgColor) / (float) 255; mBgAlpha = Color.alpha(mBgColor) / (float) 255;
} }
resetBtn.setVisibility(View.VISIBLE); mResetBtn.setVisibility(View.VISIBLE);
bgColorWithoutTransparency = Color.rgb(Color.red(bgColor), Color.green(bgColor), Color.blue(bgColor)); mBgColorWithoutTransparency = Color.rgb(Color.red(mBgColor), Color.green(mBgColor), Color.blue(mBgColor));
bgSeekBar.setOnSeekBarChangeListener(bgSeekbarChangeListener); mBgSeekBar.setOnSeekBarChangeListener(bgSeekbarChangeListener);
bgSeekBar.setProgress((int) (bgAlpha * 100)); mBgSeekBar.setProgress((int) (mBgAlpha * 100));
updateBackgroundColor(); updateBackgroundColor();
textColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, getResources().getColor(R.color.colorPrimary)); mTextColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, getResources().getColor(R.color.colorPrimary));
updateTextColor(); updateTextColor();
formula.setText("15,937*5"); mFormula.setText("15,937*5");
result.setText("79,685"); mResult.setText("79,685");
} }
@OnClick(R.id.config_save) @OnClick(R.id.config_save)
public void saveConfig() { public void saveConfig() {
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this); final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
final RemoteViews views = new RemoteViews(getPackageName(), R.layout.activity_main); final RemoteViews views = new RemoteViews(getPackageName(), R.layout.activity_main);
views.setInt(R.id.calculator_holder, "setBackgroundColor", bgColor); views.setInt(R.id.calculator_holder, "setBackgroundColor", mBgColor);
appWidgetManager.updateAppWidget(widgetId, views); appWidgetManager.updateAppWidget(mWidgetId, views);
storeWidgetBackground(); storeWidgetBackground();
requestWidgetUpdate(); requestWidgetUpdate();
final Intent resultValue = new Intent(); final Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId);
setResult(RESULT_OK, resultValue); setResult(RESULT_OK, resultValue);
finish(); finish();
} }
private void storeWidgetBackground() { private void storeWidgetBackground() {
final SharedPreferences prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE); final SharedPreferences prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
prefs.edit().putInt(Constants.WIDGET_BG_COLOR, bgColor).apply(); prefs.edit().putInt(Constants.WIDGET_BG_COLOR, mBgColor).apply();
prefs.edit().putInt(Constants.WIDGET_TEXT_COLOR, textColor).apply(); prefs.edit().putInt(Constants.WIDGET_TEXT_COLOR, mTextColor).apply();
} }
private void requestWidgetUpdate() { private void requestWidgetUpdate() {
final Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetProvider.class); final Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetProvider.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{widgetId}); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{mWidgetId});
sendBroadcast(intent); sendBroadcast(intent);
} }
private void updateBackgroundColor() { private void updateBackgroundColor() {
bgColor = adjustAlpha(bgColorWithoutTransparency, bgAlpha); mBgColor = adjustAlpha(mBgColorWithoutTransparency, mBgAlpha);
background.setBackgroundColor(bgColor); mBackground.setBackgroundColor(mBgColor);
bgColorPicker.setBackgroundColor(bgColor); mBgColorPicker.setBackgroundColor(mBgColor);
saveBtn.setBackgroundColor(bgColor); mSaveBtn.setBackgroundColor(mBgColor);
} }
private void updateTextColor() { private void updateTextColor() {
textColorPicker.setBackgroundColor(textColor); mTextColorPicker.setBackgroundColor(mTextColor);
saveBtn.setTextColor(textColor); mSaveBtn.setTextColor(mTextColor);
int[] viewIds = int[] viewIds =
new int[]{R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8, new int[]{R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8,
R.id.btn_9, R.id.btn_modulo, R.id.btn_power, R.id.btn_root, R.id.btn_clear, R.id.btn_reset, R.id.btn_divide, R.id.btn_9, R.id.btn_modulo, R.id.btn_power, R.id.btn_root, R.id.btn_clear, R.id.btn_reset, R.id.btn_divide,
R.id.btn_multiply, R.id.btn_minus, R.id.btn_plus, R.id.btn_decimal, R.id.btn_equals}; R.id.btn_multiply, R.id.btn_minus, R.id.btn_plus, R.id.btn_decimal, R.id.btn_equals};
result.setTextColor(textColor); mResult.setTextColor(mTextColor);
formula.setTextColor(textColor); mFormula.setTextColor(mTextColor);
Button btn; Button btn;
for (int i : viewIds) { for (int i : viewIds) {
btn = (Button) findViewById(i); btn = (Button) findViewById(i);
btn.setTextColor(textColor); btn.setTextColor(mTextColor);
} }
} }
@OnClick(R.id.config_bg_color) @OnClick(R.id.config_bg_color)
public void pickBackgroundColor() { public void pickBackgroundColor() {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, bgColorWithoutTransparency, new AmbilWarnaDialog.OnAmbilWarnaListener() { AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, mBgColorWithoutTransparency, new AmbilWarnaDialog.OnAmbilWarnaListener() {
@Override @Override
public void onCancel(AmbilWarnaDialog dialog) { public void onCancel(AmbilWarnaDialog dialog) {
} }
@Override @Override
public void onOk(AmbilWarnaDialog dialog, int color) { public void onOk(AmbilWarnaDialog dialog, int color) {
bgColorWithoutTransparency = color; mBgColorWithoutTransparency = color;
updateBackgroundColor(); updateBackgroundColor();
} }
}); });
@ -146,14 +146,14 @@ public class MyWidgetConfigure extends AppCompatActivity {
@OnClick(R.id.config_text_color) @OnClick(R.id.config_text_color)
public void pickTextColor() { public void pickTextColor() {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, textColor, new AmbilWarnaDialog.OnAmbilWarnaListener() { AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, mTextColor, new AmbilWarnaDialog.OnAmbilWarnaListener() {
@Override @Override
public void onCancel(AmbilWarnaDialog dialog) { public void onCancel(AmbilWarnaDialog dialog) {
} }
@Override @Override
public void onOk(AmbilWarnaDialog dialog, int color) { public void onOk(AmbilWarnaDialog dialog, int color) {
textColor = color; mTextColor = color;
updateTextColor(); updateTextColor();
} }
}); });
@ -164,7 +164,7 @@ public class MyWidgetConfigure extends AppCompatActivity {
private SeekBar.OnSeekBarChangeListener bgSeekbarChangeListener = new SeekBar.OnSeekBarChangeListener() { private SeekBar.OnSeekBarChangeListener bgSeekbarChangeListener = new SeekBar.OnSeekBarChangeListener() {
@Override @Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
bgAlpha = (float) progress / (float) 100; mBgAlpha = (float) progress / (float) 100;
updateBackgroundColor(); updateBackgroundColor();
} }

View File

@ -11,20 +11,23 @@ import android.graphics.Color;
import android.view.View; import android.view.View;
import android.widget.RemoteViews; import android.widget.RemoteViews;
import com.simplemobiletools.calculator.activities.MainActivity;
public class MyWidgetProvider extends AppWidgetProvider implements Calculator { public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
private static int[] widgetIds; private static RemoteViews mRemoteViews;
private static RemoteViews remoteViews; private static CalculatorImpl mCalc;
private static CalculatorImpl calc; private static AppWidgetManager mWidgetManager;
private static AppWidgetManager widgetManager; private static Intent mIntent;
private static Intent intent; private static Context mContext;
private static Context cxt; private static SharedPreferences mPrefs;
private static SharedPreferences prefs;
private static int[] mWidgetIds;
@Override @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
initVariables(context); initVariables(context);
intent = new Intent(context, MyWidgetProvider.class); mIntent = new Intent(context, MyWidgetProvider.class);
setupIntent(Constants.DECIMAL, R.id.btn_decimal); setupIntent(Constants.DECIMAL, R.id.btn_decimal);
setupIntent(Constants.ZERO, R.id.btn_0); setupIntent(Constants.ZERO, R.id.btn_0);
setupIntent(Constants.ONE, R.id.btn_1); setupIntent(Constants.ONE, R.id.btn_1);
@ -56,45 +59,45 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
} }
private void setupIntent(String action, int id) { private void setupIntent(String action, int id) {
intent.setAction(action); mIntent.setAction(action);
PendingIntent pendingIntent = PendingIntent.getBroadcast(cxt, 0, intent, 0); PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, mIntent, 0);
remoteViews.setOnClickPendingIntent(id, pendingIntent); mRemoteViews.setOnClickPendingIntent(id, pendingIntent);
} }
private void setupAppOpenIntent(int id) { private void setupAppOpenIntent(int id) {
final Intent intent = new Intent(cxt, MainActivity.class); final Intent intent = new Intent(mContext, MainActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(cxt, 0, intent, 0); final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
remoteViews.setOnClickPendingIntent(id, pendingIntent); mRemoteViews.setOnClickPendingIntent(id, pendingIntent);
} }
private void initVariables(Context context) { private void initVariables(Context context) {
cxt = context; mContext = context;
updateWidgetIds(); updateWidgetIds();
prefs = initPrefs(cxt); mPrefs = initPrefs(mContext);
final int defaultColor = cxt.getResources().getColor(R.color.text_grey); final int defaultColor = mContext.getResources().getColor(R.color.text_grey);
final int newBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, defaultColor); final int newBgColor = mPrefs.getInt(Constants.WIDGET_BG_COLOR, defaultColor);
final int newTextColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, Color.WHITE); final int newTextColor = mPrefs.getInt(Constants.WIDGET_TEXT_COLOR, Color.WHITE);
remoteViews = new RemoteViews(cxt.getPackageName(), R.layout.activity_main); mRemoteViews = new RemoteViews(mContext.getPackageName(), R.layout.activity_main);
remoteViews.setViewVisibility(R.id.btn_reset, View.VISIBLE); mRemoteViews.setViewVisibility(R.id.btn_reset, View.VISIBLE);
remoteViews.setInt(R.id.calculator_holder, "setBackgroundColor", newBgColor); mRemoteViews.setInt(R.id.calculator_holder, "setBackgroundColor", newBgColor);
updateTextColors(newTextColor); updateTextColors(newTextColor);
widgetManager = AppWidgetManager.getInstance(cxt); mWidgetManager = AppWidgetManager.getInstance(mContext);
final String displayValue = "0"; final String displayValue = "0";
calc = new CalculatorImpl(this, displayValue); mCalc = new CalculatorImpl(this, displayValue);
} }
private void updateWidgetIds() { private void updateWidgetIds() {
final ComponentName component = new ComponentName(cxt, MyWidgetProvider.class); final ComponentName component = new ComponentName(mContext, MyWidgetProvider.class);
widgetManager = AppWidgetManager.getInstance(cxt); mWidgetManager = AppWidgetManager.getInstance(mContext);
widgetIds = widgetManager.getAppWidgetIds(component); mWidgetIds = mWidgetManager.getAppWidgetIds(component);
} }
private void updateWidget() { private void updateWidget() {
for (int widgetId : widgetIds) { for (int widgetId : mWidgetIds) {
widgetManager.updateAppWidget(widgetId, remoteViews); mWidgetManager.updateAppWidget(widgetId, mRemoteViews);
} }
} }
@ -103,13 +106,13 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
} }
private void updateTextColors(int color) { private void updateTextColors(int color) {
int[] viewIds = final int[] viewIds =
new int[]{R.id.formula, R.id.result, R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, new int[]{R.id.formula, R.id.result, R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6,
R.id.btn_7, R.id.btn_8, R.id.btn_9, R.id.btn_modulo, R.id.btn_power, R.id.btn_root, R.id.btn_clear, R.id.btn_reset, R.id.btn_7, R.id.btn_8, R.id.btn_9, R.id.btn_modulo, R.id.btn_power, R.id.btn_root, R.id.btn_clear, R.id.btn_reset,
R.id.btn_divide, R.id.btn_multiply, R.id.btn_minus, R.id.btn_plus, R.id.btn_decimal, R.id.btn_equals}; R.id.btn_divide, R.id.btn_multiply, R.id.btn_minus, R.id.btn_plus, R.id.btn_decimal, R.id.btn_equals};
for (int i : viewIds) { for (int i : viewIds) {
remoteViews.setInt(i, "setTextColor", color); mRemoteViews.setInt(i, "setTextColor", color);
} }
} }
@ -146,52 +149,52 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
} }
private void myAction(String action, Context context) { private void myAction(String action, Context context) {
if (calc == null || remoteViews == null || widgetManager == null || prefs == null || cxt == null) { if (mCalc == null || mRemoteViews == null || mWidgetManager == null || mPrefs == null || mContext == null) {
initVariables(context); initVariables(context);
} }
switch (action) { switch (action) {
case Constants.DECIMAL: case Constants.DECIMAL:
calc.numpadClicked(R.id.btn_decimal); mCalc.numpadClicked(R.id.btn_decimal);
break; break;
case Constants.ZERO: case Constants.ZERO:
calc.numpadClicked(R.id.btn_0); mCalc.numpadClicked(R.id.btn_0);
break; break;
case Constants.ONE: case Constants.ONE:
calc.numpadClicked(R.id.btn_1); mCalc.numpadClicked(R.id.btn_1);
break; break;
case Constants.TWO: case Constants.TWO:
calc.numpadClicked(R.id.btn_2); mCalc.numpadClicked(R.id.btn_2);
break; break;
case Constants.THREE: case Constants.THREE:
calc.numpadClicked(R.id.btn_3); mCalc.numpadClicked(R.id.btn_3);
break; break;
case Constants.FOUR: case Constants.FOUR:
calc.numpadClicked(R.id.btn_4); mCalc.numpadClicked(R.id.btn_4);
break; break;
case Constants.FIVE: case Constants.FIVE:
calc.numpadClicked(R.id.btn_5); mCalc.numpadClicked(R.id.btn_5);
break; break;
case Constants.SIX: case Constants.SIX:
calc.numpadClicked(R.id.btn_6); mCalc.numpadClicked(R.id.btn_6);
break; break;
case Constants.SEVEN: case Constants.SEVEN:
calc.numpadClicked(R.id.btn_7); mCalc.numpadClicked(R.id.btn_7);
break; break;
case Constants.EIGHT: case Constants.EIGHT:
calc.numpadClicked(R.id.btn_8); mCalc.numpadClicked(R.id.btn_8);
break; break;
case Constants.NINE: case Constants.NINE:
calc.numpadClicked(R.id.btn_9); mCalc.numpadClicked(R.id.btn_9);
break; break;
case Constants.EQUALS: case Constants.EQUALS:
calc.handleEquals(); mCalc.handleEquals();
break; break;
case Constants.CLEAR: case Constants.CLEAR:
calc.handleClear(); mCalc.handleClear();
break; break;
case Constants.RESET: case Constants.RESET:
calc.handleReset(); mCalc.handleReset();
break; break;
case Constants.PLUS: case Constants.PLUS:
case Constants.MINUS: case Constants.MINUS:
@ -200,7 +203,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
case Constants.MODULO: case Constants.MODULO:
case Constants.POWER: case Constants.POWER:
case Constants.ROOT: case Constants.ROOT:
calc.handleOperation(action); mCalc.handleOperation(action);
break; break;
default: default:
break; break;
@ -209,7 +212,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
@Override @Override
public void setValue(String value) { public void setValue(String value) {
remoteViews.setTextViewText(R.id.result, value); mRemoteViews.setTextViewText(R.id.result, value);
updateWidget(); updateWidget();
} }
@ -218,16 +221,15 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
} }
@Override
public void setFormula(String value) { public void setFormula(String value) {
remoteViews.setTextViewText(R.id.formula, value); mRemoteViews.setTextViewText(R.id.formula, value);
updateWidget(); updateWidget();
} }
@Override @Override
public void onDeleted(Context context, int[] appWidgetIds) { public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds); super.onDeleted(context, appWidgetIds);
if (cxt != null) if (mContext != null)
updateWidgetIds(); updateWidgetIds();
} }
} }

View File

@ -1,13 +1,16 @@
package com.simplemobiletools.calculator; package com.simplemobiletools.calculator.activities;
import android.content.Intent; import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.appcompat.BuildConfig;
import android.text.Html; import android.text.Html;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.widget.TextView; import android.widget.TextView;
import com.simplemobiletools.calculator.R;
import java.util.Calendar; import java.util.Calendar;
import butterknife.BindView; import butterknife.BindView;
@ -15,17 +18,17 @@ import butterknife.ButterKnife;
import butterknife.OnClick; import butterknife.OnClick;
public class AboutActivity extends AppCompatActivity { public class AboutActivity extends AppCompatActivity {
@BindView(R.id.about_copyright) TextView copyright; @BindView(R.id.about_copyright) TextView mCopyright;
@BindView(R.id.about_version) TextView version; @BindView(R.id.about_version) TextView mVersion;
@BindView(R.id.about_email) TextView emailTV; @BindView(R.id.about_email) TextView mEmailTV;
private Resources res; private static Resources mRes;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about); setContentView(R.layout.activity_about);
ButterKnife.bind(this); ButterKnife.bind(this);
res = getResources(); mRes = getResources();
setupEmail(); setupEmail();
setupVersion(); setupVersion();
@ -33,23 +36,23 @@ public class AboutActivity extends AppCompatActivity {
} }
private void setupEmail() { private void setupEmail() {
final String email = res.getString(R.string.email); final String email = mRes.getString(R.string.email);
final String appName = res.getString(R.string.app_name); final String appName = mRes.getString(R.string.app_name);
final String href = "<a href=\"mailto:" + email + "?subject=" + appName + "\">" + email + "</a>"; final String href = "<a href=\"mailto:" + email + "?subject=" + appName + "\">" + email + "</a>";
emailTV.setText(Html.fromHtml(href)); mEmailTV.setText(Html.fromHtml(href));
emailTV.setMovementMethod(LinkMovementMethod.getInstance()); mEmailTV.setMovementMethod(LinkMovementMethod.getInstance());
} }
private void setupVersion() { private void setupVersion() {
final String versionName = BuildConfig.VERSION_NAME; final String versionName = BuildConfig.VERSION_NAME;
final String versionText = String.format(res.getString(R.string.version), versionName); final String versionText = String.format(mRes.getString(R.string.version), versionName);
version.setText(versionText); mVersion.setText(versionText);
} }
private void setupCopyright() { private void setupCopyright() {
final int year = Calendar.getInstance().get(Calendar.YEAR); final int year = Calendar.getInstance().get(Calendar.YEAR);
final String copyrightText = String.format(res.getString(R.string.copyright), year); final String copyrightText = String.format(mRes.getString(R.string.copyright), year);
copyright.setText(copyrightText); mCopyright.setText(copyrightText);
} }
@OnClick(R.id.about_license) @OnClick(R.id.about_license)

View File

@ -1,14 +1,17 @@
package com.simplemobiletools.calculator; package com.simplemobiletools.calculator.activities;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import com.simplemobiletools.calculator.R;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.OnClick; import butterknife.OnClick;
public class LicenseActivity extends AppCompatActivity { public class LicenseActivity extends AppCompatActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);

View File

@ -1,4 +1,4 @@
package com.simplemobiletools.calculator; package com.simplemobiletools.calculator.activities;
import android.content.ClipData; import android.content.ClipData;
import android.content.ClipboardManager; import android.content.ClipboardManager;
@ -7,11 +7,17 @@ import android.content.res.Resources;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import com.simplemobiletools.calculator.Calculator;
import com.simplemobiletools.calculator.CalculatorImpl;
import com.simplemobiletools.calculator.Constants;
import com.simplemobiletools.calculator.Formatter;
import com.simplemobiletools.calculator.R;
import com.simplemobiletools.calculator.Utils;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.OnClick; import butterknife.OnClick;
@ -19,26 +25,26 @@ import butterknife.OnLongClick;
import me.grantland.widget.AutofitHelper; import me.grantland.widget.AutofitHelper;
public class MainActivity extends AppCompatActivity implements Calculator { public class MainActivity extends AppCompatActivity implements Calculator {
@BindView(R.id.result) TextView result; @BindView(R.id.result) TextView mResult;
@BindView(R.id.formula) TextView formula; @BindView(R.id.formula) TextView mFormula;
private CalculatorImpl calc; private static CalculatorImpl mCalc;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
ButterKnife.bind(this); ButterKnife.bind(this);
calc = new CalculatorImpl(this);
mCalc = new CalculatorImpl(this);
setupResultView(); setupResultView();
AutofitHelper.create(result); AutofitHelper.create(mResult);
AutofitHelper.create(formula); AutofitHelper.create(mFormula);
} }
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater(); getMenuInflater().inflate(R.menu.menu, menu);
inflater.inflate(R.menu.menu, menu);
return true; return true;
} }
@ -56,62 +62,62 @@ public class MainActivity extends AppCompatActivity implements Calculator {
private void setupResultView() { private void setupResultView() {
final Resources res = getResources(); final Resources res = getResources();
result.setBackgroundColor(res.getColor(android.R.color.white)); mResult.setBackgroundColor(res.getColor(android.R.color.white));
result.setTextColor(res.getColor(R.color.text_grey)); mResult.setTextColor(res.getColor(R.color.text_grey));
formula.setBackgroundColor(res.getColor(android.R.color.white)); mFormula.setBackgroundColor(res.getColor(android.R.color.white));
formula.setTextColor(res.getColor(R.color.text_grey)); mFormula.setTextColor(res.getColor(R.color.text_grey));
} }
@OnClick(R.id.btn_plus) @OnClick(R.id.btn_plus)
public void plusClicked() { public void plusClicked() {
calc.handleOperation(Constants.PLUS); mCalc.handleOperation(Constants.PLUS);
} }
@OnClick(R.id.btn_minus) @OnClick(R.id.btn_minus)
public void minusClicked() { public void minusClicked() {
calc.handleOperation(Constants.MINUS); mCalc.handleOperation(Constants.MINUS);
} }
@OnClick(R.id.btn_multiply) @OnClick(R.id.btn_multiply)
public void multiplyClicked() { public void multiplyClicked() {
calc.handleOperation(Constants.MULTIPLY); mCalc.handleOperation(Constants.MULTIPLY);
} }
@OnClick(R.id.btn_divide) @OnClick(R.id.btn_divide)
public void divideClicked() { public void divideClicked() {
calc.handleOperation(Constants.DIVIDE); mCalc.handleOperation(Constants.DIVIDE);
} }
@OnClick(R.id.btn_modulo) @OnClick(R.id.btn_modulo)
public void moduloClicked() { public void moduloClicked() {
calc.handleOperation(Constants.MODULO); mCalc.handleOperation(Constants.MODULO);
} }
@OnClick(R.id.btn_power) @OnClick(R.id.btn_power)
public void powerClicked() { public void powerClicked() {
calc.handleOperation(Constants.POWER); mCalc.handleOperation(Constants.POWER);
} }
@OnClick(R.id.btn_root) @OnClick(R.id.btn_root)
public void rootClicked() { public void rootClicked() {
calc.handleOperation(Constants.ROOT); mCalc.handleOperation(Constants.ROOT);
} }
@OnClick(R.id.btn_clear) @OnClick(R.id.btn_clear)
public void clearClicked() { public void clearClicked() {
calc.handleClear(); mCalc.handleClear();
} }
@OnLongClick(R.id.btn_clear) @OnLongClick(R.id.btn_clear)
public boolean clearLongClicked() { public boolean clearLongClicked() {
calc.handleReset(); mCalc.handleReset();
return true; return true;
} }
@OnClick(R.id.btn_equals) @OnClick(R.id.btn_equals)
public void equalsClicked() { public void equalsClicked() {
calc.handleEquals(); mCalc.handleEquals();
} }
@OnClick({R.id.btn_decimal, R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8, @OnClick({R.id.btn_decimal, R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8,
@ -121,7 +127,7 @@ public class MainActivity extends AppCompatActivity implements Calculator {
} }
public void numpadClicked(int id) { public void numpadClicked(int id) {
calc.numpadClicked(id); mCalc.numpadClicked(id);
} }
@OnLongClick(R.id.formula) @OnLongClick(R.id.formula)
@ -137,11 +143,9 @@ public class MainActivity extends AppCompatActivity implements Calculator {
} }
private void copyToClipboard(boolean copyResult) { private void copyToClipboard(boolean copyResult) {
String value; String value = mFormula.getText().toString();
if (copyResult) { if (copyResult) {
value = result.getText().toString(); value = mResult.getText().toString();
} else {
value = formula.getText().toString();
} }
final ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); final ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
@ -152,22 +156,21 @@ public class MainActivity extends AppCompatActivity implements Calculator {
@Override @Override
public void setValue(String value) { public void setValue(String value) {
result.setText(value); mResult.setText(value);
} }
// used only by Robolectric // used only by Robolectric
@Override @Override
public void setValueDouble(double d) { public void setValueDouble(double d) {
calc.setValue(Formatter.doubleToString(d)); mCalc.setValue(Formatter.doubleToString(d));
calc.setLastKey(Constants.DIGIT); mCalc.setLastKey(Constants.DIGIT);
} }
@Override
public void setFormula(String value) { public void setFormula(String value) {
formula.setText(value); mFormula.setText(value);
} }
public CalculatorImpl getCalc() { public CalculatorImpl getCalc() {
return calc; return mCalc;
} }
} }

View File

@ -8,7 +8,7 @@
android:background="@color/buttons_background" android:background="@color/buttons_background"
android:orientation="vertical" android:orientation="vertical"
android:theme="@style/MainTheme" android:theme="@style/MainTheme"
tools:context=".MainActivity"> tools:context=".activities.MainActivity">
<TextView <TextView
android:id="@+id/formula" android:id="@+id/formula"