mirror of
https://github.com/MatomoCamp/matomocamp-companion-android.git
synced 2024-09-19 16:13:46 +02:00
Set alarms at the exact time instead of having them batched and delayed on Kitkat and above.
This commit is contained in:
parent
29d923a1cd
commit
54f6a6945e
1 changed files with 17 additions and 6 deletions
|
@ -10,6 +10,7 @@ import android.content.SharedPreferences;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.graphics.Typeface;
|
import android.graphics.Typeface;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.os.Build;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.support.v4.app.NotificationCompat;
|
import android.support.v4.app.NotificationCompat;
|
||||||
import android.support.v4.app.NotificationManagerCompat;
|
import android.support.v4.app.NotificationManagerCompat;
|
||||||
|
@ -17,6 +18,7 @@ import android.support.v4.app.TaskStackBuilder;
|
||||||
import android.text.Spannable;
|
import android.text.Spannable;
|
||||||
import android.text.SpannableString;
|
import android.text.SpannableString;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
import android.text.format.DateUtils;
|
||||||
import android.text.style.StyleSpan;
|
import android.text.style.StyleSpan;
|
||||||
|
|
||||||
import be.digitalia.fosdem.R;
|
import be.digitalia.fosdem.R;
|
||||||
|
@ -57,8 +59,9 @@ public class AlarmIntentService extends IntentService {
|
||||||
}
|
}
|
||||||
|
|
||||||
private PendingIntent getAlarmPendingIntent(long eventId) {
|
private PendingIntent getAlarmPendingIntent(long eventId) {
|
||||||
Intent intent = new Intent(this, AlarmReceiver.class).setAction(AlarmReceiver.ACTION_NOTIFY_EVENT).setData(
|
Intent intent = new Intent(this, AlarmReceiver.class)
|
||||||
Uri.parse(String.valueOf(eventId)));
|
.setAction(AlarmReceiver.ACTION_NOTIFY_EVENT)
|
||||||
|
.setData(Uri.parse(String.valueOf(eventId)));
|
||||||
return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
|
return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,7 +84,7 @@ public class AlarmIntentService extends IntentService {
|
||||||
// Cancel pending alarms that where scheduled between now and delay, if any
|
// Cancel pending alarms that where scheduled between now and delay, if any
|
||||||
alarmManager.cancel(pi);
|
alarmManager.cancel(pi);
|
||||||
} else {
|
} else {
|
||||||
alarmManager.set(AlarmManager.RTC_WAKEUP, notificationTime, pi);
|
setExactAlarm(alarmManager, AlarmManager.RTC_WAKEUP, notificationTime, pi);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -117,9 +120,9 @@ public class AlarmIntentService extends IntentService {
|
||||||
long startTime = intent.getLongExtra(DatabaseManager.EXTRA_EVENT_START_TIME, -1L);
|
long startTime = intent.getLongExtra(DatabaseManager.EXTRA_EVENT_START_TIME, -1L);
|
||||||
// Only schedule future events. If they start before the delay, the alarm will go off immediately
|
// Only schedule future events. If they start before the delay, the alarm will go off immediately
|
||||||
if ((startTime == -1L) || (startTime < System.currentTimeMillis())) {
|
if ((startTime == -1L) || (startTime < System.currentTimeMillis())) {
|
||||||
return;
|
break;
|
||||||
}
|
}
|
||||||
alarmManager.set(AlarmManager.RTC_WAKEUP, startTime - delay, getAlarmPendingIntent(eventId));
|
setExactAlarm(alarmManager, AlarmManager.RTC_WAKEUP, startTime - delay, getAlarmPendingIntent(eventId));
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -229,10 +232,18 @@ public class AlarmIntentService extends IntentService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void setExactAlarm(AlarmManager manager, int type, long triggerAtMillis, PendingIntent operation) {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||||
|
manager.setExact(type, triggerAtMillis, operation);
|
||||||
|
} else {
|
||||||
|
manager.set(type, triggerAtMillis, operation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private long getDelay() {
|
private long getDelay() {
|
||||||
String delayString = PreferenceManager.getDefaultSharedPreferences(this).getString(
|
String delayString = PreferenceManager.getDefaultSharedPreferences(this).getString(
|
||||||
SettingsFragment.KEY_PREF_NOTIFICATIONS_DELAY, "0");
|
SettingsFragment.KEY_PREF_NOTIFICATIONS_DELAY, "0");
|
||||||
// Convert from minutes to milliseconds
|
// Convert from minutes to milliseconds
|
||||||
return Long.parseLong(delayString) * 1000L * 60L;
|
return Long.parseLong(delayString) * DateUtils.MINUTE_IN_MILLIS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue