From 4c18ecf14292c3770fda696ad0517c01f214d9ac Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 22 Aug 2016 18:51:56 +0300 Subject: [PATCH 1/2] Fix crash permissions --- .../samples/android/sample1/MainActivity.java | 20 +++++++++++++++++++ .../android/sample1/SampleApplication.java | 19 +++++++++++++----- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/MainActivity.java b/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/MainActivity.java index 55904704f5..0c93050e54 100644 --- a/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/MainActivity.java +++ b/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/MainActivity.java @@ -1,13 +1,17 @@ package net.osmand.core.samples.android.sample1; +import android.Manifest; import android.annotation.SuppressLint; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; +import android.content.pm.PackageManager; import android.graphics.PointF; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; import android.support.v4.view.ViewCompat; import android.support.v4.view.ViewPropertyAnimatorListener; import android.text.Editable; @@ -137,6 +141,15 @@ public class MainActivity extends Activity { LANGUAGE = langCode; } + @Override + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + if(requestCode == SampleApplication.PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE && + grantResults[0] == PackageManager.PERMISSION_GRANTED) { + getSampleApplication().initPoiTypes(); + } + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -149,6 +162,13 @@ public class MainActivity extends Activity { // Inflate views setContentView(R.layout.activity_main); + if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) + != PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions(this, + new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, + SampleApplication.PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE); + } + // Get map view mapView = (AtlasMapRendererView) findViewById(R.id.mapRendererView); diff --git a/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/SampleApplication.java b/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/SampleApplication.java index 6f3e1eefcb..5b5554ebc1 100644 --- a/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/SampleApplication.java +++ b/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/SampleApplication.java @@ -1,9 +1,13 @@ package net.osmand.core.samples.android.sample1; +import android.Manifest; import android.app.Application; +import android.content.pm.PackageManager; import android.os.Environment; import android.os.Handler; import android.os.Message; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; import net.osmand.core.android.CoreResourcesFromAndroidAssets; import net.osmand.core.android.NativeCore; @@ -15,8 +19,8 @@ import net.osmand.osm.MapPoiTypes; import java.io.File; import java.lang.reflect.Field; -public class SampleApplication extends Application -{ +public class SampleApplication extends Application { + public static final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 5 ; private CoreResourcesFromAndroidAssets assetsCustom; private MapPoiTypes poiTypes; private IconsCache iconsCache; @@ -27,8 +31,13 @@ public class SampleApplication extends Application { super.onCreate(); uiHandler = new Handler(); + poiTypes = MapPoiTypes.getDefaultNoInit(); + if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) + == PackageManager.PERMISSION_GRANTED) { + initPoiTypes(); + } + - initPoiTypes(); // Initialize native core if (NativeCore.isAvailable() && !NativeCore.isLoaded()) { @@ -48,8 +57,8 @@ public class SampleApplication extends Application return iconsCache; } - private void initPoiTypes() { - poiTypes = MapPoiTypes.getDefaultNoInit(); + public void initPoiTypes() { + poiTypes.init(Environment.getExternalStorageDirectory() + "/osmand/poi_types.xml"); poiTypes.setPoiTranslator(new MapPoiTypes.PoiTranslator() { From 62a1b2045288d147d59ac5f5889f4fb08b56765b Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 22 Aug 2016 18:58:12 +0300 Subject: [PATCH 2/2] Fix crash permissions --- .../net/osmand/core/samples/android/sample1/MainActivity.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/MainActivity.java b/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/MainActivity.java index 0c93050e54..6c4523e1f0 100644 --- a/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/MainActivity.java +++ b/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/MainActivity.java @@ -663,8 +663,8 @@ public class MainActivity extends Activity { public void onLongPress(MotionEvent e) { PointI point31 = new PointI(); mapView.getLocationFromScreenPoint(new PointI((int) e.getX(), (int) e.getY()), point31); - geocode(point31); - Toast.makeText(MainActivity.this, "Geocoding...", Toast.LENGTH_SHORT).show(); + // geocode(point31); + //Toast.makeText(MainActivity.this, "Geocoding...", Toast.LENGTH_SHORT).show(); } @Override