Search in sources :

Example 16 with ExportSettingsType

use of net.osmand.plus.settings.backend.ExportSettingsType in project Osmand by osmandapp.

the class BackupInfo method createFilteredFilesToMerge.

private void createFilteredFilesToMerge(@NonNull OsmandApplication app) {
    List<Pair<LocalFile, RemoteFile>> files = new ArrayList<>();
    Set<SettingsItem> items = new HashSet<>();
    BackupHelper helper = app.getBackupHelper();
    for (Pair<LocalFile, RemoteFile> pair : filesToMerge) {
        SettingsItem item = pair.first.item;
        if (!items.contains(item)) {
            ExportSettingsType exportType = ExportSettingsType.getExportSettingsTypeForRemoteFile(pair.second);
            if (exportType != null && helper.getBackupTypePref(exportType).get()) {
                files.add(pair);
                items.add(item);
            }
        }
    }
    filteredFilesToMerge = files;
}
Also used : SettingsItem(net.osmand.plus.settings.backend.backup.items.SettingsItem) ArrayList(java.util.ArrayList) Pair(android.util.Pair) HashSet(java.util.HashSet) ExportSettingsType(net.osmand.plus.settings.backend.ExportSettingsType)

Example 17 with ExportSettingsType

use of net.osmand.plus.settings.backend.ExportSettingsType in project Osmand by osmandapp.

the class DeleteAllFilesCommand method getDeleteAllFilesListener.

private OnRequestResultListener getDeleteAllFilesListener() {
    return (resultJson, error, resultCode) -> {
        int status;
        String message;
        List<RemoteFile> remoteFiles = new ArrayList<>();
        if (!Algorithms.isEmpty(error)) {
            status = STATUS_SERVER_ERROR;
            message = "Download file list error: " + new BackupError(error);
        } else if (!Algorithms.isEmpty(resultJson)) {
            try {
                JSONObject result = new JSONObject(resultJson);
                JSONArray files = result.getJSONArray("allFiles");
                for (int i = 0; i < files.length(); i++) {
                    remoteFiles.add(new RemoteFile(files.getJSONObject(i)));
                }
                status = STATUS_SUCCESS;
                message = "OK";
            } catch (JSONException e) {
                status = STATUS_PARSE_JSON_ERROR;
                message = "Download file list error: json parsing";
            }
        } else {
            status = STATUS_EMPTY_RESPONSE_ERROR;
            message = "Download file list error: empty response";
        }
        if (status != STATUS_SUCCESS) {
            publishProgress(status, message);
        } else {
            List<RemoteFile> filesToDelete = new ArrayList<>();
            if (types != null) {
                for (RemoteFile file : remoteFiles) {
                    ExportSettingsType exportType = ExportSettingsType.getExportSettingsTypeForRemoteFile(file);
                    if (types.contains(exportType)) {
                        filesToDelete.add(file);
                    }
                }
            } else {
                filesToDelete.addAll(remoteFiles);
            }
            if (!filesToDelete.isEmpty()) {
                publishProgress(filesToDelete);
                deleteFiles(filesToDelete);
            } else {
                publishProgress(Collections.emptyMap());
            }
        }
    };
}
Also used : BackupHelper(net.osmand.plus.backup.BackupHelper) OnRequestResultListener(net.osmand.plus.utils.AndroidNetworkUtils.OnRequestResultListener) STATUS_EMPTY_RESPONSE_ERROR(net.osmand.plus.backup.BackupHelper.STATUS_EMPTY_RESPONSE_ERROR) STATUS_SUCCESS(net.osmand.plus.backup.BackupHelper.STATUS_SUCCESS) NonNull(androidx.annotation.NonNull) LIST_FILES_URL(net.osmand.plus.backup.BackupHelper.LIST_FILES_URL) HashMap(java.util.HashMap) OnDeleteFilesListener(net.osmand.plus.backup.BackupListeners.OnDeleteFilesListener) BackupError(net.osmand.plus.backup.BackupError) AndroidNetworkUtils(net.osmand.plus.utils.AndroidNetworkUtils) ExportSettingsType(net.osmand.plus.settings.backend.ExportSettingsType) ArrayList(java.util.ArrayList) STATUS_PARSE_JSON_ERROR(net.osmand.plus.backup.BackupHelper.STATUS_PARSE_JSON_ERROR) JSONException(org.json.JSONException) List(java.util.List) Nullable(androidx.annotation.Nullable) JSONObject(org.json.JSONObject) STATUS_SERVER_ERROR(net.osmand.plus.backup.BackupHelper.STATUS_SERVER_ERROR) Map(java.util.Map) Algorithms(net.osmand.util.Algorithms) RemoteFile(net.osmand.plus.backup.RemoteFile) Collections(java.util.Collections) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) ArrayList(java.util.ArrayList) List(java.util.List) BackupError(net.osmand.plus.backup.BackupError) RemoteFile(net.osmand.plus.backup.RemoteFile) ExportSettingsType(net.osmand.plus.settings.backend.ExportSettingsType)

