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.runner.AndroidJUnit4;
import com.simplemobiletools.calculator.activities.MainActivity;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

View file

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

View file

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

View file

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

View file

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

View file

@ -1,13 +1,16 @@
package com.simplemobiletools.calculator;
package com.simplemobiletools.calculator.activities;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.appcompat.BuildConfig;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
import com.simplemobiletools.calculator.R;
import java.util.Calendar;
import butterknife.BindView;
@ -15,17 +18,17 @@ import butterknife.ButterKnife;
import butterknife.OnClick;
public class AboutActivity extends AppCompatActivity {
@BindView(R.id.about_copyright) TextView copyright;
@BindView(R.id.about_version) TextView version;
@BindView(R.id.about_email) TextView emailTV;
private Resources res;
@BindView(R.id.about_copyright) TextView mCopyright;
@BindView(R.id.about_version) TextView mVersion;
@BindView(R.id.about_email) TextView mEmailTV;
private static Resources mRes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
ButterKnife.bind(this);
res = getResources();
mRes = getResources();
setupEmail();
setupVersion();
@ -33,23 +36,23 @@ public class AboutActivity extends AppCompatActivity {
}
private void setupEmail() {
final String email = res.getString(R.string.email);
final String appName = res.getString(R.string.app_name);
final String email = mRes.getString(R.string.email);
final String appName = mRes.getString(R.string.app_name);
final String href = "<a href=\"mailto:" + email + "?subject=" + appName + "\">" + email + "</a>";
emailTV.setText(Html.fromHtml(href));
emailTV.setMovementMethod(LinkMovementMethod.getInstance());
mEmailTV.setText(Html.fromHtml(href));
mEmailTV.setMovementMethod(LinkMovementMethod.getInstance());
}
private void setupVersion() {
final String versionName = BuildConfig.VERSION_NAME;
final String versionText = String.format(res.getString(R.string.version), versionName);
version.setText(versionText);
final String versionText = String.format(mRes.getString(R.string.version), versionName);
mVersion.setText(versionText);
}
private void setupCopyright() {
final int year = Calendar.getInstance().get(Calendar.YEAR);
final String copyrightText = String.format(res.getString(R.string.copyright), year);
copyright.setText(copyrightText);
final String copyrightText = String.format(mRes.getString(R.string.copyright), year);
mCopyright.setText(copyrightText);
}
@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.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.simplemobiletools.calculator.R;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class LicenseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle 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.ClipboardManager;
@ -7,11 +7,17 @@ import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
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.ButterKnife;
import butterknife.OnClick;
@ -19,26 +25,26 @@ import butterknife.OnLongClick;
import me.grantland.widget.AutofitHelper;
public class MainActivity extends AppCompatActivity implements Calculator {
@BindView(R.id.result) TextView result;
@BindView(R.id.formula) TextView formula;
@BindView(R.id.result) TextView mResult;
@BindView(R.id.formula) TextView mFormula;
private CalculatorImpl calc;
private static CalculatorImpl mCalc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
calc = new CalculatorImpl(this);
mCalc = new CalculatorImpl(this);
setupResultView();
AutofitHelper.create(result);
AutofitHelper.create(formula);
AutofitHelper.create(mResult);
AutofitHelper.create(mFormula);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@ -56,62 +62,62 @@ public class MainActivity extends AppCompatActivity implements Calculator {
private void setupResultView() {
final Resources res = getResources();
result.setBackgroundColor(res.getColor(android.R.color.white));
result.setTextColor(res.getColor(R.color.text_grey));
mResult.setBackgroundColor(res.getColor(android.R.color.white));
mResult.setTextColor(res.getColor(R.color.text_grey));
formula.setBackgroundColor(res.getColor(android.R.color.white));
formula.setTextColor(res.getColor(R.color.text_grey));
mFormula.setBackgroundColor(res.getColor(android.R.color.white));
mFormula.setTextColor(res.getColor(R.color.text_grey));
}
@OnClick(R.id.btn_plus)
public void plusClicked() {
calc.handleOperation(Constants.PLUS);
mCalc.handleOperation(Constants.PLUS);
}
@OnClick(R.id.btn_minus)
public void minusClicked() {
calc.handleOperation(Constants.MINUS);
mCalc.handleOperation(Constants.MINUS);
}
@OnClick(R.id.btn_multiply)
public void multiplyClicked() {
calc.handleOperation(Constants.MULTIPLY);
mCalc.handleOperation(Constants.MULTIPLY);
}
@OnClick(R.id.btn_divide)
public void divideClicked() {
calc.handleOperation(Constants.DIVIDE);
mCalc.handleOperation(Constants.DIVIDE);
}
@OnClick(R.id.btn_modulo)
public void moduloClicked() {
calc.handleOperation(Constants.MODULO);
mCalc.handleOperation(Constants.MODULO);
}
@OnClick(R.id.btn_power)
public void powerClicked() {
calc.handleOperation(Constants.POWER);
mCalc.handleOperation(Constants.POWER);
}
@OnClick(R.id.btn_root)
public void rootClicked() {
calc.handleOperation(Constants.ROOT);
mCalc.handleOperation(Constants.ROOT);
}
@OnClick(R.id.btn_clear)
public void clearClicked() {
calc.handleClear();
mCalc.handleClear();
}
@OnLongClick(R.id.btn_clear)
public boolean clearLongClicked() {
calc.handleReset();
mCalc.handleReset();
return true;
}
@OnClick(R.id.btn_equals)
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,
@ -121,7 +127,7 @@ public class MainActivity extends AppCompatActivity implements Calculator {
}
public void numpadClicked(int id) {
calc.numpadClicked(id);
mCalc.numpadClicked(id);
}
@OnLongClick(R.id.formula)
@ -137,11 +143,9 @@ public class MainActivity extends AppCompatActivity implements Calculator {
}
private void copyToClipboard(boolean copyResult) {
String value;
String value = mFormula.getText().toString();
if (copyResult) {
value = result.getText().toString();
} else {
value = formula.getText().toString();
value = mResult.getText().toString();
}
final ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
@ -152,22 +156,21 @@ public class MainActivity extends AppCompatActivity implements Calculator {
@Override
public void setValue(String value) {
result.setText(value);
mResult.setText(value);
}
// used only by Robolectric
@Override
public void setValueDouble(double d) {
calc.setValue(Formatter.doubleToString(d));
calc.setLastKey(Constants.DIGIT);
mCalc.setValue(Formatter.doubleToString(d));
mCalc.setLastKey(Constants.DIGIT);
}
@Override
public void setFormula(String value) {
formula.setText(value);
mFormula.setText(value);
}
public CalculatorImpl getCalc() {
return calc;
return mCalc;
}
}

View file

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