Method to import (copy) sqlite files via AIDL - pr fixes

This commit is contained in:
madwasp79 2019-02-06 21:01:27 +02:00
parent cd74efafaa
commit d87a094b5a
5 changed files with 114 additions and 46 deletions

View file

@ -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);
}

View file

@ -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<String, FileOutputStream> 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<Void, Void, GPXFile> {

View file

@ -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);
}
};
}

View file

@ -0,0 +1,3 @@
package net.osmand.aidl.tiles;
parcelable FilePartParams;

View file

@ -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<FilePartParams> CREATOR = new Creator<FilePartParams>() {
@Override
public FilePartParams createFromParcel(Parcel in) {
return new FilePartParams(in);
}
@Override
public FilePartParams[] newArray(int size) {
return new FilePartParams[size];
}
};
}