Search in sources :

Example 1 with StreamSettingsItem

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);
}
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 2 with StreamSettingsItem

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();
    }
}
Also used : InputStream(java.io.InputStream) StreamSettingsItem(net.osmand.plus.settings.backend.backup.items.StreamSettingsItem)

Example 3 with StreamSettingsItem

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)));
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) StreamSettingsItem(net.osmand.plus.settings.backend.backup.items.StreamSettingsItem) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

StreamSettingsItem (net.osmand.plus.settings.backend.backup.items.StreamSettingsItem)3 SuppressLint (android.annotation.SuppressLint)1 AsyncTask (android.os.AsyncTask)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InterruptedIOException (java.io.InterruptedIOException)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 OperationLog (net.osmand.OperationLog)1 SQLiteConnection (net.osmand.plus.api.SQLiteAPI.SQLiteConnection)1 UploadedFileInfo (net.osmand.plus.backup.BackupDbHelper.UploadedFileInfo)1 ExportSettingsType (net.osmand.plus.settings.backend.ExportSettingsType)1