Skip to content

Commit

Permalink
Merge remote-tracking branch 'literacyapp-org/cm-13.0' into cm-13.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Jo Grimstad committed Nov 15, 2016
2 parents 1112187 + 3186eb8 commit ee98901
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 17 deletions.
27 changes: 23 additions & 4 deletions app/src/main/java/com/android/calculator2/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,19 @@
import android.widget.Button;
import android.widget.FrameLayout;

import com.android.calculator2.view.GraphView;
import com.android.calculator2.view.MultiButton;
import com.android.calculator2.view.display.AdvancedDisplay.OnTextSizeChangeListener;
import com.android.calculator2.CalculatorExpressionEvaluator.EvaluateCallback;
import com.android.calculator2.view.display.AdvancedDisplay;
import com.android.calculator2.util.DigitLabelHelper;
import com.android.calculator2.util.PlayerUtil;
import com.android.calculator2.view.DisplayOverlay;
import com.android.calculator2.view.DisplayOverlay.DisplayMode;
import com.android.calculator2.view.GraphView;
import com.android.calculator2.view.MatrixEditText;
import com.android.calculator2.view.MatrixInverseView;
import com.android.calculator2.view.MatrixTransposeView;
import com.android.calculator2.view.MatrixView;
import com.android.calculator2.view.MultiButton;
import com.android.calculator2.view.display.AdvancedDisplay;
import com.android.calculator2.view.display.AdvancedDisplay.OnTextSizeChangeListener;
import com.xlythe.math.Base;
import com.xlythe.math.Constants;
import com.xlythe.math.GraphModule;
Expand All @@ -62,6 +64,8 @@

import org.literacyapp.calculator.R;

import static com.android.calculator2.util.PlayerUtil.RAW_FILE_EQUALS;

