Add the ability to import .sqlitedb files

This commit is contained in:
Alex Sytnyk 2018-09-11 19:31:13 +03:00
parent ba24bb48de
commit 928b9cd4d8
2 changed files with 90 additions and 45 deletions

View file

@ -142,6 +142,11 @@
<data android:scheme="file" android:host="*" android:mimeType="*/*" android:pathPattern=".*\\..*\\..*\\.obf" /> <data android:scheme="file" android:host="*" android:mimeType="*/*" android:pathPattern=".*\\..*\\..*\\.obf" />
<data android:scheme="file" android:host="*" android:mimeType="*/*" android:pathPattern=".*\\..*\\..*\\..*\\.obf" /> <data android:scheme="file" android:host="*" android:mimeType="*/*" android:pathPattern=".*\\..*\\..*\\..*\\.obf" />
<data android:scheme="file" android:host="*" android:mimeType="*/*" android:pathPattern=".*\\..*\\..*\\..*\\..*\\.obf" /> <data android:scheme="file" android:host="*" android:mimeType="*/*" android:pathPattern=".*\\..*\\..*\\..*\\..*\\.obf" />
<data android:scheme="file" android:host="*" android:mimeType="*/*" android:pathPattern=".*\\.sqlitedb" />
<data android:scheme="file" android:host="*" android:mimeType="*/*" android:pathPattern=".*\\..*\\.sqlitedb" />
<data android:scheme="file" android:host="*" android:mimeType="*/*" android:pathPattern=".*\\..*\\..*\\.sqlitedb" />
<data android:scheme="file" android:host="*" android:mimeType="*/*" android:pathPattern=".*\\..*\\..*\\..*\\.sqlitedb" />
<data android:scheme="file" android:host="*" android:mimeType="*/*" android:pathPattern=".*\\..*\\..*\\..*\\..*\\.sqlitedb" />
</intent-filter> </intent-filter>
<!--trying to handle emails--> <!--trying to handle emails-->

View file

@ -14,6 +14,7 @@ import android.os.ParcelFileDescriptor;
import android.provider.OpenableColumns; import android.provider.OpenableColumns;
import android.provider.Settings; import android.provider.Settings;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
@ -135,6 +136,8 @@ public class ImportHelper {
handleKmzImport(intentUri, fileName, saveFile, useImportDir); handleKmzImport(intentUri, fileName, saveFile, useImportDir);
} else if (fileName != null && fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)) { } else if (fileName != null && fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)) {
handleObfImport(intentUri, fileName); handleObfImport(intentUri, fileName);
} else if (fileName != null && fileName.endsWith(IndexConstants.SQLITE_EXT)) {
handleSqliteTileImport(intentUri, fileName);
} else { } else {
handleFavouritesImport(intentUri, fileName, saveFile, useImportDir, false); handleFavouritesImport(intentUri, fileName, saveFile, useImportDir, false);
} }
@ -394,21 +397,49 @@ public class ImportHelper {
@Override @Override
protected String doInBackground(Void... voids) { protected String doInBackground(Void... voids) {
File dest = getObfDestFile(name); String error = copyFile(getObfDestFile(name), obfFile);
if (error == null) {
app.getResourceManager().reloadIndexes(IProgress.EMPTY_PROGRESS, new ArrayList<String>());
app.getDownloadThread().updateLoadedFiles();
return app.getString(R.string.map_imported_successfully);
}
return app.getString(R.string.map_import_error) + ": " + error;
}
@Override
protected void onPostExecute(String message) {
if (isActivityNotDestroyed(activity)) {
progress.dismiss();
}
Toast.makeText(app, message, Toast.LENGTH_SHORT).show();
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@NonNull
private File getObfDestFile(@NonNull String name) {
if (name.endsWith(IndexConstants.BINARY_ROAD_MAP_INDEX_EXT)) {
return app.getAppPath(IndexConstants.ROADS_INDEX_DIR + name);
} else if (name.endsWith(IndexConstants.BINARY_WIKI_MAP_INDEX_EXT)) {
return app.getAppPath(IndexConstants.WIKI_INDEX_DIR + name);
}
return app.getAppPath(name);
}
@Nullable
private String copyFile(@NonNull File dest, @NonNull Uri uri) {
if (dest.exists()) { if (dest.exists()) {
return app.getString(R.string.file_with_name_already_exists); return app.getString(R.string.file_with_name_already_exists);
} }
String message = app.getString(R.string.map_imported_successfully); String error = null;
InputStream in = null; InputStream in = null;
OutputStream out = null; OutputStream out = null;
try { try {
final ParcelFileDescriptor pFD = app.getContentResolver().openFileDescriptor(obfFile, "r"); final ParcelFileDescriptor pFD = app.getContentResolver().openFileDescriptor(uri, "r");
if (pFD != null) { if (pFD != null) {
in = new FileInputStream(pFD.getFileDescriptor()); in = new FileInputStream(pFD.getFileDescriptor());
out = new FileOutputStream(dest); out = new FileOutputStream(dest);
Algorithms.streamCopy(in, out); Algorithms.streamCopy(in, out);
app.getResourceManager().reloadIndexes(IProgress.EMPTY_PROGRESS, new ArrayList<String>());
app.getDownloadThread().updateLoadedFiles();
try { try {
pFD.close(); pFD.close();
} catch (IOException e) { } catch (IOException e) {
@ -417,10 +448,10 @@ public class ImportHelper {
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
message = app.getString(R.string.map_import_error) + ": " + e.getMessage(); error = e.getMessage();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
message = app.getString(R.string.map_import_error) + ": " + e.getMessage(); error = e.getMessage();
} finally { } finally {
if (in != null) { if (in != null) {
try { try {
@ -437,30 +468,39 @@ public class ImportHelper {
} }
} }
} }
return error;
}
return message; @SuppressLint("StaticFieldLeak")
private void handleSqliteTileImport(final Uri uri, final String name) {
new AsyncTask<Void, Void, String>() {
ProgressDialog progress;
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(activity, app.getString(R.string.loading_smth, ""), app.getString(R.string.loading_data));
} }
@Override @Override
protected void onPostExecute(String message) { protected String doInBackground(Void... voids) {
return copyFile(app.getAppPath(IndexConstants.TILES_INDEX_DIR + name), uri);
}
@Override
protected void onPostExecute(String error) {
if (isActivityNotDestroyed(activity)) { if (isActivityNotDestroyed(activity)) {
progress.dismiss(); progress.dismiss();
} }
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show(); if (error == null) {
Toast.makeText(app, app.getString(R.string.map_imported_successfully), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(app, app.getString(R.string.map_import_error) + ": " + error, Toast.LENGTH_SHORT).show();
}
} }
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
@NonNull
private File getObfDestFile(@NonNull String name) {
if (name.endsWith(IndexConstants.BINARY_ROAD_MAP_INDEX_EXT)) {
return app.getAppPath(IndexConstants.ROADS_INDEX_DIR + name);
} else if (name.endsWith(IndexConstants.BINARY_WIKI_MAP_INDEX_EXT)) {
return app.getAppPath(IndexConstants.WIKI_INDEX_DIR + name);
}
return app.getAppPath(name);
}
private boolean isActivityNotDestroyed(Activity activity) { private boolean isActivityNotDestroyed(Activity activity) {
if (Build.VERSION.SDK_INT >= 17) { if (Build.VERSION.SDK_INT >= 17) {
return !activity.isFinishing() && !activity.isDestroyed(); return !activity.isFinishing() && !activity.isDestroyed();