Live updates supposedly done. Needs testing.
This commit is contained in:
parent
9142c1919f
commit
701a5b5d94
4 changed files with 95 additions and 17 deletions
|
@ -53,7 +53,7 @@
|
|||
android:text="@string/only_download_over_wifi"/>
|
||||
|
||||
<android.support.v7.widget.SwitchCompat
|
||||
android:id="@+id/downloadOnlyOverWiFi"
|
||||
android:id="@+id/downloadOverWiFiSwitch"
|
||||
android:layout_gravity="right"/>
|
||||
|
||||
<TextView
|
||||
|
|
|
@ -1858,7 +1858,9 @@ public class OsmandSettings {
|
|||
|
||||
// Live Updates
|
||||
public final OsmandPreference<Boolean> IS_LIVE_UPDATES_ON =
|
||||
new BooleanPreference("IS_LIVE_UPDATES_ON", false).makeGlobal();
|
||||
new BooleanPreference("is_live_updates_on", false).makeGlobal();
|
||||
public final OsmandPreference<Integer> LIVE_UPDATES_RETRIES =
|
||||
new IntPreference("live_updates_retryes", 2).makeGlobal();
|
||||
|
||||
// UI boxes
|
||||
public final CommonPreference<Boolean> TRANSPARENT_MAP_THEME =
|
||||
|
|
|
@ -1,41 +1,58 @@
|
|||
package net.osmand.plus.liveupdates;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.os.AsyncTask;
|
||||
import android.widget.Toast;
|
||||
|
||||
import net.osmand.PlatformUtil;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.OsmandSettings;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.activities.LocalIndexInfo;
|
||||
import net.osmand.plus.download.DownloadActivityType;
|
||||
import net.osmand.plus.download.DownloadValidationManager;
|
||||
import net.osmand.plus.download.IndexItem;
|
||||
import net.osmand.plus.resources.IncrementalChangesManager;
|
||||
import net.osmand.util.Algorithms;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
public class LiveUpdatesAlarmReceiver extends BroadcastReceiver {
|
||||
private static final Log LOG = PlatformUtil.getLog(LiveUpdatesAlarmReceiver.class);
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String localIndexInfo = intent.getAction();
|
||||
new PerformLiveUpdateAsyncTask(context).execute(localIndexInfo);
|
||||
String fileName = intent.getAction();
|
||||
LocalIndexInfo localIndexInfo =
|
||||
intent.getParcelableExtra(LiveUpdatesSettingsDialogFragment.LOCAL_INDEX_INFO);
|
||||
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
||||
|
||||
final OsmandApplication application = (OsmandApplication) context.getApplicationContext();
|
||||
final OsmandSettings settings = application.getSettings();
|
||||
|
||||
if (!preferenceDownloadViaWiFi(localIndexInfo, settings).get() || wifi.isWifiEnabled()) {
|
||||
new PerformLiveUpdateAsyncTask(context, localIndexInfo).execute(fileName);
|
||||
} else {
|
||||
tryRescheduleDownload(context, settings, localIndexInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public static class PerformLiveUpdateAsyncTask
|
||||
extends AsyncTask<String, Object, IncrementalChangesManager.IncrementalUpdateList> {
|
||||
private final Context context;
|
||||
private final LocalIndexInfo localIndexInfo;
|
||||
|
||||
public PerformLiveUpdateAsyncTask(Context context) {
|
||||
public PerformLiveUpdateAsyncTask(Context context, LocalIndexInfo localIndexInfo) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
protected void onPreExecute() {
|
||||
|
||||
this.localIndexInfo = localIndexInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -46,9 +63,13 @@ public class LiveUpdatesAlarmReceiver extends BroadcastReceiver {
|
|||
}
|
||||
|
||||
protected void onPostExecute(IncrementalChangesManager.IncrementalUpdateList result) {
|
||||
final OsmandApplication application = (OsmandApplication) context.getApplicationContext();
|
||||
final OsmandSettings settings = application.getSettings();
|
||||
if (result.errorMessage != null) {
|
||||
Toast.makeText(context, result.errorMessage, Toast.LENGTH_SHORT).show();
|
||||
tryRescheduleDownload(context, settings, localIndexInfo);
|
||||
} else {
|
||||
settings.LIVE_UPDATES_RETRIES.resetToDefault();
|
||||
List<IncrementalChangesManager.IncrementalUpdate> ll = result.getItemsForUpdate();
|
||||
if (ll.isEmpty()) {
|
||||
Toast.makeText(context, R.string.no_updates_available, Toast.LENGTH_SHORT).show();
|
||||
|
@ -56,11 +77,11 @@ public class LiveUpdatesAlarmReceiver extends BroadcastReceiver {
|
|||
int i = 0;
|
||||
IndexItem[] is = new IndexItem[ll.size()];
|
||||
for (IncrementalChangesManager.IncrementalUpdate iu : ll) {
|
||||
IndexItem ii = new IndexItem(iu.fileName, "Incremental update", iu.timestamp, iu.sizeText,
|
||||
iu.contentSize, iu.containerSize, DownloadActivityType.LIVE_UPDATES_FILE);
|
||||
IndexItem ii = new IndexItem(iu.fileName, "Incremental update",
|
||||
iu.timestamp, iu.sizeText, iu.contentSize,
|
||||
iu.containerSize, DownloadActivityType.LIVE_UPDATES_FILE);
|
||||
is[i++] = ii;
|
||||
}
|
||||
final OsmandApplication application = (OsmandApplication) context.getApplicationContext();
|
||||
DownloadValidationManager downloadValidationManager =
|
||||
new DownloadValidationManager(application);
|
||||
downloadValidationManager.startDownload(context, is);
|
||||
|
@ -68,4 +89,48 @@ public class LiveUpdatesAlarmReceiver extends BroadcastReceiver {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void tryRescheduleDownload(Context context, OsmandSettings settings,
|
||||
LocalIndexInfo localIndexInfo) {
|
||||
final OsmandSettings.CommonPreference<Integer> updateFrequencyPreference =
|
||||
preferenceUpdateTimes(localIndexInfo, settings);
|
||||
final Integer frequencyOrdinal = updateFrequencyPreference.get();
|
||||
if (LiveUpdatesSettingsDialogFragment.UpdateFrequencies.values()[frequencyOrdinal]
|
||||
== LiveUpdatesSettingsDialogFragment.UpdateFrequencies.HOURLY) {
|
||||
return;
|
||||
}
|
||||
final Integer retriesLeft = settings.LIVE_UPDATES_RETRIES.get();
|
||||
if (retriesLeft > 0) {
|
||||
Intent intent = new Intent(context, LiveUpdatesAlarmReceiver.class);
|
||||
final File file = new File(localIndexInfo.getFileName());
|
||||
final String fileName = Algorithms.getFileNameWithoutExtension(file);
|
||||
intent.putExtra(LiveUpdatesSettingsDialogFragment.LOCAL_INDEX_INFO, localIndexInfo);
|
||||
intent.setAction(fileName);
|
||||
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
|
||||
|
||||
long timeToRetry = System.currentTimeMillis() + AlarmManager.INTERVAL_HOUR;
|
||||
|
||||
AlarmManager alarmMgr = (AlarmManager) context
|
||||
.getSystemService(Context.ALARM_SERVICE);
|
||||
alarmMgr.set(AlarmManager.RTC, timeToRetry, alarmIntent);
|
||||
settings.LIVE_UPDATES_RETRIES.set(retriesLeft - 1);
|
||||
} else {
|
||||
settings.LIVE_UPDATES_RETRIES.resetToDefault();
|
||||
}
|
||||
}
|
||||
|
||||
private static OsmandSettings.CommonPreference<Boolean> preferenceDownloadViaWiFi(
|
||||
LocalIndexInfo item, OsmandSettings settings) {
|
||||
final String settingId = item.getFileName()
|
||||
+ LiveUpdatesSettingsDialogFragment.DOWNLOAD_VIA_WIFI_POSTFIX;
|
||||
return settings.registerBooleanPreference(settingId, false);
|
||||
}
|
||||
|
||||
private static OsmandSettings.CommonPreference<Integer> preferenceUpdateTimes(
|
||||
LocalIndexInfo item, OsmandSettings settings) {
|
||||
final String settingId = item.getFileName()
|
||||
+ LiveUpdatesSettingsDialogFragment.UPDATE_TIMES_POSTFIX;
|
||||
return settings.registerIntPreference(settingId,
|
||||
LiveUpdatesSettingsDialogFragment.UpdateFrequencies.HOURLY.ordinal());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,11 +33,12 @@ public class LiveUpdatesSettingsDialogFragment extends DialogFragment {
|
|||
private static final int UPDATE_HOURLY = 0;
|
||||
private static final int UPDATE_DAILY = 1;
|
||||
private static final int UPDATE_WEEKLY = 2;
|
||||
private static final String UPDATE_TIMES = "_update_times";
|
||||
private static final String TIME_OF_DAY_TO_UPDATE = "_time_of_day_to_update";
|
||||
public static final String UPDATE_TIMES_POSTFIX = "_update_times";
|
||||
private static final String TIME_OF_DAY_TO_UPDATE_POSTFIX = "_time_of_day_to_update";
|
||||
private static final int MORNING_UPDATE_TIME = 8;
|
||||
private static final int NIGHT_UPDATE_TIME = 21;
|
||||
private static final int SHIFT = 1000;
|
||||
public static final String DOWNLOAD_VIA_WIFI_POSTFIX = "_download_via_wifi";
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
|
@ -48,16 +49,20 @@ public class LiveUpdatesSettingsDialogFragment extends DialogFragment {
|
|||
View view = LayoutInflater.from(getActivity())
|
||||
.inflate(R.layout.dialog_live_updates_item_settings, null);
|
||||
final SwitchCompat liveUpdatesSwitch = (SwitchCompat) view.findViewById(R.id.liveUpdatesSwitch);
|
||||
final SwitchCompat downloadOverWiFiSwitch = (SwitchCompat) view.findViewById(R.id.downloadOverWiFiSwitch);
|
||||
final Spinner updateFrequencySpinner = (Spinner) view.findViewById(R.id.updateFrequencySpinner);
|
||||
final Spinner updateTimesOfDaySpinner = (Spinner) view.findViewById(R.id.updateTimesOfDaySpinner);
|
||||
|
||||
final OsmandSettings.CommonPreference<Boolean> liveUpdatePreference =
|
||||
preferenceForLocalIndex(localIndexInfo);
|
||||
final OsmandSettings.CommonPreference<Boolean> downloadViaWiFiPreference =
|
||||
preferenceDownloadViaWiFi(localIndexInfo);
|
||||
final OsmandSettings.CommonPreference<Integer> updateFrequencePreference =
|
||||
preferenceUpdateTimes(localIndexInfo);
|
||||
final OsmandSettings.CommonPreference<Integer> timeOfDayPreference =
|
||||
preferenceTimeOfDayToUpdate(localIndexInfo);
|
||||
liveUpdatesSwitch.setChecked(liveUpdatePreference.get());
|
||||
downloadOverWiFiSwitch.setChecked(downloadViaWiFiPreference.get());
|
||||
|
||||
builder.setView(view)
|
||||
.setPositiveButton(R.string.shared_string_save, new DialogInterface.OnClickListener() {
|
||||
|
@ -72,7 +77,7 @@ public class LiveUpdatesSettingsDialogFragment extends DialogFragment {
|
|||
Intent intent = new Intent(getActivity(), LiveUpdatesAlarmReceiver.class);
|
||||
final File file = new File(localIndexInfo.getFileName());
|
||||
final String fileName = Algorithms.getFileNameWithoutExtension(file);
|
||||
// intent.putExtra(LOCAL_INDEX_INFO, fileName);
|
||||
intent.putExtra(LOCAL_INDEX_INFO, localIndexInfo);
|
||||
intent.setAction(fileName);
|
||||
PendingIntent alarmIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
|
||||
|
||||
|
@ -100,6 +105,7 @@ public class LiveUpdatesSettingsDialogFragment extends DialogFragment {
|
|||
}
|
||||
|
||||
liveUpdatePreference.set(liveUpdatesSwitch.isChecked());
|
||||
downloadViaWiFiPreference.set(downloadOverWiFiSwitch.isChecked());
|
||||
alarmMgr.cancel(alarmIntent);
|
||||
if (liveUpdatesSwitch.isChecked()) {
|
||||
alarmMgr.setInexactRepeating(AlarmManager.RTC,
|
||||
|
@ -162,13 +168,18 @@ public class LiveUpdatesSettingsDialogFragment extends DialogFragment {
|
|||
return getSettings().registerBooleanPreference(settingId, false);
|
||||
}
|
||||
|
||||
private OsmandSettings.CommonPreference<Boolean> preferenceDownloadViaWiFi(LocalIndexInfo item) {
|
||||
final String settingId = item.getFileName() + DOWNLOAD_VIA_WIFI_POSTFIX;
|
||||
return getSettings().registerBooleanPreference(settingId, false);
|
||||
}
|
||||
|
||||
private OsmandSettings.CommonPreference<Integer> preferenceUpdateTimes(LocalIndexInfo item) {
|
||||
final String settingId = item.getFileName() + UPDATE_TIMES;
|
||||
final String settingId = item.getFileName() + UPDATE_TIMES_POSTFIX;
|
||||
return getSettings().registerIntPreference(settingId, UpdateFrequencies.HOURLY.ordinal());
|
||||
}
|
||||
|
||||
private OsmandSettings.CommonPreference<Integer> preferenceTimeOfDayToUpdate(LocalIndexInfo item) {
|
||||
final String settingId = item.getFileName() + TIME_OF_DAY_TO_UPDATE;
|
||||
final String settingId = item.getFileName() + TIME_OF_DAY_TO_UPDATE_POSTFIX;
|
||||
return getSettings().registerIntPreference(settingId, TimesOfDay.NIGHT.ordinal());
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue