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