Search in sources :

Example 16 with FileSettingsItem

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);
}
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 17 with FileSettingsItem

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();
        }
    };
}
Also used : OnUploadFileListener(net.osmand.plus.backup.BackupListeners.OnUploadFileListener) FileSettingsItem(net.osmand.plus.settings.backend.backup.items.FileSettingsItem) NonNull(androidx.annotation.NonNull) Nullable(androidx.annotation.Nullable) NonNull(androidx.annotation.NonNull)

Example 18 with FileSettingsItem

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);
}
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 19 with FileSettingsItem

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());
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) SettingsItem(net.osmand.plus.settings.backend.backup.items.SettingsItem) GpxSettingsItem(net.osmand.plus.settings.backend.backup.items.GpxSettingsItem) FileSettingsItem(net.osmand.plus.settings.backend.backup.items.FileSettingsItem) FileSettingsItem(net.osmand.plus.settings.backend.backup.items.FileSettingsItem)

Example 20 with FileSettingsItem

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);
    }
}
Also used : OsmandApplication(net.osmand.plus.OsmandApplication) FileSettingsItem(net.osmand.plus.settings.backend.backup.items.FileSettingsItem) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

FileSettingsItem (net.osmand.plus.settings.backend.backup.items.FileSettingsItem)40 File (java.io.File)20 SettingsItem (net.osmand.plus.settings.backend.backup.items.SettingsItem)14 GpxSettingsItem (net.osmand.plus.settings.backend.backup.items.GpxSettingsItem)12 Nullable (androidx.annotation.Nullable)10 NonNull (androidx.annotation.NonNull)8 MapMarkersGroup (net.osmand.plus.mapmarkers.MapMarkersGroup)8 GlobalSettingsItem (net.osmand.plus.settings.backend.backup.items.GlobalSettingsItem)8 ArrayList (java.util.ArrayList)6 ITileSource (net.osmand.map.ITileSource)6 OnUploadFileListener (net.osmand.plus.backup.BackupListeners.OnUploadFileListener)6 AvoidRoadInfo (net.osmand.plus.helpers.AvoidSpecificRoads.AvoidRoadInfo)6 HistoryEntry (net.osmand.plus.helpers.SearchHistoryHelper.HistoryEntry)6 FavoriteGroup (net.osmand.plus.myplaces.FavoriteGroup)6 OnlineRoutingEngine (net.osmand.plus.onlinerouting.engine.OnlineRoutingEngine)6 OpenstreetmapPoint (net.osmand.plus.plugins.osmedit.data.OpenstreetmapPoint)6 OsmNotesPoint (net.osmand.plus.plugins.osmedit.data.OsmNotesPoint)6 PoiUIFilter (net.osmand.plus.poi.PoiUIFilter)6 QuickAction (net.osmand.plus.quickaction.QuickAction)6 ApplicationModeBean (net.osmand.plus.settings.backend.ApplicationMode.ApplicationModeBean)6