2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-22 01:51:47 +00:00

WIP: Open settings when permissions have been denied twice

This commit is contained in:
Albert Vaca Cintora 2023-08-24 01:43:58 +02:00
parent 8a4da371c8
commit 7394b861d2
2 changed files with 46 additions and 5 deletions

View File

@ -32,6 +32,7 @@ import androidx.lifecycle.MutableLiveData;
import org.kde.kdeconnect.Backends.BaseLinkProvider;
import org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider;
import org.kde.kdeconnect.Backends.LoopbackBackend.LoopbackLinkProvider;
import org.kde.kdeconnect.Helpers.NotificationHelper;
import org.kde.kdeconnect.Plugins.ClibpoardPlugin.ClipboardFloatingActivity;
import org.kde.kdeconnect.Plugins.RunCommandPlugin.RunCommandActivity;
@ -80,7 +81,7 @@ public class BackgroundService extends Service {
private void registerLinkProviders() {
linkProviders.add(new LanLinkProvider(this));
// linkProviders.add(new LoopbackLinkProvider(this));
linkProviders.add(new LoopbackLinkProvider(this));
// linkProviders.add(new BluetoothLinkProvider(this));
}
@ -267,6 +268,14 @@ public class BackgroundService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("KDE/BackgroundService", "onStartCommand");
postForegroundNotification();
if (intent != null && intent.getBooleanExtra("refresh", false)) {
onNetworkChange();
}
return Service.START_STICKY;
}
public void postForegroundNotification() {
if (NotificationHelper.isPersistentNotificationEnabled(this)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(FOREGROUND_NOTIFICATION_ID, createForegroundNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE);
@ -274,10 +283,6 @@ public class BackgroundService extends Service {
startForeground(FOREGROUND_NOTIFICATION_ID, createForegroundNotification());
}
}
if (intent != null && intent.getBooleanExtra("refresh", false)) {
onNetworkChange();
}
return Service.START_STICKY;
}
public static void Start(Context context) {

View File

@ -6,7 +6,13 @@
package org.kde.kdeconnect.UserInterface;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
@ -14,6 +20,7 @@ import androidx.core.app.ActivityCompat;
import org.kde.kdeconnect_tp.R;
public class PermissionsAlertDialogFragment extends AlertDialogFragment {
private static final String KEY_PERMANENTLY_DENIED_PREFERENCES = "permanently_denied_permissions";
private static final String KEY_PERMISSIONS = "Permissions";
private static final String KEY_REQUEST_CODE = "RequestCode";
@ -23,6 +30,18 @@ public class PermissionsAlertDialogFragment extends AlertDialogFragment {
public PermissionsAlertDialogFragment() {
}
public static void PermissionsDenied(Activity activity, String[] permissions) {
for (String permission : permissions) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
// The user selected "don't show again" or denied the permission twice, so the
// system permission dialog won't show again. We want to remember this to open the
// app preferences instead the next time
SharedPreferences prefs = activity.getSharedPreferences(KEY_PERMANENTLY_DENIED_PREFERENCES, Context.MODE_PRIVATE);
prefs.edit().putBoolean(permission, true).apply();
}
}
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -36,11 +55,28 @@ public class PermissionsAlertDialogFragment extends AlertDialogFragment {
permissions = args.getStringArray(KEY_PERMISSIONS);
requestCode = args.getInt(KEY_REQUEST_CODE, 0);
setCallback(new Callback() {
@Override
public void onPositiveButtonClicked() {
SharedPreferences prefs = getContext().getSharedPreferences(KEY_PERMANENTLY_DENIED_PREFERENCES, Context.MODE_PRIVATE);
boolean permanentlyDenied = false;
for (String permission : permissions) {
if (prefs.getBoolean(permission, false)) {
permanentlyDenied = true;
break;
}
}
if (permanentlyDenied) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getContext().getPackageName(), null);
intent.setData(uri);
startActivity(intent);
} else {
ActivityCompat.requestPermissions(requireActivity(), permissions, requestCode);
}
}
});
}