Update download

This commit is contained in:
Victor Shcherb 2013-05-26 15:04:04 +02:00
parent 85008c9c88
commit 0ae7340cb1
10 changed files with 516 additions and 322 deletions

View file

@ -43,7 +43,7 @@ public class RenderingRulesStorage {
public RenderingRuleStorageProperties PROPS = new RenderingRuleStorageProperties(); public RenderingRuleStorageProperties PROPS = new RenderingRuleStorageProperties();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected TIntObjectHashMap<RenderingRule>[] tagValueGlobalRules = new TIntObjectHashMap[LENGTH_RULES]; public TIntObjectHashMap<RenderingRule>[] tagValueGlobalRules = new TIntObjectHashMap[LENGTH_RULES];
protected Map<String, RenderingRule> renderingAttributes = new LinkedHashMap<String, RenderingRule>(); protected Map<String, RenderingRule> renderingAttributes = new LinkedHashMap<String, RenderingRule>();
protected Map<String, String> renderingConstants= new LinkedHashMap<String, String>(); protected Map<String, String> renderingConstants= new LinkedHashMap<String, String>();

View file

@ -12,10 +12,11 @@
android:orientation="vertical" android:orientation="vertical"
android:visibility="gone" > android:visibility="gone" >
<LinearLayout <RelativeLayout
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginRight="3dp" android:layout_marginLeft="3dp"
android:layout_marginRight="5dp"
android:gravity="center_vertical|left" android:gravity="center_vertical|left"
android:orientation="horizontal" > android:orientation="horizontal" >
@ -23,46 +24,52 @@
android:id="@+id/IndeterminateProgressBar" android:id="@+id/IndeterminateProgressBar"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:indeterminate="true" android:indeterminate="true"
android:indeterminateOnly="true" android:indeterminateOnly="true"
android:visibility="gone" /> android:visibility="gone" />
<TextView <TextView
android:id="@+id/ProgressMessage" android:id="@+id/ProgressPercent"
android:layout_width="wrap_content" android:layout_width="48dp"
android:layout_height="48dp" android:layout_alignParentRight="true"
android:layout_marginLeft="3dp" android:layout_height="wrap_content"
android:ellipsize="marquee" android:layout_centerVertical="true"
android:gravity="left|center_vertical" android:gravity="right|center_vertical"
android:singleLine="true"
android:text="" android:text=""
android:textSize="14sp" /> android:textSize="14sp" />
<TextView <TextView
android:id="@+id/ProgressPercent" android:id="@+id/ProgressMessage"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="48dp" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_centerVertical="true"
android:gravity="right|center_vertical" android:layout_marginLeft="3dp"
android:layout_toLeftOf="@id/ProgressPercent"
android:layout_toRightOf="@id/IndeterminateProgressBar"
android:ellipsize="end"
android:gravity="left|center_vertical"
android:singleLine="true"
android:text="" android:text=""
android:textSize="14sp" /> android:textSize="14sp" />
</LinearLayout> </RelativeLayout>
<LinearLayout <LinearLayout
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="3dp" android:layout_marginLeft="5dp"
android:layout_marginRight="3dp" android:layout_marginRight="5dp"
android:gravity="center_vertical" android:gravity="center_vertical"
android:orientation="horizontal" > android:orientation="horizontal" >
<ProgressBar <ProgressBar
android:id="@+id/DeterminateProgressBar" android:id="@+id/DeterminateProgressBar"
style="?android:attr/progressBarStyleHorizontal" style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageButton <ImageView
android:id="@+id/Cancel" android:id="@+id/Cancel"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View file

@ -1401,10 +1401,18 @@ public class OsmandSettings {
return OSMAND_THEME.get() == OSMAND_LIGHT_THEME; return OSMAND_THEME.get() == OSMAND_LIGHT_THEME;
} }
public boolean isLightContent(){ public boolean isLightContentMenu(){
return OSMAND_THEME.get() != OSMAND_DARK_THEME || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB; return OSMAND_THEME.get() != OSMAND_DARK_THEME || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB;
} }
public boolean isLightContent(){
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
return false;
}
return OSMAND_THEME.get() != OSMAND_DARK_THEME ;
}
public final CommonPreference<Boolean> FLUORESCENT_OVERLAYS = public final CommonPreference<Boolean> FLUORESCENT_OVERLAYS =
new BooleanPreference("fluorescent_overlays", false).makeGlobal().cache(); new BooleanPreference("fluorescent_overlays", false).makeGlobal().cache();

View file

