work in progress

This commit is contained in:
madwasp79 2019-02-12 16:01:53 +02:00
parent 432385d15a
commit 5d648bfc6f
3 changed files with 86 additions and 53 deletions

View file

@ -26,7 +26,6 @@ import android.text.TextUtils;
import android.view.View; import android.view.View;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import java.util.concurrent.atomic.AtomicInteger;
import net.osmand.CallbackWithObject; import net.osmand.CallbackWithObject;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
@ -104,6 +103,7 @@ import java.lang.ref.WeakReference;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import static net.osmand.aidl.OsmandAidlConstants.*;
import static net.osmand.plus.OsmAndCustomizationConstants.DRAWER_ITEM_ID_SCHEME; import static net.osmand.plus.OsmAndCustomizationConstants.DRAWER_ITEM_ID_SCHEME;
@ -156,11 +156,6 @@ public class OsmandAidlApi {
private static final String AIDL_HIDE_SQLITEDB_FILE = "aidl_hide_sqlitedb_file"; private static final String AIDL_HIDE_SQLITEDB_FILE = "aidl_hide_sqlitedb_file";
private static final String AIDL_FILE_NAME = "aidl_file_name"; private static final String AIDL_FILE_NAME = "aidl_file_name";
private static final int FILE_PARAMS_ERROR = -1001;
private static final int LARGE_PARTS_ERROR = -1002;
private static final int DOUBLE_COPY_ERROR = -1003;
private static final int IO_ERROR = -1004;
private static final int UNKNOWN_FILE_TYPE = -1005;
private static final ApplicationMode DEFAULT_PROFILE = ApplicationMode.CAR; private static final ApplicationMode DEFAULT_PROFILE = ApplicationMode.CAR;
@ -1967,66 +1962,86 @@ public class OsmandAidlApi {
gpxAsyncLoaderTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); gpxAsyncLoaderTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
private Map<String, Map <Integer, FileOutputStream>>copyFilesCache = new ConcurrentHashMap<>(); private Map<String, FileCopyInfo> copyFilesCache = new ConcurrentHashMap<>();
private AtomicInteger counter = new AtomicInteger(0);
private class FileCopyInfo {
long start;
long lastAccess;
FileOutputStream FileOutputStream;
public int getNextUniqueIndex() { FileCopyInfo(long start, long lastAccess, FileOutputStream fileOutputStream) {
return counter.getAndIncrement(); this.start = start;
this.lastAccess = lastAccess;
FileOutputStream = fileOutputStream;
}
} }
int copyFile(final CopyFileParams filePart) { int copyFile(final CopyFileParams filePart) {
if (Algorithms.isEmpty(filePart.getFilename()) || filePart.getFilePartData() == null) { if (Algorithms.isEmpty(filePart.getFilename()) || filePart.getFilePartData() == null) {
return FILE_PARAMS_ERROR; return COPY_FILE_PARAMS_ERROR;
} }
if (filePart.getFilename().endsWith(IndexConstants.SQLITE_EXT)) { if (filePart.getFilename().endsWith(IndexConstants.SQLITE_EXT)) {
return copyFileImpl(filePart, app.getAppPath( return copyFileImpl(filePart,
IndexConstants.TEMP_DIR + filePart.getFilename()),
IndexConstants.TILES_INDEX_DIR); IndexConstants.TILES_INDEX_DIR);
} else { } else {
return UNKNOWN_FILE_TYPE; return COPY_FILE_UNSUPPORTED_FILE_TYPE_ERROR;
} }
} }
private int copyFileImpl(CopyFileParams fileParams, File file, String destination){ private int copyFileImpl(CopyFileParams fileParams, String destination){
if (fileParams.getFilePartData().length > 256*1024) { if (fileParams.getFilePartData().length > 256*1024) {
return LARGE_PARTS_ERROR; return COPY_FILE_PART_SIZE_LIMIT_ERROR;
} }
File file = app.getAppPath(IndexConstants.TEMP_DIR + fileParams.getFilename());
FileOutputStream fos; FileOutputStream fos;
String key = fileParams.getFilename(); String key = fileParams.getFilename();
try { try {
if (copyFilesCache.containsKey(key) && fileParams.getId()!=0) { if (!copyFilesCache.containsKey(key)){
fos = copyFilesCache.get(key).get(fileParams.getId()); if (fileParams.getActionId() == COPY_FILE_START_FLAG) {
file.delete();
if (fileParams.isTransmitComplete()) {
copyFilesCache.remove(key);
fos.close();
file.renameTo(app.getAppPath(destination + fileParams.getFilename()));
return fileParams.getId();
} else {
fos.write(fileParams.getFilePartData());
return fileParams.getId();
}
} else if (copyFilesCache.containsKey(key) && fileParams.getId()==0){
return DOUBLE_COPY_ERROR;
} else {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
fos = new FileOutputStream(file, true); fos = new FileOutputStream(file, true);
fos.write(fileParams.getFilePartData()); fos.write(fileParams.getFilePartData());
counter.getAndIncrement(); copyFilesCache.put(
Map<Integer, FileOutputStream> fileData = new ConcurrentHashMap<>(); key, new FileCopyInfo(fileParams.getStartTime(), System.currentTimeMillis(), fos));
fileData.put(counter.intValue(), fos); return COPY_FILE_OK_RESPONSE;
copyFilesCache.put(key, fileData); } else {
return counter.intValue(); return COPY_FILE_PARAMS_ERROR;
}
} else if (copyFilesCache.containsKey(key)) {
fos = copyFilesCache.get(key).FileOutputStream;
if (fileParams.getActionId() == COPY_FILE_START_FLAG) {
if (copyFilesCache.get(key).lastAccess - copyFilesCache.get(key).start > COPY_FILE_VALID_PAUSE) {
file.delete();
fos.close();
copyFilesCache.remove(key);
file.getParentFile().mkdirs();
fos = new FileOutputStream(file, true);
fos.write(fileParams.getFilePartData());
copyFilesCache.put(key,
new FileCopyInfo(fileParams.getStartTime(), System.currentTimeMillis(), fos));
return COPY_FILE_OK_RESPONSE;
} else {
return COPY_FILE_WRITE_LOCK_ERROR;
}
} else if (fileParams.getActionId() == COPY_FILE_FINISH_FLAG) {
fos.close();
copyFilesCache.remove(key);
file.renameTo(app.getAppPath(destination + fileParams.getFilename()));
return COPY_FILE_OK_RESPONSE;
} else {
copyFilesCache.get(key).lastAccess = System.currentTimeMillis();
fos.write(fileParams.getFilePartData());
return COPY_FILE_OK_RESPONSE;
}
} }
} catch (IOException e) { } catch (IOException e) {
LOG.error(e.getMessage(), e); LOG.error(e.getMessage(), e);
} }
return IO_ERROR; return COPY_FILE_IO_ERROR;
} }
private static class GpxAsyncLoaderTask extends AsyncTask<Void, Void, GPXFile> { private static class GpxAsyncLoaderTask extends AsyncTask<Void, Void, GPXFile> {

View file

@ -0,0 +1,16 @@
package net.osmand.aidl;
public interface OsmandAidlConstants {
int COPY_FILE_PARAMS_ERROR = -1001;
int COPY_FILE_PART_SIZE_LIMIT_ERROR = -1002;
int COPY_FILE_WRITE_LOCK_ERROR = -1003;
int COPY_FILE_IO_ERROR = -1004;
int COPY_FILE_UNSUPPORTED_FILE_TYPE_ERROR = -1005;
int COPY_FILE_START_FLAG = 1001;
int COPY_FILE_FINISH_FLAG = 1002;
int COPY_FILE_IN_PROGRESS_FLAG = 1000;
long COPY_FILE_VALID_PAUSE = 10000;
int COPY_FILE_OK_RESPONSE = 0;
}

View file

@ -6,14 +6,14 @@ import android.os.Parcelable;
public class CopyFileParams implements Parcelable { public class CopyFileParams implements Parcelable {
private String filename; private String filename;
private byte[] filePartData; private byte[] filePartData;
private int id; private long startTime;
private boolean isTransmitComplete; private int actionId;
public CopyFileParams(String filename, byte[] filePartData, int id, boolean isTransmitComplete) { public CopyFileParams(String filename, byte[] filePartData, long startTime, int actionId) {
this.filename = filename; this.filename = filename;
this.filePartData = filePartData; this.filePartData = filePartData;
this.id = id; this.startTime = startTime;
this.isTransmitComplete = isTransmitComplete; this.actionId = actionId;
} }
public String getFilename() { public String getFilename() {
@ -25,18 +25,19 @@ public class CopyFileParams implements Parcelable {
return filePartData; return filePartData;
} }
public int getId() { public int getActionId() {
return id; return actionId;
} }
public boolean isTransmitComplete() {return isTransmitComplete; } public long getStartTime() {
return startTime;
}
protected CopyFileParams(Parcel in) { protected CopyFileParams(Parcel in) {
filename = in.readString(); filename = in.readString();
filePartData = in.createByteArray(); filePartData = in.createByteArray();
id = in.readInt(); startTime = in.readLong();
isTransmitComplete = in.readByte() != 0; actionId = in.readInt();
} }
public static final Creator<CopyFileParams> CREATOR = new Creator<CopyFileParams>() { public static final Creator<CopyFileParams> CREATOR = new Creator<CopyFileParams>() {
@ -61,8 +62,9 @@ public class CopyFileParams implements Parcelable {
public void writeToParcel(Parcel dest, int flags) { public void writeToParcel(Parcel dest, int flags) {
dest.writeString(filename); dest.writeString(filename);
dest.writeByteArray(filePartData); dest.writeByteArray(filePartData);
dest.writeInt(id); dest.writeLong(startTime);
dest.writeByte((byte) (isTransmitComplete ? 1 : 0)); dest.writeInt(actionId);
} }