Add the ability to import obf files

This commit is contained in:
Alexander Sytnyk 2018-02-13 19:52:45 +02:00
parent f4e4b960ed
commit ff8734d505
3 changed files with 100 additions and 0 deletions

View file

@ -137,6 +137,11 @@
<data android:scheme="file" android:host="*" android:mimeType="*/*" android:pathPattern=".*\\..*\\..*\\.kmz" />
<data android:scheme="file" android:host="*" android:mimeType="*/*" android:pathPattern=".*\\..*\\..*\\..*\\.kmz" />
<data android:scheme="file" android:host="*" android:mimeType="*/*" android:pathPattern=".*\\..*\\..*\\..*\\..*\\.kmz" />
<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" />
</intent-filter>
<!--trying to handle emails-->

View file

@ -9,6 +9,8 @@
3. All your modified/created strings are in the top of the file (to make easier find what\'s translated).
PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy
-->
<string name="map_import_error">Map import error</string>
<string name="map_imported_successfully">Map imported successfully</string>
<string name="make_as_start_point">Make as Start Point</string>
<string name="shared_string_current">Current</string>
<string name="last_intermediate_dest_description">Adds the last stop along the route</string>

View file

@ -1,5 +1,6 @@
package net.osmand.plus.helpers;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
@ -12,6 +13,7 @@ import android.os.Bundle;
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.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
@ -27,6 +29,7 @@ import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import net.osmand.IProgress;
import net.osmand.IndexConstants;
import net.osmand.data.FavouritePoint;
import net.osmand.plus.FavouritesDbHelper;
@ -38,13 +41,16 @@ import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
import net.osmand.plus.myplaces.FavoritesActivity;
import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.util.Algorithms;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
@ -129,6 +135,8 @@ public class GpxImportHelper {
handleKmlImport(intentUri, fileName, saveFile, useImportDir);
} else if (fileName != null && fileName.endsWith(KMZ_SUFFIX)) {
handleKmzImport(intentUri, fileName, saveFile, useImportDir);
} else if (fileName != null && fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)) {
handleObfImport(intentUri, fileName);
} else {
handleFavouritesImport(intentUri, fileName, saveFile, useImportDir, false);
}
@ -153,6 +161,7 @@ public class GpxImportHelper {
return name;
}
@SuppressLint("StaticFieldLeak")
private void handleGpxImport(final Uri gpxFile, final String fileName, final boolean save, final boolean useImportDir) {
new AsyncTask<Void, Void, GPXFile>() {
ProgressDialog progress = null;
@ -193,6 +202,7 @@ public class GpxImportHelper {
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@SuppressLint("StaticFieldLeak")
private void handleFavouritesImport(final Uri gpxFile, final String fileName, final boolean save, final boolean useImportDir, final boolean forceImportFavourites) {
new AsyncTask<Void, Void, GPXFile>() {
ProgressDialog progress = null;
@ -233,6 +243,7 @@ public class GpxImportHelper {
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@SuppressLint("StaticFieldLeak")
private void importFavoritesImpl(final GPXFile gpxFile, final String fileName, final boolean forceImportFavourites) {
new AsyncTask<Void, Void, GPXFile>() {
ProgressDialog progress = null;
@ -269,6 +280,7 @@ public class GpxImportHelper {
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@SuppressLint("StaticFieldLeak")
private void handleKmzImport(final Uri kmzFile, final String name, final boolean save, final boolean useImportDir) {
new AsyncTask<Void, Void, GPXFile>() {
ProgressDialog progress = null;
@ -324,6 +336,7 @@ public class GpxImportHelper {
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@SuppressLint("StaticFieldLeak")
private void handleKmlImport(final Uri kmlFile, final String name, final boolean save, final boolean useImportDir) {
new AsyncTask<Void, Void, GPXFile>() {
ProgressDialog progress = null;
@ -370,6 +383,86 @@ public class GpxImportHelper {
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@SuppressLint("StaticFieldLeak")
private void handleObfImport(final Uri obfFile, 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 String doInBackground(Void... voids) {
File dest = getObfDestFile(name);
if (dest.exists()) {
return app.getString(R.string.file_with_name_already_exists);
}
String message = app.getString(R.string.map_imported_successfully);
InputStream in = null;
OutputStream out = null;
try {
final ParcelFileDescriptor pFD = app.getContentResolver().openFileDescriptor(obfFile, "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) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
message = app.getString(R.string.map_import_error) + ": " + e.getMessage();
} catch (IOException e) {
e.printStackTrace();
message = app.getString(R.string.map_import_error) + ": " + e.getMessage();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return message;
}
@Override
protected void onPostExecute(String message) {
if (isActivityNotDestroyed(activity)) {
progress.dismiss();
}
Toast.makeText(activity, 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);
}
private boolean isActivityNotDestroyed(Activity activity) {
if (Build.VERSION.SDK_INT >= 17) {
return !activity.isFinishing() && !activity.isDestroyed();