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.FloatingActionButton;
 12import android.view.View;
 13import android.widget.Button;
 14import android.widget.RelativeLayout;
 15import android.widget.TextView;
 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 RelativeLayout 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		// Setup the snackbar
 66		this.snackBar = findViewById(R.id.snackbar);
 67		final TextView snackbarAction = findViewById(R.id.snackbar_action);
 68		snackbarAction.setOnClickListener(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		// Setup the share button
 79		final Button shareButton = findViewById(R.id.share_button);
 80		if (shareButton != null) {
 81			shareButton.setOnClickListener(view -> {
 82				final Intent result = new Intent();
 83
 84				if (marker_fixed_to_loc && myLoc != null) {
 85					result.putExtra("latitude", myLoc.getLatitude());
 86					result.putExtra("longitude", myLoc.getLongitude());
 87					result.putExtra("altitude", myLoc.getAltitude());
 88					result.putExtra("accuracy", (int) myLoc.getAccuracy());
 89				} else {
 90					final IGeoPoint markerPoint = map.getMapCenter();
 91					result.putExtra("latitude", markerPoint.getLatitude());
 92					result.putExtra("longitude", markerPoint.getLongitude());
 93				}
 94
 95				setResult(RESULT_OK, result);
 96				finish();
 97			});
 98		}
 99
100		this.marker_fixed_to_loc = isLocationEnabledAndAllowed();
101
102		// Setup the fab button
103		final FloatingActionButton toggleFixedMarkerButton = findViewById(R.id.fab);
104		toggleFixedMarkerButton.setOnClickListener(view -> {
105			if (!marker_fixed_to_loc) {
106				if (!isLocationEnabled()) {
107					startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
108				} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
109					requestPermissions(REQUEST_CODE_FAB_PRESSED);
110				}
111			}
112			toggleFixedLocation();
113		});
114	}
115
116	@Override
117	public void onRequestPermissionsResult(final int requestCode,
118										   @NonNull final String[] permissions,
119										   @NonNull final int[] grantResults) {
120		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
121
122		if (grantResults.length > 0 &&
123				grantResults[0] != PackageManager.PERMISSION_GRANTED &&
124				Build.VERSION.SDK_INT >= 23 &&
125				permissions.length > 0 &&
126				(
127						Manifest.permission.LOCATION_HARDWARE.equals(permissions[0]) ||
128								Manifest.permission.ACCESS_FINE_LOCATION.equals(permissions[0]) ||
129								Manifest.permission.ACCESS_COARSE_LOCATION.equals(permissions[0])
130				) &&
131				!shouldShowRequestPermissionRationale(permissions[0])) {
132			noAskAgain = true;
133		}
134
135		if (!noAskAgain && requestCode == REQUEST_CODE_SNACKBAR_PRESSED && !isLocationEnabled() && hasLocationPermissions()) {
136			startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
137		}
138		updateUi();
139	}
140
141	@Override
142	protected void gotoLoc(final boolean setZoomLevel) {
143		if (this.myLoc != null && mapController != null) {
144			if (setZoomLevel) {
145				mapController.setZoom(Config.Map.FINAL_ZOOM_LEVEL);
146			}
147			mapController.animateTo(new GeoPoint(this.myLoc));
148		}
149	}
150
151	@Override
152	protected void setMyLoc(final Location location) {
153		this.myLoc = location;
154	}
155
156	@Override
157	protected void onPause() {
158		super.onPause();
159	}
160
161	@Override
162	protected void updateLocationMarkers() {
163		super.updateLocationMarkers();
164		if (this.myLoc != null) {
165			this.map.getOverlays().add(new MyLocation(this, null, this.myLoc));
166			if (this.marker_fixed_to_loc) {
167				map.getOverlays().add(new Marker(marker_icon, new GeoPoint(this.myLoc)));
168			} else {
169				map.getOverlays().add(new Marker(marker_icon));
170			}
171		} else {
172			map.getOverlays().add(new Marker(marker_icon));
173		}
174	}
175
176	@Override
177	public void onLocationChanged(final Location location) {
178		if (this.myLoc == null) {
179			this.marker_fixed_to_loc = true;
180		}
181		updateUi();
182		if (LocationHelper.isBetterLocation(location, this.myLoc)) {
183			final Location oldLoc = this.myLoc;
184			this.myLoc = location;
185
186			// Don't jump back to the users location if they're not moving (more or less).
187			if (oldLoc == null || (this.marker_fixed_to_loc && this.myLoc.distanceTo(oldLoc) > 1)) {
188				gotoLoc();
189			}
190
191			updateLocationMarkers();
192		}
193	}
194
195	@Override
196	public void onStatusChanged(final String provider, final int status, final Bundle extras) {
197
198	}
199
200	@Override
201	public void onProviderEnabled(final String provider) {
202
203	}
204
205	@Override
206	public void onProviderDisabled(final String provider) {
207
208	}
209
210	private boolean isLocationEnabledAndAllowed() {
211		return this.hasLocationFeature && (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || this.hasLocationPermissions()) && this.isLocationEnabled();
212	}
213
214	private void toggleFixedLocation() {
215		this.marker_fixed_to_loc = isLocationEnabledAndAllowed() && !this.marker_fixed_to_loc;
216		if (this.marker_fixed_to_loc) {
217			gotoLoc(false);
218		}
219		updateLocationMarkers();
220		updateUi();
221	}
222
223	@Override
224	protected void updateUi() {
225		if (!hasLocationFeature || noAskAgain || isLocationEnabledAndAllowed()) {
226			this.snackBar.setVisibility(View.GONE);
227		} else {
228			this.snackBar.setVisibility(View.VISIBLE);
229		}
230
231		// Setup the fab button
232		final FloatingActionButton fab = findViewById(R.id.fab);
233		if (isLocationEnabledAndAllowed()) {
234			fab.setVisibility(View.VISIBLE);
235			runOnUiThread(() -> {
236				fab.setImageResource(marker_fixed_to_loc ? R.drawable.ic_gps_fixed_white_24dp :
237						R.drawable.ic_gps_not_fixed_white_24dp);
238				fab.setContentDescription(getResources().getString(
239						marker_fixed_to_loc ? R.string.action_unfix_from_location : R.string.action_fix_to_location
240				));
241				fab.invalidate();
242			});
243		} else {
244			fab.setVisibility(View.GONE);
245		}
246	}
247}