Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Crash in LocationPickerActivity when device configuration is changed #5500

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Activity;
import android.content.Intent;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import fr.free.nrw.commons.Media;

/**
* Helper class for starting the activity
Expand Down Expand Up @@ -52,6 +53,17 @@ public LocationPicker.IntentBuilder activityKey(
return this;
}

/**
* Gets and puts media in intent
* @param media Media
* @return LocationPicker.IntentBuilder
*/
public LocationPicker.IntentBuilder media(
final Media media) {
intent.putExtra(LocationPickerConstants.MEDIA, media);
return this;
}

/**
* Gets and sets the activity
* @param activity Activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.geometry.LatLng;
import fr.free.nrw.commons.Media;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.coordinates.CoordinateEditHelper;
import fr.free.nrw.commons.filepicker.Constants;
import fr.free.nrw.commons.kvstore.JsonKvStore;
import fr.free.nrw.commons.location.LocationPermissionsHelper;
Expand All @@ -41,6 +43,9 @@
import fr.free.nrw.commons.location.LocationServiceManager;
import fr.free.nrw.commons.theme.BaseActivity;
import fr.free.nrw.commons.utils.SystemThemeUtils;
import fr.free.nrw.commons.utils.ViewUtilWrapper;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
Expand All @@ -52,13 +57,22 @@
import org.osmdroid.views.overlay.Overlay;
import org.osmdroid.views.overlay.ScaleDiskOverlay;
import org.osmdroid.views.overlay.TilesOverlay;
import timber.log.Timber;

/**
* Helps to pick location and return the result with an intent
*/
public class LocationPickerActivity extends BaseActivity implements
LocationPermissionCallback {

/**
* coordinateEditHelper: helps to edit coordinates
*/
@Inject
CoordinateEditHelper coordinateEditHelper;
/**
* media : Media object
*/
private Media media;
/**
* cameraPosition : position of picker
*/
Expand Down Expand Up @@ -125,6 +139,13 @@ public class LocationPickerActivity extends BaseActivity implements
@Inject
LocationServiceManager locationManager;

/**
* Constants
*/
private static final String CAMERA_POS = "cameraPosition";
private static final String ACTIVITY = "activity";


@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
Expand All @@ -145,8 +166,12 @@ protected void onCreate(@Nullable final Bundle savedInstanceState) {
cameraPosition = getIntent()
.getParcelableExtra(LocationPickerConstants.MAP_CAMERA_POSITION);
activity = getIntent().getStringExtra(LocationPickerConstants.ACTIVITY_KEY);
media = getIntent().getParcelableExtra(LocationPickerConstants.MEDIA);
}else{
cameraPosition = savedInstanceState.getParcelable(CAMERA_POS);
activity = savedInstanceState.getString(ACTIVITY);
media = savedInstanceState.getParcelable("sMedia");
}

bindViews();
addBackButtonListener();
addPlaceSelectedButton();
Expand Down Expand Up @@ -220,7 +245,10 @@ private void darkThemeSetup() {
*/
private void addBackButtonListener() {
final ImageView backButton = findViewById(R.id.maplibre_place_picker_toolbar_back_button);
backButton.setOnClickListener(view -> finish());
backButton.setOnClickListener(v -> {
finish();
});

}

/**
Expand Down Expand Up @@ -311,14 +339,42 @@ void placeSelected() {
+ mapView.getMapCenter().getLongitude());
applicationKvStore.putString(LAST_ZOOM, mapView.getZoomLevel() + "");
}
final Intent returningIntent = new Intent();
returningIntent.putExtra(LocationPickerConstants.MAP_CAMERA_POSITION,
new CameraPosition(new LatLng(mapView.getMapCenter().getLatitude(),
mapView.getMapCenter().getLongitude()), 14f, 0, 0));
setResult(AppCompatActivity.RESULT_OK, returningIntent);

if (media == null) {
final Intent returningIntent = new Intent();
returningIntent.putExtra(LocationPickerConstants.MAP_CAMERA_POSITION,
new CameraPosition(new LatLng(mapView.getMapCenter().getLatitude(),
mapView.getMapCenter().getLongitude()), 14f, 0, 0));
setResult(AppCompatActivity.RESULT_OK, returningIntent);
} else {
updateCoordinates(String.valueOf(mapView.getMapCenter().getLatitude()),
String.valueOf(mapView.getMapCenter().getLongitude()),
String.valueOf(0.0f));
}

finish();
}

/**
* Fetched coordinates are replaced with existing coordinates by a POST API call.
* @param Latitude to be added
* @param Longitude to be added
* @param Accuracy to be added
*/
public void updateCoordinates(final String Latitude, final String Longitude,
final String Accuracy) {
if (media == null) {
return;
}
compositeDisposable.add(coordinateEditHelper.makeCoordinatesEdit(getApplicationContext(),media,
Latitude, Longitude, Accuracy)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(s -> {
Timber.d("Coordinates are added.");
}));
}

/**
* Center the camera on the last saved location
*/
Expand Down Expand Up @@ -458,4 +514,22 @@ private void addLocationMarker(GeoPoint geoPoint) {
mapView.getOverlays().add(startMarker);
}

/**
* Saves the state of the activity
* @param outState Bundle
*/
@Override
public void onSaveInstanceState(@NonNull final Bundle outState) {
super.onSaveInstanceState(outState);
if(cameraPosition!=null){
outState.putParcelable(CAMERA_POS, cameraPosition);
}
if(activity!=null){
outState.putString(ACTIVITY, activity);
}

if(media!=null){
outState.putParcelable("sMedia", media);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public final class LocationPickerConstants {
public static final String MAP_CAMERA_POSITION
= "location.picker.cameraPosition";

public static final String MEDIA
= "location.picker.media";


private LocationPickerConstants() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -942,12 +942,14 @@ private void goToLocationPickerActivity() {
}
}

startActivityForResult(new LocationPicker.IntentBuilder()

startActivity(new LocationPicker.IntentBuilder()
.defaultLocation(new CameraPosition.Builder()
.target(new LatLng(defaultLatitude, defaultLongitude))
.zoom(16).build())
.activityKey("MediaActivity")
.build(getActivity()), REQUEST_CODE);
.media(media)
.build(getActivity()));
}

@OnClick(R.id.description_edit)
Expand Down Expand Up @@ -1125,32 +1127,7 @@ public void onActivityResult(final int requestCode, final int resultCode,
@Nullable final Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {

assert data != null;
final CameraPosition cameraPosition = LocationPicker.getCameraPosition(data);

if (cameraPosition != null) {

final String latitude = String.valueOf(cameraPosition.target.getLatitude());
final String longitude = String.valueOf(cameraPosition.target.getLongitude());
final String accuracy = String.valueOf(cameraPosition.target.getAltitude());
String currentLatitude = null;
String currentLongitude = null;

if (media.getCoordinates() != null) {
currentLatitude = String.valueOf(media.getCoordinates().getLatitude());
currentLongitude = String.valueOf(media.getCoordinates().getLongitude());
}

if (!latitude.equals(currentLatitude) || !longitude.equals(currentLongitude)) {
updateCoordinates(latitude, longitude, accuracy);
} else if (media.getCoordinates() == null) {
updateCoordinates(latitude, longitude, accuracy);
}
}

} else if (requestCode == REQUEST_CODE_EDIT_DESCRIPTION && resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE_EDIT_DESCRIPTION && resultCode == RESULT_OK) {
final String updatedWikiText = data.getStringExtra(UPDATED_WIKITEXT);
compositeDisposable.add(descriptionEditHelper.addDescription(getContext(), media,
updatedWikiText)
Expand Down Expand Up @@ -1178,11 +1155,6 @@ public void onActivityResult(final int requestCode, final int resultCode,
progressBarEditDescription.setVisibility(GONE);
editDescription.setVisibility(VISIBLE);

} else if (requestCode == REQUEST_CODE && resultCode == RESULT_CANCELED) {
viewUtil.showShortToast(getContext(),
requireContext()
.getString(R.string.coordinates_picking_unsuccessful));

} else if (requestCode == REQUEST_CODE_EDIT_DESCRIPTION && resultCode == RESULT_CANCELED) {
progressBarEditDescription.setVisibility(GONE);
editDescription.setVisibility(VISIBLE);
Expand All @@ -1200,24 +1172,6 @@ private void updateCaptions(UploadMediaDetail mediaDetail,
media.setCaptions(updatedCaptions);
}

/**
* Fetched coordinates are replaced with existing coordinates by a POST API call.
* @param Latitude to be added
* @param Longitude to be added
* @param Accuracy to be added
*/
public void updateCoordinates(final String Latitude, final String Longitude,
final String Accuracy) {
compositeDisposable.add(coordinateEditHelper.makeCoordinatesEdit(getContext(), media,
Latitude, Longitude, Accuracy)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(s -> {
Timber.d("Coordinates are added.");
coordinates.setText(prettyCoordinates(media));
}));
}

@SuppressLint("StringFormatInvalid")
@OnClick(R.id.nominateDeletion)
public void onDeleteButtonClicked(){
Expand Down Expand Up @@ -1602,4 +1556,5 @@ private int getImageBackgroundColor() {
SharedPreferences imageBackgroundColorPref = this.getImageBackgroundColorPref();
return imageBackgroundColorPref.getInt(IMAGE_BACKGROUND_COLOR, DEFAULT_IMAGE_BACKGROUND_COLOR);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fr.free.nrw.commons.locationpicker

import android.content.Context
import android.os.Looper
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.ImageView
Expand All @@ -20,10 +21,14 @@ import com.nhaarman.mockitokotlin2.times
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import fr.free.nrw.commons.LocationPicker.LocationPickerActivity
import fr.free.nrw.commons.Media
import fr.free.nrw.commons.TestCommonsApplication
import fr.free.nrw.commons.coordinates.CoordinateEditHelper
import fr.free.nrw.commons.kvstore.JsonKvStore
import fr.free.nrw.commons.upload.mediaDetails.UploadMediaDetailFragment.LAST_LOCATION
import fr.free.nrw.commons.upload.mediaDetails.UploadMediaDetailFragment.LAST_ZOOM
import io.reactivex.android.plugins.RxAndroidPlugins
import io.reactivex.schedulers.Schedulers
import org.junit.Assert
import org.junit.Assert.*
import org.junit.Before
Expand Down Expand Up @@ -93,6 +98,7 @@ class LocationPickerActivityUnitTests {
MockitoAnnotations.initMocks(this)
context = RuntimeEnvironment.getApplication().applicationContext
activity = Robolectric.buildActivity(LocationPickerActivity::class.java).get()
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }

Whitebox.setInternalState(activity, "mapView", mapView)
Whitebox.setInternalState(activity, "applicationKvStore", applicationKvStore)
Expand Down Expand Up @@ -165,4 +171,5 @@ class LocationPickerActivityUnitTests {
}


}

}
Loading