use of net.osmand.plus.settings.backend.backup.items.StreamSettingsItem in project Osmand by osmandapp.
the class BackupHelper method collectLocalFiles.
@SuppressLint("StaticFieldLeak")
void collectLocalFiles(@Nullable final OnCollectLocalFilesListener listener) {
OperationLog operationLog = new OperationLog("collectLocalFiles", DEBUG);
operationLog.startOperation();
AsyncTask<Void, LocalFile, List<LocalFile>> task = new AsyncTask<Void, LocalFile, List<LocalFile>>() {
BackupDbHelper dbHelper;
SQLiteConnection db;
Map<String, UploadedFileInfo> infos;
@Override
protected void onPreExecute() {
dbHelper = app.getBackupHelper().getDbHelper();
db = dbHelper.openConnection(true);
}
@Override
protected List<LocalFile> doInBackground(Void... voids) {
List<LocalFile> result = new ArrayList<>();
infos = dbHelper.getUploadedFileInfoMap();
List<SettingsItem> localItems = getLocalItems();
operationLog.log("getLocalItems");
for (SettingsItem item : localItems) {
String fileName = BackupHelper.getItemFileName(item);
if (item instanceof FileSettingsItem) {
FileSettingsItem fileItem = (FileSettingsItem) item;
File file = fileItem.getFile();
if (file.isDirectory()) {
if (item instanceof GpxSettingsItem) {
continue;
} else if (fileItem.getSubtype() == FileSubtype.VOICE) {
File jsFile = new File(file, file.getName() + "_" + IndexConstants.TTSVOICE_INDEX_EXT_JS);
if (jsFile.exists()) {
fileName = jsFile.getPath().replace(app.getAppPath(null).getPath() + "/", "");
createLocalFile(result, item, fileName, jsFile, jsFile.lastModified());
continue;
}
} else if (fileItem.getSubtype() == FileSubtype.TTS_VOICE) {
String langName = file.getName().replace(IndexConstants.VOICE_PROVIDER_SUFFIX, "");
File jsFile = new File(file, langName + "_" + IndexConstants.TTSVOICE_INDEX_EXT_JS);
if (jsFile.exists()) {
fileName = jsFile.getPath().replace(app.getAppPath(null).getPath() + "/", "");
createLocalFile(result, item, fileName, jsFile, jsFile.lastModified());
continue;
}
} else if (fileItem.getSubtype() == FileSubtype.TILES_MAP) {
continue;
}
List<File> dirs = new ArrayList<>();
dirs.add(file);
Algorithms.collectDirs(file, dirs);
operationLog.log("collectDirs " + file.getName() + " BEGIN");
for (File dir : dirs) {
File[] files = dir.listFiles();
if (files != null && files.length > 0) {
for (File f : files) {
if (!f.isDirectory()) {
fileName = f.getPath().replace(app.getAppPath(null).getPath() + "/", "");
createLocalFile(result, item, fileName, f, f.lastModified());
}
}
}
}
operationLog.log("collectDirs " + file.getName() + " END");
} else if (fileItem.getSubtype() == FileSubtype.TILES_MAP) {
if (file.getName().endsWith(SQLiteTileSource.EXT)) {
createLocalFile(result, item, fileName, file, file.lastModified());
}
} else {
createLocalFile(result, item, fileName, file, file.lastModified());
}
} else {
createLocalFile(result, item, fileName, null, item.getLastModifiedTime());
}
}
return result;
}
private void createLocalFile(@NonNull List<LocalFile> result, @NonNull SettingsItem item, @NonNull String fileName, @Nullable File file, long lastModifiedTime) {
LocalFile localFile = new LocalFile();
localFile.file = file;
localFile.item = item;
localFile.fileName = fileName;
localFile.localModifiedTime = lastModifiedTime;
if (infos != null) {
UploadedFileInfo fileInfo = infos.get(item.getType().name() + "___" + fileName);
if (fileInfo != null) {
localFile.uploadTime = fileInfo.getUploadTime();
String lastMd5 = fileInfo.getMd5Digest();
boolean needM5Digest = item instanceof StreamSettingsItem && ((StreamSettingsItem) item).needMd5Digest() && localFile.uploadTime < lastModifiedTime && !Algorithms.isEmpty(lastMd5);
if (needM5Digest && file != null && file.exists()) {
FileInputStream is = null;
try {
is = new FileInputStream(file);
String md5 = new String(Hex.encodeHex(DigestUtils.md5(is)));
if (md5.equals(lastMd5)) {
item.setLocalModifiedTime(localFile.uploadTime);
localFile.localModifiedTime = localFile.uploadTime;
}
} catch (IOException e) {
LOG.error(e.getMessage(), e);
} finally {
Algorithms.closeStream(is);
}
}
}
}
result.add(localFile);
publishProgress(localFile);
}
private List<SettingsItem> getLocalItems() {
List<ExportSettingsType> types = ExportSettingsType.getEnabledTypes();
Iterator<ExportSettingsType> it = types.iterator();
while (it.hasNext()) {
ExportSettingsType type = it.next();
if (!getBackupTypePref(type).get()) {
it.remove();
}
}
return app.getFileSettingsHelper().getFilteredSettingsItems(types, true, true);
}
@Override
protected void onProgressUpdate(LocalFile... localFiles) {
if (listener != null) {
listener.onFileCollected(localFiles[0]);
}
}
@Override
protected void onPostExecute(List<LocalFile> localFiles) {
if (db != null) {
db.close();
}
operationLog.finishOperation(" Files=" + localFiles.size());
if (listener != null) {
listener.onFilesCollected(localFiles);
}
}
};
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
use of net.osmand.plus.settings.backend.backup.items.StreamSettingsItem in project Osmand by osmandapp.
the class StreamSettingsItemWriter method writeToStream.
@Override
public void writeToStream(@NonNull OutputStream outputStream, @Nullable IProgress progress) throws IOException {
int bytesDivisor = 1024;
StreamSettingsItem item = getItem();
if (progress != null) {
progress.startWork((int) (item.getSize() / bytesDivisor));
}
InputStream inputStream = item.getInputStream();
if (inputStream != null) {
try {
streamCopy(inputStream, outputStream, progress, bytesDivisor);
} finally {
Algorithms.closeStream(inputStream);
}
}
if (progress != null) {
progress.finishTask();
}
}
use of net.osmand.plus.settings.backend.backup.items.StreamSettingsItem in project Osmand by osmandapp.
the class StreamSettingsItemWriter method streamCopy.
private void streamCopy(InputStream in, OutputStream out, IProgress pg, int bytesDivisor) throws IOException {
byte[] b = new byte[BUFFER_SIZE];
int read;
int cp = 0;
StreamSettingsItem item = getItem();
MessageDigest digest = null;
if (item.needMd5Digest()) {
try {
digest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
// ignore
}
}
while ((read = in.read(b)) != -1) {
out.write(b, 0, read);
if (digest != null) {
digest.update(b, 0, read);
}
cp += read;
if (pg != null && cp > bytesDivisor) {
pg.progress(cp / bytesDivisor);
cp = cp % bytesDivisor;
if (pg.isInterrupted()) {
throw new InterruptedIOException();
}
}
}
if (digest != null) {
byte[] md5sum = digest.digest();
item.setMd5Digest(new String(Hex.encodeHex(md5sum)));
}
}
Aggregations