properly handle alarmclock scheduling

This commit is contained in:
tibbi 2018-03-06 16:22:15 +01:00
parent 0cda6a133e
commit 91a1f059d3
6 changed files with 49 additions and 1 deletions

View File

@ -41,7 +41,7 @@ android {
}
dependencies {
implementation 'com.simplemobiletools:commons:3.15.1'
implementation 'com.simplemobiletools:commons:3.15.3'
implementation 'com.facebook.stetho:stetho:1.5.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
}

View File

@ -3,6 +3,8 @@
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.simplemobiletools.clock">
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
android:name=".App"
android:allowBackup="true"
@ -47,5 +49,7 @@
android:name="com.simplemobiletools.commons.activities.FAQActivity"
android:label="@string/frequently_asked_questions"
android:parentActivityName="com.simplemobiletools.commons.activities.AboutActivity"/>
<receiver android:name=".receivers.AlarmReceiver"/>
</application>
</manifest>

View File

@ -1,5 +1,10 @@
package com.simplemobiletools.clock.fragments
import android.annotation.SuppressLint
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
@ -12,10 +17,13 @@ import com.simplemobiletools.clock.adapters.AlarmsAdapter
import com.simplemobiletools.clock.dialogs.EditAlarmDialog
import com.simplemobiletools.clock.extensions.createNewAlarm
import com.simplemobiletools.clock.extensions.dbHelper
import com.simplemobiletools.clock.helpers.ALARM_ID
import com.simplemobiletools.clock.interfaces.ToggleAlarmInterface
import com.simplemobiletools.clock.models.Alarm
import com.simplemobiletools.clock.receivers.AlarmReceiver
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.commons.helpers.isLollipopPlus
import kotlinx.android.synthetic.main.fragment_alarm.view.*
import java.util.*
import kotlin.math.pow
@ -90,6 +98,7 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
if (isCorrectDay && (alarm.timeInMinutes > currentTimeInMinutes || i > 0)) {
val triggerInMinutes = alarm.timeInMinutes - currentTimeInMinutes + (i * DAY_MINUTES)
showRemainingTimeMessage(triggerInMinutes)
setupAlarmClock(alarm, triggerInMinutes * 60 - calendar.get(Calendar.SECOND))
break
} else {
calendar.add(Calendar.DAY_OF_MONTH, 1)
@ -120,4 +129,22 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
val fullString = String.format(activity!!.getString(R.string.alarm_goes_off_in), timesString.toString().trim().trimEnd(','))
activity!!.toast(fullString, Toast.LENGTH_LONG)
}
@SuppressLint("NewApi")
private fun setupAlarmClock(alarm: Alarm, triggerInSeconds: Int) {
val alarmManager = context!!.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val targetMS = System.currentTimeMillis() + triggerInSeconds * 1000
val pendingIntent = getPendingIntent(alarm)
if (isLollipopPlus()) {
val info = AlarmManager.AlarmClockInfo(targetMS, pendingIntent)
alarmManager.setAlarmClock(info, pendingIntent)
}
}
private fun getPendingIntent(alarm: Alarm): PendingIntent {
val intent = Intent(context, AlarmReceiver::class.java)
intent.putExtra(ALARM_ID, alarm.id)
return PendingIntent.getBroadcast(context, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
}

View File

@ -10,6 +10,7 @@ const val EDITED_TIME_ZONE_TITLES = "edited_time_zone_titles"
const val TABS_COUNT = 3
const val EDITED_TIME_ZONE_SEPARATOR = ":"
const val ALARM_ID = "alarm_id"
fun getDefaultTimeZoneTitle(id: Int) = getAllTimeZones().firstOrNull { it.id == id }?.title ?: ""

View File

@ -76,6 +76,8 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
return mDb.update(ALARMS_TABLE_NAME, values, selection, selectionArgs) == 1
}
fun getAlarmWithId(id: Int) = getAlarms().firstOrNull { it.id == id }
private fun fillAlarmContentValues(alarm: Alarm): ContentValues {
return ContentValues().apply {
put(COL_TIME_IN_MINUTES, alarm.timeInMinutes)

View File

@ -0,0 +1,14 @@
package com.simplemobiletools.clock.receivers
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.simplemobiletools.clock.extensions.dbHelper
import com.simplemobiletools.clock.helpers.ALARM_ID
class AlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val id = intent.getIntExtra(ALARM_ID, -1)
val alarm = context.dbHelper.getAlarmWithId(id) ?: return
}
}