9
9
import android .Manifest ;
10
10
import android .Manifest .permission ;
11
11
import android .annotation .SuppressLint ;
12
+ import android .app .ProgressDialog ;
13
+ import android .content .ActivityNotFoundException ;
12
14
import android .content .BroadcastReceiver ;
13
15
import android .content .Context ;
14
16
import android .content .Intent ;
45
47
import androidx .annotation .Nullable ;
46
48
import androidx .appcompat .app .AlertDialog ;
47
49
import androidx .core .content .ContextCompat ;
50
+ import androidx .core .content .FileProvider ;
48
51
import androidx .recyclerview .widget .DividerItemDecoration ;
49
52
import androidx .recyclerview .widget .LinearLayoutManager ;
50
53
import com .google .android .material .bottomsheet .BottomSheetBehavior ;
@@ -168,6 +171,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
168
171
private Place selectedPlace ;
169
172
private Place clickedMarkerPlace ;
170
173
private boolean isClickedMarkerBookmarked ;
174
+ private ProgressDialog progressDialog ;
171
175
private final double CAMERA_TARGET_SHIFT_FACTOR_PORTRAIT = 0.005 ;
172
176
private final double CAMERA_TARGET_SHIFT_FACTOR_LANDSCAPE = 0.004 ;
173
177
private boolean isPermissionDenied ;
@@ -257,6 +261,9 @@ public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup
257
261
258
262
initNetworkBroadCastReceiver ();
259
263
presenter = new NearbyParentFragmentPresenter (bookmarkLocationDao );
264
+ progressDialog = new ProgressDialog (getActivity ());
265
+ progressDialog .setCancelable (false );
266
+ progressDialog .setMessage ("Saving in progress..." );
260
267
setHasOptionsMenu (true );
261
268
// Inflate the layout for this fragment
262
269
return view ;
@@ -288,8 +295,9 @@ public boolean onMenuItemClick(@NonNull MenuItem item) {
288
295
screenBottomLeft .getLatitude (), screenBottomLeft .getLongitude (), 0 );
289
296
fr .free .nrw .commons .___location .LatLng screenBottomLeftLatLng = new fr .free .nrw .commons .___location .LatLng (
290
297
screenTopRight .getLatitude (), screenTopRight .getLongitude (), 0 );
291
- setProgressBarVisibility (true );
292
- savePlacesAsGPX (screenTopRightLatLng ,screenBottomLeftLatLng );
298
+ progressDialog .setTitle (getString (R .string .saving_gpx_file ));
299
+ progressDialog .show ();
300
+ savePlacesAsGPX (screenTopRightLatLng , screenBottomLeftLatLng );
293
301
} catch (Exception e ) {
294
302
throw new RuntimeException (e );
295
303
}
@@ -307,8 +315,9 @@ public boolean onMenuItemClick(@NonNull MenuItem item) {
307
315
screenBottomLeft .getLatitude (), screenBottomLeft .getLongitude (), 0 );
308
316
fr .free .nrw .commons .___location .LatLng screenBottomLeftLatLng = new fr .free .nrw .commons .___location .LatLng (
309
317
screenTopRight .getLatitude (), screenTopRight .getLongitude (), 0 );
310
- setProgressBarVisibility (true );
311
- savePlacesAsKML (screenTopRightLatLng ,screenBottomLeftLatLng );
318
+ progressDialog .setTitle (getString (R .string .saving_kml_file ));
319
+ progressDialog .show ();
320
+ savePlacesAsKML (screenTopRightLatLng , screenBottomLeftLatLng );
312
321
} catch (Exception e ) {
313
322
throw new RuntimeException (e );
314
323
}
@@ -367,10 +376,10 @@ public void onViewCreated(@NonNull final View view, @Nullable final Bundle saved
367
376
binding .map .getOverlays ().add (new MapEventsOverlay (new MapEventsReceiver () {
368
377
@ Override
369
378
public boolean singleTapConfirmedHelper (GeoPoint p ) {
370
- if (clickedMarkerPlace != null ){
379
+ if (clickedMarkerPlace != null ) {
371
380
removeMarker (clickedMarkerPlace );
372
- addMarkerToMap (clickedMarkerPlace ,isClickedMarkerBookmarked );
373
- }else {
381
+ addMarkerToMap (clickedMarkerPlace , isClickedMarkerBookmarked );
382
+ } else {
374
383
Timber .e ("CLICKED MARKER IS NULL" );
375
384
}
376
385
if (isListBottomSheetExpanded ()) {
@@ -441,7 +450,8 @@ public boolean onZoom(ZoomEvent event) {
441
450
final AdvanceQueryFragment fragment = new AdvanceQueryFragment ();
442
451
final Bundle bundle = new Bundle ();
443
452
try {
444
- bundle .putString ("query" , FileUtils .readFromResource ("/queries/radius_query_for_upload_wizard.rq" ));
453
+ bundle .putString ("query" ,
454
+ FileUtils .readFromResource ("/queries/radius_query_for_upload_wizard.rq" ));
445
455
} catch (IOException e ) {
446
456
Timber .e (e );
447
457
}
@@ -507,7 +517,8 @@ private void initRvNearbyList() {
507
517
binding .bottomSheetNearby .rvNearbyList .setLayoutManager (new LinearLayoutManager (getContext ()));
508
518
adapter = new PlaceAdapter (bookmarkLocationDao ,
509
519
place -> {
510
- moveCameraToPosition (new GeoPoint (place .___location .getLatitude (),place .___location .getLongitude ()));
520
+ moveCameraToPosition (
521
+ new GeoPoint (place .___location .getLatitude (), place .___location .getLongitude ()));
511
522
return Unit .INSTANCE ;
512
523
},
513
524
(place , isBookmarked ) -> {
@@ -1166,13 +1177,12 @@ private void savePlacesAsKML(LatLng latLng, LatLng nextlatLng) {
1166
1177
String fileName =
1167
1178
"KML_" + timeStamp + "_" + System .currentTimeMillis () + ".kml" ;
1168
1179
boolean saved = saveFile (kmlString , fileName );
1169
- setProgressBarVisibility ( false );
1180
+ progressDialog . hide ( );
1170
1181
if (saved ) {
1171
- Toast .makeText (this .getContext (),
1172
- "KML file saved successfully at /Downloads/" + fileName ,
1173
- Toast .LENGTH_SHORT ).show ();
1182
+ showOpenFileDialog (getContext (), fileName , false );
1174
1183
} else {
1175
- Toast .makeText (this .getContext (), "Failed to save KML file." ,
1184
+ Toast .makeText (this .getContext (),
1185
+ getString (R .string .failed_to_save_kml_file ),
1176
1186
Toast .LENGTH_SHORT ).show ();
1177
1187
}
1178
1188
}
@@ -1201,13 +1211,12 @@ private void savePlacesAsGPX(LatLng latLng, LatLng nextlatLng) {
1201
1211
String fileName =
1202
1212
"GPX_" + timeStamp + "_" + System .currentTimeMillis () + ".gpx" ;
1203
1213
boolean saved = saveFile (gpxString , fileName );
1204
- setProgressBarVisibility ( false );
1214
+ progressDialog . hide ( );
1205
1215
if (saved ) {
1206
- Toast .makeText (this .getContext (),
1207
- "GPX file saved successfully at /Downloads/" + fileName ,
1208
- Toast .LENGTH_SHORT ).show ();
1216
+ showOpenFileDialog (getContext (), fileName , true );
1209
1217
} else {
1210
- Toast .makeText (this .getContext (), "Failed to save KML file." ,
1218
+ Toast .makeText (this .getContext (),
1219
+ getString (R .string .failed_to_save_gpx_file ),
1211
1220
Toast .LENGTH_SHORT ).show ();
1212
1221
}
1213
1222
}
@@ -1243,6 +1252,38 @@ public static boolean saveFile(String string, String fileName) {
1243
1252
}
1244
1253
}
1245
1254
1255
+ private void showOpenFileDialog (Context context , String fileName , Boolean isGPX ) {
1256
+ String title = getString (R .string .file_saved_successfully );
1257
+ String message =
1258
+ (isGPX ) ? getString (R .string .do_you_want_to_open_gpx_file )
1259
+ : getString (R .string .do_you_want_to_open_kml_file );
1260
+ Runnable runnable = () -> openFile (context , fileName , isGPX );
1261
+ DialogUtil .showAlertDialog (getActivity (), title , message , runnable ,() -> {});
1262
+ }
1263
+
1264
+ private void openFile (Context context , String fileName , Boolean isGPX ) {
1265
+ File file = new File (
1266
+ Environment .getExternalStoragePublicDirectory (Environment .DIRECTORY_DOWNLOADS ),
1267
+ fileName );
1268
+ Uri uri = FileProvider .getUriForFile (context ,
1269
+ context .getApplicationContext ().getPackageName () + ".provider" , file );
1270
+ Intent intent = new Intent (Intent .ACTION_VIEW );
1271
+
1272
+ if (isGPX ) {
1273
+ intent .setDataAndType (uri , "application/gpx" );
1274
+ } else {
1275
+ intent .setDataAndType (uri , "application/kml" );
1276
+ }
1277
+
1278
+ intent .setFlags (Intent .FLAG_GRANT_READ_URI_PERMISSION );
1279
+ try {
1280
+ context .startActivity (intent );
1281
+ } catch (ActivityNotFoundException e ) {
1282
+ Toast .makeText (context , R .string .no_application_available_to_open_gpx_files ,
1283
+ Toast .LENGTH_SHORT ).show ();
1284
+ }
1285
+ }
1286
+
1246
1287
private static boolean isExternalStorageWritable () {
1247
1288
String state = Environment .getExternalStorageState ();
1248
1289
return Environment .MEDIA_MOUNTED .equals (state );
@@ -1486,7 +1527,8 @@ public void displayLoginSkippedWarning() {
1486
1527
.setMessage (R .string .login_alert_message )
1487
1528
.setPositiveButton (R .string .login , (dialog , which ) -> {
1488
1529
// logout of the app
1489
- BaseLogoutListener logoutListener = new BaseLogoutListener (getActivity ()); CommonsApplication app = (CommonsApplication ) getActivity ().getApplication ();
1530
+ BaseLogoutListener logoutListener = new BaseLogoutListener (getActivity ());
1531
+ CommonsApplication app = (CommonsApplication ) getActivity ().getApplication ();
1490
1532
app .clearApplicationData (getContext (), logoutListener );
1491
1533
})
1492
1534
.show ();
@@ -1557,14 +1599,15 @@ public void enableFABRecenter() {
1557
1599
* Adds a marker for the user's current position. Adds a circle which uses the accuracy * 2, to
1558
1600
* draw a circle which represents the user's position with an accuracy of 95%.
1559
1601
* <p>
1560
- * Should be called only on creation of Map, there is other method to update markers
1561
- * ___location with users move.
1602
+ * Should be called only on creation of Map, there is other method to update markers ___location
1603
+ * with users move.
1562
1604
*
1563
1605
* @param currentLatLng current ___location
1564
1606
*/
1565
1607
@ Override
1566
1608
public void addCurrentLocationMarker (final fr .free .nrw .commons .___location .LatLng currentLatLng ) {
1567
- if (null != currentLatLng && !isPermissionDenied && locationManager .isGPSProviderEnabled ()) {
1609
+ if (null != currentLatLng && !isPermissionDenied
1610
+ && locationManager .isGPSProviderEnabled ()) {
1568
1611
ExecutorUtils .get ().submit (() -> {
1569
1612
Timber .d ("Adds current ___location marker" );
1570
1613
recenterMarkerToPosition (
@@ -1668,9 +1711,9 @@ public fr.free.nrw.commons.___location.LatLng getCameraTarget() {
1668
1711
/**
1669
1712
* Sets marker icon according to marker status. Sets title and distance.
1670
1713
*
1671
- * @param isBookmarked true if place is bookmarked
1714
+ * @param isBookmarked true if place is bookmarked
1672
1715
* @param place
1673
- * @param currentLatLng current ___location
1716
+ * @param currentLatLng current ___location
1674
1717
*/
1675
1718
public void updateMarker (final boolean isBookmarked , final Place place ,
1676
1719
@ Nullable final fr .free .nrw .commons .___location .LatLng currentLatLng ) {
@@ -1691,18 +1734,18 @@ private void highlightNearestPlace(Place nearestPlace) {
1691
1734
/**
1692
1735
* Returns drawable of marker icon for given place
1693
1736
*
1694
- * @param place where marker is to be added
1737
+ * @param place where marker is to be added
1695
1738
* @param isBookmarked true if place is bookmarked
1696
1739
* @return returns the drawable of marker according to the place information
1697
1740
*/
1698
1741
private @ DrawableRes int getIconFor (Place place , Boolean isBookmarked ) {
1699
- if (nearestPlace != null ) {
1700
- if (place .name .equals (nearestPlace .name )) {
1742
+ if (nearestPlace != null ) {
1743
+ if (place .name .equals (nearestPlace .name )) {
1701
1744
// Highlight nearest place only when user clicks on the home nearby banner
1702
1745
highlightNearestPlace (place );
1703
- return (isBookmarked ?
1704
- R .drawable .ic_custom_map_marker_purple_bookmarked :
1705
- R .drawable .ic_custom_map_marker_purple );
1746
+ return (isBookmarked ?
1747
+ R .drawable .ic_custom_map_marker_purple_bookmarked :
1748
+ R .drawable .ic_custom_map_marker_purple );
1706
1749
}
1707
1750
}
1708
1751
if (place .isMonument ()) {
@@ -1743,10 +1786,10 @@ public boolean onItemSingleTapUp(int index, OverlayItem item) {
1743
1786
hideBottomSheet ();
1744
1787
if (clickedMarkerPlace != null ) {
1745
1788
removeMarker (clickedMarkerPlace );
1746
- addMarkerToMap (clickedMarkerPlace ,isClickedMarkerBookmarked );
1789
+ addMarkerToMap (clickedMarkerPlace , isClickedMarkerBookmarked );
1747
1790
}
1748
1791
clickedMarkerPlace = place ;
1749
- isClickedMarkerBookmarked = isBookMarked ;
1792
+ isClickedMarkerBookmarked = isBookMarked ;
1750
1793
bottomSheetDetailsBehavior .setState (BottomSheetBehavior .STATE_COLLAPSED );
1751
1794
return true ;
1752
1795
}
@@ -1789,10 +1832,10 @@ public boolean onItemSingleTapUp(int index, OverlayItem item) {
1789
1832
hideBottomSheet ();
1790
1833
if (clickedMarkerPlace != null ) {
1791
1834
removeMarker (clickedMarkerPlace );
1792
- addMarkerToMap (clickedMarkerPlace ,isClickedMarkerBookmarked );
1835
+ addMarkerToMap (clickedMarkerPlace , isClickedMarkerBookmarked );
1793
1836
}
1794
- clickedMarkerPlace = place ;
1795
- isClickedMarkerBookmarked = false ;
1837
+ clickedMarkerPlace = place ;
1838
+ isClickedMarkerBookmarked = false ;
1796
1839
bottomSheetDetailsBehavior .setState (BottomSheetBehavior .STATE_COLLAPSED );
1797
1840
return true ;
1798
1841
}
@@ -2134,10 +2177,10 @@ public void clearAllMarkers() {
2134
2177
binding .map .getOverlays ().add (new MapEventsOverlay (new MapEventsReceiver () {
2135
2178
@ Override
2136
2179
public boolean singleTapConfirmedHelper (GeoPoint p ) {
2137
- if (clickedMarkerPlace != null ){
2180
+ if (clickedMarkerPlace != null ) {
2138
2181
removeMarker (clickedMarkerPlace );
2139
- addMarkerToMap (clickedMarkerPlace ,isClickedMarkerBookmarked );
2140
- }else {
2182
+ addMarkerToMap (clickedMarkerPlace , isClickedMarkerBookmarked );
2183
+ } else {
2141
2184
Timber .e ("CLICKED MARKER IS NULL" );
2142
2185
}
2143
2186
if (isListBottomSheetExpanded ()) {
0 commit comments