Validating live updates alarms on start.

This commit is contained in:
GaidamakUA 2016-01-20 15:45:59 +02:00
parent 4738ec6e9e
commit 8841ad343b
3 changed files with 101 additions and 61 deletions

View file

@ -1,6 +1,8 @@
package net.osmand.plus;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
@ -18,10 +20,14 @@ import net.osmand.map.WorldRegion;
import net.osmand.osm.AbstractPoiType;
import net.osmand.osm.MapPoiTypes;
import net.osmand.plus.activities.DayNightHelper;
import net.osmand.plus.activities.LocalIndexHelper;
import net.osmand.plus.activities.LocalIndexInfo;
import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.download.ui.AbstractLoadLocalIndexTask;
import net.osmand.plus.helpers.AvoidSpecificRoads;
import net.osmand.plus.helpers.WaypointHelper;
import net.osmand.plus.liveupdates.LiveUpdatesHelper;
import net.osmand.plus.monitoring.LiveMonitoringHelper;
import net.osmand.plus.poi.PoiFiltersHelper;
import net.osmand.plus.render.MapRenderRepositories;
@ -51,6 +57,14 @@ import java.util.Random;
import btools.routingapp.BRouterServiceConnection;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.getPendingIntent;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceLastCheck;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceLiveUpdatesOn;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceTimeOfDayToUpdate;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceUpdateFrequency;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.runLiveUpdate;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.setAlarmForPendingIntent;
/**
*/
public class AppInitializer implements IProgress {
@ -297,7 +311,6 @@ public class AppInitializer implements IProgress {
}
public void onCreateApplication() {
// always update application mode to default
OsmandSettings osmandSettings = app.getSettings();
@ -360,7 +373,6 @@ public class AppInitializer implements IProgress {
}
private <T> T startupInit(T object, Class<T> class1) {
long t = System.currentTimeMillis();
if(t - startTime > 7) {
@ -371,7 +383,6 @@ public class AppInitializer implements IProgress {
}
public net.osmand.router.RoutingConfiguration.Builder getLazyDefaultRoutingConfig() {
long tm = System.currentTimeMillis();
try {
@ -470,6 +481,8 @@ public class AppInitializer implements IProgress {
// restore backuped favorites to normal file
restoreBackupForFavoritesFiles();
notifyEvent(InitEvents.RESTORE_BACKUPS);
checkLiveUpdatesAlerts();
LocalIndexHelper helper = new LocalIndexHelper(app);
} catch (RuntimeException e) {
e.printStackTrace();
warnings.add(e.getMessage());
@ -482,9 +495,37 @@ public class AppInitializer implements IProgress {
}
}
private void checkLiveUpdatesAlerts() {
OsmandSettings settings = app.getSettings();
if (!settings.IS_LIVE_UPDATES_ON.get()) {
return;
}
LocalIndexHelper helper = new LocalIndexHelper(app);
List<LocalIndexInfo> fullMaps = helper.getLocalFullMaps(new AbstractLoadLocalIndexTask() {
@Override
public void loadFile(LocalIndexInfo... loaded) {
}
});
AlarmManager alarmMgr = (AlarmManager) app.getSystemService(Context.ALARM_SERVICE);
for (LocalIndexInfo fullMap : fullMaps) {
if (!preferenceLiveUpdatesOn(fullMap, settings).get()) {
continue;
}
int updateFrequencyOrd = preferenceUpdateFrequency(fullMap, settings).get();
LiveUpdatesHelper.UpdateFrequency updateFrequency =
LiveUpdatesHelper.UpdateFrequency.values()[updateFrequencyOrd];
long lastCheck = preferenceLastCheck(fullMap, settings).get();
if (System.currentTimeMillis() - lastCheck > updateFrequency.getTime() * 2) {
runLiveUpdate(app, fullMap, false);
PendingIntent alarmIntent = getPendingIntent(app, fullMap);
int timeOfDayOrd = preferenceTimeOfDayToUpdate(fullMap, settings).get();
LiveUpdatesHelper.TimeOfDay timeOfDayToUpdate =
LiveUpdatesHelper.TimeOfDay.values()[timeOfDayOrd];
setAlarmForPendingIntent(alarmIntent, alarmMgr, updateFrequency, timeOfDayToUpdate);
}
}
}
private void restoreBackupForFavoritesFiles() {
final File appDir = app.getAppPath(null);
@ -686,5 +727,4 @@ public class AppInitializer implements IProgress {
}
}

View file

@ -90,26 +90,20 @@ public class LiveUpdatesHelper {
public static void setAlarmForPendingIntent(PendingIntent alarmIntent, AlarmManager alarmMgr, UpdateFrequency updateFrequency, TimeOfDay timeOfDayToUpdate) {
long timeOfFirstUpdate;
long updateInterval;
switch (updateFrequency) {
case HOURLY:
timeOfFirstUpdate = System.currentTimeMillis() + SHIFT;
updateInterval = AlarmManager.INTERVAL_HOUR;
break;
case DAILY:
timeOfFirstUpdate = getNextUpdateTime(timeOfDayToUpdate);
updateInterval = AlarmManager.INTERVAL_DAY;
break;
case WEEKLY:
timeOfFirstUpdate = getNextUpdateTime(timeOfDayToUpdate);
updateInterval = AlarmManager.INTERVAL_DAY * 7;
break;
default:
throw new IllegalStateException("Unexpected update frequency:"
+ updateFrequency);
}
alarmMgr.setInexactRepeating(AlarmManager.RTC,
timeOfFirstUpdate, updateInterval, alarmIntent);
timeOfFirstUpdate, updateFrequency.getTime(), alarmIntent);
}
private static long getNextUpdateTime(TimeOfDay timeOfDayToUpdate) {
@ -145,17 +139,27 @@ public class LiveUpdatesHelper {
}
public static enum UpdateFrequency {
HOURLY(R.string.hourly),
DAILY(R.string.daily),
WEEKLY(R.string.weekly);
HOURLY(R.string.hourly, AlarmManager.INTERVAL_HOUR),
DAILY(R.string.daily, AlarmManager.INTERVAL_DAY),
WEEKLY(R.string.weekly, AlarmManager.INTERVAL_DAY * 7);
private final int localizedId;
private final long time;
UpdateFrequency(int localizedId) {
UpdateFrequency(int localizedId, long time) {
this.localizedId = localizedId;
this.time = time;
}
public int getLocalizedId() {
return localizedId;
}
public long getTime() {
return time;
}
}
public static void runLiveUpdate(Context context, final LocalIndexInfo info, boolean forceUpdate) {
final String fnExt = Algorithms.getFileNameWithoutExtension(new File(info.getFileName()));
new PerformLiveUpdateAsyncTask(context, info, forceUpdate).execute(new String[]{fnExt});
}
}

View file

@ -43,6 +43,7 @@ import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceForLocalIn
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceLastCheck;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceTimeOfDayToUpdate;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceUpdateFrequency;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.runLiveUpdate;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.setAlarmForPendingIntent;
public class LiveUpdatesSettingsDialogFragment extends DialogFragment {
@ -158,7 +159,7 @@ public class LiveUpdatesSettingsDialogFragment extends DialogFragment {
timeOfDayPreference.set(timeOfDayInt);
if (liveUpdatesSwitch.isChecked() && getSettings().IS_LIVE_UPDATES_ON.get()) {
runLiveUpdate(localIndexInfo, false);
runLiveUpdate(getActivity(), localIndexInfo, false);
UpdateFrequency updateFrequency = UpdateFrequency.values()[updateFrequencyInt];
TimeOfDay timeOfDayToUpdate = TimeOfDay.values()[timeOfDayInt];
setAlarmForPendingIntent(alarmIntent, alarmMgr, updateFrequency, timeOfDayToUpdate);
@ -172,18 +173,13 @@ public class LiveUpdatesSettingsDialogFragment extends DialogFragment {
.setNeutralButton(R.string.update_now, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
runLiveUpdate(localIndexInfo, true);
runLiveUpdate(getActivity(), localIndexInfo, true);
sizeTextView.setText(getUpdatesSize(fileNameWithoutExtension, changesManager));
}
});
return builder.create();
}
void runLiveUpdate(final LocalIndexInfo info, boolean forceUpdate) {
final String fnExt = Algorithms.getFileNameWithoutExtension(new File(info.getFileName()));
new PerformLiveUpdateAsyncTask(getActivity(), info, forceUpdate).execute(new String[]{fnExt});
}
private static String getUpdatesSize(String fileNameWithoutExtension,
IncrementalChangesManager changesManager) {
String size;