diff options
author | Cameron Mace <cameron@mapbox.com> | 2016-05-02 12:42:29 -0400 |
---|---|---|
committer | Tobrun <tobrun@mapbox.com> | 2016-05-04 12:35:05 +0200 |
commit | 265df1faa49a3bb496b422933ab03fc93ce0b457 (patch) | |
tree | 6e7806a5d074699f7a30243e28d85015ea06b0f6 | |
parent | 90cd425c5acb42f80b1464458e5e57982eae1125 (diff) | |
download | qtlocation-mapboxgl-265df1faa49a3bb496b422933ab03fc93ce0b457.tar.gz |
[android] #4896 - added permission request and id fixes
4 files changed, 54 insertions, 16 deletions
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationDotColor.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationDotColor.java index 167b7e627c..ded5f73440 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationDotColor.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationDotColor.java @@ -1,8 +1,13 @@ package com.mapbox.mapboxsdk.testapp.activity.userlocation; +import android.Manifest; +import android.content.pm.PackageManager; import android.location.Location; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.annotation.UiThread; +import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; @@ -23,9 +28,10 @@ public class MyLocationDotColor extends AppCompatActivity implements LocationLis private MapView mapView; private MapboxMap map; - private Location location; private boolean firstRun; + private static final int PERMISSIONS_LOCATION = 0; + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -40,8 +46,6 @@ public class MyLocationDotColor extends AppCompatActivity implements LocationLis actionBar.setDisplayShowHomeEnabled(true); } - location = LocationServices.getLocationServices(this).getLastLocation(); - mapView = (MapView) findViewById(R.id.mapView); mapView.setAccessToken(getString(R.string.mapbox_access_token)); mapView.onCreate(savedInstanceState); @@ -51,25 +55,24 @@ public class MyLocationDotColor extends AppCompatActivity implements LocationLis map = mapboxMap; - mapboxMap.setMyLocationEnabled(true); - mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom((location != null ? new LatLng(location) : new LatLng(0, 0)), 15)); + toggleGps(!mapboxMap.isMyLocationEnabled()); } }); // handle default button clicks - findViewById(R.id.defaultUserDotColoringButton).setOnClickListener(new View.OnClickListener() { + findViewById(R.id.default_user_dot_coloring_button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { map.getMyLocationViewSettings().setAccuracyTintColor(ContextCompat.getColor(MyLocationDotColor.this, R.color.my_location_ring)); - map.getMyLocationViewSettings().setForegroundTintColor(ContextCompat.getColor(MyLocationDotColor.this, R.color.primaryDark)); + map.getMyLocationViewSettings().setForegroundTintColor(ContextCompat.getColor(MyLocationDotColor.this, R.color.mapbox_blue)); } }); // handle tint user dot button clicks - findViewById(R.id.tintUserDotButton).setOnClickListener(new View.OnClickListener() { + findViewById(R.id.tint_user_dot_button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -80,7 +83,7 @@ public class MyLocationDotColor extends AppCompatActivity implements LocationLis }); // handle tint accuracy ring button clicks - findViewById(R.id.UserAccuracyRingTintButton).setOnClickListener(new View.OnClickListener() { + findViewById(R.id.user_accuracy_ring_tint_button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -142,4 +145,40 @@ public class MyLocationDotColor extends AppCompatActivity implements LocationLis return super.onOptionsItemSelected(item); } } + + @UiThread + public void toggleGps(boolean enableGps) { + if (enableGps) { + if ((ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) || + (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) { + ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_LOCATION); + } else { + enableLocation(true); + } + } else { + enableLocation(false); + } + } + + private void enableLocation(boolean enabled) { + if (enabled) { + map.setMyLocationEnabled(true); + if (map.getMyLocation() != null) { + map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(map.getMyLocation().getLatitude(), map.getMyLocation().getLongitude()), 15)); + } + } else { + map.setMyLocationEnabled(false); + } + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { + switch (requestCode) { + case PERMISSIONS_LOCATION: { + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + enableLocation(true); + } + } + } + } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_my_location_dot_color.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_my_location_dot_color.xml index b4345b891e..8e57d08368 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_my_location_dot_color.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_my_location_dot_color.xml @@ -17,8 +17,7 @@ android:layout_below="@+id/toolbar" android:layout_width="match_parent" android:layout_height="match_parent" - app:style_url="@string/style_mapbox_streets" - app:scroll_enabled="false" /> + app:style_url="@string/style_mapbox_streets" /> <LinearLayout android:layout_width="match_parent" @@ -28,21 +27,21 @@ android:weightSum="3"> <Button - android:id="@+id/defaultUserDotColoringButton" + android:id="@+id/default_user_dot_coloring_button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/button_user_dot_default" /> <Button - android:id="@+id/tintUserDotButton" + android:id="@+id/tint_user_dot_button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/button_user_dot_tint" /> <Button - android:id="@+id/UserAccuracyRingTintButton" + android:id="@+id/user_accuracy_ring_tint_button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/colors.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/colors.xml index 4a970bb6ab..891ab8d7a8 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/colors.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/colors.xml @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> <resources> <color name="primary">#1E8CAB</color> - <color name="primaryDark">#166B83</color> + <color name="primary_dark">#166B83</color> <color name="accent">#E55E5E</color> <color name="white">#F9F9F9</color> <color name="mapbox_green">#56B881</color> diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/styles.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/styles.xml index 200665796e..d01b9d313f 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/styles.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/styles.xml @@ -5,7 +5,7 @@ <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/primary</item> - <item name="colorPrimaryDark">@color/primaryDark</item> + <item name="colorPrimaryDark">@color/primary_dark</item> <item name="colorAccent">@color/accent</item> <item name="android:windowBackground">@color/white</item> </style> |