Add the ability to import .sqlitedb files
This commit is contained in:
parent
ba24bb48de
commit
928b9cd4d8
2 changed files with 90 additions and 45 deletions
|
@ -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=".*\\.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>
|
||||
|
||||
<!--trying to handle emails-->
|
||||
|
|
|
@ -14,6 +14,7 @@ import android.os.ParcelFileDescriptor;
|
|||
import android.provider.OpenableColumns;
|
||||
import android.provider.Settings;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.text.SpannableStringBuilder;
|
||||
|
@ -135,6 +136,8 @@ public class ImportHelper {
|
|||
handleKmzImport(intentUri, fileName, saveFile, useImportDir);
|
||||
} else if (fileName != null && fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)) {
|
||||
handleObfImport(intentUri, fileName);
|
||||
} else if (fileName != null && fileName.endsWith(IndexConstants.SQLITE_EXT)) {
|
||||
handleSqliteTileImport(intentUri, fileName);
|
||||
} else {
|
||||
handleFavouritesImport(intentUri, fileName, saveFile, useImportDir, false);
|
||||
}
|
||||
|
@ -394,21 +397,49 @@ public class ImportHelper {
|
|||
|
||||
@Override
|
||||
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()) {
|
||||
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;
|
||||
OutputStream out = null;
|
||||
try {
|
||||
final ParcelFileDescriptor pFD = app.getContentResolver().openFileDescriptor(obfFile, "r");
|
||||
final ParcelFileDescriptor pFD = app.getContentResolver().openFileDescriptor(uri, "r");
|
||||
if (pFD != null) {
|
||||
in = new FileInputStream(pFD.getFileDescriptor());
|
||||
out = new FileOutputStream(dest);
|
||||
Algorithms.streamCopy(in, out);
|
||||
app.getResourceManager().reloadIndexes(IProgress.EMPTY_PROGRESS, new ArrayList<String>());
|
||||
app.getDownloadThread().updateLoadedFiles();
|
||||
try {
|
||||
pFD.close();
|
||||
} catch (IOException e) {
|
||||
|
@ -417,10 +448,10 @@ public class ImportHelper {
|
|||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
message = app.getString(R.string.map_import_error) + ": " + e.getMessage();
|
||||
error = e.getMessage();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
message = app.getString(R.string.map_import_error) + ": " + e.getMessage();
|
||||
error = e.getMessage();
|
||||
} finally {
|
||||
if (in != null) {
|
||||
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
|
||||
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)) {
|
||||
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);
|
||||
}
|
||||
|
||||
@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) {
|
||||
if (Build.VERSION.SDK_INT >= 17) {
|
||||
return !activity.isFinishing() && !activity.isDestroyed();
|
||||
|
|
Loading…
Reference in a new issue