From c84ecd1b9c2cc1e6da757da89a18b712521b20ec Mon Sep 17 00:00:00 2001 From: neslihanturan Date: Mon, 10 Jun 2019 19:39:44 +0300 Subject: [PATCH] Add changes for permission made by Vivek to newly added fragments along with our nearby classes --- .../contributions/ContributionsFragment.java | 2 +- .../location/LocationServiceManager.java | 2 +- .../nrw/commons/nearby/NearbyFragment.java | 6 +- .../NearbyParentFragmentContract.java | 6 +- .../mvp/fragments/NearbyParentFragment.java | 168 +++--------------- .../NearbyParentFragmentPresenter.java | 4 +- 6 files changed, 41 insertions(+), 147 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsFragment.java b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsFragment.java index 451ea2ad0..4d68500ca 100644 --- a/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/contributions/ContributionsFragment.java @@ -501,7 +501,7 @@ public class ContributionsFragment private void onLocationPermissionGranted() { nearbyNotificationCardView.permissionType = NearbyNotificationCardView.PermissionType.NO_PERMISSION_NEEDED; - locationManager.registerLocationManager(getActivity()); + locationManager.registerLocationManager(); } private void showNearbyCardPermissionRationale() { diff --git a/app/src/main/java/fr/free/nrw/commons/location/LocationServiceManager.java b/app/src/main/java/fr/free/nrw/commons/location/LocationServiceManager.java index d639d0c73..f88001d7c 100644 --- a/app/src/main/java/fr/free/nrw/commons/location/LocationServiceManager.java +++ b/app/src/main/java/fr/free/nrw/commons/location/LocationServiceManager.java @@ -48,7 +48,7 @@ public class LocationServiceManager implements LocationListener { /** * Registers a LocationManager to listen for current location. */ - public void registerLocationManager(Context context) { + public void registerLocationManager() { if (!isLocationManagerRegistered) { isLocationManagerRegistered = requestLocationUpdatesFromProvider(LocationManager.NETWORK_PROVIDER) && requestLocationUpdatesFromProvider(LocationManager.GPS_PROVIDER); diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyFragment.java index ab43c970d..5ecac4a80 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyFragment.java @@ -44,6 +44,9 @@ import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import timber.log.Timber; +import com.google.android.material.bottomsheet.BottomSheetBehavior; +import com.google.android.material.snackbar.Snackbar; + import static fr.free.nrw.commons.contributions.MainActivity.CONTRIBUTIONS_TAB_POSITION; import static fr.free.nrw.commons.contributions.MainActivity.NEARBY_TAB_POSITION; import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED; @@ -105,6 +108,7 @@ public class NearbyFragment extends CommonsDaggerSupportFragment setRetainInstance(true); } + @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -569,7 +573,7 @@ public class NearbyFragment extends CommonsDaggerSupportFragment */ private void registerLocationUpdates() { - locationManager.registerLocationManager(getActivity()); + locationManager.registerLocationManager(); } private void showErrorMessage(String message) { diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyParentFragmentContract.java b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyParentFragmentContract.java index 94e4099bc..f76cb9585 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyParentFragmentContract.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyParentFragmentContract.java @@ -11,10 +11,6 @@ public interface NearbyParentFragmentContract { void setListFragmentExpanded(); void refreshView(); void registerLocationUpdates(LocationServiceManager locationServiceManager); - void requestLocationPermissions(LocationServiceManager locationServiceManager); - void showLocationPermissionDeniedErrorDialog(LocationServiceManager locationServiceManager); - void checkGps(LocationServiceManager locationServiceManager); - void checkLocationPermission(LocationServiceManager locationServiceManager); boolean isNetworkConnectionEstablished(); void addNetworkBroadcastReceiver(); void listOptionMenuItemClicked(); @@ -23,6 +19,8 @@ public interface NearbyParentFragmentContract { void addSearchThisAreaButtonAction(); void setSearchThisAreaButtonVisibility(boolean isVisible); void setSearchThisAreaProgressVisibility(boolean isVisible); + void checkPermissionsAndPerformAction(Runnable runnable); + void resumeFragment(); } interface UserActions { diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/fragments/NearbyParentFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/fragments/NearbyParentFragment.java index 5c686792d..d77cb12de 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/fragments/NearbyParentFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/fragments/NearbyParentFragment.java @@ -1,5 +1,7 @@ package fr.free.nrw.commons.nearby.mvp.fragments; +import android.Manifest; + import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -40,12 +42,15 @@ import fr.free.nrw.commons.nearby.mvp.presenter.NearbyParentFragmentPresenter; import fr.free.nrw.commons.utils.FragmentUtils; import fr.free.nrw.commons.utils.LocationUtils; import fr.free.nrw.commons.utils.NetworkUtils; +import fr.free.nrw.commons.utils.PermissionUtils; import fr.free.nrw.commons.wikidata.WikidataEditListener; import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import timber.log.Timber; +import static fr.free.nrw.commons.contributions.MainActivity.CONTRIBUTIONS_TAB_POSITION; +import static fr.free.nrw.commons.contributions.MainActivity.NEARBY_TAB_POSITION; import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED; import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.SEARCH_CUSTOM_AREA; @@ -122,9 +127,26 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment @Override public void onResume() { super.onResume(); + // Resume the fragment if exist resumeFragment(); + if (!((MainActivity) getActivity()).isContributionsFragmentVisible) { + checkPermissionsAndPerformAction(this::resumeFragment); + } else { + resumeFragment(); + } } + @Override + public void checkPermissionsAndPerformAction(Runnable runnable) { + PermissionUtils.checkPermissionsAndPerformAction(getActivity(), + Manifest.permission.ACCESS_FINE_LOCATION, + runnable, + () -> ((MainActivity) getActivity()).viewPager.setCurrentItem(CONTRIBUTIONS_TAB_POSITION), + R.string.location_permission_title, + R.string.location_permission_rationale_nearby); + } + + /** * Thanks to this method we make sure NearbyMapFragment is ready and attached. So that we can * prevent NPE caused by null child fragment. This method is called from child fragment when @@ -201,17 +223,6 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment nearbyParentFragmentPresenter.updateMapMarkers(nearbyPlacesInfo); } - - /** - * Resume fragments if they exists - */ - private void resumeFragment() { - Timber.d("Resume existing fragments if there is any"); - // Find the retained fragment on activity restarts - nearbyMapFragment = getMapFragment(); - nearbyListFragment = getListFragment(); - } - /** * Returns the map fragment added to child fragment manager previously, if exists. */ @@ -262,139 +273,18 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment */ @Override public void registerLocationUpdates(LocationServiceManager locationServiceManager) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (locationServiceManager.isLocationPermissionGranted(requireContext())) { - locationServiceManager.registerLocationManager(getActivity()); - } else { - // Should we show an explanation? - if (locationServiceManager.isPermissionExplanationRequired(getActivity())) { - new AlertDialog.Builder(getActivity()) - .setMessage(getString(R.string.location_permission_rationale_nearby)) - .setPositiveButton(android.R.string.ok, (dialog, which) -> { - requestLocationPermissions(locationServiceManager); - dialog.dismiss(); - }) - .setNegativeButton(android.R.string.cancel, (dialog, id) -> { - showLocationPermissionDeniedErrorDialog(locationServiceManager); - dialog.cancel(); - }) - .create() - .show(); - - } else { - // No explanation needed, we can request the permission. - requestLocationPermissions(locationServiceManager); - } - } - } else { - locationServiceManager.registerLocationManager(getActivity()); - } + locationManager.registerLocationManager(); } /** - * Request location permission if activity is not null - * @param locationServiceManager passed from presenter, to listen/un-listen location changes + * Resume fragments if they exists */ @Override - public void requestLocationPermissions(LocationServiceManager locationServiceManager) { - if (!getActivity().isFinishing()) { - locationServiceManager.requestPermissions(); - } - } - - /** - * Will warn user if location is denied - * @param locationServiceManager will be passed to checkGps if needs permission - */ - @Override - public void showLocationPermissionDeniedErrorDialog(LocationServiceManager locationServiceManager) { - new AlertDialog.Builder(getActivity()) - .setMessage(R.string.nearby_needs_permissions) - .setCancelable(false) - .setPositiveButton(R.string.give_permission, (dialog, which) -> { - //will ask for the location permission again - checkGps(locationServiceManager); - }) - .setNegativeButton(R.string.cancel, (dialog, which) -> { - //dismiss dialog and send user to contributions tab instead - dialog.cancel(); - ((MainActivity)getActivity()).viewPager.setCurrentItem(((MainActivity)getActivity()).CONTRIBUTIONS_TAB_POSITION); - }) - .create() - .show(); - - } - - /** - * Checks device GPS permission first for all API levels - * @param locationServiceManager will be used to check if provider is enable - */ - @Override - public void checkGps(LocationServiceManager locationServiceManager) { - Timber.d("checking GPS"); - if (!locationServiceManager.isProviderEnabled()) { - Timber.d("GPS is not enabled"); - new AlertDialog.Builder(getActivity()) - .setMessage(R.string.gps_disabled) - .setCancelable(false) - .setPositiveButton(R.string.enable_gps, - (dialog, id) -> { - Intent callGPSSettingIntent = new Intent( - android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); - Timber.d("Loaded settings page"); - startActivityForResult(callGPSSettingIntent, 1); - }) - .setNegativeButton(R.string.menu_cancel_upload, (dialog, id) -> { - showLocationPermissionDeniedErrorDialog(locationServiceManager); - dialog.cancel(); - }) - .create() - .show(); - } else { - Timber.d("GPS is enabled"); - checkLocationPermission(locationServiceManager); - } - } - - /** - * This method ideally should be called from inside of CheckGPS method. If device GPS is enabled - * then we need to control app specific permissions for >=M devices. For other devices, enabled - * GPS is enough for nearby, so directly call refresh view. - * @param locationServiceManager will be used to detect if location permission is granted or not - */ - @Override - public void checkLocationPermission(LocationServiceManager locationServiceManager) { - Timber.d("Checking location permission"); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (locationServiceManager.isLocationPermissionGranted(requireContext())) { - nearbyParentFragmentPresenter.updateMapAndList(LOCATION_SIGNIFICANTLY_CHANGED, null); - } else { - // Should we show an explanation? - if (locationServiceManager.isPermissionExplanationRequired(getActivity())) { - // Show an explanation to the user *asynchronously* -- don't block - // this thread waiting for the user's response! After the user - // sees the explanation, try again to request the permission. - new AlertDialog.Builder(getActivity()) - .setMessage(getString(R.string.location_permission_rationale_nearby)) - .setPositiveButton(android.R.string.ok, (dialog, which) -> { - requestLocationPermissions(locationServiceManager); - dialog.dismiss(); - }) - .setNegativeButton(android.R.string.cancel, (dialog, id) -> { - showLocationPermissionDeniedErrorDialog(locationServiceManager); - dialog.cancel(); - }) - .create() - .show(); - - } else { - // No explanation needed, we can request the permission. - requestLocationPermissions(locationServiceManager); - } - } - } else { - nearbyParentFragmentPresenter.updateMapAndList(LOCATION_SIGNIFICANTLY_CHANGED, null); - } + public void resumeFragment() { + // Find the retained fragment on activity restarts + nearbyMapFragment = getMapFragment(); + nearbyListFragment = getListFragment(); + addNetworkBroadcastReceiver(); } @Override diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/presenter/NearbyParentFragmentPresenter.java b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/presenter/NearbyParentFragmentPresenter.java index 322c18269..69d2505b7 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/presenter/NearbyParentFragmentPresenter.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/presenter/NearbyParentFragmentPresenter.java @@ -11,6 +11,7 @@ import fr.free.nrw.commons.location.LocationUpdateListener; import fr.free.nrw.commons.nearby.NearbyController; import fr.free.nrw.commons.nearby.mvp.contract.NearbyMapContract; import fr.free.nrw.commons.nearby.mvp.contract.NearbyParentFragmentContract; +import fr.free.nrw.commons.nearby.mvp.fragments.NearbyParentFragment; import fr.free.nrw.commons.utils.LocationUtils; import fr.free.nrw.commons.wikidata.WikidataEditListener; @@ -109,11 +110,12 @@ public class NearbyParentFragmentPresenter Timber.d("initializing nearby operations started"); // Add location listener to be notified about location changes locationServiceManager.addLocationListener(this); + nearbyParentFragmentView.checkPermissionsAndPerformAction(nearbyParentFragmentView::resumeFragment); nearbyParentFragmentView.registerLocationUpdates(locationServiceManager); // Nearby buttons should be active, they should be inactive only during update lockNearby(false); // This will start a consequence to check GPS depending on different API - nearbyParentFragmentView.checkGps(locationServiceManager); + //nearbyParentFragmentView.checkGps(locationServiceManager); // We will know when we went offline and online again nearbyParentFragmentView.addNetworkBroadcastReceiver(); }