Introduce new download entry
This commit is contained in:
parent
dbd3cdee5a
commit
0ccdd89b96
3 changed files with 0 additions and 458 deletions
|
@ -1,29 +0,0 @@
|
|||
package net.osmand.plus.download;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class DownloadEntry {
|
||||
public File fileToSave;
|
||||
public File fileToUnzip;
|
||||
public boolean unzip;
|
||||
public Long dateModified;
|
||||
public double sizeMB;
|
||||
public String baseName;
|
||||
public int parts;
|
||||
public File existingBackupFile;
|
||||
public DownloadEntry attachedEntry;
|
||||
public boolean isAsset;
|
||||
public boolean isRoadMap;
|
||||
|
||||
public DownloadEntry() {
|
||||
// default
|
||||
}
|
||||
|
||||
public DownloadEntry(String assetName, String fileName, long dateModified) {
|
||||
this.dateModified = dateModified;
|
||||
fileToUnzip = new File(fileName);
|
||||
fileToSave = new File(assetName);
|
||||
isAsset = true;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,336 +0,0 @@
|
|||
package net.osmand.plus.download;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
import net.osmand.IProgress;
|
||||
import net.osmand.LogUtil;
|
||||
import net.osmand.Version;
|
||||
import net.osmand.data.IndexConstants;
|
||||
import net.osmand.plus.R;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
|
||||
public class DownloadFileHelper {
|
||||
|
||||
private final static Log log = LogUtil.getLog(DownloadFileHelper.class);
|
||||
private static final int BUFFER_SIZE = 32256;
|
||||
protected final int TRIES_TO_DOWNLOAD = 15;
|
||||
protected final long TIMEOUT_BETWEEN_DOWNLOADS = 8000;
|
||||
private final Activity ctx;
|
||||
private boolean interruptDownloading = false;
|
||||
|
||||
|
||||
public DownloadFileHelper(Activity ctx){
|
||||
this.ctx = ctx;
|
||||
}
|
||||
|
||||
public interface DownloadFileShowWarning {
|
||||
|
||||
public void showWarning(String warning);
|
||||
}
|
||||
|
||||
protected void downloadFile(String fileName, FileOutputStream out, URL url, String part, String indexOfAllFiles,
|
||||
IProgress progress, boolean forceWifi) throws IOException, InterruptedException {
|
||||
InputStream is = null;
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int read = 0;
|
||||
int length = 0;
|
||||
int fileread = 0;
|
||||
int triesDownload = TRIES_TO_DOWNLOAD;
|
||||
boolean first = true;
|
||||
try {
|
||||
while (triesDownload > 0) {
|
||||
try {
|
||||
if (!first) {
|
||||
log.info("Reconnecting"); //$NON-NLS-1$
|
||||
try {
|
||||
Thread.sleep(TIMEOUT_BETWEEN_DOWNLOADS);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestProperty("User-Agent", Version.getFullVersion(ctx)); //$NON-NLS-1$
|
||||
conn.setReadTimeout(30000);
|
||||
if (fileread > 0) {
|
||||
String range = "bytes="+fileread + "-" + (length -1); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
conn.setRequestProperty("Range", range); //$NON-NLS-1$
|
||||
}
|
||||
conn.setConnectTimeout(30000);
|
||||
log.info(conn.getResponseMessage() + " " + conn.getResponseCode()); //$NON-NLS-1$
|
||||
boolean wifiConnectionBroken = forceWifi && !isWifiConnected();
|
||||
if ((conn.getResponseCode() != HttpURLConnection.HTTP_PARTIAL &&
|
||||
conn.getResponseCode() != HttpURLConnection.HTTP_OK ) || wifiConnectionBroken) {
|
||||
conn.disconnect();
|
||||
triesDownload--;
|
||||
continue;
|
||||
}
|
||||
is = conn.getInputStream();
|
||||
// long skipped = 0;
|
||||
// while (skipped < fileread) {
|
||||
// skipped += is.skip(fileread - skipped);
|
||||
// }
|
||||
if (first) {
|
||||
length = conn.getContentLength();
|
||||
String taskName = ctx.getString(R.string.downloading_file) + indexOfAllFiles +" " + fileName;
|
||||
if(part != null){
|
||||
taskName += part;
|
||||
}
|
||||
progress.startTask(taskName, length / 1024); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
first = false;
|
||||
while ((read = is.read(buffer)) != -1) {
|
||||
if(interruptDownloading){
|
||||
throw new InterruptedException();
|
||||
}
|
||||
out.write(buffer, 0, read);
|
||||
fileread += read;
|
||||
progress.remaining((length - fileread) / 1024);
|
||||
}
|
||||
if(length <= fileread){
|
||||
triesDownload = 0;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("IOException", e); //$NON-NLS-1$
|
||||
triesDownload--;
|
||||
}
|
||||
|
||||
}
|
||||
} finally {
|
||||
if (is != null) {
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
if(length != fileread || length == 0){
|
||||
throw new IOException("File was not fully read"); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public boolean isWifiConnected(){
|
||||
ConnectivityManager mgr = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo ni = mgr.getActiveNetworkInfo();
|
||||
return ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI;
|
||||
}
|
||||
|
||||
public boolean downloadFile(final String fileName, DownloadEntry de, IProgress progress,
|
||||
List<File> toReIndex, String indexOfAllFiles,
|
||||
DownloadFileShowWarning showWarningCallback, boolean forceWifi) throws InterruptedException {
|
||||
FileOutputStream out = null;
|
||||
try {
|
||||
|
||||
out = new FileOutputStream(de.fileToSave);
|
||||
try {
|
||||
String urlPrefix = "http://"+DownloadOsmandIndexesHelper.INDEX_DOWNLOAD_DOMAIN+"/download?event=2&file=";
|
||||
String urlSuffix = "&" + Version.getVersionAsURLParam(ctx);
|
||||
if(de.isRoadMap) {
|
||||
urlSuffix += "&road=yes";
|
||||
}
|
||||
if (de.parts == 1) {
|
||||
URL url = new URL(urlPrefix + fileName + urlSuffix); //$NON-NLS-1$
|
||||
downloadFile(fileName, out, url, null, indexOfAllFiles, progress, forceWifi);
|
||||
} else {
|
||||
for (int i = 1; i <= de.parts; i++) {
|
||||
URL url = new URL(urlPrefix + fileName + "-" + i + urlSuffix); //$NON-NLS-1$
|
||||
downloadFile(fileName, out, url, " [" + i + "/" + de.parts + "]", indexOfAllFiles, progress, forceWifi);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
out.close();
|
||||
out = null;
|
||||
}
|
||||
|
||||
unzipFile(de, progress, toReIndex);
|
||||
|
||||
showWarningCallback.showWarning(ctx.getString(R.string.download_index_success));
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
log.error("Exception ocurred", e); //$NON-NLS-1$
|
||||
showWarningCallback.showWarning(ctx.getString(R.string.error_io_error));
|
||||
if (out != null) {
|
||||
try {
|
||||
out.close();
|
||||
} catch (IOException e1) {
|
||||
}
|
||||
}
|
||||
// Possibly file is corrupted
|
||||
de.fileToSave.delete();
|
||||
return false;
|
||||
} catch (InterruptedException e) {
|
||||
// Possibly file is corrupted
|
||||
de.fileToSave.delete();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private void unzipFile(DownloadEntry de, IProgress progress, List<File> toReIndex)
|
||||
throws FileNotFoundException, IOException {
|
||||
if (de.fileToSave.getName().endsWith(".zip")) { //$NON-NLS-1$
|
||||
if (de.unzip) {
|
||||
de.fileToUnzip.mkdirs();
|
||||
}
|
||||
CountingInputStream fin = new CountingInputStream(new FileInputStream(de.fileToSave));
|
||||
ZipInputStream zipIn = new ZipInputStream(fin);
|
||||
ZipEntry entry = null;
|
||||
boolean first = true;
|
||||
int len = (int) de.fileToSave.length();
|
||||
progress.startTask(ctx.getString(R.string.unzipping_file), len / 1024);
|
||||
while ((entry = zipIn.getNextEntry()) != null) {
|
||||
if (entry.isDirectory() || entry.getName().endsWith(IndexConstants.GEN_LOG_EXT)) {
|
||||
continue;
|
||||
}
|
||||
File fs;
|
||||
if (!de.unzip) {
|
||||
if (first) {
|
||||
fs = de.fileToUnzip;
|
||||
first = false;
|
||||
} else {
|
||||
String name = entry.getName();
|
||||
// small simplification
|
||||
int ind = name.lastIndexOf('_');
|
||||
if (ind > 0) {
|
||||
// cut version
|
||||
int i = name.indexOf('.', ind);
|
||||
if (i > 0) {
|
||||
name = name.substring(0, ind) + name.substring(i, name.length());
|
||||
}
|
||||
}
|
||||
fs = new File(de.fileToUnzip.getParent(), name);
|
||||
}
|
||||
} else {
|
||||
fs = new File(de.fileToUnzip, entry.getName());
|
||||
}
|
||||
FileOutputStream out = new FileOutputStream(fs);
|
||||
int read;
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int remaining = len;
|
||||
while ((read = zipIn.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, read);
|
||||
remaining -= fin.lastReadCount();
|
||||
progress.remaining(remaining / 1024);
|
||||
}
|
||||
out.close();
|
||||
|
||||
if (de.dateModified != null) {
|
||||
fs.setLastModified(de.dateModified);
|
||||
}
|
||||
toReIndex.add(fs);
|
||||
}
|
||||
zipIn.close();
|
||||
de.fileToSave.delete(); // zip is no needed more
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void setInterruptDownloading(boolean interruptDownloading) {
|
||||
this.interruptDownloading = interruptDownloading;
|
||||
}
|
||||
|
||||
public boolean isInterruptDownloading() {
|
||||
return interruptDownloading;
|
||||
}
|
||||
|
||||
private static class CountingInputStream extends InputStream {
|
||||
|
||||
private final InputStream delegate;
|
||||
private int count;
|
||||
|
||||
public CountingInputStream(InputStream delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
public int lastReadCount() {
|
||||
int last = count;
|
||||
count = 0;
|
||||
return last;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int available() throws IOException {
|
||||
return delegate.available();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
delegate.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return delegate.equals(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return delegate.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mark(int readlimit) {
|
||||
delegate.mark(readlimit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean markSupported() {
|
||||
return delegate.markSupported();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
int read = delegate.read();
|
||||
if (read > 0) {
|
||||
this.count++;;
|
||||
}
|
||||
return read;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] buffer, int offset, int length)
|
||||
throws IOException {
|
||||
int read = delegate.read(buffer, offset, length);
|
||||
if (read > 0) {
|
||||
this.count += read;
|
||||
}
|
||||
return read;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] buffer) throws IOException {
|
||||
int read = delegate.read(buffer);
|
||||
if (read > 0) {
|
||||
this.count += read;
|
||||
}
|
||||
return read;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() throws IOException {
|
||||
delegate.reset();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long skip(long byteCount) throws IOException {
|
||||
return delegate.skip(byteCount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return delegate.toString();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,93 +0,0 @@
|
|||
package net.osmand.plus.download;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import net.osmand.data.IndexConstants;
|
||||
import net.osmand.plus.download.DownloadOsmandIndexesHelper.IndexItem;
|
||||
|
||||
public class IndexFileList implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private boolean downloadedFromInternet = false;
|
||||
IndexItem basemap;
|
||||
ArrayList<IndexItem> indexFiles = new ArrayList<IndexItem>();
|
||||
private String mapversion;
|
||||
|
||||
private Comparator<IndexItem> comparator = new Comparator<IndexItem>(){
|
||||
@Override
|
||||
public int compare(IndexItem o1, IndexItem o2) {
|
||||
String object1 = o1.getFileName();
|
||||
String object2 = o2.getFileName();
|
||||
if(object1.endsWith(IndexConstants.ANYVOICE_INDEX_EXT_ZIP)){
|
||||
if(object2.endsWith(IndexConstants.ANYVOICE_INDEX_EXT_ZIP)){
|
||||
return object1.compareTo(object2);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
} else if(object2.endsWith(IndexConstants.ANYVOICE_INDEX_EXT_ZIP)){
|
||||
return 1;
|
||||
}
|
||||
return object1.compareTo(object2);
|
||||
}
|
||||
};
|
||||
|
||||
public void setDownloadedFromInternet(boolean downloadedFromInternet) {
|
||||
this.downloadedFromInternet = downloadedFromInternet;
|
||||
}
|
||||
|
||||
public boolean isDownloadedFromInternet() {
|
||||
return downloadedFromInternet;
|
||||
}
|
||||
|
||||
public void setMapVersion(String mapversion) {
|
||||
this.mapversion = mapversion;
|
||||
}
|
||||
|
||||
public void add(IndexItem indexItem) {
|
||||
if (indexItem.isAccepted()) {
|
||||
indexFiles.add(indexItem);
|
||||
}
|
||||
if(indexItem.getFileName().toLowerCase().startsWith("world_basemap")) {
|
||||
basemap = indexItem;
|
||||
}
|
||||
}
|
||||
|
||||
public void sort(){
|
||||
Collections.sort(indexFiles, comparator);
|
||||
}
|
||||
|
||||
public boolean isAcceptable() {
|
||||
return (indexFiles != null && !indexFiles.isEmpty()) || (mapversion != null);
|
||||
}
|
||||
|
||||
public List<IndexItem> getIndexFiles() {
|
||||
return indexFiles;
|
||||
}
|
||||
|
||||
public IndexItem getBasemap() {
|
||||
return basemap;
|
||||
}
|
||||
|
||||
public boolean isIncreasedMapVersion() {
|
||||
try {
|
||||
int mapVersionInList = Integer.parseInt(mapversion);
|
||||
return IndexConstants.BINARY_MAP_VERSION < mapVersionInList;
|
||||
} catch (NumberFormatException e) {
|
||||
//ignore this...
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public IndexItem getIndexFilesByName(String key) {
|
||||
for(IndexItem i : indexFiles) {
|
||||
if(i.getFileName().equals(key)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue