ShareLocationActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import android.Manifest;
  4import android.content.Intent;
  5import android.content.pm.PackageManager;
  6import android.location.Location;
  7import android.location.LocationListener;
  8import android.os.Build;
  9import android.os.Bundle;
 10import android.support.annotation.NonNull;
 11import android.support.design.widget.CoordinatorLayout;
 12import android.support.design.widget.FloatingActionButton;
 13import android.support.design.widget.Snackbar;
 14import android.view.View;
 15import android.widget.Button;
 16
 17import org.osmdroid.api.IGeoPoint;
 18import org.osmdroid.util.GeoPoint;
 19
 20import eu.siacs.conversations.Config;
 21import eu.siacs.conversations.R;
 22import eu.siacs.conversations.ui.util.LocationHelper;
 23import eu.siacs.conversations.ui.widget.Marker;
 24import eu.siacs.conversations.ui.widget.MyLocation;
 25
 26public class ShareLocationActivity extends LocationActivity implements LocationListener {
 27
 28	private Snackbar snackBar;
 29	private boolean marker_fixed_to_loc = false;
 30	private static final String KEY_FIXED_TO_LOC = "fixed_to_loc";
 31	private Boolean noAskAgain = false;
 32
 33	@Override
 34	protected void onSaveInstanceState(@NonNull final Bundle outState) {
 35		super.onSaveInstanceState(outState);
 36
 37		outState.putBoolean(KEY_FIXED_TO_LOC, marker_fixed_to_loc);
 38	}
 39
 40	@Override
 41	protected void onRestoreInstanceState(@NonNull final Bundle savedInstanceState) {
 42		super.onRestoreInstanceState(savedInstanceState);
 43
 44		if (savedInstanceState.containsKey(KEY_FIXED_TO_LOC)) {
 45			this.marker_fixed_to_loc = savedInstanceState.getBoolean(KEY_FIXED_TO_LOC);
 46		}
 47	}
 48
 49	@Override
 50	protected void onCreate(final Bundle savedInstanceState) {
 51		super.onCreate(savedInstanceState);
 52
 53		setContentView(R.layout.activity_share_location);
 54		setSupportActionBar(findViewById(R.id.toolbar));
 55		configureActionBar(getSupportActionBar());
 56		setupMapView(Config.Map.INITIAL_POS);
 57
 58		// Setup the cancel button
 59		final Button cancelButton = findViewById(R.id.cancel_button);
 60		cancelButton.setOnClickListener(view -> {
 61			setResult(RESULT_CANCELED);
 62			finish();
 63		});
 64
 65		final CoordinatorLayout snackBarCoordinator = findViewById(R.id.snackbarCoordinator);
 66		if (snackBarCoordinator != null) {
 67			this.snackBar = Snackbar.make(snackBarCoordinator, R.string.location_disabled, Snackbar.LENGTH_INDEFINITE);
 68			snackBar.setAction(R.string.enable, view -> {
 69				if (isLocationEnabledAndAllowed()) {
 70					updateUi();
 71				} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !hasLocationPermissions()) {
 72					requestPermissions(REQUEST_CODE_SNACKBAR_PRESSED);
 73				} else if (!isLocationEnabled()) {
 74					startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
 75				}
 76			});
 77		}
 78
 79		// Setup the share button
 80		final Button shareButton = findViewById(R.id.share_button);
 81		if (shareButton != null) {
 82			shareButton.setOnClickListener(view -> {
 83				final Intent result = new Intent();
 84
 85				if (marker_fixed_to_loc && myLoc != null) {
 86					result.putExtra("latitude", myLoc.getLatitude());
 87					result.putExtra("longitude", myLoc.getLongitude());
 88					result.putExtra("altitude", myLoc.getAltitude());
 89					result.putExtra("accuracy", (int) myLoc.getAccuracy());
 90				} else {
 91					final IGeoPoint markerPoint = map.getMapCenter();
 92					result.putExtra("latitude", markerPoint.getLatitude());
 93					result.putExtra("longitude", markerPoint.getLongitude());
 94				}
 95
 96				setResult(RESULT_OK, result);
 97				finish();
 98			});
 99		}
100
101		this.marker_fixed_to_loc = isLocationEnabledAndAllowed();
102
103		// Setup the fab button
104		final FloatingActionButton toggleFixedMarkerButton = findViewById(R.id.fab);
105		toggleFixedMarkerButton.setOnClickListener(view -> {
106			if (!marker_fixed_to_loc) {
107				if (!isLocationEnabled()) {
108					startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
109				} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
110					requestPermissions(REQUEST_CODE_FAB_PRESSED);
111				}
112			}
113			toggleFixedLocation();
114		});
115	}
116
117	@Override
118	public void onRequestPermissionsResult(final int requestCode,
119										   @NonNull final String[] permissions,
120										   @NonNull final int[] grantResults) {
121		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
122
123		if (grantResults.length > 0 &&
124				grantResults[0] != PackageManager.PERMISSION_GRANTED &&
125				Build.VERSION.SDK_INT >= 23 &&
126				permissions.length > 0 &&
127				(
128						Manifest.permission.LOCATION_HARDWARE.equals(permissions[0]) ||
129								Manifest.permission.ACCESS_FINE_LOCATION.equals(permissions[0]) ||
130								Manifest.permission.ACCESS_COARSE_LOCATION.equals(permissions[0])
131				) &&
132				!shouldShowRequestPermissionRationale(permissions[0])) {
133			noAskAgain = true;
134		}
135
136		if (!noAskAgain && requestCode == REQUEST_CODE_SNACKBAR_PRESSED && !isLocationEnabled() && hasLocationPermissions()) {
137			startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
138		}
139		updateUi();
140	}
141
142	@Override
143	protected void gotoLoc(final boolean setZoomLevel) {
144		if (this.myLoc != null && mapController != null) {
145			if (setZoomLevel) {
146				mapController.setZoom(Config.Map.FINAL_ZOOM_LEVEL);
147			}
148			mapController.animateTo(new GeoPoint(this.myLoc));
149		}
150	}
151
152	@Override
153	protected void setMyLoc(final Location location) {
154		this.myLoc = location;
155	}
156
157	@Override
158	protected void onPause() {
159		super.onPause();
160	}
161
162	@Override
163	protected void updateLocationMarkers() {
164		super.updateLocationMarkers();
165		if (this.myLoc != null) {
166			this.map.getOverlays().add(new MyLocation(this, null, this.myLoc));
167			if (this.marker_fixed_to_loc) {
168				map.getOverlays().add(new Marker(marker_icon, new GeoPoint(this.myLoc)));
169			} else {
170				map.getOverlays().add(new Marker(marker_icon));
171			}
172		} else {
173			map.getOverlays().add(new Marker(marker_icon));
174		}
175	}
176
177	@Override
178	public void onLocationChanged(final Location location) {
179		if (this.myLoc == null) {
180			this.marker_fixed_to_loc = true;
181		}
182		updateUi();
183		if (LocationHelper.isBetterLocation(location, this.myLoc)) {
184			final Location oldLoc = this.myLoc;
185			this.myLoc = location;
186
187			// Don't jump back to the users location if they're not moving (more or less).
188			if (oldLoc == null || (this.marker_fixed_to_loc && this.myLoc.distanceTo(oldLoc) > 1)) {
189				gotoLoc();
190			}
191
192			updateLocationMarkers();
193		}
194	}
195
196	@Override
197	public void onStatusChanged(final String provider, final int status, final Bundle extras) {
198
199	}
200
201	@Override
202	public void onProviderEnabled(final String provider) {
203
204	}
205
206	@Override
207	public void onProviderDisabled(final String provider) {
208
209	}
210
211	private boolean isLocationEnabledAndAllowed() {
212		return this.hasLocationFeature && (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || this.hasLocationPermissions()) && this.isLocationEnabled();
213	}
214
215	private void toggleFixedLocation() {
216		this.marker_fixed_to_loc = isLocationEnabledAndAllowed() && !this.marker_fixed_to_loc;
217		if (this.marker_fixed_to_loc) {
218			gotoLoc(false);
219		}
220		updateLocationMarkers();
221		updateUi();
222	}
223
224	@Override
225	protected void updateUi() {
226		if (!hasLocationFeature || noAskAgain || isLocationEnabledAndAllowed()) {
227			this.snackBar.dismiss();
228		} else {
229			this.snackBar.show();
230		}
231
232		// Setup the fab button
233		final FloatingActionButton fab = findViewById(R.id.fab);
234		if (isLocationEnabledAndAllowed()) {
235			fab.setVisibility(View.VISIBLE);
236			runOnUiThread(() -> {
237				fab.setImageResource(marker_fixed_to_loc ? R.drawable.ic_gps_fixed_white_24dp :
238						R.drawable.ic_gps_not_fixed_white_24dp);
239				fab.setContentDescription(getResources().getString(
240						marker_fixed_to_loc ? R.string.action_unfix_from_location : R.string.action_fix_to_location
241				));
242				fab.invalidate();
243			});
244		} else {
245			fab.setVisibility(View.GONE);
246		}
247	}
248}