From 3cb6e8757bcec0a5a0205ecd3450aac28cb14473 Mon Sep 17 00:00:00 2001 From: Brandon Presley Date: Wed, 19 Oct 2022 19:59:22 +1300 Subject: [PATCH] Make timer alarm have a stop button --- android/app/build.gradle | 2 + android/app/src/main/AndroidManifest.xml | 23 +-- .../src/main/java/com/massive/Fullscreen.kt | 185 ------------------ .../src/main/java/com/massive/TimerDone.kt | 23 +++ .../src/main/java/com/massive/TimerService.kt | 2 +- .../main/res/layout/activity_fullscreen.xml | 51 ----- .../main/res/layout/activity_timer_done.xml | 30 +++ 7 files changed, 65 insertions(+), 251 deletions(-) delete mode 100644 android/app/src/main/java/com/massive/Fullscreen.kt create mode 100644 android/app/src/main/java/com/massive/TimerDone.kt delete mode 100644 android/app/src/main/res/layout/activity_fullscreen.xml create mode 100644 android/app/src/main/res/layout/activity_timer_done.xml diff --git a/android/app/build.gradle b/android/app/build.gradle index 509a407..d21f33d 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -149,6 +149,8 @@ android { dependencies { implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'com.google.android.material:material:1.4.+' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + implementation 'androidx.databinding:databinding-runtime:7.1.2' def work_version = "2.7.1" implementation "androidx.work:work-runtime:$work_version" diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 58511a8..b0372ee 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -21,17 +21,12 @@ android:roundIcon="@mipmap/ic_launcher_round" android:theme="@style/AppTheme"> - + android:name=".TimerDone" + android:exported="false"> + + + android:exported="false" /> + android:exported="false" /> \ No newline at end of file diff --git a/android/app/src/main/java/com/massive/Fullscreen.kt b/android/app/src/main/java/com/massive/Fullscreen.kt deleted file mode 100644 index d64e4bf..0000000 --- a/android/app/src/main/java/com/massive/Fullscreen.kt +++ /dev/null @@ -1,185 +0,0 @@ -package com.massive - -import androidx.appcompat.app.AppCompatActivity -import android.annotation.SuppressLint -import android.content.Intent -import android.os.Build -import android.os.Bundle -import android.os.Handler -import android.os.Looper -import android.util.Log -import android.view.MotionEvent -import android.view.View -import android.view.WindowInsets -import android.widget.LinearLayout -import android.widget.TextView -import com.massive.databinding.ActivityFullscreenBinding - -/** - * An example full-screen activity that shows and hides the system UI (i.e. - * status bar and navigation/system bar) with user interaction. - */ -class Fullscreen : AppCompatActivity() { - - private lateinit var binding: ActivityFullscreenBinding - private lateinit var fullscreenContent: TextView - private lateinit var fullscreenContentControls: LinearLayout - private val hideHandler = Handler(Looper.myLooper()!!) - - @SuppressLint("InlinedApi") - private val hidePart2Runnable = Runnable { - // Delayed removal of status and navigation bar - if (Build.VERSION.SDK_INT >= 30) { - fullscreenContent.windowInsetsController?.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars()) - } else { - // Note that some of these constants are new as of API 16 (Jelly Bean) - // and API 19 (KitKat). It is safe to use them, as they are inlined - // at compile-time and do nothing on earlier devices. - fullscreenContent.systemUiVisibility = - View.SYSTEM_UI_FLAG_LOW_PROFILE or - View.SYSTEM_UI_FLAG_FULLSCREEN or - View.SYSTEM_UI_FLAG_LAYOUT_STABLE or - View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or - View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or - View.SYSTEM_UI_FLAG_HIDE_NAVIGATION - } - } - private val showPart2Runnable = Runnable { - // Delayed display of UI elements - supportActionBar?.show() - fullscreenContentControls.visibility = View.VISIBLE - } - private var isFullscreen: Boolean = false - - private val hideRunnable = Runnable { hide() } - - /** - * Touch listener to use for in-layout UI controls to delay hiding the - * system UI. This is to prevent the jarring behavior of controls going away - * while interacting with activity UI. - */ - private val delayHideTouchListener = View.OnTouchListener { view, motionEvent -> - when (motionEvent.action) { - MotionEvent.ACTION_DOWN -> if (AUTO_HIDE) { - delayedHide(AUTO_HIDE_DELAY_MILLIS) - } - MotionEvent.ACTION_UP -> view.performClick() - else -> { - } - } - false - } - - private fun stop() { - Log.d("Fullscreen", "Stopping...") - applicationContext.stopService(Intent(applicationContext, TimerService::class.java)) - applicationContext.stopService(Intent(applicationContext, AlarmService::class.java)) - val intent = Intent(applicationContext, MainActivity::class.java) - intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK - applicationContext.startActivity(intent) - } - - @SuppressLint("ClickableViewAccessibility") - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - - binding = ActivityFullscreenBinding.inflate(layoutInflater) - setContentView(binding.root) - - supportActionBar?.setDisplayHomeAsUpEnabled(true) - - isFullscreen = true - - // Set up the user interaction to manually show or hide the system UI. - fullscreenContent = binding.fullscreenContent - fullscreenContent.setOnClickListener { toggle() } - - fullscreenContentControls = binding.fullscreenContentControls - - // Upon interacting with UI controls, delay any scheduled hide() - // operations to prevent the jarring behavior of controls going away - // while interacting with the UI. - binding.dummyButton.setOnTouchListener(delayHideTouchListener) - - binding.dummyButton.setOnClickListener { - stop() - } - - binding.fullscreenContent.setOnClickListener { - stop() - } - } - - override fun onPostCreate(savedInstanceState: Bundle?) { - super.onPostCreate(savedInstanceState) - - // Trigger the initial hide() shortly after the activity has been - // created, to briefly hint to the user that UI controls - // are available. - delayedHide(100) - } - - private fun toggle() { - if (isFullscreen) { - hide() - } else { - show() - } - } - - private fun hide() { - // Hide UI first - supportActionBar?.hide() - fullscreenContentControls.visibility = View.GONE - isFullscreen = false - - // Schedule a runnable to remove the status and navigation bar after a delay - hideHandler.removeCallbacks(showPart2Runnable) - hideHandler.postDelayed(hidePart2Runnable, UI_ANIMATION_DELAY.toLong()) - } - - private fun show() { - // Show the system bar - if (Build.VERSION.SDK_INT >= 30) { - fullscreenContent.windowInsetsController?.show(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars()) - } else { - fullscreenContent.systemUiVisibility = - View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or - View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION - } - isFullscreen = true - - // Schedule a runnable to display UI elements after a delay - hideHandler.removeCallbacks(hidePart2Runnable) - hideHandler.postDelayed(showPart2Runnable, UI_ANIMATION_DELAY.toLong()) - } - - /** - * Schedules a call to hide() in [delayMillis], canceling any - * previously scheduled calls. - */ - private fun delayedHide(delayMillis: Int) { - hideHandler.removeCallbacks(hideRunnable) - hideHandler.postDelayed(hideRunnable, delayMillis.toLong()) - } - - companion object { - /** - * Whether or not the system UI should be auto-hidden after - * [AUTO_HIDE_DELAY_MILLIS] milliseconds. - */ - private const val AUTO_HIDE = true - - /** - * If [AUTO_HIDE] is set, the number of milliseconds to wait after - * user interaction before hiding the system UI. - */ - private const val AUTO_HIDE_DELAY_MILLIS = 3000 - - /** - * Some older devices needs a small delay between UI widget updates - * and a change of the status and navigation bar. - */ - private const val UI_ANIMATION_DELAY = 300 - } -} \ No newline at end of file diff --git a/android/app/src/main/java/com/massive/TimerDone.kt b/android/app/src/main/java/com/massive/TimerDone.kt new file mode 100644 index 0000000..cf6870f --- /dev/null +++ b/android/app/src/main/java/com/massive/TimerDone.kt @@ -0,0 +1,23 @@ +package com.massive + +import android.content.Intent +import androidx.appcompat.app.AppCompatActivity +import android.os.Bundle +import android.util.Log +import android.view.View + +class TimerDone : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_timer_done) + } + + fun stop(view: View) { + Log.d("TimerDone", "Stopping...") + applicationContext.stopService(Intent(applicationContext, TimerService::class.java)) + applicationContext.stopService(Intent(applicationContext, AlarmService::class.java)) + val intent = Intent(applicationContext, MainActivity::class.java) + intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK + applicationContext.startActivity(intent) + } +} \ No newline at end of file diff --git a/android/app/src/main/java/com/massive/TimerService.kt b/android/app/src/main/java/com/massive/TimerService.kt index 0ce3397..99fe279 100644 --- a/android/app/src/main/java/com/massive/TimerService.kt +++ b/android/app/src/main/java/com/massive/TimerService.kt @@ -73,7 +73,7 @@ class TimerService : Service() { finishIntent, PendingIntent.FLAG_IMMUTABLE ) - val fullIntent = Intent(applicationContext, Fullscreen::class.java) + val fullIntent = Intent(applicationContext, TimerDone::class.java) val fullPending = PendingIntent.getActivity( applicationContext, diff --git a/android/app/src/main/res/layout/activity_fullscreen.xml b/android/app/src/main/res/layout/activity_fullscreen.xml deleted file mode 100644 index f3f0f64..0000000 --- a/android/app/src/main/res/layout/activity_fullscreen.xml +++ /dev/null @@ -1,51 +0,0 @@ - - - - - - - - - - - -