diff options
author | Tobrun <tobrun.van.nuland@gmail.com> | 2016-08-11 17:33:02 -0400 |
---|---|---|
committer | Tobrun <tobrun.van.nuland@gmail.com> | 2016-08-11 18:13:12 -0400 |
commit | 0ae941d2a8a89276e1f0c2cf3870104d0e94fbc4 (patch) | |
tree | 553f6c9bc211374834d7734a991bd7f83804e9db /platform/android | |
parent | a3e20bae32c17fe976a345e2b38166116d331c1f (diff) | |
download | qtlocation-mapboxgl-0ae941d2a8a89276e1f0c2cf3870104d0e94fbc4.tar.gz |
[android] #5934 - resolves memory leaks on animators + location listeners
Diffstat (limited to 'platform/android')
4 files changed, 58 insertions, 12 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java index 318eec80b4..54f165ac86 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java @@ -1493,6 +1493,14 @@ public class MapView extends FrameLayout { if (mMapboxMap.getUiSettings().isZoomControlsEnabled()) { mZoomButtonsController.setVisible(false); } + + // make sure we don't leak location listener + if (mMyLocationListener != null) { + // cleanup to prevent memory leak + LocationServices services = LocationServices.getLocationServices(getContext()); + services.removeLocationListener(mMyLocationListener); + mMyLocationListener = null; + } } // Called when view is hidden and shown diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java index 72f8196091..bfc1711af2 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationView.java @@ -320,11 +320,6 @@ public class MyLocationView extends View { setBearing(position.bearing); } - public void onPause() { - compassListener.onPause(); - toggleGps(false); - } - public void onResume() { if (myBearingTrackingMode == MyBearingTracking.COMPASS) { compassListener.onResume(); @@ -334,6 +329,37 @@ public class MyLocationView extends View { } } + public void onPause() { + compassListener.onPause(); + toggleGps(false); + } + + @Override + protected void onDetachedFromWindow() { + super.onDetachedFromWindow(); + // cleanup to prevent memory leaks + if (locationChangeAnimator != null) { + locationChangeAnimator.cancel(); + locationChangeAnimator = null; + } + + if (accuracyAnimator != null) { + accuracyAnimator.cancel(); + accuracyAnimator = null; + } + + if (directionAnimator != null) { + directionAnimator.cancel(); + directionAnimator = null; + } + + if (userLocationListener != null) { + LocationServices services = LocationServices.getLocationServices(getContext()); + services.removeLocationListener(userLocationListener); + userLocationListener = null; + } + } + public void update() { if (isEnabled()) { myLocationBehavior.invalidate(); diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationTintActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationTintActivity.java index f665b88f60..ba86fa5774 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationTintActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationTintActivity.java @@ -60,7 +60,7 @@ public class MyLocationTintActivity extends AppCompatActivity implements Locatio final MyLocationViewSettings myLocationViewSettings = mapboxMap.getMyLocationViewSettings(); // handle default button clicks - ViewUtils.attachClickListener(MyLocationTintActivity.this, R.id.default_user_dot_coloring_button,new View.OnClickListener() { + ViewUtils.attachClickListener(MyLocationTintActivity.this, R.id.default_user_dot_coloring_button, new View.OnClickListener() { @Override public void onClick(View view) { myLocationViewSettings.setAccuracyTintColor(ContextCompat.getColor(MyLocationTintActivity.this, R.color.my_location_ring)); @@ -69,7 +69,7 @@ public class MyLocationTintActivity extends AppCompatActivity implements Locatio }); // handle tint user dot button clicks - ViewUtils.attachClickListener(MyLocationTintActivity.this, R.id.tint_user_dot_button,new View.OnClickListener() { + ViewUtils.attachClickListener(MyLocationTintActivity.this, R.id.tint_user_dot_button, new View.OnClickListener() { @Override public void onClick(View view) { myLocationViewSettings.setAccuracyTintColor(ContextCompat.getColor(MyLocationTintActivity.this, R.color.mapbox_green)); @@ -78,7 +78,7 @@ public class MyLocationTintActivity extends AppCompatActivity implements Locatio }); // handle tint accuracy ring button clicks - ViewUtils.attachClickListener(MyLocationTintActivity.this, R.id.user_accuracy_ring_tint_button,new View.OnClickListener() { + ViewUtils.attachClickListener(MyLocationTintActivity.this, R.id.user_accuracy_ring_tint_button, new View.OnClickListener() { @Override public void onClick(View view) { myLocationViewSettings.setAccuracyTintColor(ContextCompat.getColor(MyLocationTintActivity.this, R.color.accent)); @@ -88,7 +88,6 @@ public class MyLocationTintActivity extends AppCompatActivity implements Locatio } }); - LocationServices.getLocationServices(this).addLocationListener(this); } @Override @@ -100,6 +99,12 @@ public class MyLocationTintActivity extends AppCompatActivity implements Locatio } @Override + protected void onStart() { + super.onStart(); + LocationServices.getLocationServices(this).addLocationListener(this); + } + + @Override public void onResume() { super.onResume(); mapView.onResume(); @@ -112,6 +117,12 @@ public class MyLocationTintActivity extends AppCompatActivity implements Locatio } @Override + protected void onStop() { + super.onStop(); + LocationServices.getLocationServices(this).removeLocationListener(this); + } + + @Override public void onLowMemory() { super.onLowMemory(); mapView.onLowMemory(); @@ -180,7 +191,7 @@ public class MyLocationTintActivity extends AppCompatActivity implements Locatio public static void attachClickListener(@NonNull Activity activity, @IdRes int buttonId, @Nullable View.OnClickListener clickListener) { View view = activity.findViewById(buttonId); - if(view!=null){ + if (view != null) { view.setOnClickListener(clickListener); } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationTrackingModeActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationTrackingModeActivity.java index 273b5134c3..cf0d4c809a 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationTrackingModeActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/userlocation/MyLocationTrackingModeActivity.java @@ -20,12 +20,12 @@ import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; + import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; import com.mapbox.mapboxsdk.constants.MapboxConstants; import com.mapbox.mapboxsdk.constants.MyBearingTracking; import com.mapbox.mapboxsdk.constants.MyLocationTracking; import com.mapbox.mapboxsdk.geometry.LatLng; -import com.mapbox.mapboxsdk.location.LocationServices; import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; @@ -145,7 +145,7 @@ public class MyLocationTrackingModeActivity extends AppCompatActivity implements } } - private void setInitialPosition(LatLng latLng){ + private void setInitialPosition(LatLng latLng) { mMapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14)); mMapboxMap.setMyLocationEnabled(true); mLocationSpinner.setEnabled(true); @@ -274,6 +274,7 @@ public class MyLocationTrackingModeActivity extends AppCompatActivity implements mMapboxMap.getTrackingSettings().setDismissBearingTrackingOnGesture(state); Toast.makeText(this, "Dismiss bearing mode on gesture = " + state, Toast.LENGTH_SHORT).show(); item.setChecked(state); + return true; default: return super.onOptionsItemSelected(item); } |