ShareLocationActivity.java

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