Example 18 with ExportSettingsType

use of net.osmand.plus.settings.backend.ExportSettingsType in project Osmand by osmandapp.

the class BackupHelper method generateBackupInfo.

@SuppressLint("StaticFieldLeak")
void generateBackupInfo(@NonNull final Map<String, LocalFile> localFiles, @NonNull final Map<String, RemoteFile> uniqueRemoteFiles, @NonNull final Map<String, RemoteFile> deletedRemoteFiles, @Nullable final OnGenerateBackupInfoListener listener) {
    OperationLog operationLog = new OperationLog("generateBackupInfo", DEBUG, 200);
    operationLog.startOperation();
    AsyncTask<Void, Void, BackupInfo> task = new AsyncTask<Void, Void, BackupInfo>() {

        @Override
        protected BackupInfo doInBackground(Void... voids) {
            BackupInfo info = new BackupInfo();
            /*
				operationLog.log("=== localFiles ===");
				for (LocalFile localFile : localFiles.values()) {
					operationLog.log(localFile.toString());
				}
				operationLog.log("=== localFiles ===");
				operationLog.log("=== uniqueRemoteFiles ===");
				for (RemoteFile remoteFile : uniqueRemoteFiles.values()) {
					operationLog.log(remoteFile.toString());
				}
				operationLog.log("=== uniqueRemoteFiles ===");
				operationLog.log("=== deletedRemoteFiles ===");
				for (RemoteFile remoteFile : deletedRemoteFiles.values()) {
					operationLog.log(remoteFile.toString());
				}
				operationLog.log("=== deletedRemoteFiles ===");
				*/
            List<RemoteFile> remoteFiles = new ArrayList<>(uniqueRemoteFiles.values());
            remoteFiles.addAll(deletedRemoteFiles.values());
            for (RemoteFile remoteFile : remoteFiles) {
                ExportSettingsType exportType = ExportSettingsType.getExportSettingsTypeForRemoteFile(remoteFile);
                if (exportType == null || !ExportSettingsType.isTypeEnabled(exportType) || remoteFile.isRecordedVoiceFile()) {
                    continue;
                }
                LocalFile localFile = localFiles.get(remoteFile.getTypeNamePath());
                if (localFile != null) {
                    long remoteUploadTime = remoteFile.getClienttimems();
                    long localUploadTime = localFile.uploadTime;
                    if (remoteFile.isDeleted()) {
                        info.localFilesToDelete.add(localFile);
                    } else if (remoteUploadTime == localUploadTime) {
                        if (localUploadTime < localFile.localModifiedTime) {
                            info.filesToUpload.add(localFile);
                            info.filesToDownload.add(remoteFile);
                        }
                    } else {
                        info.filesToMerge.add(new Pair<>(localFile, remoteFile));
                        info.filesToDownload.add(remoteFile);
                    }
                    long localFileSize = localFile.file == null ? 0 : localFile.file.length();
                    long remoteFileSize = remoteFile.getFilesize();
                    if (remoteFileSize > 0 && localFileSize > 0 && localFileSize != remoteFileSize && !info.filesToDownload.contains(remoteFile)) {
                        info.filesToDownload.add(remoteFile);
                    }
                }
                if (localFile == null && !remoteFile.isDeleted()) {
                    UploadedFileInfo fileInfo = dbHelper.getUploadedFileInfo(remoteFile.getType(), remoteFile.getName());
                    // suggest to remove only if file exists in db
                    if (fileInfo != null) {
                        info.filesToDelete.add(remoteFile);
                    }
                    info.filesToDownload.add(remoteFile);
                }
            }
            for (LocalFile localFile : localFiles.values()) {
                ExportSettingsType exportType = localFile.item != null ? ExportSettingsType.getExportSettingsTypeForItem(localFile.item) : null;
                if (exportType == null || !ExportSettingsType.isTypeEnabled(exportType)) {
                    continue;
                }
                boolean hasRemoteFile = uniqueRemoteFiles.containsKey(localFile.getTypeFileName());
                if (!hasRemoteFile) {
                    boolean isEmpty = localFile.item instanceof CollectionSettingsItem<?> && ((CollectionSettingsItem<?>) localFile.item).isEmpty();
                    if (!isEmpty) {
                        info.filesToUpload.add(localFile);
                    }
                }
            }
            info.createItemCollections(app);
            operationLog.log("=== filesToUpload ===");
            for (LocalFile localFile : info.filesToUpload) {
                operationLog.log(localFile.toString());
            }
            operationLog.log("=== filesToUpload ===");
            operationLog.log("=== filesToDownload ===");
            for (RemoteFile remoteFile : info.filesToDownload) {
                operationLog.log(remoteFile.toString());
            }
            operationLog.log("=== filesToDownload ===");
            operationLog.log("=== filesToDelete ===");
            for (RemoteFile remoteFile : info.filesToDelete) {
                operationLog.log(remoteFile.toString());
            }
            operationLog.log("=== filesToDelete ===");
            operationLog.log("=== filesToMerge ===");
            for (Pair<LocalFile, RemoteFile> filePair : info.filesToMerge) {
                operationLog.log("LOCAL=" + filePair.first.toString() + " REMOTE=" + filePair.second.toString());
            }
            operationLog.log("=== filesToMerge ===");
            return info;
        }

        @Override
        protected void onPostExecute(BackupInfo backupInfo) {
            operationLog.finishOperation(backupInfo.toString());
            if (listener != null) {
                listener.onBackupInfoGenerated(backupInfo, null);
            }
        }
    };
    task.executeOnExecutor(executor);
}
Also used : AsyncTask(android.os.AsyncTask) ArrayList(java.util.ArrayList) OperationLog(net.osmand.OperationLog) UploadedFileInfo(net.osmand.plus.backup.BackupDbHelper.UploadedFileInfo) ExportSettingsType(net.osmand.plus.settings.backend.ExportSettingsType) Pair(android.util.Pair) SuppressLint(android.annotation.SuppressLint)