@ -6,14 +6,13 @@ import java.io.FilenameFilter;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.access.AccessibleToast; import net.osmand.access.AccessibleToast;
import net.osmand.map.RegionCountry;
import net.osmand.map.RegionRegistry;
import net.osmand.plus.ClientContext; import net.osmand.plus.ClientContext;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandPlugin;
@ -28,21 +27,17 @@ import net.osmand.plus.download.DownloadIndexAdapter;
import net.osmand.plus.download.DownloadIndexesThread; import net.osmand.plus.download.DownloadIndexesThread;
import net.osmand.plus.download.IndexItem; import net.osmand.plus.download.IndexItem;
import net.osmand.plus.download.IndexItemCategory; import net.osmand.plus.download.IndexItemCategory;
import net.osmand.plus.download.SrtmIndexItem;
import net.osmand.plus.srtmplugin.SRTMPlugin; import net.osmand.plus.srtmplugin.SRTMPlugin;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.AlertDialog.Builder; import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.os.AsyncTask.Status; import android.os.AsyncTask.Status;
import android.os.Bundle; import android.os.Bundle;
import android.os.StatFs;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.view.View; import android.view.View;
@ -52,6 +47,7 @@ import android.widget.CheckBox;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ExpandableListAdapter; import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView; import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -74,20 +70,16 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
public static final String FILTER_KEY = "filter"; public static final String FILTER_KEY = "filter";
private static DownloadIndexesThread downloadListIndexThread; private static DownloadIndexesThread downloadListIndexThread;
private TreeMap<IndexItem, List<DownloadEntry>> entriesToDownload = new TreeMap<IndexItem, List<DownloadEntry>>();
private DownloadActivityType type = DownloadActivityType.NORMAL_FILE; private DownloadActivityType type = DownloadActivityType.NORMAL_FILE;
public static final int MAXIMUM_AVAILABLE_FREE_DOWNLOADS = 10;
private int MAXIMUM_AVAILABLE_FREE_DOWNLOADS = 10;
private TextWatcher textWatcher ; private TextWatcher textWatcher ;
private EditText filterText; private EditText filterText;
private OsmandSettings settings; private OsmandSettings settings;
private ArrayAdapter<String> spinnerAdapter; private ArrayAdapter<String> spinnerAdapter;
private List<SrtmIndexItem> cachedSRTMFiles;
private View progressView; private View progressView;
@ -95,6 +87,7 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
private ProgressBar determinateProgressBar; private ProgressBar determinateProgressBar;
private TextView progressMessage; private TextView progressMessage;
private TextView progressPercent; private TextView progressPercent;
private ImageView cancel;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -109,6 +102,16 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
progressView = findViewById(R.id.ProgressView); progressView = findViewById(R.id.ProgressView);
progressMessage = (TextView) findViewById(R.id.ProgressMessage); progressMessage = (TextView) findViewById(R.id.ProgressMessage);
progressPercent = (TextView) findViewById(R.id.ProgressPercent); progressPercent = (TextView) findViewById(R.id.ProgressPercent);
cancel = (ImageView) findViewById(R.id.Cancel);
int d = settings.isLightContent() ? R.drawable.a_1_navigation_cancel_small_light : R.drawable.a_1_navigation_cancel_small_dark;
cancel.setImageDrawable(getResources().getDrawable(d));
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
makeSureUserCancelDownload();
}
});
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setTitle(R.string.local_index_download); getSupportActionBar().setTitle(R.string.local_index_download);
// recreation upon rotation is prevented in manifest file // recreation upon rotation is prevented in manifest file
@ -116,7 +119,7 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
downloadFilesCheckFreeVersion(flattenDownloadEntries()); downloadFilesCheckFreeVersion();
} }
}); });
@ -152,7 +155,7 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
List<IndexItem> list = new ArrayList<IndexItem>(); List<IndexItem> list = new ArrayList<IndexItem>();
downloadListIndexThread.setUiActivity(this); downloadListIndexThread.setUiActivity(this);
if (downloadListIndexThread.getCachedIndexFiles() != null && downloadListIndexThread.isDownloadedFromInternet()) { if (downloadListIndexThread.getCachedIndexFiles() != null && downloadListIndexThread.isDownloadedFromInternet()) {
list = getFilteredByType(); downloadListIndexThread.runCategorization(type);
} else { } else {
downloadListIndexThread.runReloadIndexFiles(); downloadListIndexThread.runReloadIndexFiles();
} }
@ -185,7 +188,14 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
updateProgress(false, downloadListIndexThread.getCurrentRunningTask()); BasicProgressAsyncTask<?, ?, ?> t = downloadListIndexThread.getCurrentRunningTask();
updateProgress(false);
if(t instanceof DownloadIndexesThread.DownloadIndexesAsyncTask) {
View mainView = findViewById(R.id.MainLayout);
if (mainView != null) {
mainView.setKeepScreenOn(true);
}
}
} }
@ -214,17 +224,6 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
} }
public void updateLoadedFiles() {
if (type == DownloadActivityType.SRTM_FILE) {
if (cachedSRTMFiles != null) {
for (SrtmIndexItem i : cachedSRTMFiles) {
((SrtmIndexItem) i).updateExistingTiles(getMyApplication().getResourceManager().getIndexFileNames());
}
}
((DownloadIndexAdapter) getExpandableListAdapter()).notifyDataSetInvalidated();
}
}
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
@ -261,26 +260,23 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
return type; return type;
} }
public List<DownloadEntry> flattenDownloadEntries() { public Map<IndexItem, List<DownloadEntry>> getEntriesToDownload() {
List<DownloadEntry> res = new ArrayList<DownloadEntry>(); if(downloadListIndexThread == null) {
for(List<DownloadEntry> ens : entriesToDownload.values()) { return new LinkedHashMap<IndexItem, List<DownloadEntry>>();
if(ens != null) {
res.addAll(ens);
}
} }
return res; return downloadListIndexThread.getEntriesToDownload();
} }
public TreeMap<IndexItem, List<DownloadEntry>> getEntriesToDownload() { public String getFilterText() {
return entriesToDownload; return filterText.getText().toString();
} }
public void deselectAll() {
private void deselectAll() {
final DownloadIndexAdapter listAdapter = (DownloadIndexAdapter)getExpandableListAdapter(); final DownloadIndexAdapter listAdapter = (DownloadIndexAdapter)getExpandableListAdapter();
entriesToDownload.clear(); downloadListIndexThread.getEntriesToDownload().clear();
listAdapter.notifyDataSetInvalidated(); listAdapter.notifyDataSetInvalidated();
findViewById(R.id.DownloadButton).setVisibility(View.GONE); findViewById(R.id.DownloadButton).setVisibility(View.GONE);
} }
@ -305,35 +301,53 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
for (int j = 0; j < listAdapter.getGroupCount(); j++) { for (int j = 0; j < listAdapter.getGroupCount(); j++) {
for (int i = 0; i < listAdapter.getChildrenCount(j); i++) { for (int i = 0; i < listAdapter.getChildrenCount(j); i++) {
IndexItem es = listAdapter.getChild(j, i); IndexItem es = listAdapter.getChild(j, i);
if (!entriesToDownload.containsKey(es)) { if (!getEntriesToDownload().containsKey(es)) {
selected++; selected++;
entriesToDownload.put(es, es.createDownloadEntry(getClientContext(), type, new ArrayList<DownloadEntry>(1))); getEntriesToDownload().put(es, es.createDownloadEntry(getClientContext(), type, new ArrayList<DownloadEntry>(1)));
} }
} }
} }
AccessibleToast.makeText(this, MessageFormat.format(getString(R.string.items_were_selected), selected), Toast.LENGTH_SHORT).show(); AccessibleToast.makeText(this, MessageFormat.format(getString(R.string.items_were_selected), selected), Toast.LENGTH_SHORT).show();
listAdapter.notifyDataSetInvalidated(); listAdapter.notifyDataSetInvalidated();
if(selected > 0){ if(selected > 0){
makeDownloadVisible(); updateDownloadButton(true);
} }
} }
public void makeDownloadVisible() { public void updateDownloadButton(boolean scroll) {
findViewById(R.id.DownloadButton).setVisibility(View.VISIBLE); int x = getListView().getScrollX();
if(Version.isFreeVersion(getMyApplication())) { int y = getListView().getScrollY();
int left = MAXIMUM_AVAILABLE_FREE_DOWNLOADS - settings.NUMBER_OF_FREE_DOWNLOADS.get() - entriesToDownload.size(); if (getEntriesToDownload().isEmpty()) {
boolean excessLimit = left < 0; findViewById(R.id.DownloadButton).setVisibility(View.GONE);
if(left < 0) left = 0; } else {
String t = getString(R.string.download_files); BasicProgressAsyncTask<?, ?, ?> task = downloadListIndexThread.getCurrentRunningTask();
if (getType() != DownloadActivityType.HILLSHADE_FILE || getType() != DownloadActivityType.SRTM_FILE) { boolean running = task instanceof DownloadIndexesThread.DownloadIndexesAsyncTask;
t += " (" +(excessLimit?"! ":"") + getString(R.string.files_limit, left).toLowerCase() + ")"; ((Button) findViewById(R.id.DownloadButton)).setEnabled(!running);
String text;
int downloads = downloadListIndexThread. getDownloads();
if (!running) {
text = getString(R.string.download_files) + " " + downloads;
} else {
text = getString(R.string.downloading_file_new) + " " + downloads;
} }
((Button) findViewById(R.id.DownloadButton)).setText(t); findViewById(R.id.DownloadButton).setVisibility(View.VISIBLE);
if (Version.isFreeVersion(getMyApplication())) {
int left = MAXIMUM_AVAILABLE_FREE_DOWNLOADS - settings.NUMBER_OF_FREE_DOWNLOADS.get() - downloads;
boolean excessLimit = left < 0;
if (left < 0)
left = 0;
if (getType() != DownloadActivityType.HILLSHADE_FILE || getType() != DownloadActivityType.SRTM_FILE) {
text += " (" + (excessLimit ? "! " : "") + getString(R.string.files_limit, left).toLowerCase() + ")";
}
}
((Button) findViewById(R.id.DownloadButton)).setText(text);
}
if (scroll) {
getListView().scrollTo(x, y);
} }
} }
public void selectDownloadType() { public void selectDownloadType() {
Builder bld = new AlertDialog.Builder(this); Builder bld = new AlertDialog.Builder(this);
final DownloadActivityType[] items = getDownloadTypes(); final DownloadActivityType[] items = getDownloadTypes();
@ -383,81 +397,11 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
public void changeType(final DownloadActivityType tp) { public void changeType(final DownloadActivityType tp) {
if (downloadListIndexThread != null && type != tp) { if (downloadListIndexThread != null && type != tp) {
type = tp; type = tp;
AsyncTask<Void, Void, List<IndexItem>> t = new AsyncTask<Void, Void, List<IndexItem>>(){ downloadListIndexThread.runCategorization(tp);
private List<IndexItemCategory> cats;
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(DownloadIndexActivity.this,
getString(R.string.downloading), getString(R.string.downloading_list_indexes));
}
@Override
protected List<IndexItem> doInBackground(Void... params) {
final List<IndexItem> filtered = getFilteredByType();
cats = IndexItemCategory.categorizeIndexItems(getClientContext(), filtered);
return filtered;
}
@Override
protected void onPostExecute(List<IndexItem> filtered) {
entriesToDownload.clear();
DownloadIndexAdapter a = ((DownloadIndexAdapter) getExpandableListAdapter());
// Strange null pointer fix (reproduce?)
if (a != null) {
a.setIndexFiles(filtered, cats);
a.notifyDataSetChanged();
a.getFilter().filter(filterText.getText());
}
progressDialog.dismiss();
}
};
t.execute();
} }
} }
public List<IndexItem> getFilteredByType() {
final List<IndexItem> filtered = new ArrayList<IndexItem>();
if (type == DownloadActivityType.SRTM_FILE) {
Map<String, String> indexFileNames = getMyApplication().getResourceManager().getIndexFileNames();
if (cachedSRTMFiles == null) {
cachedSRTMFiles = new ArrayList<SrtmIndexItem>();
List<RegionCountry> countries = RegionRegistry.getRegionRegistry().getCountries();
for (RegionCountry rc : countries) {
if (rc.tiles.size() > 35 && rc.getSubRegions().size() > 0) {
for (RegionCountry ch : rc.getSubRegions()) {
cachedSRTMFiles.add(new SrtmIndexItem(ch, indexFileNames));
}
} else {
cachedSRTMFiles.add(new SrtmIndexItem(rc, indexFileNames));
}
}
filtered.addAll(cachedSRTMFiles);
} else {
for (SrtmIndexItem s : cachedSRTMFiles) {
s.updateExistingTiles(indexFileNames);
filtered.add(s);
}
}
}
List<IndexItem> cachedIndexFiles = downloadListIndexThread.getCachedIndexFiles();
if (cachedIndexFiles != null) {
for (IndexItem file : cachedIndexFiles) {
if (file.getType() == type) {
filtered.add(file);
}
}
}
return filtered;
}
public ExpandableListView getListView() { public ExpandableListView getListView() {
return super.getExpandableListView(); return super.getExpandableListView();
} }
@ -466,15 +410,18 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
return super.getExpandableListAdapter(); return super.getExpandableListAdapter();
} }
// TODO private void makeSureUserCancelDownload() {
private void makeSureUserCancelDownload(final DialogInterface dlg) {
Builder bld = new AlertDialog.Builder(this); Builder bld = new AlertDialog.Builder(this);
bld.setTitle(getString(R.string.default_buttons_cancel)); bld.setTitle(getString(R.string.default_buttons_cancel));
bld.setMessage(R.string.confirm_interrupt_download); bld.setMessage(R.string.confirm_interrupt_download);
bld.setPositiveButton(R.string.default_buttons_yes, new OnClickListener() { bld.setPositiveButton(R.string.default_buttons_yes, new OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
dlg.dismiss(); dialog.dismiss();
BasicProgressAsyncTask<?, ?, ?> t = downloadListIndexThread.getCurrentRunningTask();
if(t != null) {
t.setInterrupted(true);
}
} }
}); });
bld.setNegativeButton(R.string.default_buttons_no, null); bld.setNegativeButton(R.string.default_buttons_no, null);
@ -489,15 +436,8 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
if(ch.isChecked()){ if(ch.isChecked()){
ch.setChecked(!ch.isChecked()); ch.setChecked(!ch.isChecked());
entriesToDownload.remove(e); getEntriesToDownload().remove(e);
if(entriesToDownload.isEmpty()){ updateDownloadButton(true);
int x = getListView().getScrollX();
int y = getListView().getScrollY();
findViewById(R.id.DownloadButton).setVisibility(View.GONE);
getListView().scrollTo(x, y);
} else {
makeDownloadVisible();
}
return true; return true;
} }
@ -505,11 +445,8 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
if (download.size() > 0) { if (download.size() > 0) {
// if(!fileToUnzip.exists()){ // if(!fileToUnzip.exists()){
// builder.setMessage(MessageFormat.format(getString(R.string.download_question), baseName, extractDateAndSize(e.getValue()))); // builder.setMessage(MessageFormat.format(getString(R.string.download_question), baseName, extractDateAndSize(e.getValue())));
entriesToDownload.put(e, download); getEntriesToDownload().put(e, download);
int x = getListView().getScrollX(); updateDownloadButton(true);
int y = getListView().getScrollY();
makeDownloadVisible();
getListView().scrollTo(x, y);
ch.setChecked(!ch.isChecked()); ch.setChecked(!ch.isChecked());
} }
return true; return true;
@ -548,105 +485,80 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
return files; return files;
} }
protected void downloadFilesCheckFreeVersion(List<DownloadEntry> list) { protected void downloadFilesCheckFreeVersion() {
if (Version.isFreeVersion(getMyApplication()) ) { if (Version.isFreeVersion(getMyApplication()) ) {
int total = settings.NUMBER_OF_FREE_DOWNLOADS.get(); int total = settings.NUMBER_OF_FREE_DOWNLOADS.get();
boolean wiki = false; boolean wiki = false;
for (DownloadEntry es : list) { for (IndexItem es : downloadListIndexThread.getEntriesToDownload().keySet()) {
if (es.baseName != null && es.baseName.contains("_wiki")) { if (es.getBasename() != null && es.getBasename().contains("_wiki")) {
wiki = true; wiki = true;
break; break;
} else if (es.type != DownloadActivityType.SRTM_FILE) { } else if (es.getType() != DownloadActivityType.SRTM_FILE) {
total++; total++;
} }
} }
if (total > MAXIMUM_AVAILABLE_FREE_DOWNLOADS || wiki) { if (total > MAXIMUM_AVAILABLE_FREE_DOWNLOADS || wiki) {
String msgTx = getString(R.string.free_version_message, MAXIMUM_AVAILABLE_FREE_DOWNLOADS + "", "(" + total + ") "); String msgTx = getString(R.string.free_version_message, MAXIMUM_AVAILABLE_FREE_DOWNLOADS + "");
Builder msg = new AlertDialog.Builder(this); Builder msg = new AlertDialog.Builder(this);
msg.setTitle(R.string.free_version_title); msg.setTitle(R.string.free_version_title);
msg.setMessage(msgTx); msg.setMessage(msgTx);
msg.setPositiveButton(R.string.default_buttons_ok, null); msg.setPositiveButton(R.string.default_buttons_ok, null);
msg.show(); msg.show();
} else { } else {
downloadFilesPreCheckSRTM( list); downloadFilesCheckInternet();
} }
} else { } else {
downloadFilesPreCheckSRTM( list); downloadFilesCheckInternet();
} }
} }
protected void downloadFilesPreCheckSRTM(final List<DownloadEntry> list) {
if (type == DownloadActivityType.SRTM_FILE &&
OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) instanceof SRTMPlugin &&
!OsmandPlugin.getEnabledPlugin(SRTMPlugin.class).isPaid()) {
Builder msg = new AlertDialog.Builder(this);
msg.setTitle(R.string.srtm_paid_version_title);
msg.setMessage(getString(R.string.srtm_paid_version_msg));
msg.setPositiveButton(R.string.default_buttons_ok, new OnClickListener() {
@Override protected void downloadFilesCheckInternet() {
public void onClick(DialogInterface dialog, int which) {
downloadFilesCheckInternet(list);
}
});
msg.show();
} else {
downloadFilesCheckInternet(list);
}
}
protected void downloadFilesCheckInternet(final List<DownloadEntry> list) {
if(!getMyApplication().getExternalServiceAPI().isWifiConnected()) { if(!getMyApplication().getExternalServiceAPI().isWifiConnected()) {
Builder builder = new AlertDialog.Builder(this); Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.download_using_mobile_internet)); builder.setMessage(getString(R.string.download_using_mobile_internet));
builder.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() { builder.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
downloadFilesPreCheckSpace(list); downloadFilesPreCheckSpace();
} }
}); });
builder.setNegativeButton(R.string.default_buttons_no, null); builder.setNegativeButton(R.string.default_buttons_no, null);
builder.show(); builder.show();
} else { } else {
downloadFilesPreCheckSpace(list); downloadFilesPreCheckSpace();
} }
} }
protected void downloadFilesPreCheckSpace(List<DownloadEntry> list) { protected void downloadFilesPreCheckSpace() {
double sz = 0; double sz = 0;
for(DownloadEntry es : list){ List<DownloadEntry> list = downloadListIndexThread.flattenDownloadEntries();
for (DownloadEntry es : list) {
sz += es.sizeMB; sz += es.sizeMB;
} }
// get availabile space // get availabile space
File dir = getMyApplication().getAppPath("").getParentFile(); double asz = downloadListIndexThread.getAvailableSpace();
double asz = -1; if (asz != -1 && asz > 0 && sz / asz > 0.4) {
if(dir.canRead()){
StatFs fs = new StatFs(dir.getAbsolutePath());
asz = (((long) fs.getAvailableBlocks()) * fs.getBlockSize()) / (1 << 20);
}
if(asz != -1 && asz < sz ){
AccessibleToast.makeText(this, getString(R.string.download_files_not_enough_space, sz, asz), Toast.LENGTH_LONG).show();
} else {
Builder builder = new AlertDialog.Builder(this); Builder builder = new AlertDialog.Builder(this);
if (asz > 0 && sz/asz > 0.4) { builder.setMessage(MessageFormat.format(getString(R.string.download_files_question_space), list.size(), sz, asz));
builder.setMessage(MessageFormat.format(getString(R.string.download_files_question_space), list.size(), sz,
asz));
} else {
builder.setMessage(MessageFormat.format(getString(R.string.download_files_question), list.size(), sz));
}
builder.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() { builder.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
downloadListIndexThread.runDownloadFiles(entriesToDownload); downloadListIndexThread.runDownloadFiles();
} }
}); });
builder.setNegativeButton(R.string.default_buttons_no, null); builder.setNegativeButton(R.string.default_buttons_no, null);
builder.show(); builder.show();
} else {
downloadListIndexThread.runDownloadFiles();
} }
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
@ -664,7 +576,8 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
} }
public void updateProgress(boolean updateOnlyProgress, BasicProgressAsyncTask<?, ?, ?> basicProgressAsyncTask) { public void updateProgress(boolean updateOnlyProgress) {
BasicProgressAsyncTask<?, ?, ?> basicProgressAsyncTask = downloadListIndexThread.getCurrentRunningTask();
if(updateOnlyProgress) { if(updateOnlyProgress) {
if(!basicProgressAsyncTask.isIndeterminate()) { if(!basicProgressAsyncTask.isIndeterminate()) {
progressPercent.setText(basicProgressAsyncTask.getProgressPercentage() +"%"); progressPercent.setText(basicProgressAsyncTask.getProgressPercentage() +"%");
@ -677,6 +590,7 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
boolean indeterminate = basicProgressAsyncTask.isIndeterminate(); boolean indeterminate = basicProgressAsyncTask.isIndeterminate();
indeterminateProgressBar.setVisibility(!indeterminate ? View.GONE : View.VISIBLE); indeterminateProgressBar.setVisibility(!indeterminate ? View.GONE : View.VISIBLE);
determinateProgressBar.setVisibility(indeterminate ? View.GONE : View.VISIBLE); determinateProgressBar.setVisibility(indeterminate ? View.GONE : View.VISIBLE);
cancel.setVisibility(indeterminate ? View.GONE : View.VISIBLE);
progressPercent.setVisibility(indeterminate ? View.GONE : View.VISIBLE); progressPercent.setVisibility(indeterminate ? View.GONE : View.VISIBLE);
progressMessage.setText(basicProgressAsyncTask.getDescription()); progressMessage.setText(basicProgressAsyncTask.getDescription());
@ -685,6 +599,7 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
determinateProgressBar.setProgress(basicProgressAsyncTask.getProgressPercentage()); determinateProgressBar.setProgress(basicProgressAsyncTask.getProgressPercentage());
} }
} }
updateDownloadButton(false);
} }
} }
} }

