diff --git a/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl b/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl index abb28b3abe..22e9170791 100644 --- a/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl +++ b/OsmAnd/src/net/osmand/aidl/IOsmAndAidlInterface.aidl @@ -79,6 +79,7 @@ import net.osmand.aidl.gpx.CreateGpxBitmapParams; import net.osmand.aidl.tiles.ASqliteDbFile; import net.osmand.aidl.plugins.PluginParams; +import net.osmand.aidl.tiles.FilePartParams; @@ -180,5 +181,5 @@ interface IOsmAndAidlInterface { boolean getBitmapForGpx(in CreateGpxBitmapParams file, IOsmAndAidlCallback callback); - boolean appendDataToFile(in String filename, in byte[] data); + boolean copyFileOverApi(in FilePartParams filePart); } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java index f7c09440d2..e7132a6d7a 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java @@ -44,6 +44,7 @@ import net.osmand.aidl.navdrawer.NavDrawerFooterParams; import net.osmand.aidl.plugins.PluginParams; import net.osmand.aidl.search.SearchResult; import net.osmand.aidl.tiles.ASqliteDbFile; +import net.osmand.aidl.tiles.FilePartParams; import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; @@ -98,14 +99,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.lang.ref.WeakReference; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import static net.osmand.plus.OsmAndCustomizationConstants.DRAWER_ITEM_ID_SCHEME; @@ -1966,46 +1960,48 @@ public class OsmandAidlApi { gpxAsyncLoaderTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } - private static int partsCounter = 0; - private static long startTime = 0; + private Map copyFilesCache = new HashMap<>(); - boolean appendDataToFile(final String filename, final byte[] data) { - File file = app.getAppPath(IndexConstants.TILES_INDEX_DIR + filename); - if (partsCounter == 0) { - startTime = System.currentTimeMillis(); - LOG.debug("Start time = " + startTime); - } - - if (filename.isEmpty() || data == null) { + boolean copyFileOverApi(final FilePartParams filePart) { + if (filePart.getFilename().isEmpty() || filePart.getFilePartData() == null) { return false; - - }else if (data.length == 0) { - if (file.exists() && filename.endsWith(IndexConstants.SQLITE_EXT)) { - LOG.debug("Copied file: " + filename +" (size = " + file.length() - + " bytes) in: " + ((System.currentTimeMillis() - startTime) + " ms")); - partsCounter = 0; - } - } else { - FileOutputStream fos; - file.getParentFile().mkdirs(); - try { - if (partsCounter == 0) { - fos = new FileOutputStream(file); - } else { - fos = new FileOutputStream(file, true); - } - fos.write(data); - partsCounter++; - fos.close(); - - } catch (IOException e) { - e.printStackTrace(); - } - } - return true; + + if (filePart.getFilename().endsWith(IndexConstants.SQLITE_EXT)) { + return copySqlFileImpl(filePart, app.getAppPath(IndexConstants.TILES_INDEX_DIR + filePart.getFilename())); + } + + return false; } + private boolean copySqlFileImpl(FilePartParams filePart, File file){ + FileOutputStream fos; + try { + if (copyFilesCache.containsKey(filePart.getFilename())) { + fos = copyFilesCache.get(filePart.getFilename()); + if (file.length() == filePart.getSentSize()) { + fos.write(filePart.getFilePartData()); + return true; + } else if (file.length() == filePart.getSize()) { + copyFilesCache.remove(file.getName()); + fos.close(); + return true; + } + } else { + if (file.exists()) file.delete(); + file.getParentFile().mkdirs(); + fos = new FileOutputStream(file, true); + fos.write(filePart.getFilePartData()); + copyFilesCache.put(filePart.getFilename(), fos); + return true; + } + + } catch (IOException e) { + LOG.error(e.getMessage(), e); + + } + return false; + } private static class GpxAsyncLoaderTask extends AsyncTask { diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java index d1e6a487de..504e4af64e 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java @@ -66,6 +66,7 @@ import net.osmand.aidl.plugins.PluginParams; import net.osmand.aidl.search.SearchParams; import net.osmand.aidl.search.SearchResult; import net.osmand.aidl.tiles.ASqliteDbFile; +import net.osmand.aidl.tiles.FilePartParams; import net.osmand.plus.OsmandApplication; import net.osmand.util.Algorithms; @@ -847,9 +848,9 @@ public class OsmandAidlService extends Service { } @Override - public boolean appendDataToFile(String filename, byte[] data) { + public boolean copyFileOverApi(FilePartParams filePart) { OsmandAidlApi api = getApi("appendDataToFile"); - return api != null && api.appendDataToFile(filename, data); + return api != null && api.copyFileOverApi(filePart); } }; } diff --git a/OsmAnd/src/net/osmand/aidl/tiles/FilePartParams.aidl b/OsmAnd/src/net/osmand/aidl/tiles/FilePartParams.aidl new file mode 100644 index 0000000000..ad329c6c73 --- /dev/null +++ b/OsmAnd/src/net/osmand/aidl/tiles/FilePartParams.aidl @@ -0,0 +1,3 @@ +package net.osmand.aidl.tiles; + +parcelable FilePartParams; diff --git a/OsmAnd/src/net/osmand/aidl/tiles/FilePartParams.java b/OsmAnd/src/net/osmand/aidl/tiles/FilePartParams.java new file mode 100644 index 0000000000..8ad27cd61c --- /dev/null +++ b/OsmAnd/src/net/osmand/aidl/tiles/FilePartParams.java @@ -0,0 +1,67 @@ +package net.osmand.aidl.tiles; + +import android.os.Parcel; +import android.os.Parcelable; + +public class FilePartParams implements Parcelable { + private String filename; + private long size; + private long sentSize; + private byte[] filePartData; + + public FilePartParams(String filename, String filePartId, long size, long sentSize, byte[] filePartData) { + this.filename = filename; + this.size = size; + this.sentSize = sentSize; + this.filePartData = filePartData; + } + + public String getFilename() { + return filename; + } + + + public long getSize() { + return size; + } + + public long getSentSize() { + return sentSize; + } + + public byte[] getFilePartData() { + return filePartData; + } + + protected FilePartParams(Parcel in) { + filename = in.readString(); + size = in.readLong(); + sentSize = in.readLong(); + filePartData = in.createByteArray(); + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeString(filename); + dest.writeLong(size); + dest.writeLong(sentSize); + dest.writeByteArray(filePartData); + } + + @Override + public int describeContents() { + return 0; + } + + public static final Creator CREATOR = new Creator() { + @Override + public FilePartParams createFromParcel(Parcel in) { + return new FilePartParams(in); + } + + @Override + public FilePartParams[] newArray(int size) { + return new FilePartParams[size]; + } + }; +}