Added app shortcut to start the stopwatch

This commit is contained in:
MohitMali 2022-11-16 19:07:48 +05:30 committed by MohitMaliDeveloper
parent 7e4301b81d
commit 9dc2c48df6
9 changed files with 73 additions and 2 deletions

View file

@ -37,6 +37,7 @@
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.TOGGLE_STOPWATCH" />
<action android:name="android.intent.action.SHOW_ALARMS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

View file

@ -3,6 +3,11 @@ package com.simplemobiletools.clock
import android.app.Application
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.graphics.drawable.Icon
import android.os.Build
import android.os.CountDownTimer
import android.os.Handler
import android.os.Looper
@ -11,8 +16,9 @@ import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.ProcessLifecycleOwner
import com.facebook.stetho.Stetho
import com.simplemobiletools.clock.activities.SplashActivity
import com.simplemobiletools.clock.extensions.*
import com.simplemobiletools.clock.helpers.Stopwatch
import com.simplemobiletools.clock.helpers.*
import com.simplemobiletools.clock.helpers.Stopwatch.State
import com.simplemobiletools.clock.models.TimerEvent
import com.simplemobiletools.clock.models.TimerState
@ -40,6 +46,23 @@ class App : Application(), LifecycleObserver {
}
checkUseEnglish()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
val shortcutManager = getSystemService(ShortcutManager::class.java)
val intent = Intent(this, SplashActivity::class.java).apply {
putExtra(OPEN_TAB, TAB_STOPWATCH)
putExtra(TOGGLE_STOPWATCH, true)
action = "android.intent.action.TOGGLE_STOPWATCH"
}
val shortcut = ShortcutInfo.Builder(this, "id1")
.setShortLabel("Stopwatch")
.setLongLabel("Start Stopwatch")
.setIcon(Icon.createWithResource(this, R.drawable.ic_stopwatch_vector))
.setIntent(
intent
)
.build()
shortcutManager.dynamicShortcuts = listOf(shortcut)
}
}
override fun onTerminate() {

View file

@ -106,6 +106,11 @@ class MainActivity : SimpleActivity() {
val timerId = intent.getIntExtra(TIMER_ID, INVALID_TIMER_ID)
(view_pager.adapter as ViewPagerAdapter).updateTimerPosition(timerId)
}
if (tabToOpen == TAB_STOPWATCH) {
if (intent.getBooleanExtra(TOGGLE_STOPWATCH, false)) {
(view_pager.adapter as ViewPagerAdapter).startStopWatch()
}
}
}
super.onNewIntent(intent)
}
@ -152,7 +157,9 @@ class MainActivity : SimpleActivity() {
val timerId = intent.getIntExtra(TIMER_ID, INVALID_TIMER_ID)
viewPagerAdapter.updateTimerPosition(timerId)
}
if (tabToOpen == TAB_STOPWATCH) {
config.toggleStopWatch = intent.getBooleanExtra(TOGGLE_STOPWATCH, false)
}
view_pager.offscreenPageLimit = TABS_COUNT - 1
view_pager.currentItem = tabToOpen
}

View file

@ -13,6 +13,13 @@ class SplashActivity : BaseSplashActivity() {
startActivity(this)
}
}
intent?.action == "android.intent.action.TOGGLE_STOPWATCH" -> {
Intent(this, MainActivity::class.java).apply {
putExtra(OPEN_TAB, TAB_STOPWATCH)
putExtra(TOGGLE_STOPWATCH, intent.getBooleanExtra(TOGGLE_STOPWATCH, false))
startActivity(this)
}
}
intent.extras?.containsKey(OPEN_TAB) == true -> {
Intent(this, MainActivity::class.java).apply {
putExtra(OPEN_TAB, intent.getIntExtra(OPEN_TAB, TAB_CLOCK))

View file

@ -12,6 +12,7 @@ import com.simplemobiletools.clock.helpers.TABS_COUNT
import com.simplemobiletools.clock.helpers.TAB_ALARM
import com.simplemobiletools.clock.helpers.TAB_CLOCK
import com.simplemobiletools.clock.helpers.TAB_TIMER
import com.simplemobiletools.clock.helpers.TAB_STOPWATCH
import com.simplemobiletools.commons.models.AlarmSound
class ViewPagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {
@ -57,4 +58,8 @@ class ViewPagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {
fun updateTimerPosition(timerId: Int) {
(fragments[TAB_TIMER] as? TimerFragment)?.updatePosition(timerId)
}
fun startStopWatch() {
(fragments[TAB_STOPWATCH] as? StopwatchFragment)?.startStopWatch()
}
}

View file

@ -91,6 +91,11 @@ class StopwatchFragment : Fragment() {
if (Stopwatch.laps.isNotEmpty()) {
updateSorting(Lap.sorting)
}
if (requireContext().config.toggleStopWatch) {
requireContext().config.toggleStopWatch = false
startStopWatch()
}
}
override fun onPause() {
@ -185,6 +190,12 @@ class StopwatchFragment : Fragment() {
}
}
fun startStopWatch() {
if (Stopwatch.state == Stopwatch.State.STOPPED) {
togglePlayPause()
}
}
private fun updateLaps() {
stopwatchAdapter.updateItems(Stopwatch.laps)
}

View file

@ -49,6 +49,10 @@ class Config(context: Context) : BaseConfig(context) {
get() = prefs.getString(TIMER_LABEL, null)
set(label) = prefs.edit().putString(TIMER_LABEL, label).apply()
var toggleStopWatch: Boolean
get() = prefs.getBoolean(TOGGLE_STOPWATCH, false)
set(label) = prefs.edit().putBoolean(TOGGLE_STOPWATCH, label).apply()
var alarmSort: Int
get() = prefs.getInt(ALARMS_SORT_BY, SORT_BY_CREATION_ORDER)
set(alarmSort) = prefs.edit().putInt(ALARMS_SORT_BY, alarmSort).apply()

View file

@ -13,6 +13,7 @@ const val TIMER_SOUND_URI = "timer_sound_uri"
const val TIMER_SOUND_TITLE = "timer_sound_title"
const val TIMER_CHANNEL_ID = "timer_channel_id"
const val TIMER_LABEL = "timer_label"
const val TOGGLE_STOPWATCH = "toggle_stopwatch"
const val TIMER_MAX_REMINDER_SECS = "timer_max_reminder_secs"
const val ALARM_MAX_REMINDER_SECS = "alarm_max_reminder_secs"
const val ALARM_LAST_CONFIG = "alarm_last_config"

View file

@ -0,0 +1,12 @@
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@drawable/ic_stopwatch_vector"
android:shortcutId="compose"
android:shortcutShortLabel="@string/startup">
<intent
android:action="FLAG_ACTIVITY_NEW_TASK"
android:targetClass="com.simplemobiletools.clock.App"
android:targetPackage="com.simplemobiletools.clock" />
</shortcut>
</shortcuts>