package nz.ac.otago.infosci.timekeeper; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.os.CountDownTimer; import android.os.VibrationEffect; import android.os.Vibrator; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.ProgressBar; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatDelegate; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; public class TimerActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final View root = getWindow().getDecorView(); final Window window = getWindow(); AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); // keep screen on window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // completely black background root.setBackgroundColor(Color.BLACK); // fullscreen root.setSystemUiVisibility( View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_IMMERSIVE | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION ); setContentView(R.layout.activity_timer); final TextView txtCounter = findViewById(R.id.txtCounter); final ProgressBar progressBar = findViewById(R.id.progressBar); final TextView txtTime = findViewById(R.id.txtTime); Intent it = getIntent(); final int endHour = it.getIntExtra("endHour", -1); final int endMinutes = it.getIntExtra("endMinutes", -1); final LocalTime start = LocalTime.now(); final LocalTime end = LocalTime.of(endHour, endMinutes); final int duration = Math.toIntExact(ChronoUnit.MILLIS.between(start, end)); progressBar.setMax(duration); progressBar.setMin(0); final DateTimeFormatter timeFmt = DateTimeFormatter.ofPattern("h:mm a"); new CountDownTimer(duration, 5000) { public void onTick(long millisUntilFinished) { LocalTime now = LocalTime.now(); int remaining = Math.toIntExact(ChronoUnit.MINUTES.between(now, end)); txtCounter.setText(String.valueOf(remaining + 1)); progressBar.setProgress(duration - Math.toIntExact(millisUntilFinished), true); txtTime.setText(timeFmt.format(now).toLowerCase()); } public void onFinish() { progressBar.setBackgroundColor(Color.RED); ((Vibrator) getApplicationContext().getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(500, 255)); } }.start(); } }