mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-12-27 19:15:50 +01:00
Actually improve requesting all permissions
This commit is contained in:
parent
2422e02e5d
commit
b3b665ab6c
@ -27,6 +27,7 @@ import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
@ -34,6 +35,8 @@ import androidx.recyclerview.widget.RecyclerView;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
@ -45,6 +48,7 @@ public class WelcomeFragmentPermissions extends Fragment {
|
||||
private RecyclerView permissionsListView;
|
||||
private PermissionAdapter permissionAdapter;
|
||||
private Button requestAllButton;
|
||||
private List<String> requestingPermissions = new ArrayList<>();
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
@ -53,7 +57,14 @@ public class WelcomeFragmentPermissions extends Fragment {
|
||||
View view = inflater.inflate(R.layout.fragment_welcome_permissions, container, false);
|
||||
|
||||
requestAllButton = view.findViewById(R.id.button_request_all);
|
||||
requestAllButton.setOnClickListener(v -> PermissionsUtils.requestAllPermissions(requireActivity()));
|
||||
requestAllButton.setOnClickListener(v -> {
|
||||
List<PermissionsUtils.PermissionDetails> wantedPermissions = PermissionsUtils.getRequiredPermissionsList(requireActivity());
|
||||
requestingPermissions = new ArrayList<>();
|
||||
for (PermissionsUtils.PermissionDetails wantedPermission : wantedPermissions) {
|
||||
requestingPermissions.add(wantedPermission.getPermission());
|
||||
}
|
||||
requestAllPermissions();
|
||||
});
|
||||
|
||||
// Initialize RecyclerView and data
|
||||
permissionsListView = view.findViewById(R.id.permissions_list);
|
||||
@ -73,6 +84,28 @@ public class WelcomeFragmentPermissions extends Fragment {
|
||||
if (PermissionsUtils.checkAllPermissions(requireActivity())) {
|
||||
requestAllButton.setEnabled(false);
|
||||
}
|
||||
if (!requestingPermissions.isEmpty()) {
|
||||
requestAllPermissions();
|
||||
}
|
||||
}
|
||||
|
||||
public void requestAllPermissions() {
|
||||
if (!requestingPermissions.isEmpty()) {
|
||||
Iterator<String> it = requestingPermissions.iterator();
|
||||
while (it.hasNext()) {
|
||||
String currentPermission = it.next();
|
||||
if (PermissionsUtils.specialPermissions.contains(currentPermission)) {
|
||||
it.remove();
|
||||
if (!PermissionsUtils.checkPermission(requireActivity(), currentPermission)) {
|
||||
PermissionsUtils.requestPermission(requireActivity(), currentPermission);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
String[] combinedPermissions = requestingPermissions.toArray(new String[0]);
|
||||
requestingPermissions.clear();
|
||||
ActivityCompat.requestPermissions(requireActivity(), combinedPermissions, 0);
|
||||
}
|
||||
}
|
||||
|
||||
private class PermissionHolder extends RecyclerView.ViewHolder {
|
||||
|
@ -48,9 +48,17 @@ import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
public class PermissionsUtils {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(PermissionsUtils.class);
|
||||
|
||||
private static final String CUSTOM_PERM_NOTIFICATION_LISTENER = "custom_perm_notifications_listener";
|
||||
private static final String CUSTOM_PERM_NOTIFICATION_SERVICE = "custom_perm_notifications_service";
|
||||
private static final String CUSTOM_PERM_DISPLAY_OVER = "custom_perm_display_over";
|
||||
public static final String CUSTOM_PERM_NOTIFICATION_LISTENER = "custom_perm_notifications_listener";
|
||||
public static final String CUSTOM_PERM_NOTIFICATION_SERVICE = "custom_perm_notifications_service";
|
||||
public static final String CUSTOM_PERM_DISPLAY_OVER = "custom_perm_display_over";
|
||||
|
||||
public static final List<String> specialPermissions = new ArrayList<String>() {{
|
||||
add(CUSTOM_PERM_NOTIFICATION_LISTENER);
|
||||
add(CUSTOM_PERM_NOTIFICATION_SERVICE);
|
||||
add(CUSTOM_PERM_DISPLAY_OVER);
|
||||
add(Manifest.permission.ACCESS_FINE_LOCATION);
|
||||
add(Manifest.permission.ACCESS_BACKGROUND_LOCATION);
|
||||
}};
|
||||
|
||||
public static ArrayList<PermissionDetails> getRequiredPermissionsList(Activity activity) {
|
||||
ArrayList<PermissionDetails> permissionsList = new ArrayList<>();
|
||||
@ -190,30 +198,6 @@ public class PermissionsUtils {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void requestAllPermissions(Activity activity) {
|
||||
List<PermissionDetails> wantedPermissions = getRequiredPermissionsList(activity);
|
||||
|
||||
if (!wantedPermissions.isEmpty()) {
|
||||
ArrayList<String> wantedPermissionsStrings = new ArrayList<>();
|
||||
for (PermissionDetails wantedPermission : wantedPermissions) {
|
||||
wantedPermissionsStrings.add(wantedPermission.getPermission());
|
||||
}
|
||||
if (!wantedPermissionsStrings.isEmpty()) {
|
||||
if (wantedPermissionsStrings.contains(CUSTOM_PERM_NOTIFICATION_LISTENER) && !checkPermission(activity, CUSTOM_PERM_NOTIFICATION_LISTENER))
|
||||
requestPermission(activity, CUSTOM_PERM_NOTIFICATION_LISTENER);
|
||||
if (wantedPermissionsStrings.contains(CUSTOM_PERM_NOTIFICATION_SERVICE) && !checkPermission(activity, CUSTOM_PERM_NOTIFICATION_SERVICE))
|
||||
requestPermission(activity, CUSTOM_PERM_NOTIFICATION_SERVICE);
|
||||
if (wantedPermissionsStrings.contains(CUSTOM_PERM_DISPLAY_OVER) && !checkPermission(activity, CUSTOM_PERM_DISPLAY_OVER))
|
||||
requestPermission(activity, CUSTOM_PERM_DISPLAY_OVER);
|
||||
if (wantedPermissionsStrings.contains(Manifest.permission.ACCESS_FINE_LOCATION) && !checkPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION))
|
||||
requestPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION);
|
||||
if (wantedPermissionsStrings.contains(Manifest.permission.ACCESS_BACKGROUND_LOCATION) && !checkPermission(activity, Manifest.permission.ACCESS_BACKGROUND_LOCATION))
|
||||
requestPermission(activity, Manifest.permission.ACCESS_BACKGROUND_LOCATION);
|
||||
ActivityCompat.requestPermissions(activity, wantedPermissionsStrings.toArray(new String[0]), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void requestPermission(Activity activity, String permission) {
|
||||
if (permission.equals(CUSTOM_PERM_NOTIFICATION_LISTENER)) {
|
||||
showNotifyListenerPermissionsDialog(activity);
|
||||
|
Loading…
Reference in New Issue
Block a user