use of net.osmand.plus.settings.backend.backup.items.FileSettingsItem 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.FileSettingsItem in project OsmAnd by osmandapp.
the class NetworkWriter method getUploadDirListener.
@NonNull
private OnUploadFileListener getUploadDirListener(@NonNull SettingsItem item, @NonNull String itemFileName, int itemWork) {
return new OnUploadFileListener() {
private int itemProgress = 0;
private int deltaProgress = 0;
private boolean uploadStarted = false;
@Override
public void onFileUploadStarted(@NonNull String type, @NonNull String fileName, int work) {
if (!uploadStarted && listener != null) {
uploadStarted = true;
listener.onItemUploadStarted(item, itemFileName, itemWork);
}
}
@Override
public void onFileUploadProgress(@NonNull String type, @NonNull String fileName, int progress, int deltaWork) {
if (listener != null) {
deltaProgress += deltaWork;
if ((deltaProgress > (itemWork / 100)) || ((itemProgress + deltaProgress) >= itemWork)) {
itemProgress += deltaProgress;
listener.onItemUploadProgress(item, itemFileName, itemProgress, deltaProgress);
deltaProgress = 0;
}
}
}
@Override
public void onFileUploadDone(@NonNull String type, @NonNull String fileName, long uploadTime, @Nullable String error) {
if (item instanceof FileSettingsItem) {
FileSettingsItem fileItem = (FileSettingsItem) item;
String itemFileName = BackupHelper.getFileItemName(fileItem);
if (backupHelper.getApp().getAppPath(itemFileName).isDirectory()) {
backupHelper.updateFileUploadTime(item.getType().name(), itemFileName, uploadTime);
}
if (fileItem.needMd5Digest() && !Algorithms.isEmpty(fileItem.getMd5Digest())) {
backupHelper.updateFileMd5Digest(item.getType().name(), fileName, fileItem.getMd5Digest());
}
}
if (listener != null) {
listener.onItemFileUploadDone(item, fileName, uploadTime, error);
}
}
@Override
public boolean isUploadCancelled() {
return isCancelled();
}
};
}
use of net.osmand.plus.settings.backend.backup.items.FileSettingsItem 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.FileSettingsItem in project Osmand by osmandapp.
the class BackupImporter method updateFilesInfo.
private void updateFilesInfo(@NonNull Map<String, RemoteFile> remoteFiles, @NonNull List<SettingsItem> settingsItemList) {
Map<String, RemoteFile> remoteFilesMap = new HashMap<>(remoteFiles);
for (SettingsItem settingsItem : settingsItemList) {
List<RemoteFile> foundRemoteFiles = getItemRemoteFiles(settingsItem, remoteFilesMap);
for (RemoteFile remoteFile : foundRemoteFiles) {
settingsItem.setLastModifiedTime(remoteFile.getClienttimems());
remoteFile.item = settingsItem;
if (settingsItem instanceof FileSettingsItem) {
FileSettingsItem fileSettingsItem = (FileSettingsItem) settingsItem;
fileSettingsItem.setSize(remoteFile.getFilesize());
}
}
}
}
use of net.osmand.plus.settings.backend.backup.items.FileSettingsItem in project Osmand by osmandapp.
the class BackupImporter method importItemFile.
private void importItemFile(@NonNull RemoteFile remoteFile, @NonNull SettingsItem item, boolean forceReadData) {
OsmandApplication app = backupHelper.getApp();
File tempDir = FileUtils.getTempDir(app);
FileInputStream is = null;
try {
SettingsItemReader<? extends SettingsItem> reader = item.getReader();
if (reader != null) {
String fileName = remoteFile.getTypeNamePath();
File tempFile = new File(tempDir, fileName);
String error = backupHelper.downloadFile(tempFile, remoteFile, getOnDownloadItemFileListener(item));
if (Algorithms.isEmpty(error)) {
is = new FileInputStream(tempFile);
reader.readFromStream(is, remoteFile.getName());
if (forceReadData) {
item.apply();
}
backupHelper.updateFileUploadTime(remoteFile.getType(), remoteFile.getName(), remoteFile.getClienttimems());
if (item instanceof FileSettingsItem) {
String itemFileName = BackupHelper.getFileItemName((FileSettingsItem) item);
if (app.getAppPath(itemFileName).isDirectory()) {
backupHelper.updateFileUploadTime(item.getType().name(), itemFileName, remoteFile.getClienttimems());
}
}
} else {
throw new IOException("Error reading temp item file " + fileName + ": " + error);
}
}
item.applyAdditionalParams(reader);
} catch (IllegalArgumentException e) {
item.getWarnings().add(app.getString(R.string.settings_item_read_error, item.getName()));
LOG.error("Error reading item data: " + item.getName(), e);
} catch (IOException e) {
item.getWarnings().add(app.getString(R.string.settings_item_read_error, item.getName()));
LOG.error("Error reading item data: " + item.getName(), e);
} catch (UserNotRegisteredException e) {
item.getWarnings().add(app.getString(R.string.settings_item_read_error, item.getName()));
LOG.error("Error reading item data: " + item.getName(), e);
} finally {
Algorithms.closeStream(is);
}
}
Aggregations