diff --git a/app/src/main/java/me/micrusa/amaztimer/widget.java b/app/src/main/java/me/micrusa/amaztimer/widget.java index b73dd961..566b56fd 100644 --- a/app/src/main/java/me/micrusa/amaztimer/widget.java +++ b/app/src/main/java/me/micrusa/amaztimer/widget.java @@ -12,8 +12,10 @@ import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; +import android.view.View.OnLongClickListener; import android.widget.Button; import android.widget.TextView; +import android.widget.Toast; import clc.sliteplugin.flowboard.AbstractPlugin; import clc.sliteplugin.flowboard.ISpringBoardHostStub; @@ -29,10 +31,15 @@ public class widget extends AbstractPlugin { private boolean mHasActive = false; private ISpringBoardHostStub mHost = null; private int v; - //Setup objects - private Button plus, plus2, plus3, minus, minus2, minus3, start; + //Define items + private Button plus, plus2, plus3, minus, minus2, minus3, start, cancel; private TextView sets, rest, work, time, hr, rSets, status; private ConstraintLayout L1, L2; + //Define timers and timer booleans + private CountDownTimer workTimer; + private CountDownTimer restTimer; + private boolean workStarted = false; + private boolean restStarted = false; //Classes private utils utils = new utils(); //Default values @@ -144,15 +151,7 @@ public void onClick(View view) { final CountDownTimer PrepareTimer = new CountDownTimer(5 * 1000, 1000) { @Override public void onTick(long l) { - v = (int) l / 1000; - time.setText(utils.sToMinS(v)); - if(v<4){ - if(v==1){ - utils.vibrate(defValues.lVibration, gView.getContext());} - if(v!=1){ - utils.vibrate(defValues.sVibration, gView.getContext());} - } - + timerUpdate((int) l / 1000); } @Override @@ -163,6 +162,27 @@ public void onFinish() { } }); + + //Cancel button + //To avoid accidental clicks, just a long click will cancel it + cancel.setOnLongClickListener(new OnLongClickListener() { + @Override + public boolean onLongClick(View v) { + //Display start layout + L1.setVisibility(View.VISIBLE); + L2.setVisibility(View.GONE); + //Stop timers + stopTimers(); + return true; + } + }); + cancel.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + //Send toast + Toast.makeText(gView.getContext(), gView.getResources().getString(R.string.canceltoast), Toast.LENGTH_SHORT); + } + }); return this.mView; } @@ -175,6 +195,7 @@ private void init(){ minus2 = this.mView.findViewById(R.id.minus); minus3 = this.mView.findViewById(R.id.minus3); start = this.mView.findViewById(R.id.start); + cancel = this.mView.findViewById(R.id.cancel); //TextViews sets = this.mView.findViewById(R.id.sets); rest = this.mView.findViewById(R.id.rest); @@ -188,24 +209,41 @@ private void init(){ L2 = this.mView.findViewById(R.id.timerScreen); } + private void timerUpdate(int v){ + this.init(); + time.setText(utils.sToMinS(v)); + if(v<4){ + if(v==1){ + utils.vibrate(defValues.lVibration, this.mView.getContext());} + if(v!=1){ + utils.vibrate(defValues.sVibration, this.mView.getContext());} + } + } + + private void stopTimers(){ + if(this.workStarted){ + this.workTimer.cancel(); + } + if(this.restStarted){ + this.restTimer.cancel(); + } + } + private void startTimer(final View c, final String sWork, final String sRest, final int work, final int rest, final hrSensor hrSensor){ this.init(); - status.setText(sWork); - L2.setBackgroundColor(c.getResources().getColor(R.color.red)); + this.workStarted = true; + this.restStarted = false; if(!this.mHasActive){ + this.workStarted = false; + this.restStarted = false; return; } - CountDownTimer Timer = new CountDownTimer(work * 1000, 1000) { + status.setText(sWork); + L2.setBackgroundColor(c.getResources().getColor(R.color.red)); + this.workTimer = new CountDownTimer(work * 1000, 1000) { @Override public void onTick(long l) { - v = (int) l / 1000; - time.setText(utils.sToMinS(v)); - if(v<4){ - if(v==1){ - utils.vibrate(defValues.lVibration, c.getContext());} - if(v!=1){ - utils.vibrate(defValues.sVibration, c.getContext());} - } + timerUpdate((int) l / 1000); } @Override @@ -213,27 +251,24 @@ public void onFinish() { restTimer(c, sWork, sRest, work, rest, hrSensor); } }; - Timer.start(); + this.workTimer.start(); } private void restTimer(final View c, final String sWork, final String sRest, final int work, final int rest, final hrSensor hrSensor){ this.init(); - status.setText(sRest); - L2.setBackgroundColor(c.getResources().getColor(R.color.green)); + this.workStarted = false; + this.restStarted = true; if(!this.mHasActive){ + this.workStarted = false; + this.restStarted = false; return; } - CountDownTimer Timer = new CountDownTimer(rest * 1000, 1000) { + status.setText(sRest); + L2.setBackgroundColor(c.getResources().getColor(R.color.green)); + this.restTimer = new CountDownTimer(rest * 1000, 1000) { @Override public void onTick(long l) { - v = (int) l / 1000; - time.setText(utils.sToMinS(v)); - if (v < 4) { - if (v != 1) { - utils.vibrate(defValues.sVibration, c.getContext()); - } else{ - utils.vibrate(defValues.lVibration, c.getContext());} - } + timerUpdate((int) l / 1000); } @Override @@ -249,7 +284,7 @@ public void onFinish() { } } }; - Timer.start(); + this.restTimer.start(); } //Return the icon for this page, used when the page is disabled in the app list. In this case, the launcher icon is used diff --git a/app/src/main/res/layout/amaztimer.xml b/app/src/main/res/layout/amaztimer.xml index 0eee0686..754e7360 100755 --- a/app/src/main/res/layout/amaztimer.xml +++ b/app/src/main/res/layout/amaztimer.xml @@ -10,7 +10,7 @@ android:id="@+id/startScreen" android:layout_width="match_parent" android:layout_height="match_parent" - android:visibility="gone" + android:visibility="visible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" @@ -160,7 +160,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:keepScreenOn="true" - android:visibility="visible" + android:visibility="gone" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" @@ -170,7 +170,7 @@ android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginBottom="12dp" + android:layout_marginBottom="4dp" android:text="--:--" android:textColor="@color/black" android:textSize="36sp" @@ -182,11 +182,12 @@ android:id="@+id/heartbeat" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:layout_marginBottom="36dp" android:drawableStart="@drawable/heartbeat" android:text="--" android:textColor="@color/black" android:textSize="24sp" - app:layout_constraintBottom_toTopOf="@+id/remSets" + app:layout_constraintBottom_toTopOf="@+id/time" app:layout_constraintEnd_toEndOf="@+id/time" app:layout_constraintHorizontal_bias="0.45" app:layout_constraintStart_toStartOf="@+id/time" /> @@ -207,14 +208,25 @@ android:id="@+id/status" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginBottom="28dp" android:text="--------------" android:textColor="@color/black" android:textSize="30sp" - app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintBottom_toTopOf="@+id/cancel" app:layout_constraintEnd_toEndOf="@+id/time" app:layout_constraintHorizontal_bias="0.523" app:layout_constraintStart_toStartOf="@+id/time" /> + +