-
-
Save anonymous/6713261222647889ef74db8e876be8ee to your computer and use it in GitHub Desktop.
MainActivity and object class for my timer app.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alxmjo.simplymindful.ui; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.os.CountDownTimer; | |
import android.preference.PreferenceManager; | |
import android.support.design.widget.FloatingActionButton; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.view.View; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import com.alxmjo.simplymindful.R; | |
import com.alxmjo.simplymindful.session.Session; | |
import com.alxmjo.simplymindful.settings.SettingsActivity; | |
import com.alxmjo.simplymindful.utilities.AlarmReceiver; | |
import com.mikhaellopez.circularprogressbar.CircularProgressBar; | |
import android.widget.ImageButton; | |
import android.widget.TextView; | |
import java.util.concurrent.TimeUnit; | |
public class MainActivity extends AppCompatActivity { | |
private static final String TAG = "MainActivity"; | |
// Session and timer | |
public Session session; | |
private CountDownTimer countDownTimer; | |
// Controls | |
private boolean isTicking; | |
// Views | |
private CircularProgressBar circularProgressBar; | |
private FloatingActionButton startPauseButton; | |
private ImageButton resetButton; | |
private TextView prepDigits; | |
private TextView mindDigits; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
PreferenceManager.setDefaultValues(this, R.xml.preferences, false); | |
// Initialize session | |
initializeSession(); | |
// Initialize views | |
circularProgressBar = (CircularProgressBar) findViewById(R.id.circular_progress_bar); | |
startPauseButton = (FloatingActionButton) findViewById(R.id.start_pause_button); | |
prepDigits = (TextView) findViewById(R.id.preparation_digits); | |
mindDigits = (TextView) findViewById(R.id.mindfulness_digits); | |
// Set views | |
setPrepDigits(session.getPrepRemaining()); | |
setMindDigits(session.getMindRemaining()); | |
} | |
// ---Session lifecycle methods--- | |
private void initializeSession() { | |
session = new Session(this); | |
isTicking = false; | |
} | |
public void startSession(View v) { | |
if (!isTicking) { | |
// Runs on play press | |
countDownTimer = new CountDownTimer(session.getPrepRemaining() + session.getMindRemaining(), 10) { | |
@Override | |
public void onTick(long millisUntilFinished) { | |
if (millisUntilFinished > session.getMindRemaining()) { | |
// Preparation timer | |
session.setPrepRemaining(millisUntilFinished - session.getMindRemaining()); | |
setPrepDigits(millisUntilFinished - session.getMindRemaining()); | |
} else { | |
// Mindfulness timer | |
prepDigits.setVisibility(View.INVISIBLE); | |
session.setMindRemaining(millisUntilFinished); | |
circularProgressBar.setProgress(((session.getMindLength() - millisUntilFinished) / (float) session.getMindLength()) * 100); | |
setMindDigits(millisUntilFinished); | |
} | |
} | |
@Override | |
public void onFinish() { | |
session = null; | |
isTicking = false; | |
circularProgressBar.setProgress(100); | |
initializeSession(); | |
} | |
}; | |
countDownTimer.start(); | |
isTicking = true; | |
startPauseButton.setImageDrawable(getResources().getDrawable(android.R.drawable.ic_media_pause)); | |
// Alarm | |
AlarmReceiver.scheduleAlarm(this); | |
} else { | |
// Runs on pause press | |
countDownTimer.cancel(); | |
isTicking = false; | |
startPauseButton.setImageDrawable(getResources().getDrawable(android.R.drawable.ic_media_play)); | |
} | |
} | |
public void resetSession(View v) { | |
// Runs on reset press | |
if (countDownTimer != null) { | |
countDownTimer.cancel(); | |
} | |
startPauseButton.setImageDrawable(getResources().getDrawable(android.R.drawable.ic_media_play)); | |
circularProgressBar.setProgress(0); | |
prepDigits.setVisibility(View.VISIBLE); | |
initializeSession(); | |
setPrepDigits(session.getPrepRemaining()); | |
setMindDigits(session.getMindRemaining()); | |
} | |
// ---UI methods--- | |
public void setPrepDigits(long l) { | |
if (l == 0) { | |
prepDigits.setVisibility(View.INVISIBLE); | |
} else { | |
String s = Long.toString(l / 1000); | |
prepDigits.setText(s.replaceFirst("^0+(?!$)", "")); | |
} | |
} | |
public void setMindDigits(long l) { | |
if (l >= 3600000) { | |
String hms = String.format("%02d %02d %02d", TimeUnit.MILLISECONDS.toHours(l), | |
TimeUnit.MILLISECONDS.toMinutes(l) % TimeUnit.HOURS.toMinutes(1), | |
TimeUnit.MILLISECONDS.toSeconds(l) % TimeUnit.MINUTES.toSeconds(1)); | |
mindDigits.setText(hms.replaceFirst("^0+(?!$)", "")); | |
} else if (l < 3600000 && l >= 60000) { | |
String ms = String.format("%02d %02d", | |
TimeUnit.MILLISECONDS.toMinutes(l) % TimeUnit.HOURS.toMinutes(1), | |
TimeUnit.MILLISECONDS.toSeconds(l) % TimeUnit.MINUTES.toSeconds(1)); | |
mindDigits.setText(ms.replaceFirst("^0+(?!$)", "")); | |
} else { | |
String s = String.format("%02d", | |
TimeUnit.MILLISECONDS.toSeconds(l) % TimeUnit.MINUTES.toSeconds(1)); | |
mindDigits.setText(s.replaceFirst("^0+(?!$)", "")); | |
} | |
} | |
// ---Menu methods--- | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
Intent intent = new Intent(MainActivity.this, SettingsActivity.class); | |
startActivity(intent); | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alxmjo.simplymindful.session; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.preference.PreferenceManager; | |
public class Session { | |
private long prepLength; | |
private long mindLength; | |
private long prepRemaining; | |
private long mindRemaining; | |
// Constructor | |
public Session(Context context) { | |
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); | |
prepLength = 1000 * Long.parseLong(sharedPreferences.getString("preparation_length", "")); | |
mindLength = 60000 * Long.parseLong(sharedPreferences.getString("mindfulness_length", "")); | |
prepRemaining = prepLength; | |
mindRemaining = mindLength; | |
} | |
// Getters and setters | |
public void setPrepRemaining(long prepRemaining) { | |
this.prepRemaining = prepRemaining; | |
} | |
public long getPrepRemaining() { | |
return prepRemaining; | |
} | |
public void setMindRemaining(long mindRemaining) { | |
this.mindRemaining = mindRemaining; | |
} | |
public long getMindRemaining() { | |
return mindRemaining; | |
} | |
public long getPrepLength() { | |
return prepLength; | |
} | |
public long getMindLength() { | |
return mindLength; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment