Skip to content

Commit

Permalink
New process for scheduling alarms
Browse files Browse the repository at this point in the history
  • Loading branch information
sazid committed Sep 11, 2018
1 parent 9b32d20 commit 93b7818
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 48 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
42 changes: 28 additions & 14 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,39 @@
android:name="io.fabric.ApiKey"
android:value="321384046aa77d30c21a4829f883d9aec4b23d1d" />

<!--<service-->
<!--android:name=".MyFirebaseMessagingService">-->
<!--<intent-filter>-->
<!--<action android:name="com.google.firebase.MESSAGING_EVENT"/>-->
<!--</intent-filter>-->
<!--</service>-->
<!-- <service -->
<!-- android:name=".MyFirebaseMessagingService"> -->
<!-- <intent-filter> -->
<!-- <action android:name="com.google.firebase.MESSAGING_EVENT"/> -->
<!-- </intent-filter> -->
<!-- </service> -->

<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
See README(https://goo.gl/l4GJaQ) for more. -->
<!--<meta-data-->
<!--android:name="com.google.firebase.messaging.default_notification_icon"-->
<!--android:resource="@drawable/ic_stat_ic_notification" />-->

<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
notification message. See README(https://goo.gl/6BKBk7) for more. -->
<!--
Set custom default icon. This is used when no icon is set for incoming notification messages.
See README(https://goo.gl/l4GJaQ) for more.
-->
<!-- <meta-data -->
<!-- android:name="com.google.firebase.messaging.default_notification_icon" -->
<!-- android:resource="@drawable/ic_stat_ic_notification" /> -->


<!--
Set color used with incoming notification messages. This is used when no color is set for the incoming
notification message. See README(https://goo.gl/6BKBk7) for more.
-->
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />

<receiver
android:name=".NoticeAlarmReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.mohammedsazid.intent.action.CHECK_NOTICE" />
</intent-filter>
</receiver>
</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.mohammedsazid.android.aiub;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.SystemClock;
import android.util.Log;

import java.util.concurrent.TimeUnit;

import static android.content.Context.ALARM_SERVICE;

public class NoticeAlarmReceiver extends BroadcastReceiver {

// reschedule every one hour
private static final long REPEAT_INTERVAL = 1;
public static final String CHECK_NOTICE_ACTION = "com.mohammedsazid.intent.action.CHECK_NOTICE";

private static PendingIntent getPendingIntent(Context context) {
Intent intent = new Intent();
intent.setClass(context, NoticeAlarmReceiver.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(CHECK_NOTICE_ACTION);

return PendingIntent.getBroadcast(context, 0, intent, 0);
}

public static void scheduleNewCheck(Context context) {
long deferTime = TimeUnit.HOURS.toMillis(REPEAT_INTERVAL);
long when = SystemClock.elapsedRealtime() + deferTime;
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);

PendingIntent pi = getPendingIntent(context);

if (alarmManager != null) {
// cancel any previous alarm
alarmManager.cancel(pi);

int SDK_INT = Build.VERSION.SDK_INT;
if (SDK_INT < Build.VERSION_CODES.KITKAT)
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, pi);
else if (SDK_INT < Build.VERSION_CODES.M)
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, pi);
else
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, pi);
}
}

@Override
public void onReceive(Context context, Intent intent) {
NoticeCheckJobIntentService.startActionCheckNotice(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@
import java.util.concurrent.TimeUnit;

public class NoticeCheckJobIntentService extends JobIntentService {
private static final long REPEAT_INTERVAL = TimeUnit.HOURS.toMinutes(1);
private static final String PREF_NOTICES_KEY = "PREF_NOTICES_KEY";


public static void startActionCheckNotice(Context context) {
Intent intent = new Intent(context, NoticeCheckJobIntentService.class);
enqueueWork(context, NoticeCheckJobIntentService.class, 1, intent);
ContextCompat.startForegroundService(context, intent);
// ContextCompat.startForegroundService(context, intent);
}

@NonNull
Expand Down Expand Up @@ -82,7 +81,9 @@ public void onCreate() {

@Override
protected void onHandleWork(@NonNull Intent intent) {
Log.d("SERVICE", "Starting work");
handleActionCheckNotice();
Log.d("SERVICE", "Work done");
}

private void parseNoticeHTML(String url) {
Expand Down Expand Up @@ -141,40 +142,9 @@ private void parseNoticeHTML(String url) {
}
}

private void scheduleNewCheck(long minutes) {
long deferTime = TimeUnit.MINUTES.toMillis(minutes);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

Intent intent = new Intent();
intent.setClass(this, NoticeCheckJobIntentService.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent pi = PendingIntent.getService(this, 0, intent, 0);

// cancel any previous alarm
if (alarmManager != null) {
alarmManager.cancel(pi);
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (alarmManager != null) {
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + deferTime,
pi);
}
} else {
if (alarmManager != null) {
alarmManager.set(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + deferTime,
pi);
}
}
}

private void handleActionCheckNotice() {
parseNoticeHTML("http://www.aiub.edu/category/notices");
scheduleNewCheck(REPEAT_INTERVAL);
NoticeAlarmReceiver.scheduleNewCheck(this);
Log.d("SERVICE", "Check complete");

try {
Expand All @@ -186,5 +156,7 @@ private void handleActionCheckNotice() {
e.printStackTrace();
Crashlytics.logException(e);
}

stopSelf();
}
}

0 comments on commit 93b7818

Please sign in to comment.