View file

@ -516,7 +516,7 @@ public class MapActivityActions implements DialogProvider {
final TargetPointsHelper targets = getTargets(); final TargetPointsHelper targets = getTargets();
View view = mapActivity.getLayoutInflater().inflate(R.layout.calculate_route, null); View view = mapActivity.getLayoutInflater().inflate(R.layout.calculate_route, null);
boolean lc = mapActivity.getMyApplication().getSettings().isLightContent(); boolean lc = mapActivity.getMyApplication().getSettings().isLightContentMenu();
final CheckBox nonoptimal = (CheckBox) view.findViewById(R.id.OptimalCheckox); final CheckBox nonoptimal = (CheckBox) view.findViewById(R.id.OptimalCheckox);
final ToggleButton[] buttons = new ToggleButton[ApplicationMode.values().length]; final ToggleButton[] buttons = new ToggleButton[ApplicationMode.values().length];
buttons[ApplicationMode.CAR.ordinal()] = (ToggleButton) view.findViewById(R.id.CarButton); buttons[ApplicationMode.CAR.ordinal()] = (ToggleButton) view.findViewById(R.id.CarButton);
@ -982,7 +982,7 @@ public class MapActivityActions implements DialogProvider {
final OsmandMapTileView mapView = mapActivity.getMapView(); final OsmandMapTileView mapView = mapActivity.getMapView();
final OsmandApplication app = mapActivity.getMyApplication(); final OsmandApplication app = mapActivity.getMyApplication();
ContextMenuAdapter optionsMenuHelper = new ContextMenuAdapter(app); ContextMenuAdapter optionsMenuHelper = new ContextMenuAdapter(app);
boolean light = app.getSettings().isLightContent(); boolean light = app.getSettings().isLightContentMenu();
// 1. Where am I // 1. Where am I
optionsMenuHelper.registerItem(R.string.where_am_i, optionsMenuHelper.registerItem(R.string.where_am_i,

View file

@ -18,6 +18,7 @@ public class DownloadEntry {
public int parts; public int parts;
public File existingBackupFile; public File existingBackupFile;
public boolean isAsset; public boolean isAsset;
public String assetName;
public DownloadActivityType type; public DownloadActivityType type;
public List<String> srtmFilesToDownload; public List<String> srtmFilesToDownload;
@ -30,6 +31,7 @@ public class DownloadEntry {
public DownloadEntry(String assetName, String fileName, long dateModified) { public DownloadEntry(String assetName, String fileName, long dateModified) {
this.dateModified = dateModified; this.dateModified = dateModified;
targetFile = new File(fileName); targetFile = new File(fileName);
this.assetName = assetName;
isAsset = true; isAsset = true;
} }

View file

@ -40,6 +40,10 @@ public class DownloadFileHelper {
public void showWarning(String warning); public void showWarning(String warning);
} }
public static boolean isInterruptedException(IOException e) {
return e != null && e.getMessage().equals("Interrupted");
}
private InputStream getInputStreamToDownload(final URL url, final boolean forceWifi) throws IOException { private InputStream getInputStreamToDownload(final URL url, final boolean forceWifi) throws IOException {
InputStream cis = new InputStream() { InputStream cis = new InputStream() {
byte[] buffer = new byte[BUFFER_SIZE]; byte[] buffer = new byte[BUFFER_SIZE];
@ -110,27 +114,20 @@ public class DownloadFileHelper {
@Override @Override
public int read(byte[] buffer, int offset, int len) throws IOException { public int read(byte[] buffer, int offset, int len) throws IOException {
int r = 0; if (bufLen == -1) {
while(len > 0) { return -1;
if(bufLen == -1) {
return -1;
}
int av = bufLen - bufRead;
if(len > av){
System.arraycopy(this.buffer, bufRead, buffer, offset, av);
len -= av;
offset += av;
bufRead += av;
r += av;
refillBuffer();
} else {
System.arraycopy(this.buffer, bufRead, buffer, offset, len);
bufRead += len;
r += len;
return r;
}
} }
return r; if (bufRead >= bufLen) {
refillBuffer();
}
if (bufLen == -1) {
return -1;
}
int av = bufLen - bufRead;
int min = Math.min(len, av);
System.arraycopy(this.buffer, bufRead, buffer, offset, min);
bufRead += min;
return min;
} }
@Override @Override
@ -159,18 +156,22 @@ public class DownloadFileHelper {
readAgain = false; readAgain = false;
bufRead = 0; bufRead = 0;
if ((bufLen = is.read(buffer)) != -1) { if ((bufLen = is.read(buffer)) != -1) {
if (interruptDownloading) {
throw new IOException("Interrupted");
}
fileread += bufLen; fileread += bufLen;
if (interruptDownloading) {
break;
}
} }
} catch (IOException e) { } catch (IOException e) {
if(interruptDownloading)
log.error("IOException", e); //$NON-NLS-1$ log.error("IOException", e); //$NON-NLS-1$
triesDownload--; triesDownload--;
reconnect(); reconnect();
readAgain = true; readAgain = true;
} }
} }
if (interruptDownloading) {
throw new IOException("Interrupted");
}
} }
@Override @Override
@ -210,7 +211,7 @@ public class DownloadFileHelper {
} }
} }
de.fileToDownload = de.targetFile; de.fileToDownload = de.targetFile;
if(de.targetFile.exists() && !de.unzipFolder) { if(!de.unzipFolder) {
de.fileToDownload = new File(de.targetFile.getParentFile(), de.targetFile.getName() +".download"); de.fileToDownload = new File(de.targetFile.getParentFile(), de.targetFile.getName() +".download");
} }
unzipFile(de, progress, downloadInputStreams); unzipFile(de, progress, downloadInputStreams);
@ -235,9 +236,14 @@ public class DownloadFileHelper {
} }
private void unzipFile(DownloadEntry de, IProgress progress, List<InputStream> is) throws IOException { private void unzipFile(DownloadEntry de, IProgress progress, List<InputStream> is) throws IOException {
String taskName = ctx.getString(R.string.downloading_file_new) + " " + de.baseName;
CountingMultiInputStream fin = new CountingMultiInputStream(is); CountingMultiInputStream fin = new CountingMultiInputStream(is);
int len = (int) fin.available(); int len = (int) fin.available();
int mb = (int) (len / (1024f*1024f));
if(mb == 0) {
mb = 1;
}
String taskName = ctx.getString(R.string.downloading_file_new) + " " + de.baseName /*+ " " + mb + " MB"*/;
progress.startTask(taskName, len / 1024); progress.startTask(taskName, len / 1024);
if (!de.zipStream) { if (!de.zipStream) {
copyFile(de, progress, fin, len, fin, de.fileToDownload); copyFile(de, progress, fin, len, fin, de.fileToDownload);

View file

@ -4,13 +4,23 @@ package net.osmand.plus.download;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.access.AccessibleToast; import net.osmand.access.AccessibleToast;
import net.osmand.map.RegionCountry;
import net.osmand.map.RegionRegistry;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.OsmandSettings.OsmandPreference; import net.osmand.plus.OsmandSettings.OsmandPreference;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.Version; import net.osmand.plus.Version;
@ -18,28 +28,36 @@ import net.osmand.plus.activities.DownloadIndexActivity;
import net.osmand.plus.base.BasicProgressAsyncTask; import net.osmand.plus.base.BasicProgressAsyncTask;
import net.osmand.plus.download.DownloadFileHelper.DownloadFileShowWarning; import net.osmand.plus.download.DownloadFileHelper.DownloadFileShowWarning;
import net.osmand.plus.resources.ResourceManager; import net.osmand.plus.resources.ResourceManager;
import net.osmand.plus.srtmplugin.SRTMPlugin;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.os.AsyncTask.Status; import android.os.AsyncTask.Status;
import android.os.Build;
import android.os.StatFs;
import android.view.View; import android.view.View;
import android.widget.Toast; import android.widget.Toast;
public class DownloadIndexesThread { public class DownloadIndexesThread {
private DownloadIndexActivity uiActivity = null; private DownloadIndexActivity uiActivity = null;
private IndexFileList indexFiles = null; private IndexFileList indexFiles = null;
private List<SrtmIndexItem> cachedSRTMFiles;
private Map<IndexItem, List<DownloadEntry>> entriesToDownload = Collections.synchronizedMap(new TreeMap<IndexItem, List<DownloadEntry>>());
private Set<DownloadEntry> currentDownloads = new HashSet<DownloadEntry>();
private final Context ctx; private final Context ctx;
private OsmandApplication app; private OsmandApplication app;
private final static Log log = PlatformUtil.getLog(DownloadIndexesThread.class); private final static Log log = PlatformUtil.getLog(DownloadIndexesThread.class);
private DownloadFileHelper downloadFileHelper; private DownloadFileHelper downloadFileHelper;
private BasicProgressAsyncTask<?, ?, ?> currentRunningTask; private List<BasicProgressAsyncTask<?, ?, ?> > currentRunningTask = Collections.synchronizedList(new ArrayList<BasicProgressAsyncTask<?, ?, ?>>());
public DownloadIndexesThread(Context ctx) { public DownloadIndexesThread(Context ctx) {
this.ctx = ctx; this.ctx = ctx;
@ -51,6 +69,16 @@ public class DownloadIndexesThread {
this.uiActivity = uiActivity; this.uiActivity = uiActivity;
} }
public List<DownloadEntry> flattenDownloadEntries() {
List<DownloadEntry> res = new ArrayList<DownloadEntry>();
for(List<DownloadEntry> ens : getEntriesToDownload().values()) {
if(ens != null) {
res.addAll(ens);
}
}
return res;
}
public List<IndexItem> getCachedIndexFiles() { public List<IndexItem> getCachedIndexFiles() {
return indexFiles != null ? indexFiles.getIndexFiles() : null; return indexFiles != null ? indexFiles.getIndexFiles() : null;
} }
@ -62,25 +90,33 @@ public class DownloadIndexesThread {
public class DownloadIndexesAsyncTask extends BasicProgressAsyncTask<IndexItem, Object, String> implements DownloadFileShowWarning { public class DownloadIndexesAsyncTask extends BasicProgressAsyncTask<IndexItem, Object, String> implements DownloadFileShowWarning {
private OsmandPreference<Integer> downloads; private OsmandPreference<Integer> downloads;
private TreeMap<IndexItem, List<DownloadEntry>> entriesToDownload;
public DownloadIndexesAsyncTask(Context ctx, TreeMap<IndexItem, List<DownloadEntry>> entriesToDownload) { public DownloadIndexesAsyncTask(Context ctx) {
super(ctx); super(ctx);
this.entriesToDownload = entriesToDownload;
downloads = app.getSettings().NUMBER_OF_FREE_DOWNLOADS; downloads = app.getSettings().NUMBER_OF_FREE_DOWNLOADS;
} }
@Override
public void setInterrupted(boolean interrupted) {
super.setInterrupted(interrupted);
if(interrupted) {
downloadFileHelper.setInterruptDownloading(true);
}
}
@Override @Override
protected void onProgressUpdate(Object... values) { protected void onProgressUpdate(Object... values) {
for (Object o : values) { for (Object o : values) {
if (o instanceof DownloadEntry) { if (o instanceof DownloadEntry) {
if (uiActivity != null) { if (uiActivity != null) {
((DownloadIndexAdapter) uiActivity.getExpandableListAdapter()).notifyDataSetInvalidated(); ((DownloadIndexAdapter) uiActivity.getExpandableListAdapter()).notifyDataSetInvalidated();
if (entriesToDownload.isEmpty()) { uiActivity.updateDownloadButton(false);
uiActivity.findViewById(R.id.DownloadButton).setVisibility(View.GONE); }
} else { } else if (o instanceof IndexItem) {
uiActivity.makeDownloadVisible(); entriesToDownload.remove(o);
} if (uiActivity != null) {
((DownloadIndexAdapter) uiActivity.getExpandableListAdapter()).notifyDataSetInvalidated();
uiActivity.updateDownloadButton(false);
} }
} else if (o instanceof String) { } else if (o instanceof String) {
AccessibleToast.makeText(ctx, (String) o, Toast.LENGTH_LONG).show(); AccessibleToast.makeText(ctx, (String) o, Toast.LENGTH_LONG).show();
@ -91,7 +127,7 @@ public class DownloadIndexesThread {
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
currentRunningTask = this; currentRunningTask.add( this);
super.onPreExecute(); super.onPreExecute();
if (uiActivity != null) { if (uiActivity != null) {
downloadFileHelper.setInterruptDownloading(false); downloadFileHelper.setInterruptDownloading(false);
@ -99,39 +135,39 @@ public class DownloadIndexesThread {
if (mainView != null) { if (mainView != null) {
mainView.setKeepScreenOn(true); mainView.setKeepScreenOn(true);
} }
startTask(ctx.getString(R.string.downloading), -1);
} }
} }
@Override @Override
protected void onPostExecute(String result) { protected void onPostExecute(String result) {
if (result != null) { if (result != null && result.length() > 0) {
AccessibleToast.makeText(ctx, result, Toast.LENGTH_LONG).show(); AccessibleToast.makeText(ctx, result, Toast.LENGTH_LONG).show();
} }
currentDownloads.clear();
if (uiActivity != null) { if (uiActivity != null) {
View mainView = uiActivity.findViewById(R.id.MainLayout); View mainView = uiActivity.findViewById(R.id.MainLayout);
if (mainView != null) { if (mainView != null) {
mainView.setKeepScreenOn(false); mainView.setKeepScreenOn(false);
} }
uiActivity.updateLoadedFiles(); updateLoadedFiles();
DownloadIndexAdapter adapter = ((DownloadIndexAdapter) uiActivity.getExpandableListAdapter()); DownloadIndexAdapter adapter = ((DownloadIndexAdapter) uiActivity.getExpandableListAdapter());
if (adapter != null) { if (adapter != null) {
adapter.notifyDataSetInvalidated(); adapter.notifyDataSetInvalidated();
} }
} }
currentRunningTask.remove(this);
if(uiActivity != null) { if(uiActivity != null) {
uiActivity.updateProgress(false, null); uiActivity.updateProgress(false);
} }
} }
private int countAllDownloadEntry(IndexItem... filesToDownload){
int t = 0; private void updateLoadedFiles() {
for(IndexItem i : filesToDownload){ if (uiActivity != null) {
List<DownloadEntry> list = entriesToDownload.get(i); ((DownloadIndexAdapter) uiActivity.getExpandableListAdapter()).notifyDataSetInvalidated();
if(list != null){ ((DownloadIndexAdapter) uiActivity.getExpandableListAdapter()).updateLoadedFiles();
t += list.size();
}
} }
return t;
} }
@Override @Override
@ -139,18 +175,49 @@ public class DownloadIndexesThread {
try { try {
List<File> filesToReindex = new ArrayList<File>(); List<File> filesToReindex = new ArrayList<File>();
boolean forceWifi = downloadFileHelper.isWifiConnected(); boolean forceWifi = downloadFileHelper.isWifiConnected();
int counter = 1; currentDownloads = new HashSet<DownloadEntry>();
int all = countAllDownloadEntry(filesToDownload); String breakDownloadMessage = null;
for (int i = 0; i < filesToDownload.length; i++) { downloadCycle : while(!entriesToDownload.isEmpty() ) {
IndexItem filename = filesToDownload[i];
List<DownloadEntry> list = entriesToDownload.get(filename); Iterator<Entry<IndexItem, List<DownloadEntry>>> it = entriesToDownload.entrySet().iterator();
IndexItem file = null;
List<DownloadEntry> list = null;
while(it.hasNext()) {
Entry<IndexItem, List<DownloadEntry>> n = it.next();
if(!currentDownloads.containsAll(n.getValue())) {
file = n.getKey();
list = n.getValue();
break;
}
}
if(file == null) {
break downloadCycle;
}
if (list != null) { if (list != null) {
boolean success = false;
for (DownloadEntry entry : list) { for (DownloadEntry entry : list) {
String indexOfAllFiles = all <= 1 ? "" : (" [" + counter + "/" + all + "]"); if(currentDownloads.contains(entry)) {
counter++; continue;
boolean result = downloadFile(entry, filesToReindex, indexOfAllFiles, forceWifi); }
currentDownloads.add(entry);
double asz = getAvailableSpace();
// validate interrupted
if(downloadFileHelper.isInterruptDownloading()) {
break downloadCycle;
}
// validate enough space
if (asz != -1 && entry.sizeMB > asz) {
breakDownloadMessage = app.getString(R.string.download_files_not_enough_space, entry.sizeMB, asz);
break downloadCycle;
}
if (exceedsFreelimit(entry)) {
breakDownloadMessage = app.getString(R.string.free_version_message, DownloadIndexActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS
+ "");
break downloadCycle;
}
boolean result = downloadFile(entry, filesToReindex, forceWifi);
success = result || success;
if (result) { if (result) {
entriesToDownload.remove(filename);
if (entry.type != DownloadActivityType.SRTM_FILE && entry.type != DownloadActivityType.HILLSHADE_FILE) { if (entry.type != DownloadActivityType.SRTM_FILE && entry.type != DownloadActivityType.HILLSHADE_FILE) {
downloads.set(downloads.get() + 1); downloads.set(downloads.get() + 1);
} }
@ -161,30 +228,60 @@ public class DownloadIndexesThread {
publishProgress(entry); publishProgress(entry);
} }
} }
if(success) {
entriesToDownload.remove(file);
}
}
}
String warn = reindexFiles(filesToReindex);
if(breakDownloadMessage != null) {
if(warn != null) {
warn = breakDownloadMessage + "\n" + warn;
} else {
warn = breakDownloadMessage;
} }
} }
boolean vectorMapsToReindex = false; return warn;
for (File f : filesToReindex) {
if (f.getName().endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)) {
vectorMapsToReindex = true;
break;
}
}
// reindex vector maps all at one time
ResourceManager manager = app.getResourceManager();
manager.indexVoiceFiles(this);
if (vectorMapsToReindex) {
List<String> warnings = manager.indexingMaps(this);
if (!warnings.isEmpty()) {
return warnings.get(0);
}
}
} catch (InterruptedException e) { } catch (InterruptedException e) {
log.info("Download Interrupted");
// do not dismiss dialog // do not dismiss dialog
} }
return null; return null;
} }
private boolean exceedsFreelimit(DownloadEntry entry) {
return Version.isFreeVersion(app) &&
entry.type != DownloadActivityType.SRTM_FILE && entry.type != DownloadActivityType.HILLSHADE_FILE
&& downloads.get() >= DownloadIndexActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS;
}
private String reindexFiles(List<File> filesToReindex) {
boolean vectorMapsToReindex = false;
for (File f : filesToReindex) {
if (f.getName().endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)) {
vectorMapsToReindex = true;
break;
}
}
// reindex vector maps all at one time
ResourceManager manager = app.getResourceManager();
manager.indexVoiceFiles(this);
List<String> warnings = new ArrayList<String>();
if (vectorMapsToReindex) {
warnings = manager.indexingMaps(this);
}
if (cachedSRTMFiles != null) {
for (SrtmIndexItem i : cachedSRTMFiles) {
((SrtmIndexItem) i).updateExistingTiles(app.getResourceManager().getIndexFileNames());
}
}
if (!warnings.isEmpty()) {
return warnings.get(0);
}
return null;
}
private void trackEvent(DownloadEntry entry) { private void trackEvent(DownloadEntry entry) {
String v = Version.getAppName(app); String v = Version.getAppName(app);
if (Version.isProductionVersion(app)) { if (Version.isProductionVersion(app)) {
@ -202,14 +299,16 @@ public class DownloadIndexesThread {
} }
public boolean downloadFile(DownloadEntry de, List<File> filesToReindex, String indexOfAllFiles, boolean forceWifi) public boolean downloadFile(DownloadEntry de, List<File> filesToReindex, boolean forceWifi)
throws InterruptedException { throws InterruptedException {
boolean res = false; boolean res = false;
if (de.isAsset) { if (de.isAsset) {
try { try {
ResourceManager.copyAssets(app.getAssets(), de.targetFile.getPath(), de.targetFile); if (uiActivity != null) {
de.targetFile.setLastModified(de.dateModified); ResourceManager.copyAssets(uiActivity.getAssets(), de.assetName, de.targetFile);
res = true; de.targetFile.setLastModified(de.dateModified);
res = true;
}
} catch (IOException e) { } catch (IOException e) {
log.error("Copy exception", e); log.error("Copy exception", e);
} }
@ -217,7 +316,7 @@ public class DownloadIndexesThread {
res = downloadFileHelper.downloadFile(de, this, filesToReindex, this, forceWifi); res = downloadFileHelper.downloadFile(de, this, filesToReindex, this, forceWifi);
} }
if (res && de.attachedEntry != null) { if (res && de.attachedEntry != null) {
return downloadFile(de.attachedEntry, filesToReindex, indexOfAllFiles, forceWifi); return downloadFile(de.attachedEntry, filesToReindex, forceWifi);
} }
return res; return res;
} }
@ -225,7 +324,7 @@ public class DownloadIndexesThread {
@Override @Override
protected void updateProgress(boolean updateOnlyProgress) { protected void updateProgress(boolean updateOnlyProgress) {
if(uiActivity != null) { if(uiActivity != null) {
uiActivity.updateProgress(updateOnlyProgress, this); uiActivity.updateProgress(updateOnlyProgress);
} }
} }
} }
@ -238,19 +337,8 @@ public class DownloadIndexesThread {
return false; return false;
} }
public void runDownloadFiles(TreeMap<IndexItem, List<DownloadEntry>> entriesToDownload){
if(checkRunning()) {
return;
}
IndexItem[] indexes = entriesToDownload.keySet().toArray(new IndexItem[0]);
DownloadIndexesAsyncTask task = new DownloadIndexesAsyncTask(ctx, entriesToDownload);
task.execute(indexes);
}
public void runReloadIndexFiles() { public void runReloadIndexFiles() {
if(checkRunning()) { checkRunning();
return;
}
final BasicProgressAsyncTask<Void, Void, IndexFileList> inst = new BasicProgressAsyncTask<Void, Void, IndexFileList>(ctx) { final BasicProgressAsyncTask<Void, Void, IndexFileList> inst = new BasicProgressAsyncTask<Void, Void, IndexFileList>(ctx) {
@Override @Override
@ -260,12 +348,9 @@ public class DownloadIndexesThread {
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
currentRunningTask = this; currentRunningTask.add(this);
super.onPreExecute(); super.onPreExecute();
this.message = ctx.getString(R.string.downloading_list_indexes); this.message = ctx.getString(R.string.downloading_list_indexes);
if(uiActivity != null) {
uiActivity.updateProgress(false, this);
}
} }
protected void onPostExecute(IndexFileList result) { protected void onPostExecute(IndexFileList result) {
@ -274,22 +359,23 @@ public class DownloadIndexesThread {
boolean basemapExists = uiActivity.getMyApplication().getResourceManager().containsBasemap(); boolean basemapExists = uiActivity.getMyApplication().getResourceManager().containsBasemap();
IndexItem basemap = indexFiles.getBasemap(); IndexItem basemap = indexFiles.getBasemap();
if (!basemapExists && basemap != null) { if (!basemapExists && basemap != null) {
List<DownloadEntry> downloadEntry = basemap List<DownloadEntry> downloadEntry = basemap.createDownloadEntry(uiActivity.getClientContext(),
.createDownloadEntry(uiActivity.getClientContext(), uiActivity.getType(), new ArrayList<DownloadEntry>()); uiActivity.getType(), new ArrayList<DownloadEntry>());
uiActivity.getEntriesToDownload().put(basemap, downloadEntry); uiActivity.getEntriesToDownload().put(basemap, downloadEntry);
AccessibleToast.makeText(uiActivity, R.string.basemap_was_selected_to_download, Toast.LENGTH_LONG).show(); AccessibleToast.makeText(uiActivity, R.string.basemap_was_selected_to_download, Toast.LENGTH_LONG).show();
uiActivity.findViewById(R.id.DownloadButton).setVisibility(View.VISIBLE); uiActivity.findViewById(R.id.DownloadButton).setVisibility(View.VISIBLE);
} }
uiActivity.setListAdapter(new DownloadIndexAdapter(uiActivity, uiActivity.getFilteredByType()));
if (indexFiles.isIncreasedMapVersion()) { if (indexFiles.isIncreasedMapVersion()) {
showWarnDialog(); showWarnDialog();
} }
} else { } else {
AccessibleToast.makeText(ctx, R.string.list_index_files_was_not_loaded, Toast.LENGTH_LONG).show(); AccessibleToast.makeText(ctx, R.string.list_index_files_was_not_loaded, Toast.LENGTH_LONG).show();
} }
if(uiActivity != null) { currentRunningTask.remove(this);
uiActivity.updateProgress(false, null); if (uiActivity != null) {
uiActivity.updateProgress(false);
} }
runCategorization(uiActivity.getType());
} }
@ -318,22 +404,191 @@ public class DownloadIndexesThread {
@Override @Override
protected void updateProgress(boolean updateOnlyProgress) { protected void updateProgress(boolean updateOnlyProgress) {
if(uiActivity != null) { if (uiActivity != null) {
uiActivity.updateProgress(updateOnlyProgress, this); uiActivity.updateProgress(updateOnlyProgress);
} }
}; };
}; };
inst.execute(new Void[0]); execute(inst, new Void[0]);
}
public void runDownloadFiles(){
if(checkRunning()) {
return;
}
DownloadIndexesAsyncTask task = new DownloadIndexesAsyncTask(ctx);
execute(task, new IndexItem[0]);
}
private <P>void execute(BasicProgressAsyncTask<P, ?, ?> task, P... indexItems) {
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
// TODO check
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, indexItems);
} else {
task.execute(indexItems);
}
}
public Map<IndexItem, List<DownloadEntry>> getEntriesToDownload() {
return entriesToDownload;
}
public void runCategorization(final DownloadActivityType type) {
final BasicProgressAsyncTask<Void, Void, List<IndexItem>> inst = new BasicProgressAsyncTask<Void, Void, List<IndexItem>>(ctx) {
private List<IndexItemCategory> cats;
@Override
protected void onPreExecute() {
super.onPreExecute();
currentRunningTask.add(this);
this.message = ctx.getString(R.string.downloading_list_indexes);
if(uiActivity != null) {
uiActivity.updateProgress(false);
}
}
@Override
protected List<IndexItem> doInBackground(Void... params) {
final List<IndexItem> filtered = getFilteredByType();
cats = IndexItemCategory.categorizeIndexItems(app, filtered);
return filtered;
};
public List<IndexItem> getFilteredByType() {
final List<IndexItem> filtered = new ArrayList<IndexItem>();
if (type == DownloadActivityType.SRTM_FILE) {
Map<String, String> indexFileNames = app.getResourceManager().getIndexFileNames();
if (cachedSRTMFiles == null) {
cachedSRTMFiles = new ArrayList<SrtmIndexItem>();
synchronized (cachedSRTMFiles) {
List<RegionCountry> countries = RegionRegistry.getRegionRegistry().getCountries();
for (RegionCountry rc : countries) {
if (rc.tiles.size() > 35 && rc.getSubRegions().size() > 0) {
for (RegionCountry ch : rc.getSubRegions()) {
cachedSRTMFiles.add(new SrtmIndexItem(ch, indexFileNames));
}
} else {
cachedSRTMFiles.add(new SrtmIndexItem(rc, indexFileNames));
}
}
filtered.addAll(cachedSRTMFiles);
}
} else {
synchronized (cachedSRTMFiles) {
for (SrtmIndexItem s : cachedSRTMFiles) {
s.updateExistingTiles(indexFileNames);
filtered.add(s);
}
}
}
}
List<IndexItem> cachedIndexFiles = getCachedIndexFiles();
if (cachedIndexFiles != null) {
for (IndexItem file : cachedIndexFiles) {
if (file.getType() == type) {
filtered.add(file);
}
}
}
return filtered;
}
@Override
protected void onPostExecute(List<IndexItem> filtered) {
if (uiActivity != null) {
DownloadIndexAdapter a = ((DownloadIndexAdapter) uiActivity.getExpandableListAdapter());
a.setIndexFiles(filtered, cats);
a.notifyDataSetChanged();
a.getFilter().filter(uiActivity.getFilterText());
if (type == DownloadActivityType.SRTM_FILE && OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) instanceof SRTMPlugin
&& !OsmandPlugin.getEnabledPlugin(SRTMPlugin.class).isPaid()) {
Builder msg = new AlertDialog.Builder(uiActivity);
msg.setTitle(R.string.srtm_paid_version_title);
msg.setMessage(R.string.srtm_paid_version_msg);
msg.setNegativeButton(R.string.button_upgrade_osmandplus, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:net.osmand.srtmPlugin.paid"));
try {
ctx.startActivity(intent);
} catch (ActivityNotFoundException e) {
}
}
});
msg.setPositiveButton(R.string.default_buttons_ok, null);
msg.show();
}
}
currentRunningTask.remove(this);
if(uiActivity != null) {
uiActivity.updateProgress(false);
}
}
@Override
protected void updateProgress(boolean updateOnlyProgress) {
if(uiActivity != null) {
uiActivity.updateProgress(updateOnlyProgress);
}
};
};
execute(inst, new Void[0]);
} }
public BasicProgressAsyncTask<?, ?, ?> getCurrentRunningTask() { public boolean isDownloadRunning(){
if(currentRunningTask != null && currentRunningTask.getStatus() == Status.FINISHED) { for(int i =0; i<currentRunningTask.size(); i++) {
currentRunningTask = null; if (currentRunningTask.get(i) instanceof DownloadIndexesAsyncTask) {
return true;
}
} }
return currentRunningTask; return false;
}
public BasicProgressAsyncTask<?, ?, ?> getCurrentRunningTask() {
for(int i = 0; i< currentRunningTask.size(); ) {
if(currentRunningTask.get(i).getStatus() == Status.FINISHED) {
currentRunningTask.remove(i);
} else {
i++;
}
}
if(currentRunningTask.size() > 0) {
return currentRunningTask.get(0);
}
return null;
}
public double getAvailableSpace() {
File dir = app.getAppPath("").getParentFile();
double asz = -1;
if (dir.canRead()) {
StatFs fs = new StatFs(dir.getAbsolutePath());
asz = (((long) fs.getAvailableBlocks()) * fs.getBlockSize()) / (1 << 20);
}
return asz;
}
public int getDownloads() {
int i = 0;
Collection<List<DownloadEntry>> vs = getEntriesToDownload().values();
for (List<DownloadEntry> v : vs) {
for(DownloadEntry e : v) {
if(!currentDownloads.contains(e)) {
i++;
}
}
}
if(!currentDownloads.isEmpty()) {
i++;
}
return i;
} }

View file

@ -84,6 +84,7 @@ public class SrtmIndexItem extends IndexItem {
entry.urlToDownload = url +"file=" + fullName; entry.urlToDownload = url +"file=" + fullName;
// url + "file=" + fileName; // url + "file=" + fileName;
entry.unzipFolder = false; entry.unzipFolder = false;
entry.zipStream = true;
entry.dateModified = System.currentTimeMillis(); entry.dateModified = System.currentTimeMillis();
entry.sizeMB = 10; entry.sizeMB = 10;
entry.parts = 1; entry.parts = 1;

View file

@ -419,7 +419,7 @@ public class ParkingPositionPlugin extends OsmandPlugin {
public void registerOptionsMenuItems(final MapActivity mapActivity, ContextMenuAdapter helper) { public void registerOptionsMenuItems(final MapActivity mapActivity, ContextMenuAdapter helper) {
if (parkingLayer != null) { if (parkingLayer != null) {
if (getParkingPosition() != null) { if (getParkingPosition() != null) {
boolean l = app.getSettings().isLightContent(); boolean l = app.getSettings().isLightContentMenu();
helper.registerItem(R.string.osmand_parking_delete, l ? R.drawable.a_1_navigation_cancel_light : R.drawable.a_1_navigation_cancel_dark, helper.registerItem(R.string.osmand_parking_delete, l ? R.drawable.a_1_navigation_cancel_light : R.drawable.a_1_navigation_cancel_dark,
new OnContextMenuClick() { new OnContextMenuClick() {
@Override @Override