diff --git a/OsmAnd/res/layout/list_group_title_with_switch.xml b/OsmAnd/res/layout/list_group_title_with_switch.xml index 18a0a4e592..1d42f5c253 100644 --- a/OsmAnd/res/layout/list_group_title_with_switch.xml +++ b/OsmAnd/res/layout/list_group_title_with_switch.xml @@ -4,7 +4,8 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="64dp" - android:orientation="vertical"> + android:orientation="vertical" + android:background="?attr/bg_color"> diff --git a/OsmAnd/res/layout/local_index_live_updates_list_item.xml b/OsmAnd/res/layout/local_index_live_updates_list_item.xml index 3ae0b200ed..044fc59cd1 100644 --- a/OsmAnd/res/layout/local_index_live_updates_list_item.xml +++ b/OsmAnd/res/layout/local_index_live_updates_list_item.xml @@ -3,7 +3,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="72dp" - android:background="?attr/expandable_list_item_background" + android:background="?attr/bg_color" android:descendantFocusability="blocksDescendants" android:minHeight="@dimen/list_item_height" android:orientation="horizontal" diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index a53f337c6d..b93ad0eaf4 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -2141,4 +2141,9 @@ Afghanistan, Albania, Algeria, Andorra, Angola, Anguilla, Antigua and Barbuda, A Update time "Updates: %s" "Last map change: %s" + Hourly + Daily + Weekly + Morning + Night \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadValidationManager.java b/OsmAnd/src/net/osmand/plus/download/DownloadValidationManager.java index 85787c61d1..98253e2ce7 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadValidationManager.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadValidationManager.java @@ -2,7 +2,6 @@ package net.osmand.plus.download; import android.app.Dialog; import android.content.ActivityNotFoundException; -import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; @@ -39,7 +38,7 @@ public class DownloadValidationManager { return downloadThread; } - public void startDownload(Context context, IndexItem... items) { + public void startDownload(FragmentActivity context, IndexItem... items) { downloadFilesWithAllChecks(context, items); } @@ -47,7 +46,7 @@ public class DownloadValidationManager { return app; } - public void downloadFilesCheck_3_ValidateSpace(final Context context, final IndexItem... items) { + public void downloadFilesCheck_3_ValidateSpace(final FragmentActivity context, final IndexItem... items) { long szLong = 0; int i = 0; for (IndexItem es : downloadThread.getCurrentDownloadingItems()) { @@ -77,7 +76,7 @@ public class DownloadValidationManager { } } - private void downloadFileCheck_Final_Run(Context context, IndexItem[] items) { + private void downloadFileCheck_Final_Run(FragmentActivity context, IndexItem[] items) { downloadThread.runDownloadFiles(items); if (context instanceof DownloadEvents) { ((DownloadEvents) context).downloadInProgress(); @@ -85,16 +84,16 @@ public class DownloadValidationManager { } - protected void downloadFilesWithAllChecks(Context context, IndexItem[] items) { + protected void downloadFilesWithAllChecks(FragmentActivity context, IndexItem[] items) { downloadFilesCheck_1_FreeVersion(context, items); } - protected void downloadFilesCheck_1_FreeVersion(Context context, IndexItem[] items) { + protected void downloadFilesCheck_1_FreeVersion(FragmentActivity context, IndexItem[] items) { if (Version.isFreeVersion(getMyApplication())) { int total = settings.NUMBER_OF_FREE_DOWNLOADS.get(); if (total > MAXIMUM_AVAILABLE_FREE_DOWNLOADS) { if (context instanceof FragmentActivity) { - FragmentActivity activity = (FragmentActivity) context; + FragmentActivity activity = context; new InstallPaidVersionDialogFragment() .show(activity.getSupportFragmentManager(), InstallPaidVersionDialogFragment.TAG); } @@ -106,7 +105,7 @@ public class DownloadValidationManager { } } - protected void downloadFilesCheck_2_Internet(final Context context, final IndexItem[] items) { + protected void downloadFilesCheck_2_Internet(final FragmentActivity context, final IndexItem[] items) { if (!getMyApplication().getSettings().isWifiConnected()) { if (getMyApplication().getSettings().isInternetConnectionAvailable()) { AlertDialog.Builder builder = new AlertDialog.Builder(context); @@ -128,7 +127,7 @@ public class DownloadValidationManager { } - public void makeSureUserCancelDownload(Context ctx, final IndexItem item) { + public void makeSureUserCancelDownload(FragmentActivity ctx, final IndexItem item) { AlertDialog.Builder bld = new AlertDialog.Builder(ctx); bld.setTitle(ctx.getString(R.string.shared_string_cancel)); bld.setMessage(R.string.confirm_interrupt_download); diff --git a/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesAlarmReceiver.java b/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesAlarmReceiver.java index 26cf02df81..d42acd94c5 100644 --- a/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesAlarmReceiver.java +++ b/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesAlarmReceiver.java @@ -21,7 +21,7 @@ public class LiveUpdatesAlarmReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { String fileName = intent.getAction(); LocalIndexInfo localIndexInfo = - intent.getParcelableExtra(LiveUpdatesSettingsDialogFragment.LOCAL_INDEX_INFO); + intent.getParcelableExtra(LiveUpdatesHelper.LOCAL_INDEX_INFO); WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); final OsmandApplication application = (OsmandApplication) context.getApplicationContext(); diff --git a/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesFragment.java b/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesFragment.java index c3147d1e0f..32f6ff3bfd 100644 --- a/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesFragment.java +++ b/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesFragment.java @@ -1,5 +1,7 @@ package net.osmand.plus.liveupdates; +import android.app.AlarmManager; +import android.app.PendingIntent; import android.content.Context; import android.content.res.Resources; import android.graphics.drawable.Drawable; @@ -43,9 +45,11 @@ import static net.osmand.plus.liveupdates.LiveUpdatesHelper.TimeOfDay; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.UpdateFrequency; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.formatDateTime; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.getNameToDisplay; +import static net.osmand.plus.liveupdates.LiveUpdatesHelper.getPendingIntent; 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.setAlarmForPendingIntent; public class LiveUpdatesFragment extends Fragment { public static final String TITILE = "Live Updates"; @@ -70,8 +74,6 @@ public class LiveUpdatesFragment extends Fragment { listView = (ExpandableListView) view.findViewById(android.R.id.list); // View header = inflater.inflate(R.layout.live_updates_header, listView, false); - View topShadowView = inflater.inflate(R.layout.shadow_top, listView, false); - listView.addHeaderView(topShadowView); View bottomShadowView = inflater.inflate(R.layout.shadow_bottom, listView, false); listView.addFooterView(bottomShadowView); adapter = new LocalIndexesAdapter(this); @@ -179,24 +181,24 @@ public class LiveUpdatesFragment extends Fragment { @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { - View v = convertView; + View view = convertView; String group = getGroup(groupPosition); - if (v == null) { + if (view == null) { LayoutInflater inflater = LayoutInflater.from(ctx); - v = inflater.inflate(R.layout.list_group_title_with_switch, parent, false); + view = inflater.inflate(R.layout.list_group_title_with_switch, parent, false); } - TextView nameView = ((TextView) v.findViewById(R.id.section_name)); + TextView nameView = ((TextView) view.findViewById(R.id.section_name)); nameView.setText(group); - v.setOnClickListener(null); + view.setOnClickListener(null); TypedValue typedValue = new TypedValue(); Resources.Theme theme = ctx.getTheme(); theme.resolveAttribute(R.attr.ctx_menu_info_view_bg, typedValue, true); - v.setBackgroundColor(typedValue.data); + view.setBackgroundColor(typedValue.data); - SwitchCompat liveUpdatesSwitch = (SwitchCompat) v.findViewById(R.id.liveUpdatesSwitch); - View topShadowView = v.findViewById(R.id.bottomShadowView); + SwitchCompat liveUpdatesSwitch = (SwitchCompat) view.findViewById(R.id.liveUpdatesSwitch); + View topShadowView = view.findViewById(R.id.bottomShadowView); if (groupPosition == SHOULD_UPDATE_GROUP_POSITION) { topShadowView.setVisibility(View.GONE); liveUpdatesSwitch.setVisibility(View.VISIBLE); @@ -207,15 +209,30 @@ public class LiveUpdatesFragment extends Fragment { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { settings.IS_LIVE_UPDATES_ON.set(isChecked); - int liveUpdatesStateId = isChecked ? R.string.shared_string_on - : R.string.shared_string_off; + AlarmManager alarmMgr = (AlarmManager) getActivity() + .getSystemService(Context.ALARM_SERVICE); + for (LocalIndexInfo localIndexInfo : dataShouldUpdate) { + PendingIntent alarmIntent = getPendingIntent(getActivity(), + localIndexInfo); + if(isChecked) { + final OsmandSettings.CommonPreference updateFrequencyPreference = + preferenceUpdateFrequency(localIndexInfo, getSettings()); + final OsmandSettings.CommonPreference timeOfDayPreference = + preferenceTimeOfDayToUpdate(localIndexInfo, getSettings()); + UpdateFrequency updateFrequency = UpdateFrequency.values()[updateFrequencyPreference.get()]; + TimeOfDay timeOfDayToUpdate = TimeOfDay.values()[timeOfDayPreference.get()]; + setAlarmForPendingIntent(alarmIntent, alarmMgr, updateFrequency, timeOfDayToUpdate); + } else { + alarmMgr.cancel(alarmIntent); + } + } } }); } else { topShadowView.setVisibility(View.VISIBLE); liveUpdatesSwitch.setVisibility(View.GONE); } - return v; + return view; } @Override @@ -312,9 +329,9 @@ public class LiveUpdatesFragment extends Fragment { final UpdateFrequency frequency = UpdateFrequency.values()[frequencyId]; final TimeOfDay timeOfDay = TimeOfDay.values()[timeOfDateToUpdateId]; subheaderTextView.setVisibility(View.VISIBLE); - String subheaderText = frequency.toString(); + String subheaderText = fragment.getString(frequency.getLocalizedId()); if (frequency != UpdateFrequency.HOURLY) { - subheaderText += " • " + timeOfDay.toString(); + subheaderText += " • " + fragment.getString(timeOfDay.getLocalizedId()); } subheaderTextView.setText(subheaderText); subheaderTextView.setTextColor(fragment.getActivity().getResources() diff --git a/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesHelper.java b/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesHelper.java index 4b57f7a47b..77057b5d89 100644 --- a/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesHelper.java +++ b/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesHelper.java @@ -1,11 +1,19 @@ package net.osmand.plus.liveupdates; +import android.app.AlarmManager; +import android.app.PendingIntent; import android.content.Context; +import android.content.Intent; import net.osmand.plus.OsmandSettings; +import net.osmand.plus.R; import net.osmand.plus.activities.LocalIndexInfo; import net.osmand.plus.activities.OsmandActionBarActivity; import net.osmand.plus.helpers.FileNameTranslationHelper; +import net.osmand.util.Algorithms; + +import java.io.File; +import java.util.Calendar; public class LiveUpdatesHelper { private static final String UPDATE_TIMES_POSTFIX = "_update_times"; @@ -13,6 +21,13 @@ public class LiveUpdatesHelper { private static final String DOWNLOAD_VIA_WIFI_POSTFIX = "_download_via_wifi"; private static final String LIVE_UPDATES_ON_POSTFIX = "_live_updates_on"; private static final String LAST_UPDATE_ATTEMPT_ON_POSTFIX = "_last_update_attempt"; + public static final String LOCAL_INDEX_INFO = "local_index_info"; + + + 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 int DEFAULT_LAST_CHECK = -1; public static OsmandSettings.CommonPreference preferenceForLocalIndex( @@ -64,14 +79,83 @@ public class LiveUpdatesHelper { return dateFormat.format(dateTime) + " " + timeFormat.format(dateTime); } - public static enum TimeOfDay { - MORNING, - NIGHT + public static PendingIntent getPendingIntent(Context context, LocalIndexInfo localIndexInfo) { + Intent intent = new Intent(context, LiveUpdatesAlarmReceiver.class); + final File file = new File(localIndexInfo.getFileName()); + final String fileName = Algorithms.getFileNameWithoutExtension(file); + intent.putExtra(LOCAL_INDEX_INFO, localIndexInfo); + intent.setAction(fileName); + return PendingIntent.getBroadcast(context, 0, intent, 0); } - public enum UpdateFrequency { - HOURLY, - DAILY, - WEEKLY + 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); + } + + private static long getNextUpdateTime(TimeOfDay timeOfDayToUpdate) { + Calendar calendar = Calendar.getInstance(); + if (timeOfDayToUpdate == TimeOfDay.MORNING) { + calendar.add(Calendar.DATE, 1); + calendar.set(Calendar.HOUR_OF_DAY, MORNING_UPDATE_TIME); + } else if (timeOfDayToUpdate == TimeOfDay.NIGHT) { + calendar.add(Calendar.DATE, 1); + calendar.set(Calendar.HOUR_OF_DAY, NIGHT_UPDATE_TIME); + } + return calendar.getTimeInMillis(); + } + + public static enum TimeOfDay { + MORNING(R.string.morning), + NIGHT(R.string.Night); + private final int localizedId; + + TimeOfDay(int localizedId) { + this.localizedId = localizedId; + } + + public int getLocalizedId() { + return localizedId; + } + + + @Override + public String toString() { + return super.toString(); + } + } + + public static enum UpdateFrequency { + HOURLY(R.string.hourly), + DAILY(R.string.daily), + WEEKLY(R.string.weekly); + private final int localizedId; + + UpdateFrequency(int localizedId) { + this.localizedId = localizedId; + } + + public int getLocalizedId() { + return localizedId; + } } } diff --git a/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesSettingsDialogFragment.java b/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesSettingsDialogFragment.java index 84d0d373e9..648521ddf0 100644 --- a/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesSettingsDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesSettingsDialogFragment.java @@ -5,7 +5,6 @@ import android.app.Dialog; import android.app.PendingIntent; import android.content.Context; import android.content.DialogInterface; -import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; @@ -32,26 +31,21 @@ import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; import java.io.File; -import java.util.Calendar; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.TimeOfDay; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.UpdateFrequency; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.formatDateTime; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.getNameToDisplay; +import static net.osmand.plus.liveupdates.LiveUpdatesHelper.getPendingIntent; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceDownloadViaWiFi; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceForLocalIndex; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceTimeOfDayToUpdate; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceUpdateFrequency; +import static net.osmand.plus.liveupdates.LiveUpdatesHelper.setAlarmForPendingIntent; public class LiveUpdatesSettingsDialogFragment extends DialogFragment { private static final Log LOG = PlatformUtil.getLog(LiveUpdatesAlarmReceiver.class); private static final String LOCAL_INDEX = "local_index"; - public static final String LOCAL_INDEX_INFO = "local_index_info"; - - - private static final int MORNING_UPDATE_TIME = 8; - private static final int NIGHT_UPDATE_TIME = 21; - private static final int SHIFT = 1000; @NonNull @Override @@ -86,7 +80,7 @@ public class LiveUpdatesSettingsDialogFragment extends DialogFragment { preferenceForLocalIndex(localIndexInfo, getSettings()); final OsmandSettings.CommonPreference downloadViaWiFiPreference = preferenceDownloadViaWiFi(localIndexInfo, getSettings()); - final OsmandSettings.CommonPreference updateFrequencePreference = + final OsmandSettings.CommonPreference updateFrequencyPreference = preferenceUpdateFrequency(localIndexInfo, getSettings()); final OsmandSettings.CommonPreference timeOfDayPreference = preferenceTimeOfDayToUpdate(localIndexInfo, getSettings()); @@ -95,7 +89,7 @@ public class LiveUpdatesSettingsDialogFragment extends DialogFragment { updateSize(fileNameWithoutExtension, changesManager, sizeTextView); - updateFrequencySpinner.setSelection(updateFrequencePreference.get()); + updateFrequencySpinner.setSelection(updateFrequencyPreference.get()); updateFrequencySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { @@ -131,48 +125,25 @@ public class LiveUpdatesSettingsDialogFragment extends DialogFragment { .setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { + liveUpdatePreference.set(liveUpdatesSwitch.isChecked()); + downloadViaWiFiPreference.set(downloadOverWiFiCheckBox.isChecked()); + final int updateFrequencyInt = updateFrequencySpinner.getSelectedItemPosition(); - updateFrequencePreference.set(updateFrequencyInt); - UpdateFrequency updateFrequency = UpdateFrequency.values()[updateFrequencyInt]; + updateFrequencyPreference.set(updateFrequencyInt); AlarmManager alarmMgr = (AlarmManager) getActivity() .getSystemService(Context.ALARM_SERVICE); - 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, localIndexInfo); - intent.setAction(fileName); - PendingIntent alarmIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, 0); + PendingIntent alarmIntent = getPendingIntent(getActivity(), localIndexInfo); final int timeOfDayInt = updateTimesOfDaySpinner.getSelectedItemPosition(); timeOfDayPreference.set(timeOfDayInt); - TimeOfDay timeOfDayToUpdate = TimeOfDay.values()[timeOfDayInt]; - 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); - } - liveUpdatePreference.set(liveUpdatesSwitch.isChecked()); - downloadViaWiFiPreference.set(downloadOverWiFiCheckBox.isChecked()); - alarmMgr.cancel(alarmIntent); - if (liveUpdatesSwitch.isChecked()) { - alarmMgr.setInexactRepeating(AlarmManager.RTC, - timeOfFirstUpdate, updateInterval, alarmIntent); + if (liveUpdatesSwitch.isChecked() && getSettings().IS_LIVE_UPDATES_ON.get()) { + UpdateFrequency updateFrequency = UpdateFrequency.values()[updateFrequencyInt]; + TimeOfDay timeOfDayToUpdate = TimeOfDay.values()[timeOfDayInt]; + setAlarmForPendingIntent(alarmIntent, alarmMgr, updateFrequency, timeOfDayToUpdate); + } else { + alarmMgr.cancel(alarmIntent); } getLiveUpdatesFragment().notifyLiveUpdatesChanged(); } @@ -207,18 +178,6 @@ public class LiveUpdatesSettingsDialogFragment extends DialogFragment { sizeTextView.setText(getString(R.string.updates_size_pattern, size)); } - private long getNextUpdateTime(TimeOfDay timeOfDayToUpdate) { - Calendar calendar = Calendar.getInstance(); - if (timeOfDayToUpdate == TimeOfDay.MORNING) { - calendar.add(Calendar.DATE, 1); - calendar.set(Calendar.HOUR_OF_DAY, MORNING_UPDATE_TIME); - } else if (timeOfDayToUpdate == TimeOfDay.NIGHT) { - calendar.add(Calendar.DATE, 1); - calendar.set(Calendar.HOUR_OF_DAY, NIGHT_UPDATE_TIME); - } - return calendar.getTimeInMillis(); - } - private LiveUpdatesFragment getLiveUpdatesFragment() { return (LiveUpdatesFragment) getParentFragment(); } diff --git a/OsmAnd/src/net/osmand/plus/liveupdates/PerformLiveUpdateAsyncTask.java b/OsmAnd/src/net/osmand/plus/liveupdates/PerformLiveUpdateAsyncTask.java index 44bfd1db2e..e0f767dcd6 100644 --- a/OsmAnd/src/net/osmand/plus/liveupdates/PerformLiveUpdateAsyncTask.java +++ b/OsmAnd/src/net/osmand/plus/liveupdates/PerformLiveUpdateAsyncTask.java @@ -14,12 +14,12 @@ import net.osmand.plus.activities.LocalIndexInfo; import net.osmand.plus.download.AbstractDownloadActivity; import net.osmand.plus.download.DownloadActivityType; import net.osmand.plus.download.DownloadIndexesThread; -import net.osmand.plus.download.DownloadValidationManager; import net.osmand.plus.download.IndexItem; import net.osmand.plus.resources.IncrementalChangesManager; import net.osmand.util.Algorithms; import java.io.File; +import java.util.ArrayList; import java.util.List; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceUpdateFrequency; @@ -41,15 +41,19 @@ public class PerformLiveUpdateAsyncTask activity.setSupportProgressBarIndeterminateVisibility(true); OsmandSettings settings = activity.getMyApplication().getSettings(); } - final OsmandApplication myApplication = (OsmandApplication) context.getApplicationContext(); + final OsmandApplication myApplication = getMyApplication(); OsmandSettings.CommonPreference lastCheckPreference = LiveUpdatesHelper.preferenceLastCheck(localIndexInfo, myApplication.getSettings()); lastCheckPreference.set(System.currentTimeMillis()); } + private OsmandApplication getMyApplication() { + return (OsmandApplication) context.getApplicationContext(); + } + @Override protected IncrementalChangesManager.IncrementalUpdateList doInBackground(String... params) { - final OsmandApplication myApplication = (OsmandApplication) context.getApplicationContext(); + final OsmandApplication myApplication = getMyApplication(); IncrementalChangesManager cm = myApplication.getResourceManager().getChangesManager(); return cm.getUpdatesByMonth(params[0]); } @@ -60,7 +64,7 @@ public class PerformLiveUpdateAsyncTask AbstractDownloadActivity activity = (AbstractDownloadActivity) context; activity.setSupportProgressBarIndeterminateVisibility(false); } - final OsmandApplication application = (OsmandApplication) context.getApplicationContext(); + final OsmandApplication application = getMyApplication(); final OsmandSettings settings = application.getSettings(); if (result.errorMessage != null) { Toast.makeText(context, result.errorMessage, Toast.LENGTH_SHORT).show(); @@ -71,21 +75,44 @@ public class PerformLiveUpdateAsyncTask if (ll.isEmpty()) { Toast.makeText(context, R.string.no_updates_available, Toast.LENGTH_SHORT).show(); } else { - int i = 0; - IndexItem[] is = new IndexItem[ll.size()]; + ArrayList itemsToDownload = new ArrayList<>(ll.size()); for (IncrementalChangesManager.IncrementalUpdate iu : ll) { - IndexItem ii = new IndexItem(iu.fileName, "Incremental update", + IndexItem indexItem = new IndexItem(iu.fileName, "Incremental update", iu.timestamp, iu.sizeText, iu.contentSize, iu.containerSize, DownloadActivityType.LIVE_UPDATES_FILE); - is[i++] = ii; + itemsToDownload.add(indexItem); } - DownloadValidationManager downloadValidationManager = - new DownloadValidationManager(application); + DownloadIndexesThread downloadThread = application.getDownloadThread(); if (context instanceof DownloadIndexesThread.DownloadEvents) { - downloadValidationManager.getDownloadThread() - .setUiActivity((DownloadIndexesThread.DownloadEvents) context); + downloadThread.setUiActivity((DownloadIndexesThread.DownloadEvents) context); + } + boolean downloadViaWiFi = + LiveUpdatesHelper.preferenceDownloadViaWiFi(localIndexInfo, settings).get(); + if (getMyApplication().getSettings().isInternetConnectionAvailable()) { + if (settings.isWifiConnected() || downloadViaWiFi) { + long szLong = 0; + int i = 0; + for (IndexItem es : downloadThread.getCurrentDownloadingItems()) { + szLong += es.getContentSize(); + i++; + } + for (IndexItem es : itemsToDownload) { + szLong += es.getContentSize(); + i++; + } + double sz = ((double) szLong) / (1 << 20); + // get availabile space + double asz = downloadThread.getAvailableSpace(); + if (asz == -1 || asz <= 0 || sz / asz <= 0.4) { + IndexItem[] itemsArray = new IndexItem[itemsToDownload.size()]; + itemsArray = itemsToDownload.toArray(itemsArray); + downloadThread.runDownloadFiles(itemsArray); + if (context instanceof DownloadIndexesThread.DownloadEvents) { + ((DownloadIndexesThread.DownloadEvents) context).downloadInProgress(); + } + } + } } - downloadValidationManager.startDownload(context, is); } } } @@ -104,7 +131,7 @@ public class PerformLiveUpdateAsyncTask 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.putExtra(LiveUpdatesHelper.LOCAL_INDEX_INFO, localIndexInfo); intent.setAction(fileName); PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);