Example 19 with ExportSettingsType

use of net.osmand.plus.settings.backend.ExportSettingsType 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);
}
Also used : ArrayList(java.util.ArrayList) OperationLog(net.osmand.OperationLog) UploadedFileInfo(net.osmand.plus.backup.BackupDbHelper.UploadedFileInfo) GpxSettingsItem(net.osmand.plus.settings.backend.backup.items.GpxSettingsItem) SettingsItem(net.osmand.plus.settings.backend.backup.items.SettingsItem) CollectionSettingsItem(net.osmand.plus.settings.backend.backup.items.CollectionSettingsItem) StreamSettingsItem(net.osmand.plus.settings.backend.backup.items.StreamSettingsItem) FileSettingsItem(net.osmand.plus.settings.backend.backup.items.FileSettingsItem) FileSettingsItem(net.osmand.plus.settings.backend.backup.items.FileSettingsItem) NonNull(androidx.annotation.NonNull) List(java.util.List) ArrayList(java.util.ArrayList) ExportSettingsType(net.osmand.plus.settings.backend.ExportSettingsType) AsyncTask(android.os.AsyncTask) GpxSettingsItem(net.osmand.plus.settings.backend.backup.items.GpxSettingsItem) StreamSettingsItem(net.osmand.plus.settings.backend.backup.items.StreamSettingsItem) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SQLiteConnection(net.osmand.plus.api.SQLiteAPI.SQLiteConnection) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File) Nullable(androidx.annotation.Nullable) SuppressLint(android.annotation.SuppressLint)

Example 20 with ExportSettingsType

use of net.osmand.plus.settings.backend.ExportSettingsType in project Osmand by osmandapp.

the class DeleteOldFilesCommand method getDeleteOldFilesListener.

