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