From 894a36fed8fd71624e214a433431a6780130a555 Mon Sep 17 00:00:00 2001 From: max-klaus Date: Wed, 3 Feb 2021 21:17:18 +0300 Subject: [PATCH 1/6] Fix OutOfMemory while uploading image to OPR --- .../mapcontextmenu/UploadPhotosAsyncTask.java | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/UploadPhotosAsyncTask.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/UploadPhotosAsyncTask.java index e552cb1373..93010034c0 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/UploadPhotosAsyncTask.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/UploadPhotosAsyncTask.java @@ -4,7 +4,6 @@ import android.content.DialogInterface; import android.content.DialogInterface.OnDismissListener; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.graphics.Matrix; import android.net.Uri; import android.os.AsyncTask; @@ -29,6 +28,7 @@ import org.openplacereviews.opendb.util.exception.FailedVerificationException; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.InputStream; import java.lang.ref.WeakReference; import java.util.ArrayList; @@ -116,7 +116,7 @@ public class UploadPhotosAsyncTask extends AsyncTask { return success; } - private boolean uploadImageToPlace(InputStream image) { + private boolean uploadImageToPlace(InputStream image) throws IOException { boolean success = false; InputStream serverData = new ByteArrayInputStream(compressImageToJpeg(image)); String baseUrl = OPRConstants.getBaseUrl(app); @@ -178,27 +178,31 @@ public class UploadPhotosAsyncTask extends AsyncTask { } } - private byte[] compressImageToJpeg(InputStream image) { + private byte[] compressImageToJpeg(InputStream image) throws IOException { BufferedInputStream bufferedInputStream = new BufferedInputStream(image); + bufferedInputStream.mark(1024 * 200); BitmapFactory.Options opts = new BitmapFactory.Options(); - opts.inSampleSize = 4; - Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream, null, opts); + opts.inJustDecodeBounds = true; + BitmapFactory.decodeStream(bufferedInputStream, null, opts); + int w = opts.outWidth; + int h = opts.outHeight; + bufferedInputStream.reset(); ByteArrayOutputStream os = new ByteArrayOutputStream(); - int h = bmp.getHeight(); - int w = bmp.getWidth(); boolean scale = false; + int divider = 1; while (w > MAX_IMAGE_LENGTH || h > MAX_IMAGE_LENGTH) { - w = w / 2; - h = h / 2; + w /= 2; + h /= 2; + divider *= 2; scale = true; } + Bitmap bmp; if (scale) { - Matrix matrix = new Matrix(); - matrix.postScale(w, h); - Bitmap resizedBitmap = Bitmap.createBitmap( - bmp, 0, 0, w, h, matrix, false); - bmp.recycle(); - bmp = resizedBitmap; + opts = new BitmapFactory.Options(); + opts.inSampleSize = divider; + bmp = BitmapFactory.decodeStream(bufferedInputStream, null, opts); + } else { + bmp = BitmapFactory.decodeStream(bufferedInputStream); } bmp.compress(Bitmap.CompressFormat.JPEG, 90, os); return os.toByteArray(); From 4dc202dbc9c6e59e9f6f195d0b7f8d38314beb74 Mon Sep 17 00:00:00 2001 From: max-klaus Date: Wed, 3 Feb 2021 21:30:43 +0300 Subject: [PATCH 2/6] Fix mark / reset --- .../mapcontextmenu/UploadPhotosAsyncTask.java | 40 ++++++++++++++----- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/UploadPhotosAsyncTask.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/UploadPhotosAsyncTask.java index 93010034c0..2cfc70f14e 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/UploadPhotosAsyncTask.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/UploadPhotosAsyncTask.java @@ -28,7 +28,6 @@ import org.openplacereviews.opendb.util.exception.FailedVerificationException; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.io.InputStream; import java.lang.ref.WeakReference; import java.util.ArrayList; @@ -102,10 +101,11 @@ public class UploadPhotosAsyncTask extends AsyncTask { private boolean handleSelectedImage(final Uri uri) { boolean success = false; InputStream inputStream = null; + int[] imageDimensions = null; try { inputStream = app.getContentResolver().openInputStream(uri); if (inputStream != null) { - success = uploadImageToPlace(inputStream); + imageDimensions = calcImageDimensions(inputStream); } } catch (Exception e) { LOG.error(e); @@ -113,12 +113,27 @@ public class UploadPhotosAsyncTask extends AsyncTask { } finally { Algorithms.closeStream(inputStream); } + if (imageDimensions != null && imageDimensions.length == 2) { + try { + inputStream = app.getContentResolver().openInputStream(uri); + if (inputStream != null) { + int width = imageDimensions[0]; + int height = imageDimensions[1]; + success = uploadImageToPlace(inputStream, width, height); + } + } catch (Exception e) { + LOG.error(e); + app.showToastMessage(R.string.cannot_upload_image); + } finally { + Algorithms.closeStream(inputStream); + } + } return success; } - private boolean uploadImageToPlace(InputStream image) throws IOException { + private boolean uploadImageToPlace(InputStream image, int width, int height) { boolean success = false; - InputStream serverData = new ByteArrayInputStream(compressImageToJpeg(image)); + InputStream serverData = new ByteArrayInputStream(compressImageToJpeg(image, width, height)); String baseUrl = OPRConstants.getBaseUrl(app); // all these should be constant String url = baseUrl + "api/ipfs/image"; @@ -178,16 +193,18 @@ public class UploadPhotosAsyncTask extends AsyncTask { } } - private byte[] compressImageToJpeg(InputStream image) throws IOException { + private int[] calcImageDimensions(InputStream image) { BufferedInputStream bufferedInputStream = new BufferedInputStream(image); - bufferedInputStream.mark(1024 * 200); BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; BitmapFactory.decodeStream(bufferedInputStream, null, opts); - int w = opts.outWidth; - int h = opts.outHeight; - bufferedInputStream.reset(); - ByteArrayOutputStream os = new ByteArrayOutputStream(); + return new int[] { opts.outWidth, opts.outHeight }; + } + + private byte[] compressImageToJpeg(InputStream image, int width, int height) { + BufferedInputStream bufferedInputStream = new BufferedInputStream(image); + int w = width; + int h = height; boolean scale = false; int divider = 1; while (w > MAX_IMAGE_LENGTH || h > MAX_IMAGE_LENGTH) { @@ -198,12 +215,13 @@ public class UploadPhotosAsyncTask extends AsyncTask { } Bitmap bmp; if (scale) { - opts = new BitmapFactory.Options(); + BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inSampleSize = divider; bmp = BitmapFactory.decodeStream(bufferedInputStream, null, opts); } else { bmp = BitmapFactory.decodeStream(bufferedInputStream); } + ByteArrayOutputStream os = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.JPEG, 90, os); return os.toByteArray(); } From 4a6360689368c363f1a396a1747a3625c3c0b4bb Mon Sep 17 00:00:00 2001 From: max-klaus Date: Thu, 4 Feb 2021 12:49:52 +0300 Subject: [PATCH 3/6] Fix npe in NavigationService --- OsmAnd/src/net/osmand/plus/NavigationService.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/NavigationService.java b/OsmAnd/src/net/osmand/plus/NavigationService.java index 16f1afddcc..8e525c0f91 100644 --- a/OsmAnd/src/net/osmand/plus/NavigationService.java +++ b/OsmAnd/src/net/osmand/plus/NavigationService.java @@ -4,16 +4,13 @@ import android.app.Notification; import android.app.Service; import android.content.Context; import android.content.Intent; -import android.location.LocationManager; import android.os.Binder; import android.os.IBinder; -import android.util.Log; import android.widget.Toast; import androidx.annotation.NonNull; import net.osmand.Location; -import net.osmand.PlatformUtil; import net.osmand.plus.helpers.LocationServiceHelper; import net.osmand.plus.helpers.LocationServiceHelper.LocationCallback; import net.osmand.plus.notifications.OsmandNotification; @@ -137,10 +134,12 @@ public class NavigationService extends Service { app.setNavigationService(null); usedBy = 0; // remove updates - try { - locationServiceHelper.removeLocationUpdates(); - } catch (SecurityException e) { - // Location service permission not granted + if (locationServiceHelper != null) { + try { + locationServiceHelper.removeLocationUpdates(); + } catch (SecurityException e) { + // Location service permission not granted + } } // remove notification stopForeground(Boolean.TRUE); From ddaaaa6d432ed65a2060bd86b02cfaac81bb7932 Mon Sep 17 00:00:00 2001 From: max-klaus Date: Thu, 4 Feb 2021 13:20:16 +0300 Subject: [PATCH 4/6] Hide off service (location) toast --- OsmAnd/src/net/osmand/plus/NavigationService.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/NavigationService.java b/OsmAnd/src/net/osmand/plus/NavigationService.java index 8e525c0f91..cb7d249d10 100644 --- a/OsmAnd/src/net/osmand/plus/NavigationService.java +++ b/OsmAnd/src/net/osmand/plus/NavigationService.java @@ -92,12 +92,6 @@ public class NavigationService extends Service { @Override public void onLocationAvailability(boolean locationAvailable) { - if (!locationAvailable) { - OsmandApplication app = (OsmandApplication) getApplication(); - if (app != null) { - app.showToastMessage(getString(R.string.off_router_service_no_gps_available)); - } - } } }); } catch (SecurityException e) { From 2fbdb121e407e76f4c9cf4aeaec0aa3c0a9cc4f2 Mon Sep 17 00:00:00 2001 From: max-klaus Date: Thu, 4 Feb 2021 16:41:50 +0300 Subject: [PATCH 5/6] Fix npe --- .../net/osmand/plus/voice/JSMediaCommandPlayerImpl.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/voice/JSMediaCommandPlayerImpl.java b/OsmAnd/src/net/osmand/plus/voice/JSMediaCommandPlayerImpl.java index 80fd5381ab..fd9caafe2c 100644 --- a/OsmAnd/src/net/osmand/plus/voice/JSMediaCommandPlayerImpl.java +++ b/OsmAnd/src/net/osmand/plus/voice/JSMediaCommandPlayerImpl.java @@ -90,9 +90,12 @@ public class JSMediaCommandPlayerImpl extends MediaCommandPlayerImpl { if (voiceDir.getName().contains("tts")) { return false; } - for (File f : voiceDir.listFiles()) { - if (f.getName().endsWith(IndexConstants.TTSVOICE_INDEX_EXT_JS)) { - return true; + File[] files = voiceDir.listFiles(); + if (files != null) { + for (File f : files) { + if (f.getName().endsWith(IndexConstants.TTSVOICE_INDEX_EXT_JS)) { + return true; + } } } return false; From b35e8ea6bedea6c3d9e1e7680fc410a01808bb27 Mon Sep 17 00:00:00 2001 From: max-klaus Date: Thu, 4 Feb 2021 16:46:51 +0300 Subject: [PATCH 6/6] Process xml namespaces for java --- OsmAnd-java/src/main/java/net/osmand/PlatformUtil.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/PlatformUtil.java b/OsmAnd-java/src/main/java/net/osmand/PlatformUtil.java index ffc9bf05b0..fd3fa2e2eb 100644 --- a/OsmAnd-java/src/main/java/net/osmand/PlatformUtil.java +++ b/OsmAnd-java/src/main/java/net/osmand/PlatformUtil.java @@ -23,7 +23,9 @@ public class PlatformUtil { } public static XmlPullParser newXMLPullParser() throws XmlPullParserException{ - return new org.kxml2.io.KXmlParser(); + org.kxml2.io.KXmlParser xmlParser = new org.kxml2.io.KXmlParser(); + xmlParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); + return xmlParser; } public static XmlSerializer newSerializer() {