diff --git a/OsmAnd/AndroidManifest.xml b/OsmAnd/AndroidManifest.xml index d77f0f8500..7986405e54 100644 --- a/OsmAnd/AndroidManifest.xml +++ b/OsmAnd/AndroidManifest.xml @@ -137,6 +137,11 @@ + + + + + diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index ad57d5f7b0..8a80f4e083 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -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 --> + Map import error + Map imported successfully Make as Start Point Current Adds the last stop along the route diff --git a/OsmAnd/src/net/osmand/plus/helpers/GpxImportHelper.java b/OsmAnd/src/net/osmand/plus/helpers/GpxImportHelper.java index 4c905a3f5b..617b270e2f 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/GpxImportHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/GpxImportHelper.java @@ -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() { 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() { 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() { 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() { 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() { 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() { + + 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()); + 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();