• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.wallpaper.module;
17 
18 import android.app.AlarmManager;
19 import android.app.PendingIntent;
20 import android.content.Context;
21 import android.content.Intent;
22 
23 import java.util.Calendar;
24 import java.util.concurrent.TimeUnit;
25 
26 /**
27  * Schedules and cancels alarms on Android's {@link AlarmManager} to occur every 24 hours to perform
28  * daily logging.
29  */
30 public class DailyLoggingAlarmScheduler {
31 
32     private static final int UNUSED_REQUEST_CODE = 0;
33     private static final int UNUSED_REQUEST_FLAGS = 0;
34 
35     /**
36      * Sets a new alarm to fire approximately 24 hours after the last one, or immediately if it has
37      * not fired in over 24 hours.
38      */
setAlarm(Context appContext)39     public static void setAlarm(Context appContext) {
40         Injector injector = InjectorProvider.getInjector();
41         AlarmManagerWrapper alarmManagerWrapper = injector.getAlarmManagerWrapper(appContext);
42         WallpaperPreferences preferences = injector.getPreferences(appContext);
43 
44         long lastTimestamp = preferences.getLastDailyLogTimestamp();
45 
46         Calendar oneDayAgo = Calendar.getInstance();
47         long currentTimeMillis = System.currentTimeMillis();
48         oneDayAgo.setTimeInMillis(currentTimeMillis);
49         oneDayAgo.add(Calendar.DAY_OF_YEAR, -1);
50 
51         long triggerAtMillis;
52         if (lastTimestamp == -1 || lastTimestamp < oneDayAgo.getTimeInMillis()) {
53             // Schedule for right now (a minute from now, to ensure the trigger time is in the future) and
54             // then every ~24 hours later.
55             Calendar oneMinuteFromNow = Calendar.getInstance();
56             oneMinuteFromNow.setTimeInMillis(currentTimeMillis);
57             oneMinuteFromNow.add(Calendar.MINUTE, 1);
58             triggerAtMillis = oneMinuteFromNow.getTimeInMillis();
59         } else {
60             // Schedule for 24 hours after the last daily log, and every ~24 hours after that.
61             Calendar oneDayFromNow = Calendar.getInstance();
62             oneDayFromNow.setTimeInMillis(lastTimestamp);
63             oneDayFromNow.add(Calendar.DAY_OF_YEAR, 1);
64             triggerAtMillis = oneDayFromNow.getTimeInMillis();
65         }
66 
67         // Cancel any existing daily logging alarms. Then do the actual scheduling of the new alarm.
68         PendingIntent pendingIntent = createAlarmReceiverPendingIntent(appContext);
69         alarmManagerWrapper.cancel(pendingIntent);
70 
71         pendingIntent = createAlarmReceiverPendingIntent(appContext);
72         alarmManagerWrapper.setInexactRepeating(
73                 AlarmManager.RTC /* type */,
74                 triggerAtMillis /* triggerAtMillis */,
75                 TimeUnit.MILLISECONDS.convert(24, TimeUnit.HOURS) /* intervalMillis */,
76                 pendingIntent /* operation */);
77     }
78 
createAlarmReceiverPendingIntent(Context appContext)79     private static PendingIntent createAlarmReceiverPendingIntent(Context appContext) {
80         Intent intent = new Intent(appContext, DailyLoggingAlarmReceiver.class);
81         return PendingIntent.getBroadcast(
82                 appContext, UNUSED_REQUEST_CODE, intent, UNUSED_REQUEST_FLAGS);
83     }
84 
85 }
86