Improve support for Andrioid M

Checks for and requests runtime location permissions (fixes #2110)
Improved the styling of the location FAB
Removed redudant drawables
Fixed various Java and Android warnings and deprecated functions
A few other tweaks
This commit is contained in:
Leith Bade
2015-08-18 18:54:56 +10:00
parent ac86ce39ad
commit 6ad2006d3f
45 changed files with 68 additions and 147 deletions

View File

@@ -23,4 +23,6 @@
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
</manifest>

View File

@@ -1,11 +1,15 @@
package com.mapbox.mapboxgl.testapp;
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.PointF;
import android.location.Location;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
@@ -16,13 +20,10 @@ import android.view.GestureDetector;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import com.crashlytics.android.Crashlytics;
import com.mapbox.mapboxgl.annotations.MarkerOptions;
import com.mapbox.mapboxgl.annotations.Polygon;
import com.mapbox.mapboxgl.annotations.PolygonOptions;
import com.mapbox.mapboxgl.annotations.Polyline;
import com.mapbox.mapboxgl.annotations.PolylineOptions;
import com.mapbox.mapboxgl.geometry.LatLng;
import com.mapbox.mapboxgl.views.MapView;
@@ -44,6 +45,9 @@ public class MainActivity extends AppCompatActivity {
private static final String STATE_IS_ANNOTATIONS_ON = "isAnnotationsOn";
private static final String STATE_SELECTED_STYLE = "selectedStyle";
// Used for permissions requests
private static final int PERMISSIONS_LOCATION = 0;
//
// Instance members
//
@@ -78,8 +82,10 @@ public class MainActivity extends AppCompatActivity {
setSupportActionBar(toolbar);
final ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);
if (ab != null) {
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);
}
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
@@ -229,6 +235,17 @@ public class MainActivity extends AppCompatActivity {
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSIONS_LOCATION: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
enableGps();
}
}
}
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@@ -331,14 +348,24 @@ public class MainActivity extends AppCompatActivity {
*/
private void toggleGps(boolean enableGps) {
if (enableGps) {
mMapView.setMyLocationEnabled(enableGps);
mLocationFAB.setColorFilter(getResources().getColor(R.color.primary));
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 {
enableGps();
}
} else {
mMapView.setMyLocationEnabled(enableGps);
mMapView.setMyLocationEnabled(false);
mLocationFAB.setColorFilter(Color.TRANSPARENT);
}
}
private void enableGps() {
mMapView.setMyLocationEnabled(true);
mLocationFAB.setColorFilter(ContextCompat.getColor(this, R.color.primary));
}
/**
* Enable / Disable Annotations.
*
@@ -364,7 +391,6 @@ public class MainActivity extends AppCompatActivity {
private void addMarkers() {
LatLng backLot = new LatLng(38.649441, -121.369064);
MapView map = mMapView;
mMapView.addMarker(new MarkerOptions()
.position(backLot)
.title("Back Lot")
@@ -383,7 +409,7 @@ public class MainActivity extends AppCompatActivity {
String geojsonStr = Util.loadStringFromAssets(this, "small_line.geojson");
LatLng[] latLngs = Util.parseGeoJSONCoordinates(geojsonStr);
MapView map = mMapView;
Polyline line = map.addPolyline(new PolylineOptions()
map.addPolyline(new PolylineOptions()
.add(latLngs)
.width(2)
.color(Color.RED));
@@ -394,17 +420,16 @@ public class MainActivity extends AppCompatActivity {
}
private void addPolygon() {
String geojsonStr = null;
try {
geojsonStr = Util.loadStringFromAssets(this, "small_poly.geojson");
String geojsonStr = Util.loadStringFromAssets(this, "small_poly.geojson");
LatLng[] latLngs = Util.parseGeoJSONCoordinates(geojsonStr);
MapView map = mMapView;
ArrayList<PolygonOptions> opts = new ArrayList<PolygonOptions>();
ArrayList<PolygonOptions> opts = new ArrayList<>();
opts.add(new PolygonOptions()
.add(latLngs)
.strokeColor(Color.MAGENTA)
.fillColor(Color.BLUE));
Polygon polygon = map.addPolygons(opts).get(0);
map.addPolygons(opts).get(0);
} catch (Exception e) {
Log.e(TAG, "Error adding Polygon: "+ e);
e.printStackTrace();
@@ -415,76 +440,6 @@ public class MainActivity extends AppCompatActivity {
mMapView.removeAnnotations();
}
// This class handles outdoor class change events
private class OutdoorClassSpinnerListener implements AdapterView.OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
ArrayList<String> styleClasses = new ArrayList<>(1);
switch (position) {
// Day
case 0:
styleClasses.add("day");
mMapView.setStyleClasses(styleClasses);
break;
// Night
case 1:
styleClasses.add("night");
mMapView.setStyleClasses(styleClasses);
break;
default:
onNothingSelected(parent);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
mMapView.removeAllStyleClasses();
}
}
// This class handles satellite class change events
private class SatelliteClassSpinnerListener implements AdapterView.OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
ArrayList<String> styleClasses = new ArrayList<>(2);
switch (position) {
// Labels + Contours
case 0:
styleClasses.add("labels");
styleClasses.add("contours");
mMapView.setStyleClasses(styleClasses);
break;
// Labels Only
case 1:
styleClasses.add("labels");
mMapView.setStyleClasses(styleClasses);
break;
// No Labels
case 2:
mMapView.setStyleClasses(styleClasses);
break;
default:
onNothingSelected(parent);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
mMapView.removeAllStyleClasses();
}
}
// Called when FPS changes
private class MyOnFpsChangedListener implements MapView.OnFpsChangedListener {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 683 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 722 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 471 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 549 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 465 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 484 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 323 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 341 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 860 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 622 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 660 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 918 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 976 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@@ -47,13 +47,13 @@
<android.support.design.widget.FloatingActionButton
android:id="@+id/locationFAB"
android:src="@drawable/ic_my_location_white_24dp"
android:src="@drawable/ic_my_location_black_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
app:backgroundTint="@color/white"
app:borderWidth="0dp"
app:elevation="0dp"
/>
</android.support.design.widget.CoordinatorLayout>

View File

@@ -3,7 +3,7 @@
<item
android:id="@+id/action_debug"
android:icon="@drawable/ic_action_about"
android:icon="@android:drawable/ic_menu_info_details"
android:title="@string/action_debug"
android:checkable="true"
/>
@@ -14,7 +14,7 @@
android:checkable="true"
/>
<item android:id="@+id/action_compass"
android:icon="@drawable/ic_explore_white_24dp"
android:icon="@android:drawable/ic_menu_compass"
android:title="@string/action_compass"
android:checkable="true"
/>

View File

@@ -4,10 +4,10 @@
<string name="app_name">Mapbox GL</string>
<string name="action_gps">Toggle GPS location</string>
<string name="action_location_none">Toggle Location Bearing Tracking</string>
<string name="action_location_follow">Follow</string>
<string name="action_location_bearing">Bearing</string>
<string name="action_compass">Toggle Compass</string>
<string name="action_location_none">Disable tracking</string>
<string name="action_location_follow">Use follow tracking mode</string>
<string name="action_location_bearing">Use bearing tracking mode tracking</string>
<string name="action_compass">Toggle compass</string>
<string name="action_debug">Toggle debug mode</string>
<string name="action_point_annotations">Toggle point annotations</string>

View File

@@ -7,7 +7,6 @@
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primaryDark</item>
<item name="colorAccent">@color/accent</item>
<item name="android:windowBackground">@color/white</item>
</style>
</resources>