Skip to content

Commit

Permalink
Android 13 permission related fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
srishti-R committed Sep 21, 2023
1 parent f577053 commit 1294d90
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 125 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO"/>
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="com.google.android.apps.photos.permission.GOOGLE_PHOTOS" />
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void initiateCameraPick(Activity activity) {
}

PermissionUtils.checkPermissionsAndPerformAction(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
PermissionUtils.PERMISSIONS_STORAGE,
() -> {
if (defaultKvStore.getBoolean("inAppCameraFirstRun")) {
defaultKvStore.putBoolean("inAppCameraFirstRun", false);
Expand Down Expand Up @@ -159,7 +159,7 @@ public void initiateCustomGalleryPickWithPermission(final Activity activity) {
setPickerConfiguration(activity,true);

PermissionUtils.checkPermissionsAndPerformAction(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
PermissionUtils.PERMISSIONS_STORAGE,
() -> FilePicker.openCustomSelector(activity, 0),
R.string.storage_permission_title,
R.string.write_storage_permission_rationale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ public void onResume() {
}

private void checkPermissionsAndShowNearbyCardView() {
if (PermissionUtils.hasPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)) {
if (PermissionUtils.hasPermission(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION})) {
onLocationPermissionGranted();
} else if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)
&& store.getBoolean("displayLocationPermissionForCardView", true)
Expand All @@ -452,7 +452,7 @@ private void checkPermissionsAndShowNearbyCardView() {

private void requestLocationPermission() {
PermissionUtils.checkPermissionsAndPerformAction(getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
this::onLocationPermissionGranted,
this::displayYouWontSeeNearbyMessage,
-1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void onCreate(Bundle savedInstanceState) {
if (VERSION.SDK_INT >= VERSION_CODES.Q) {
PermissionUtils.checkPermissionsAndPerformAction(
this,
permission.ACCESS_MEDIA_LOCATION,
new String[]{permission.ACCESS_MEDIA_LOCATION},
() -> {},
R.string.media_location_permission_denied,
R.string.add_location_manually
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private void registerNetworkReceiver() {
private void performMapReadyActions() {
if (isMapBoxReady) {
if(!applicationKvStore.getBoolean("doNotAskForLocationPermission", false) ||
PermissionUtils.hasPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)){
PermissionUtils.hasPermission(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION})){
checkPermissionsAndPerformAction();
}else{
isPermissionDenied = true;
Expand Down Expand Up @@ -404,8 +404,8 @@ private void showErrorMessage(final String message) {
public void checkPermissionsAndPerformAction() {
Timber.d("Checking permission and perfoming action");
PermissionUtils.checkPermissionsAndPerformAction(getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION,
() -> locationPermissionGranted(),
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
this::locationPermissionGranted,
() -> isPermissionDenied = true,
R.string.location_permission_title,
R.string.location_permission_rationale_nearby);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private void requestForLocationAccess(
Dialog locationOffDialog
) {
PermissionUtils.checkPermissionsAndPerformAction(activity,
permission.ACCESS_FINE_LOCATION,
new String[]{permission.ACCESS_FINE_LOCATION},
() -> {
if(!isLocationAccessToAppsTurnedOn()) {
showLocationOffDialog(locationOffDialog);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,12 @@ public void run() {

@OnClick(R.id.mediaDetailImageViewSpacer)
public void launchZoomActivity(final View view) {
final boolean permission = PermissionUtils.
hasPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);

if (permission) {
final boolean hasPermission = PermissionUtils.hasPermission(getActivity(), PermissionUtils.PERMISSIONS_STORAGE);
if (hasPermission) {
launchZoomActivityAfterPermissionCheck(view);
}
else {
} else {
PermissionUtils.checkPermissionsAndPerformAction(getActivity(),
Manifest.permission.READ_EXTERNAL_STORAGE,
PermissionUtils.PERMISSIONS_STORAGE,
() -> {
launchZoomActivityAfterPermissionCheck(view);
},
Expand Down Expand Up @@ -1076,7 +1073,7 @@ public void onActivityResult(final int requestCode, final int resultCode,

} else if (requestCode == REQUEST_CODE && resultCode == RESULT_CANCELED) {
viewUtil.showShortToast(getContext(),
Objects.requireNonNull(getContext())
requireContext()
.getString(R.string.coordinates_picking_unsuccessful));

} else if (requestCode == REQUEST_CODE_EDIT_DESCRIPTION && resultCode == RESULT_CANCELED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ private void addCheckBoxCallback() {
private void performMapReadyActions() {
if (((MainActivity)getActivity()).activeFragment == ActiveFragment.NEARBY && isMapBoxReady) {
if(!applicationKvStore.getBoolean("doNotAskForLocationPermission", false) ||
PermissionUtils.hasPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)){
PermissionUtils.hasPermission(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION})){
checkPermissionsAndPerformAction();
}else{
isPermissionDenied = true;
Expand Down Expand Up @@ -1215,8 +1215,8 @@ public void setTabItemContributions() {
public void checkPermissionsAndPerformAction() {
Timber.d("Checking permission and perfoming action");
PermissionUtils.checkPermissionsAndPerformAction(getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION,
() -> locationPermissionGranted(),
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
this::locationPermissionGranted,
() -> isPermissionDenied = true,
R.string.location_permission_title,
R.string.location_permission_rationale_nearby);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
import androidx.preference.PreferenceViewHolder;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.MultiplePermissionsReport;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.multi.MultiplePermissionsListener;
import com.karumi.dexter.listener.single.BasePermissionListener;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils;
Expand Down Expand Up @@ -371,7 +375,7 @@ public void onItemClick(AdapterView<?> adapterView, View view, int i,
Locale defLocale = new Locale(languageCode);
if(keyListPreference.equals("appUiDefaultLanguagePref")) {
appUiLanguageListPreference.setSummary(defLocale.getDisplayLanguage(defLocale));
setLocale(Objects.requireNonNull(getActivity()), languageCode);
setLocale(requireActivity(), languageCode);
getActivity().recreate();
final Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
Expand Down Expand Up @@ -410,8 +414,8 @@ private void setUpRecentLanguagesSection(List<Language> recentLanguages,
separator.setVisibility(View.VISIBLE);
final RecentLanguagesAdapter recentLanguagesAdapter
= new RecentLanguagesAdapter(
getActivity(),
recentLanguagesDao.getRecentLanguages(),
getActivity(),
recentLanguagesDao.getRecentLanguages(),
selectedLanguages);
languageHistoryListView.setAdapter(recentLanguagesAdapter);
}
Expand All @@ -436,7 +440,7 @@ private void onRecentLanguageClicked(String keyListPreference, Dialog dialog, Ad
final Locale defLocale = new Locale(recentLanguageCode);
if (keyListPreference.equals("appUiDefaultLanguagePref")) {
appUiLanguageListPreference.setSummary(defLocale.getDisplayLanguage(defLocale));
setLocale(Objects.requireNonNull(getActivity()), recentLanguageCode);
setLocale(requireActivity(), recentLanguageCode);
getActivity().recreate();
final Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
Expand Down Expand Up @@ -506,24 +510,34 @@ private String getCurrentLanguageCode(final String preferenceKey) {
* First checks for external storage permissions and then sends logs via email
*/
private void checkPermissionsAndSendLogs() {
if (PermissionUtils.hasPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
if (PermissionUtils.hasPermission(getActivity(), PermissionUtils.PERMISSIONS_STORAGE)) {
commonsLogSender.send(getActivity(), null);
} else {
requestExternalStoragePermissions();
}
}

/**
* Requests external storage permissions and shows a toast stating that log collection has started
* Requests external storage permissions and shows a toast stating that log collection has
* started
*/
private void requestExternalStoragePermissions() {
Dexter.withActivity(getActivity())
.withPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.withListener(new BasePermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse response) {
ViewUtil.showLongToast(getActivity(), getResources().getString(R.string.log_collection_started));
}
}).check();
.withPermissions(PermissionUtils.PERMISSIONS_STORAGE)
.withListener(new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport report) {
ViewUtil.showLongToast(getActivity(),
getResources().getString(R.string.log_collection_started));
}

@Override
public void onPermissionRationaleShouldBeShown(
List<PermissionRequest> permissions, PermissionToken token) {

}
})
.onSameThread()
.check();
}
}
58 changes: 30 additions & 28 deletions app/src/main/java/fr/free/nrw/commons/upload/UploadActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.free.nrw.commons.upload;

import static fr.free.nrw.commons.contributions.ContributionController.ACTION_INTERNAL_UPLOADS;
import static fr.free.nrw.commons.utils.PermissionUtils.PERMISSIONS_STORAGE;
import static fr.free.nrw.commons.wikidata.WikidataConstants.PLACE_OBJECT;

import android.Manifest;
Expand Down Expand Up @@ -56,6 +57,7 @@
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import java.security.Permission;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -146,20 +148,19 @@ protected void onCreate(Bundle savedInstanceState) {
compositeDisposable = new CompositeDisposable();
init();
nearbyPopupAnswers = new HashMap<>();

PermissionUtils.checkPermissionsAndPerformAction(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
this::receiveSharedItems,
R.string.storage_permission_title,
R.string.write_storage_permission_rationale_for_image_share);
PERMISSIONS_STORAGE,
this::receiveSharedItems,
R.string.storage_permission_title,
R.string.write_storage_permission_rationale_for_image_share);
//getting the current dpi of the device and if it is less than 320dp i.e. overlapping
//threshold, thumbnails automatically minimizes
DisplayMetrics metrics = getResources().getDisplayMetrics();
float dpi = (metrics.widthPixels)/(metrics.density);
if (dpi<=321) {
onRlContainerTitleClicked();
}
if (PermissionUtils.hasPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
if (PermissionUtils.hasPermission(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION})) {
locationManager.registerLocationManager();
}
locationManager.requestLocationUpdatesFromProvider(LocationManager.GPS_PROVIDER);
Expand All @@ -181,7 +182,7 @@ private void initProgressDialog() {

private void initThumbnailsRecyclerView() {
rvThumbnails.setLayoutManager(new LinearLayoutManager(this,
LinearLayoutManager.HORIZONTAL, false));
LinearLayoutManager.HORIZONTAL, false));
thumbnailsAdapter = new ThumbnailsAdapter(() -> currentSelectedPosition);
rvThumbnails.setAdapter(thumbnailsAdapter);

Expand All @@ -193,7 +194,7 @@ private void initViewPager() {
vpUpload.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
int positionOffsetPixels) {

}

Expand Down Expand Up @@ -238,29 +239,31 @@ protected void onResume() {
*/
protected void checkBlockStatus() {
compositeDisposable.add(userClient.isUserBlockedFromCommons()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.filter(result -> result)
.subscribe(result -> DialogUtil.showAlertDialog(
this,
getString(R.string.block_notification_title),
getString(R.string.block_notification),
getString(R.string.ok),
this::finish,
true)));
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.filter(result -> result)
.subscribe(result -> DialogUtil.showAlertDialog(
this,
getString(R.string.block_notification_title),
getString(R.string.block_notification),
getString(R.string.ok),
this::finish,
true)));
}

private void checkStoragePermissions() {
PermissionUtils.checkPermissionsAndPerformAction(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
final boolean hasAllPermissions = PermissionUtils.hasPermission(this, PERMISSIONS_STORAGE);
if (!hasAllPermissions) {
PermissionUtils.checkPermissionsAndPerformAction(this,
PERMISSIONS_STORAGE,
() -> {
//TODO handle this
},
R.string.storage_permission_title,
R.string.write_storage_permission_rationale_for_image_share);
}
}


@Override
protected void onStop() {
super.onStop();
Expand Down Expand Up @@ -328,7 +331,7 @@ public void onUploadMediaDeleted(int index) {
@Override
public void updateTopCardTitle() {
tvTopCardTitle.setText(getResources()
.getQuantityString(R.plurals.upload_count_title, uploadableFiles.size(), uploadableFiles.size()));
.getQuantityString(R.plurals.upload_count_title, uploadableFiles.size(), uploadableFiles.size()));
}

@Override
Expand Down Expand Up @@ -370,13 +373,13 @@ private void receiveSharedItems() {
} else {
//Show thumbnails
if (uploadableFiles.size()
> 1) {//If there is only file, no need to show the image thumbnails
> 1) {//If there is only file, no need to show the image thumbnails
thumbnailsAdapter.setUploadableFiles(uploadableFiles);
} else {
llContainerTopCard.setVisibility(View.GONE);
}
tvTopCardTitle.setText(getResources()
.getQuantityString(R.plurals.upload_count_title, uploadableFiles.size(), uploadableFiles.size()));
.getQuantityString(R.plurals.upload_count_title, uploadableFiles.size(), uploadableFiles.size()));

fragments = new ArrayList<>();
/* Suggest users to turn battery optimisation off when uploading more than a few files.
Expand Down Expand Up @@ -419,7 +422,7 @@ private void receiveSharedItems() {
UploadMediaDetailFragment uploadMediaDetailFragment = new UploadMediaDetailFragment();

LocationPermissionsHelper locationPermissionsHelper = new LocationPermissionsHelper(
this, locationManager, null);
this, locationManager, null);
if (locationPermissionsHelper.isLocationAccessToAppsTurnedOn()) {
currLocation = locationManager.getLastLocation();
}
Expand Down Expand Up @@ -525,8 +528,8 @@ private float getLocationDifference(LatLng currLocation, LatLng prevLocation) {
}
float[] distance = new float[2];
Location.distanceBetween(
currLocation.getLatitude(), currLocation.getLongitude(),
prevLocation.getLatitude(), prevLocation.getLongitude(), distance);
currLocation.getLatitude(), currLocation.getLongitude(),
prevLocation.getLatitude(), prevLocation.getLongitude(), distance);
return distance[0];
}

Expand Down Expand Up @@ -670,5 +673,4 @@ public void onBackPressed() {
this::finish
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ object DownloadUtils {
}
PermissionUtils.checkPermissionsAndPerformAction(
activity,
permission.WRITE_EXTERNAL_STORAGE,
PermissionUtils.PERMISSIONS_STORAGE,
{ enqueueRequest(activity, req) },
{
Toast.makeText(
Expand Down
Loading

0 comments on commit 1294d90

Please sign in to comment.