diff --git a/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesFragment.java b/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesFragment.java index a7890796eb..09fa2ae61d 100644 --- a/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesFragment.java +++ b/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesFragment.java @@ -18,7 +18,6 @@ import android.widget.ExpandableListView; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; -import android.widget.Toast; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; @@ -28,10 +27,7 @@ import net.osmand.plus.activities.LocalIndexInfo; import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.download.AbstractDownloadActivity; import net.osmand.plus.download.DownloadActivity; -import net.osmand.plus.download.DownloadActivityType; -import net.osmand.plus.download.IndexItem; import net.osmand.plus.download.ui.AbstractLoadLocalIndexTask; -import net.osmand.plus.helpers.FileNameTranslationHelper; import net.osmand.plus.resources.IncrementalChangesManager; import net.osmand.util.Algorithms; @@ -44,6 +40,8 @@ import java.util.List; import java.util.Set; 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.preferenceLiveUpdatesOn; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceUpdateFrequency; @@ -263,32 +261,12 @@ public class LiveUpdatesFragment extends Fragment { } - private static OsmandSettings.CommonPreference preferenceForLocalIndex( - String idPostfix, - LocalIndexInfo item, - LiveUpdatesFragment fragment) { - final OsmandApplication myApplication = fragment.getMyActivity().getMyApplication(); - final OsmandSettings settings = myApplication.getSettings(); - final String settingId = item.getFileName() + idPostfix; - return settings.registerBooleanPreference(settingId, false); - } - private void expandAllGroups() { for (int i = 0; i < adapter.getGroupCount(); i++) { listView.expandGroup(i); } } - void runLiveUpdate(final LocalIndexInfo info) { - final String fnExt = Algorithms.getFileNameWithoutExtension(new File(info.getFileName())); - new PerformLiveUpdateAsyncTask(getMyActivity()).execute(new String[]{fnExt}); - adapter.notifyLiveUpdatesChanged(); - } - - LocalIndexInfo getLocalIndexInfo(int groupPosition, int childPosition) { - return adapter.getChild(groupPosition, childPosition); - } - private static class LocalFullMapsViewHolder { private final ImageView icon; private final TextView nameTextView; @@ -322,7 +300,7 @@ public class LiveUpdatesFragment extends Fragment { final String fileNameWithoutExtension = Algorithms.getFileNameWithoutExtension(new File(item.getFileName())); - nameTextView.setText(getNameToDisplay(item)); + nameTextView.setText(getNameToDisplay(item, fragment.getMyActivity())); if (shouldUpdatePreference.get()) { final Integer frequencyId = preferenceUpdateFrequency(item, fragment.getSettings()).get(); final UpdateFrequency frequency = UpdateFrequency.values()[frequencyId]; @@ -347,7 +325,7 @@ public class LiveUpdatesFragment extends Fragment { } final long timestamp = changesManager.getTimestamp(fileNameWithoutExtension); - String formattedDate = LiveUpdatesFragment.formatDateTime(fragment.getActivity(), timestamp); + String formattedDate = formatDateTime(fragment.getActivity(), timestamp); descriptionTextView.setText(context.getString(R.string.last_update, formattedDate)); final View.OnClickListener clickListener = new View.OnClickListener() { @@ -365,13 +343,6 @@ public class LiveUpdatesFragment extends Fragment { return fragment.getMyActivity().getMyApplication().getIconsCache() .getPaintedContentIcon(drawable, secondaryColor); } - - private String getNameToDisplay(LocalIndexInfo child) { - String mapName = FileNameTranslationHelper.getFileName(fragment.getActivity(), - fragment.getMyActivity().getMyApplication().getResourceManager().getOsmandRegions(), - child.getFileName()); - return mapName; - } } public static class LoadLocalIndexTask @@ -417,52 +388,4 @@ public class LiveUpdatesFragment extends Fragment { adapter.sort(); } } - - public static class PerformLiveUpdateAsyncTask - extends AsyncTask { - private final AbstractDownloadActivity activity; - - public PerformLiveUpdateAsyncTask(AbstractDownloadActivity activity) { - this.activity = activity; - } - - protected void onPreExecute() { - activity.setSupportProgressBarIndeterminateVisibility(true); - - } - - @Override - protected IncrementalChangesManager.IncrementalUpdateList doInBackground(String... params) { - final OsmandApplication myApplication = activity.getMyApplication(); - IncrementalChangesManager cm = myApplication.getResourceManager().getChangesManager(); - return cm.getUpdatesByMonth(params[0]); - } - - protected void onPostExecute(IncrementalChangesManager.IncrementalUpdateList result) { - activity.setSupportProgressBarIndeterminateVisibility(false); - if (result.errorMessage != null) { - Toast.makeText(activity, result.errorMessage, Toast.LENGTH_SHORT).show(); - } else { - List ll = result.getItemsForUpdate(); - if (ll.isEmpty()) { - Toast.makeText(activity, R.string.no_updates_available, Toast.LENGTH_SHORT).show(); - } else { - 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); - is[i++] = ii; - } - activity.startDownload(is); - } - } - } - } - - private static String formatDateTime(Context ctx, long dateTime) { - java.text.DateFormat dateFormat = android.text.format.DateFormat.getMediumDateFormat(ctx); - java.text.DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(ctx); - return dateFormat.format(dateTime) + " " + timeFormat.format(dateTime); - } } diff --git a/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesHelper.java b/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesHelper.java index b628c194e1..b20477148a 100644 --- a/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesHelper.java +++ b/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesHelper.java @@ -1,11 +1,12 @@ package net.osmand.plus.liveupdates; +import android.content.Context; + import net.osmand.plus.OsmandSettings; import net.osmand.plus.activities.LocalIndexInfo; +import net.osmand.plus.activities.OsmandActionBarActivity; +import net.osmand.plus.helpers.FileNameTranslationHelper; -/** - * Created by GaidamakUA on 12/18/15. - */ public class LiveUpdatesHelper { private static final String UPDATE_TIMES_POSTFIX = "_update_times"; private static final String TIME_OF_DAY_TO_UPDATE_POSTFIX = "_time_of_day_to_update"; @@ -43,6 +44,19 @@ public class LiveUpdatesHelper { return settings.registerIntPreference(settingId, TimesOfDay.NIGHT.ordinal()); } + public static String getNameToDisplay(LocalIndexInfo child, OsmandActionBarActivity activity) { + String mapName = FileNameTranslationHelper.getFileName(activity, + activity.getMyApplication().getResourceManager().getOsmandRegions(), + child.getFileName()); + return mapName; + } + + public static String formatDateTime(Context ctx, long dateTime) { + java.text.DateFormat dateFormat = android.text.format.DateFormat.getMediumDateFormat(ctx); + java.text.DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(ctx); + return dateFormat.format(dateTime) + " " + timeFormat.format(dateTime); + } + public static enum TimesOfDay { MORNING, NIGHT diff --git a/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesSettingsDialogFragment.java b/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesSettingsDialogFragment.java index 879f04e25a..94a89cfa04 100644 --- a/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesSettingsDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesSettingsDialogFragment.java @@ -14,6 +14,8 @@ import android.support.v7.widget.SwitchCompat; import android.view.LayoutInflater; import android.view.View; import android.widget.AdapterView; +import android.widget.Button; +import android.widget.CheckBox; import android.widget.Spinner; import android.widget.TextView; @@ -21,7 +23,9 @@ 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.activities.OsmandActionBarActivity; +import net.osmand.plus.download.AbstractDownloadActivity; +import net.osmand.plus.download.DownloadActivity; +import net.osmand.plus.resources.IncrementalChangesManager; import net.osmand.util.Algorithms; import java.io.File; @@ -29,6 +33,8 @@ import java.util.Calendar; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.TimesOfDay; 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.preferenceDownloadViaWiFi; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceForLocalIndex; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceTimeOfDayToUpdate; @@ -53,14 +59,22 @@ public class LiveUpdatesSettingsDialogFragment extends DialogFragment { .inflate(R.layout.dialog_live_updates_item_settings, null); final TextView regionNameTextView = (TextView) view.findViewById(R.id.regionNameTextView); final TextView countryNameTextView = (TextView) view.findViewById(R.id.countryNameTextView); + final TextView lastUpdateTextView = (TextView) view.findViewById(R.id.lastUpdateTextView); final SwitchCompat liveUpdatesSwitch = (SwitchCompat) view.findViewById(R.id.liveUpdatesSwitch); - final SwitchCompat downloadOverWiFiSwitch = (SwitchCompat) view.findViewById(R.id.downloadOverWiFiSwitch); + final CheckBox downloadOverWiFiSwitch = (CheckBox) view.findViewById(R.id.downloadOverWiFiSwitch); final Spinner updateFrequencySpinner = (Spinner) view.findViewById(R.id.updateFrequencySpinner); final Spinner updateTimesOfDaySpinner = (Spinner) view.findViewById(R.id.updateTimesOfDaySpinner); + final TextView updateTimesOfDayTextView = (TextView) view.findViewById(R.id.updateTimesOfDayLabel); + final TextView sizeTextView = (TextView) view.findViewById(R.id.sizeTextView); + final Button removeUpdatesButton = (Button) view.findViewById(R.id.removeUpdatesButton); - regionNameTextView.setText(localIndexInfo.getName()); - // countryNameTextView.setText(localIndexInfo.getWorldRegion().getLocaleName()); - countryNameTextView.setVisibility(View.VISIBLE); + regionNameTextView.setText(getNameToDisplay(localIndexInfo, getMyActivity())); + final String fileNameWithoutExtension = + Algorithms.getFileNameWithoutExtension(new File(localIndexInfo.getFileName())); + final IncrementalChangesManager changesManager = getMyApplication().getResourceManager().getChangesManager(); + final long timestamp = changesManager.getTimestamp(fileNameWithoutExtension); + String formattedDate = formatDateTime(getActivity(), timestamp); + lastUpdateTextView.setText(getString(R.string.update_date_pattern, formattedDate)); final OsmandSettings.CommonPreference liveUpdatePreference = preferenceForLocalIndex(localIndexInfo, getSettings()); final OsmandSettings.CommonPreference downloadViaWiFiPreference = @@ -72,6 +86,40 @@ public class LiveUpdatesSettingsDialogFragment extends DialogFragment { liveUpdatesSwitch.setChecked(true); downloadOverWiFiSwitch.setChecked(downloadViaWiFiPreference.get()); + updateSize(fileNameWithoutExtension, changesManager, sizeTextView); + + updateFrequencySpinner.setSelection(updateFrequencePreference.get()); + updateFrequencySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + UpdateFrequency updateFrequency = UpdateFrequency.values()[position]; + switch (updateFrequency) { + case HOURLY: + updateTimesOfDaySpinner.setVisibility(View.GONE); + updateTimesOfDayTextView.setVisibility(View.GONE); + break; + case DAILY: + case WEEKLY: + updateTimesOfDaySpinner.setVisibility(View.VISIBLE); + updateTimesOfDayTextView.setVisibility(View.VISIBLE); + break; + } + } + + @Override + public void onNothingSelected(AdapterView parent) { + + } + }); + removeUpdatesButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + changesManager.deleteUpdates(fileNameWithoutExtension); + getLiveUpdatesFragment().notifyLiveUpdatesChanged(); + updateSize(fileNameWithoutExtension, changesManager, sizeTextView); + } + }); + builder.setView(view) .setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() { @Override @@ -126,35 +174,33 @@ public class LiveUpdatesSettingsDialogFragment extends DialogFragment { .setNeutralButton(R.string.update_now, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - getLiveUpdatesFragment().runLiveUpdate(localIndexInfo); + runLiveUpdate(localIndexInfo); + updateSize(fileNameWithoutExtension, changesManager, sizeTextView); } }); - - updateFrequencySpinner.setSelection(updateFrequencePreference.get()); - updateFrequencySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView parent, View view, int position, long id) { - UpdateFrequency updateFrequency = UpdateFrequency.values()[position]; - switch (updateFrequency) { - case HOURLY: - updateTimesOfDaySpinner.setVisibility(View.GONE); - break; - case DAILY: - case WEEKLY: - updateTimesOfDaySpinner.setVisibility(View.VISIBLE); - break; - } - } - - @Override - public void onNothingSelected(AdapterView parent) { - - } - }); - return builder.create(); } + void runLiveUpdate(final LocalIndexInfo info) { + final String fnExt = Algorithms.getFileNameWithoutExtension(new File(info.getFileName())); + new PerformLiveUpdateAsyncTask(getMyActivity()).execute(new String[]{fnExt}); + getLiveUpdatesFragment().notifyLiveUpdatesChanged(); + } + + private void updateSize(String fileNameWithoutExtension, + IncrementalChangesManager changesManager, + TextView sizeTextView) { + String size; + long updatesSize = changesManager.getUpdatesSize(fileNameWithoutExtension); + updatesSize /= (1 << 10); + if (updatesSize > 100) { + size = DownloadActivity.formatMb.format(new Object[]{(float) updatesSize / (1 << 10)}); + } else { + size = updatesSize + " KB"; + } + sizeTextView.setText(size); + } + private long getNextUpdateTime(TimesOfDay timeOfDayToUpdate) { Calendar calendar = Calendar.getInstance(); if (timeOfDayToUpdate == TimesOfDay.MORNING) { @@ -176,7 +222,11 @@ public class LiveUpdatesSettingsDialogFragment extends DialogFragment { } private OsmandApplication getMyApplication() { - return ((OsmandActionBarActivity) this.getActivity()).getMyApplication(); + return getMyActivity().getMyApplication(); + } + + private AbstractDownloadActivity getMyActivity() { + return (AbstractDownloadActivity) this.getActivity(); } public static LiveUpdatesSettingsDialogFragment createInstance(LocalIndexInfo localIndexInfo) { diff --git a/OsmAnd/src/net/osmand/plus/liveupdates/PerformLiveUpdateAsyncTask.java b/OsmAnd/src/net/osmand/plus/liveupdates/PerformLiveUpdateAsyncTask.java new file mode 100644 index 0000000000..30fd5629f4 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/liveupdates/PerformLiveUpdateAsyncTask.java @@ -0,0 +1,55 @@ +package net.osmand.plus.liveupdates; + +import android.os.AsyncTask; +import android.widget.Toast; + +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.download.AbstractDownloadActivity; +import net.osmand.plus.download.DownloadActivityType; +import net.osmand.plus.download.IndexItem; +import net.osmand.plus.resources.IncrementalChangesManager; + +import java.util.List; + +public class PerformLiveUpdateAsyncTask + extends AsyncTask { + private final AbstractDownloadActivity activity; + + public PerformLiveUpdateAsyncTask(AbstractDownloadActivity activity) { + this.activity = activity; + } + + protected void onPreExecute() { + activity.setSupportProgressBarIndeterminateVisibility(true); + + } + + @Override + protected IncrementalChangesManager.IncrementalUpdateList doInBackground(String... params) { + final OsmandApplication myApplication = activity.getMyApplication(); + IncrementalChangesManager cm = myApplication.getResourceManager().getChangesManager(); + return cm.getUpdatesByMonth(params[0]); + } + + protected void onPostExecute(IncrementalChangesManager.IncrementalUpdateList result) { + activity.setSupportProgressBarIndeterminateVisibility(false); + if (result.errorMessage != null) { + Toast.makeText(activity, result.errorMessage, Toast.LENGTH_SHORT).show(); + } else { + List ll = result.getItemsForUpdate(); + if (ll.isEmpty()) { + Toast.makeText(activity, R.string.no_updates_available, Toast.LENGTH_SHORT).show(); + } else { + 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); + is[i++] = ii; + } + activity.startDownload(is); + } + } + } +} diff --git a/OsmAnd/src/net/osmand/plus/resources/IncrementalChangesManager.java b/OsmAnd/src/net/osmand/plus/resources/IncrementalChangesManager.java index 296a0688dd..db7964eda0 100644 --- a/OsmAnd/src/net/osmand/plus/resources/IncrementalChangesManager.java +++ b/OsmAnd/src/net/osmand/plus/resources/IncrementalChangesManager.java @@ -119,7 +119,7 @@ public class IncrementalChangesManager { } protected static String formatSize(long vl) { - return (float) ((vl * 1000 / (1 << 20l)) / 1000.0f) + ""; + return (vl * 1000 / (1 << 20l)) / 1000.0f + ""; } public static long calculateSize(List list) { @@ -347,6 +347,18 @@ public class IncrementalChangesManager { return size; } + public void deleteUpdates(String fileName){ + RegionUpdateFiles ruf = regions.get(fileName.toLowerCase()); + for (List regionUpdates : ruf.dayUpdates.values()) { + for (RegionUpdate regionUpdate : regionUpdates) { + regionUpdate.file.delete(); + } + } + for (RegionUpdate regionUpdate : ruf.monthUpdates.values()) { + regionUpdate.file.delete(); + } + } + public long getTimestamp(String fileName) { RegionUpdateFiles ruf = regions.get(fileName.toLowerCase()); return getTimestamp(ruf);