diff --git a/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlugin.java b/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlugin.java index 16d4d05a..d67d87cc 100644 --- a/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/MprisReceiverPlugin/MprisReceiverPlugin.java @@ -20,10 +20,8 @@ package org.kde.kdeconnect.Plugins.MprisReceiverPlugin; -import android.app.Activity; import android.content.ComponentName; import android.content.Context; -import android.content.Intent; import android.media.session.MediaController; import android.media.session.MediaSessionManager; import android.os.Build; @@ -36,7 +34,9 @@ import org.kde.kdeconnect.Helpers.AppsHelper; import org.kde.kdeconnect.NetworkPacket; import org.kde.kdeconnect.Plugins.NotificationsPlugin.NotificationReceiver; import org.kde.kdeconnect.Plugins.Plugin; +import org.kde.kdeconnect.UserInterface.AlertDialogFragment; import org.kde.kdeconnect.UserInterface.MainActivity; +import org.kde.kdeconnect.UserInterface.StartActivityAlertDialogFragment; import org.kde.kdeconnect_tp.R; import java.util.HashMap; @@ -44,7 +44,6 @@ import java.util.List; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; -import androidx.appcompat.app.AlertDialog; @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.OnActiveSessionsChangedListener { @@ -208,18 +207,15 @@ public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.O } @Override - public AlertDialog getErrorDialog(final Activity deviceActivity) { - - return new AlertDialog.Builder(deviceActivity) + public AlertDialogFragment getErrorDialog() { + return new StartActivityAlertDialogFragment.Builder() .setTitle(R.string.pref_plugin_mpris) .setMessage(R.string.no_permission_mprisreceiver) - .setPositiveButton(R.string.open_settings, (dialogInterface, i) -> { - Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); - deviceActivity.startActivityForResult(intent, MainActivity.RESULT_NEEDS_RELOAD); - }) - .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - //Do nothing - }) + .setPositiveButton(R.string.open_settings) + .setNegativeButton(R.string.cancel) + .setIntentAction("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS") + .setStartForResult(true) + .setRequestCode(MainActivity.RESULT_NEEDS_RELOAD) .create(); } diff --git a/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationsPlugin.java b/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationsPlugin.java index 69ab6e10..170ab685 100644 --- a/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationsPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationsPlugin.java @@ -21,7 +21,6 @@ package org.kde.kdeconnect.Plugins.NotificationsPlugin; import android.annotation.TargetApi; -import android.app.Activity; import android.app.Notification; import android.app.PendingIntent; import android.app.RemoteInput; @@ -43,8 +42,10 @@ import android.util.Log; import org.kde.kdeconnect.Helpers.AppsHelper; import org.kde.kdeconnect.NetworkPacket; import org.kde.kdeconnect.Plugins.Plugin; +import org.kde.kdeconnect.UserInterface.AlertDialogFragment; import org.kde.kdeconnect.UserInterface.MainActivity; import org.kde.kdeconnect.UserInterface.PluginSettingsFragment; +import org.kde.kdeconnect.UserInterface.StartActivityAlertDialogFragment; import org.kde.kdeconnect_tp.R; import java.io.ByteArrayOutputStream; @@ -57,7 +58,6 @@ import java.util.Map; import java.util.Set; import androidx.annotation.RequiresApi; -import androidx.appcompat.app.AlertDialog; import androidx.core.app.NotificationCompat; @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) @@ -490,17 +490,15 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver. } @Override - public AlertDialog getErrorDialog(final Activity deviceActivity) { - return new AlertDialog.Builder(deviceActivity) + public AlertDialogFragment getErrorDialog() { + return new StartActivityAlertDialogFragment.Builder() .setTitle(R.string.pref_plugin_notifications) .setMessage(R.string.no_permissions) - .setPositiveButton(R.string.open_settings, (dialogInterface, i) -> { - Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); - deviceActivity.startActivityForResult(intent, MainActivity.RESULT_NEEDS_RELOAD); - }) - .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - //Do nothing - }) + .setPositiveButton(R.string.open_settings) + .setNegativeButton(R.string.cancel) + .setIntentAction("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS") + .setStartForResult(true) + .setRequestCode(MainActivity.RESULT_NEEDS_RELOAD) .create(); } diff --git a/src/org/kde/kdeconnect/Plugins/Plugin.java b/src/org/kde/kdeconnect/Plugins/Plugin.java index 6a606cb1..73317b39 100644 --- a/src/org/kde/kdeconnect/Plugins/Plugin.java +++ b/src/org/kde/kdeconnect/Plugins/Plugin.java @@ -35,7 +35,6 @@ import org.kde.kdeconnect.UserInterface.PluginSettingsFragment; import org.kde.kdeconnect_tp.R; import androidx.annotation.StringRes; -import androidx.appcompat.app.AlertDialog; import androidx.core.content.ContextCompat; public abstract class Plugin { @@ -240,7 +239,7 @@ public abstract class Plugin { * the problem (and how to fix it, if possible) to the user. */ - public AlertDialog getErrorDialog(Activity deviceActivity) { + public AlertDialogFragment getErrorDialog() { return null; } diff --git a/src/org/kde/kdeconnect/UserInterface/DeviceFragment.java b/src/org/kde/kdeconnect/UserInterface/DeviceFragment.java index 24cb3ab4..6594993a 100644 --- a/src/org/kde/kdeconnect/UserInterface/DeviceFragment.java +++ b/src/org/kde/kdeconnect/UserInterface/DeviceFragment.java @@ -332,9 +332,9 @@ public class DeviceFragment extends Fragment { } DeviceFragment.this.createPluginsList(device.getFailedPlugins(), R.string.plugins_failed_to_load, (plugin) -> { - AlertDialog dialog = plugin.getErrorDialog(mActivity); + AlertDialogFragment dialog = plugin.getErrorDialog(); if (dialog != null) { - dialog.show(); + dialog.show(getChildFragmentManager(), null); } }); DeviceFragment.this.createPluginsList(device.getPluginsWithoutPermissions(), R.string.plugins_need_permission, (plugin) -> { diff --git a/src/org/kde/kdeconnect/UserInterface/StartActivityAlertDialogFragment.java b/src/org/kde/kdeconnect/UserInterface/StartActivityAlertDialogFragment.java new file mode 100644 index 00000000..019cc27c --- /dev/null +++ b/src/org/kde/kdeconnect/UserInterface/StartActivityAlertDialogFragment.java @@ -0,0 +1,101 @@ +/* + * Copyright 2019 Erik Duisters + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.kde.kdeconnect.UserInterface; + +import android.content.Intent; +import android.os.Bundle; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +public class StartActivityAlertDialogFragment extends AlertDialogFragment { + private static final String KEY_INTENT_ACTION = "IntentAction"; + private static final String KEY_REQUEST_CODE = "RequestCode"; + private static final String KEY_START_FOR_RESULT = "StartForResult"; + + private String intentAction; + private int requestCode; + private boolean startForResult; + + public StartActivityAlertDialogFragment() {} + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + Bundle args = getArguments(); + + if (args == null || !args.containsKey(KEY_INTENT_ACTION)) { + throw new RuntimeException("You must call Builder.setIntentAction() to set the intent action"); + } + + intentAction = args.getString(KEY_INTENT_ACTION); + requestCode = args.getInt(KEY_REQUEST_CODE, 0); + startForResult = args.getBoolean(KEY_START_FOR_RESULT); + + if (startForResult && !args.containsKey(KEY_REQUEST_CODE)) { + throw new RuntimeException("You requested startForResult but you did not set the requestCode"); + } + + setCallback(new Callback() { + @Override + public void onPositiveButtonClicked() { + Intent intent = new Intent(intentAction); + + if (startForResult) { + requireActivity().startActivityForResult(intent, requestCode); + } else { + requireActivity().startActivity(intent); + } + } + }); + } + + public static class Builder extends AlertDialogFragment.AbstractBuilder { + @Override + public StartActivityAlertDialogFragment.Builder getThis() { + return this; + } + + public StartActivityAlertDialogFragment.Builder setIntentAction(@NonNull String intentAction) { + args.putString(KEY_INTENT_ACTION, intentAction); + + return getThis(); + } + + public StartActivityAlertDialogFragment.Builder setRequestCode(int requestCode) { + args.putInt(KEY_REQUEST_CODE, requestCode); + + return getThis(); + } + + public StartActivityAlertDialogFragment.Builder setStartForResult(boolean startForResult) { + args.putBoolean(KEY_START_FOR_RESULT, startForResult); + + return getThis(); + } + + @Override + protected StartActivityAlertDialogFragment createFragment() { + return new StartActivityAlertDialogFragment(); + } + } +}