package nz.ac.otago.infosci.timekeeper; import android.content.Intent; import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; import android.util.Log; 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.text.DateFormat; import java.text.SimpleDateFormat; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.Date; import java.util.TimeZone; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.TimeUnit; public class TimerActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent it = getIntent(); final int endHour = it.getIntExtra("endHour", -1); final int endMinutes = it.getIntExtra("endMinutes", -1); final int updatePeriod = it.getIntExtra("updatePeriod", -1); final boolean dimScreen = it.getBooleanExtra("dimScreen", false); final View root = getWindow().getDecorView(); final Window window = getWindow(); // force the dark layout AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); // keep screen on window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); final int flags = View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_IMMERSIVE | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; // fullscreen, sticky immersive root.setSystemUiVisibility(flags); // re-enable immersive mode after a system UI change (such as switching back from another app) root.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() { @Override public void onSystemUiVisibilityChange(int visibility) { if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) { root.setSystemUiVisibility(flags); } } }); if(dimScreen) { WindowManager.LayoutParams params = getWindow().getAttributes(); params.screenBrightness = 0.025f; getWindow().setAttributes(params); } setContentView(R.layout.activity_timer); final TextView txtCounter = findViewById(R.id.txtCounter); final TextView txtPercent = findViewById(R.id.txtPercent); final ProgressBar progressBar = findViewById(R.id.progressBar); final TextView txtTime = findViewById(R.id.txtTime); final TextView txtStatus = findViewById(R.id.txtStatus); txtStatus.setText(""); txtPercent.setText("0%"); 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"); final DateFormat fmt = new SimpleDateFormat("m:ss"); fmt.setTimeZone(TimeZone.getTimeZone("UTC")); Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { synchronized public void run() { runOnUiThread(new Runnable() { @Override public void run() { LocalTime now = LocalTime.now(); int remaining = Math.toIntExact(ChronoUnit.MILLIS.between(now, end)); int percent = Math.round(((duration - remaining) / (float) duration) * 100); txtTime.setText(timeFmt.format(now).toLowerCase()); if (remaining > 0) { long minutesRemaining = TimeUnit.MILLISECONDS.toMinutes(remaining) + 1; if (minutesRemaining > 2) { txtCounter.setText(String.valueOf(minutesRemaining)); } else { txtCounter.setText(fmt.format(new Date(remaining))); } txtPercent.setText(percent+"%"); progressBar.setProgress(duration - Math.toIntExact(remaining), true); } else { if(dimScreen) { WindowManager.LayoutParams params = getWindow().getAttributes(); params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE; getWindow().setAttributes(params); progressBar.setBackgroundColor(Color.RED); } progressBar.setProgress(progressBar.getMax()); txtCounter.setText(fmt.format(new Date(Math.abs(remaining)))); txtPercent.setText("100%"); txtStatus.setText("Overtime!"); } } }); } }, 0, updatePeriod * 1000); } }