public class Calculator extends Activity
implements OnTextSizeChangeListener, EvaluateCallback, OnLongClickListener {

Expand Down Expand Up @@ -345,6 +349,12 @@ public void onUserInteraction() {
}

public void onButtonClick(View view) {

// Play audio for numbers and operators
if (view.getTag() != null && !RAW_FILE_EQUALS.equals(view.getTag())) {
PlayerUtil.playRawFile(this, view.getTag().toString());
}

mCurrentButton = view;
switch (view.getId()) {
case R.id.eq:
Expand Down Expand Up @@ -593,6 +603,15 @@ public void onAnimationStart(Animator animation) {
}

private void onResult(final String result) {

// Play audio for result
if (TextUtils.isDigitsOnly(result) && Integer.parseInt(result) < 10) {
View view = findViewById(DigitLabelHelper.getIdForDigit(Integer.parseInt(result)));
PlayerUtil.playResult(this, view.getTag().toString());
} else {
PlayerUtil.playRawFile(this, RAW_FILE_EQUALS);
}

// Make the clear button appear immediately.
setClearVisibility(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.ClipboardManager;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
Expand All @@ -13,13 +14,16 @@
import com.android.calculator2.Calculator;
import com.android.calculator2.CalculatorExpressionEvaluator;
import com.android.calculator2.CalculatorExpressionTokenizer;
import com.android.calculator2.util.DigitLabelHelper;
import com.android.calculator2.util.PlayerUtil;
import com.android.calculator2.view.display.AdvancedDisplay;
import com.xlythe.floatingview.FloatingView;
import com.xlythe.math.History;
import com.xlythe.math.Persist;

import org.literacyapp.calculator.R;

import static com.android.calculator2.util.PlayerUtil.RAW_FILE_EQUALS;

public class FloatingCalculator extends FloatingView {
// Calc logic
Expand All @@ -43,7 +47,7 @@ public View inflateButton() {
}

public View inflateView() {
View child = View.inflate(getContext(), R.layout.floating_calculator, null);
final View child = View.inflate(getContext(), R.layout.floating_calculator, null);

mTokenizer = new CalculatorExpressionTokenizer(this);
mEvaluator = new CalculatorExpressionEvaluator(mTokenizer);
Expand All @@ -69,7 +73,11 @@ public boolean onLongClick(View v) {
mClear = (ImageButton) child.findViewById(R.id.clear);
mListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
public void onClick(final View v) {
if (v.getTag() != null && !RAW_FILE_EQUALS.equals(v.getTag())) {
PlayerUtil.playRawFile(getContext(), v.getTag().toString());
}

if(v instanceof Button) {
if(((Button) v).getText().toString().equals("=")) {
mEvaluator.evaluate(mDisplay.getText(), new CalculatorExpressionEvaluator.EvaluateCallback() {
Expand All @@ -78,6 +86,13 @@ public void onEvaluate(String expr, String result, int errorResourceId) {
if (errorResourceId != Calculator.INVALID_RES_ID) {
onError(errorResourceId);
} else {
// Play audio for result
if (result != null && TextUtils.isDigitsOnly(result) && Integer.parseInt(result) < 10) {
View view = child.findViewById(DigitLabelHelper.getIdForDigit(Integer.parseInt(result)));
PlayerUtil.playResult(getContext(), view.getTag().toString());
} else {
PlayerUtil.playRawFile(getContext(), RAW_FILE_EQUALS);
}
setText(result);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@ public void getTextForDigits(Context context, DigitLabelHelperCallback callback)
}
}
}

public static int getIdForDigit(int digit) {
return sDigitIds[digit];
}
}
47 changes: 47 additions & 0 deletions app/src/main/java/com/android/calculator2/util/PlayerUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.android.calculator2.util;

import android.content.Context;
import android.media.MediaPlayer;

/**
* Created by GSC on 12/11/2016.
*/
public class PlayerUtil {

public static String RAW_FILE_EQUALS = "equals";

public static final String[] NUMBER_RAW_FILES = new String[] {
"digit_0",
"digit_1",
"digit_2",
"digit_3",
"digit_4",
"digit_5",
"digit_6",
"digit_7",
"digit_8",
"digit_9"
};

public static void playRawFile(Context context, String rawFile) {
int rawId = context.getResources().getIdentifier(rawFile, "raw", context.getPackageName());
if (rawId != 0) {
MediaPlayer mediaPlayer = MediaPlayer.create(context, rawId);
mediaPlayer.start();
}
}

public static void playResult(final Context context, final String result) {
int rawId = context.getResources().getIdentifier(RAW_FILE_EQUALS, "raw", context.getPackageName());
if (rawId != 0) {
final MediaPlayer mediaPlayer = MediaPlayer.create(context, rawId);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
playRawFile(context, result);
}
});
mediaPlayer.start();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import android.content.Context;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.RemoteViews;

import com.android.calculator2.CalculatorExpressionTokenizer;
import com.android.calculator2.util.DigitLabelHelper;
import com.android.calculator2.util.PlayerUtil;
import com.xlythe.math.Base;
import com.xlythe.math.Constants;
import com.xlythe.math.EquationFormatter;
Expand All @@ -24,22 +26,25 @@

import java.text.DecimalFormatSymbols;

import static com.android.calculator2.util.PlayerUtil.NUMBER_RAW_FILES;
import static com.android.calculator2.util.PlayerUtil.RAW_FILE_EQUALS;

public class CalculatorWidget extends AppWidgetProvider {
public final static String PREFERENCE_WIDGET_PREAMBLE = "com.android.calculator2.CALC_WIDGET_VALUE_";
public static final String DIGIT_0 = "com.android.calculator2.0";
public static final String DIGIT_1 = "com.android.calculator2.1";
public static final String DIGIT_2 = "com.android.calculator2.2";
public static final String DIGIT_3 = "com.android.calculator2.3";
public static final String DIGIT_4 = "com.android.calculator2.4";
public static final String DIGIT_5 = "com.android.calculator2.5";
public static final String DIGIT_6 = "com.android.calculator2.6";
public static final String DIGIT_7 = "com.android.calculator2.7";
public static final String DIGIT_8 = "com.android.calculator2.8";
public static final String DIGIT_9 = "com.android.calculator2.9";
public static final String DIGIT_0 = "com.android.calculator2.digit_0";
public static final String DIGIT_1 = "com.android.calculator2.digit_1";
public static final String DIGIT_2 = "com.android.calculator2.digit_2";
public static final String DIGIT_3 = "com.android.calculator2.digit_3";
public static final String DIGIT_4 = "com.android.calculator2.digit_4";
public static final String DIGIT_5 = "com.android.calculator2.digit_5";
public static final String DIGIT_6 = "com.android.calculator2.digit_6";
public static final String DIGIT_7 = "com.android.calculator2.digit_7";
public static final String DIGIT_8 = "com.android.calculator2.digit_8";
public static final String DIGIT_9 = "com.android.calculator2.digit_9";
public static final String DOT = "com.android.calculator2.dot";
public static final String PLUS = "com.android.calculator2.plus";
public static final String MINUS = "com.android.calculator2.minus";
public static final String MUL = "com.android.calculator2.mul";
public static final String MUL = "com.android.calculator2.multiplied_by";
public static final String DIV = "com.android.calculator2.div";
public static final String EQUALS = "com.android.calculator2.equals";
public static final String CLR = "com.android.calculator2.clear";
Expand All @@ -60,6 +65,12 @@ public void onReceive(Context context, Intent intent) {
if(value.equals(context.getResources().getString(R.string.error_syntax))) value = "";
mClearText = intent.getBooleanExtra(SHOW_CLEAR, false);

// Play audio for numbers and operators
String action = intent.getAction().substring(intent.getAction().lastIndexOf(".")+1);
if (!RAW_FILE_EQUALS.equals(action)) {
PlayerUtil.playRawFile(context, action);
}

if(intent.getAction().equals(DIGIT_0)) {
if(mClearText) {
value = "";
Expand Down Expand Up @@ -158,6 +169,14 @@ public void onReceive(Context context, Intent intent) {

// Try to save it to history
if(!value.equals(context.getResources().getString(R.string.error_syntax))) {

// Play audio for result
if (TextUtils.isDigitsOnly(value) && Integer.parseInt(value) < 10) {
PlayerUtil.playResult(context, NUMBER_RAW_FILES[Integer.parseInt(value)]);
} else {
PlayerUtil.playRawFile(context, RAW_FILE_EQUALS);
}

final Persist persist = new Persist(context);
persist.load();
if(persist.getMode() == null) persist.setMode(Base.DECIMAL);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/equals_graph_button.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

<Button
android:id="@+id/eq"
android:tag="equals"
style="@style/PadButtonStyle.Numeric"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/layout/floating_calculator_basic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@

<Button
android:id="@+id/digit7"
android:tag="digit_7"
style="@style/Theme.Calculator.ButtonStyle.FloatingCalc"
android:layout_width="@dimen/floating_window_button_height"
android:layout_height="@dimen/floating_window_button_height"
android:layout_gravity="center" />

<Button
android:id="@+id/digit4"
android:tag="digit_4"
style="@style/Theme.Calculator.ButtonStyle.FloatingCalc"
android:layout_width="@dimen/floating_window_button_height"
android:layout_height="@dimen/floating_window_button_height"
android:layout_gravity="center" />

<Button
android:id="@+id/digit1"
android:tag="digit_1"
style="@style/Theme.Calculator.ButtonStyle.FloatingCalc"
android:layout_width="@dimen/floating_window_button_height"
android:layout_height="@dimen/floating_window_button_height"
Expand All @@ -49,27 +52,31 @@

<Button
android:id="@+id/digit8"
android:tag="digit_8"
style="@style/Theme.Calculator.ButtonStyle.FloatingCalc"
android:layout_width="@dimen/floating_window_button_height"
android:layout_height="@dimen/floating_window_button_height"
android:layout_gravity="center" />

<Button
android:id="@+id/digit5"
android:tag="digit_5"
style="@style/Theme.Calculator.ButtonStyle.FloatingCalc"
android:layout_width="@dimen/floating_window_button_height"
android:layout_height="@dimen/floating_window_button_height"
android:layout_gravity="center" />

<Button
android:id="@+id/digit2"
android:tag="digit_2"
style="@style/Theme.Calculator.ButtonStyle.FloatingCalc"
android:layout_width="@dimen/floating_window_button_height"
android:layout_height="@dimen/floating_window_button_height"
android:layout_gravity="center" />

<Button
android:id="@+id/digit0"
android:tag="digit_0"
style="@style/Theme.Calculator.ButtonStyle.FloatingCalc"
android:layout_width="@dimen/floating_window_button_height"
android:layout_height="@dimen/floating_window_button_height"
Expand All @@ -85,20 +92,23 @@

<Button
android:id="@+id/digit9"
android:tag="digit_9"
style="@style/Theme.Calculator.ButtonStyle.FloatingCalc"
android:layout_width="@dimen/floating_window_button_height"
android:layout_height="@dimen/floating_window_button_height"
android:layout_gravity="center" />

<Button
android:id="@+id/digit6"
android:tag="digit_6"
style="@style/Theme.Calculator.ButtonStyle.FloatingCalc"
android:layout_width="@dimen/floating_window_button_height"
android:layout_height="@dimen/floating_window_button_height"
android:layout_gravity="center" />

<Button
android:id="@+id/digit3"
android:tag="digit_3"
style="@style/Theme.Calculator.ButtonStyle.FloatingCalc"
android:layout_width="@dimen/floating_window_button_height"
android:layout_height="@dimen/floating_window_button_height"
Expand Down Expand Up @@ -129,6 +139,7 @@

<Button
android:id="@+id/mul"
android:tag="multiplied_by"
style="@style/Theme.Calculator.ButtonStyle.FloatingCalc"
android:layout_width="@dimen/floating_window_button_height"
android:layout_height="@dimen/floating_window_button_height"
Expand All @@ -137,6 +148,7 @@

<Button
android:id="@+id/sub"
android:tag="minus"
style="@style/Theme.Calculator.ButtonStyle.FloatingCalc"
android:layout_width="@dimen/floating_window_button_height"
android:layout_height="@dimen/floating_window_button_height"
Expand All @@ -145,6 +157,7 @@

<Button
android:id="@+id/plus"
android:tag="plus"
style="@style/Theme.Calculator.ButtonStyle.FloatingCalc"
android:layout_width="@dimen/floating_window_button_height"
android:layout_height="@dimen/floating_window_button_height"
Expand All @@ -153,6 +166,7 @@

<Button
android:id="@+id/eq"
android:tag="equals"
style="@style/Theme.Calculator.ButtonStyle.FloatingCalc"
android:layout_width="@dimen/floating_window_button_height"
android:layout_height="@dimen/floating_window_button_height"
Expand Down
Loading

0 comments on commit ee98901

Please sign in to comment.