private OnRequestResultListener getDeleteOldFilesListener() {
    return (resultJson, error, resultCode) -> {
        int status;
        String message;
        List<RemoteFile> remoteFiles = new ArrayList<>();
        if (!Algorithms.isEmpty(error)) {
            status = STATUS_SERVER_ERROR;
            message = "Download file list error: " + new BackupError(error);
        } else if (!Algorithms.isEmpty(resultJson)) {
            try {
                JSONObject result = new JSONObject(resultJson);
                JSONArray allFiles = result.getJSONArray("allFiles");
                for (int i = 0; i < allFiles.length(); i++) {
                    remoteFiles.add(new RemoteFile(allFiles.getJSONObject(i)));
                }
                JSONArray uniqueFiles = result.getJSONArray("uniqueFiles");
                for (int i = 0; i < uniqueFiles.length(); i++) {
                    remoteFiles.remove(new RemoteFile(uniqueFiles.getJSONObject(i)));
                }
                status = STATUS_SUCCESS;
                message = "OK";
            } catch (JSONException e) {
                status = STATUS_PARSE_JSON_ERROR;
                message = "Download file list error: json parsing";
            }
        } else {
            status = STATUS_EMPTY_RESPONSE_ERROR;
            message = "Download file list error: empty response";
        }
        if (status != STATUS_SUCCESS) {
            publishProgress(status, message);
        } else {
            List<RemoteFile> filesToDelete = new ArrayList<>();
            if (types != null) {
                for (RemoteFile file : remoteFiles) {
                    ExportSettingsType exportType = ExportSettingsType.getExportSettingsTypeForRemoteFile(file);
                    if (types.contains(exportType)) {
                        filesToDelete.add(file);
                    }
                }
            } else {
                filesToDelete.addAll(remoteFiles);
            }
            if (!filesToDelete.isEmpty()) {
                publishProgress(filesToDelete);
                deleteFiles(filesToDelete);
            } else {
                publishProgress(Collections.emptyMap());
            }
        }
    };
}
Also used : BackupHelper(net.osmand.plus.backup.BackupHelper) OnRequestResultListener(net.osmand.plus.utils.AndroidNetworkUtils.OnRequestResultListener) STATUS_EMPTY_RESPONSE_ERROR(net.osmand.plus.backup.BackupHelper.STATUS_EMPTY_RESPONSE_ERROR) STATUS_SUCCESS(net.osmand.plus.backup.BackupHelper.STATUS_SUCCESS) NonNull(androidx.annotation.NonNull) LIST_FILES_URL(net.osmand.plus.backup.BackupHelper.LIST_FILES_URL) HashMap(java.util.HashMap) OnDeleteFilesListener(net.osmand.plus.backup.BackupListeners.OnDeleteFilesListener) BackupError(net.osmand.plus.backup.BackupError) AndroidNetworkUtils(net.osmand.plus.utils.AndroidNetworkUtils) ExportSettingsType(net.osmand.plus.settings.backend.ExportSettingsType) ArrayList(java.util.ArrayList) STATUS_PARSE_JSON_ERROR(net.osmand.plus.backup.BackupHelper.STATUS_PARSE_JSON_ERROR) JSONException(org.json.JSONException) List(java.util.List) Nullable(androidx.annotation.Nullable) JSONObject(org.json.JSONObject) STATUS_SERVER_ERROR(net.osmand.plus.backup.BackupHelper.STATUS_SERVER_ERROR) Map(java.util.Map) Algorithms(net.osmand.util.Algorithms) RemoteFile(net.osmand.plus.backup.RemoteFile) Collections(java.util.Collections) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) ArrayList(java.util.ArrayList) List(java.util.List) BackupError(net.osmand.plus.backup.BackupError) RemoteFile(net.osmand.plus.backup.RemoteFile) ExportSettingsType(net.osmand.plus.settings.backend.ExportSettingsType)

Aggregations

ExportSettingsType (net.osmand.plus.settings.backend.ExportSettingsType)36 ArrayList (java.util.ArrayList)18 List (java.util.List)10 SettingsCategoryItems (net.osmand.plus.settings.fragments.SettingsCategoryItems)9 ExportSettingsCategory (net.osmand.plus.settings.backend.ExportSettingsCategory)6 View (android.view.View)5 ImageView (android.widget.ImageView)5 TextView (android.widget.TextView)5 File (java.io.File)5 LinkedHashMap (java.util.LinkedHashMap)5 Nullable (androidx.annotation.Nullable)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 SettingsItem (net.osmand.plus.settings.backend.backup.items.SettingsItem)4 NonNull (androidx.annotation.NonNull)3 EnumMap (java.util.EnumMap)3 ApplicationMode (net.osmand.plus.settings.backend.ApplicationMode)3 SuppressLint (android.annotation.SuppressLint)2 Typeface (android.graphics.Typeface)2 AsyncTask (android.os.AsyncTask)2