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