Live updates most of design and functionality done.

This commit is contained in:
GaidamakUA 2015-12-22 18:30:57 +02:00
parent 06d41c7e55
commit f2b53cfa71
5 changed files with 169 additions and 115 deletions

View file

@ -18,7 +18,6 @@ import android.widget.ExpandableListView;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings; 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.activities.OsmandBaseExpandableListAdapter;
import net.osmand.plus.download.AbstractDownloadActivity; import net.osmand.plus.download.AbstractDownloadActivity;
import net.osmand.plus.download.DownloadActivity; 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.download.ui.AbstractLoadLocalIndexTask;
import net.osmand.plus.helpers.FileNameTranslationHelper;
import net.osmand.plus.resources.IncrementalChangesManager; import net.osmand.plus.resources.IncrementalChangesManager;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
@ -44,6 +40,8 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.UpdateFrequency; 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.preferenceLiveUpdatesOn;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceUpdateFrequency; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceUpdateFrequency;
@ -263,32 +261,12 @@ public class LiveUpdatesFragment extends Fragment {
} }
private static OsmandSettings.CommonPreference<Boolean> 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() { private void expandAllGroups() {
for (int i = 0; i < adapter.getGroupCount(); i++) { for (int i = 0; i < adapter.getGroupCount(); i++) {
listView.expandGroup(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 static class LocalFullMapsViewHolder {
private final ImageView icon; private final ImageView icon;
private final TextView nameTextView; private final TextView nameTextView;
@ -322,7 +300,7 @@ public class LiveUpdatesFragment extends Fragment {
final String fileNameWithoutExtension = final String fileNameWithoutExtension =
Algorithms.getFileNameWithoutExtension(new File(item.getFileName())); Algorithms.getFileNameWithoutExtension(new File(item.getFileName()));
nameTextView.setText(getNameToDisplay(item)); nameTextView.setText(getNameToDisplay(item, fragment.getMyActivity()));
if (shouldUpdatePreference.get()) { if (shouldUpdatePreference.get()) {
final Integer frequencyId = preferenceUpdateFrequency(item, fragment.getSettings()).get(); final Integer frequencyId = preferenceUpdateFrequency(item, fragment.getSettings()).get();
final UpdateFrequency frequency = UpdateFrequency.values()[frequencyId]; final UpdateFrequency frequency = UpdateFrequency.values()[frequencyId];
@ -347,7 +325,7 @@ public class LiveUpdatesFragment extends Fragment {
} }
final long timestamp = changesManager.getTimestamp(fileNameWithoutExtension); 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)); descriptionTextView.setText(context.getString(R.string.last_update, formattedDate));
final View.OnClickListener clickListener = new View.OnClickListener() { final View.OnClickListener clickListener = new View.OnClickListener() {
@ -365,13 +343,6 @@ public class LiveUpdatesFragment extends Fragment {
return fragment.getMyActivity().getMyApplication().getIconsCache() return fragment.getMyActivity().getMyApplication().getIconsCache()
.getPaintedContentIcon(drawable, secondaryColor); .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 public static class LoadLocalIndexTask
@ -417,52 +388,4 @@ public class LiveUpdatesFragment extends Fragment {
adapter.sort(); adapter.sort();
} }
} }
public static class PerformLiveUpdateAsyncTask
extends AsyncTask<String, Object, IncrementalChangesManager.IncrementalUpdateList> {
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<IncrementalChangesManager.IncrementalUpdate> 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);
}
} }

View file

@ -1,11 +1,12 @@
package net.osmand.plus.liveupdates; package net.osmand.plus.liveupdates;
import android.content.Context;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
import net.osmand.plus.activities.LocalIndexInfo; 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 { public class LiveUpdatesHelper {
private static final String UPDATE_TIMES_POSTFIX = "_update_times"; private 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 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()); 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 { public static enum TimesOfDay {
MORNING, MORNING,
NIGHT NIGHT

View file

@ -14,6 +14,8 @@ import android.support.v7.widget.SwitchCompat;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.TextView; import android.widget.TextView;
@ -21,7 +23,9 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings; 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.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 net.osmand.util.Algorithms;
import java.io.File; 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.TimesOfDay;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.UpdateFrequency; 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.preferenceDownloadViaWiFi;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceForLocalIndex; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceForLocalIndex;
import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceTimeOfDayToUpdate; 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); .inflate(R.layout.dialog_live_updates_item_settings, null);
final TextView regionNameTextView = (TextView) view.findViewById(R.id.regionNameTextView); final TextView regionNameTextView = (TextView) view.findViewById(R.id.regionNameTextView);
final TextView countryNameTextView = (TextView) view.findViewById(R.id.countryNameTextView); 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 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 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 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()); regionNameTextView.setText(getNameToDisplay(localIndexInfo, getMyActivity()));
// countryNameTextView.setText(localIndexInfo.getWorldRegion().getLocaleName()); final String fileNameWithoutExtension =
countryNameTextView.setVisibility(View.VISIBLE); 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<Boolean> liveUpdatePreference = final OsmandSettings.CommonPreference<Boolean> liveUpdatePreference =
preferenceForLocalIndex(localIndexInfo, getSettings()); preferenceForLocalIndex(localIndexInfo, getSettings());
final OsmandSettings.CommonPreference<Boolean> downloadViaWiFiPreference = final OsmandSettings.CommonPreference<Boolean> downloadViaWiFiPreference =
@ -72,6 +86,40 @@ public class LiveUpdatesSettingsDialogFragment extends DialogFragment {
liveUpdatesSwitch.setChecked(true); liveUpdatesSwitch.setChecked(true);
downloadOverWiFiSwitch.setChecked(downloadViaWiFiPreference.get()); 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) builder.setView(view)
.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() { .setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() {
@Override @Override
@ -126,35 +174,33 @@ public class LiveUpdatesSettingsDialogFragment extends DialogFragment {
.setNeutralButton(R.string.update_now, new DialogInterface.OnClickListener() { .setNeutralButton(R.string.update_now, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { 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(); 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) { private long getNextUpdateTime(TimesOfDay timeOfDayToUpdate) {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
if (timeOfDayToUpdate == TimesOfDay.MORNING) { if (timeOfDayToUpdate == TimesOfDay.MORNING) {
@ -176,7 +222,11 @@ public class LiveUpdatesSettingsDialogFragment extends DialogFragment {
} }
private OsmandApplication getMyApplication() { private OsmandApplication getMyApplication() {
return ((OsmandActionBarActivity) this.getActivity()).getMyApplication(); return getMyActivity().getMyApplication();
}
private AbstractDownloadActivity getMyActivity() {
return (AbstractDownloadActivity) this.getActivity();
} }
public static LiveUpdatesSettingsDialogFragment createInstance(LocalIndexInfo localIndexInfo) { public static LiveUpdatesSettingsDialogFragment createInstance(LocalIndexInfo localIndexInfo) {

View file

@ -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<String, Object, IncrementalChangesManager.IncrementalUpdateList> {
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<IncrementalChangesManager.IncrementalUpdate> 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);
}
}
}
}

View file

@ -119,7 +119,7 @@ public class IncrementalChangesManager {
} }
protected static String formatSize(long vl) { 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<IncrementalUpdate> list) { public static long calculateSize(List<IncrementalUpdate> list) {
@ -347,6 +347,18 @@ public class IncrementalChangesManager {
return size; return size;
} }
public void deleteUpdates(String fileName){
RegionUpdateFiles ruf = regions.get(fileName.toLowerCase());
for (List<RegionUpdate> 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) { public long getTimestamp(String fileName) {
RegionUpdateFiles ruf = regions.get(fileName.toLowerCase()); RegionUpdateFiles ruf = regions.get(fileName.toLowerCase());
return getTimestamp(ruf); return getTimestamp(ruf);