Commit 6250b679 authored by Albert Vaca Cintora's avatar Albert Vaca Cintora

Notifications plugin now shows as "without permission" instead of "failed"

Removed the concept of "failed" plugins
parent b1904a69
......@@ -105,7 +105,6 @@
<string name="category_connected_devices">Connected devices</string>
<string name="category_not_paired_devices">Available devices</string>
<string name="category_remembered_devices">Remembered devices</string>
<string name="plugins_failed_to_load">Plugins failed to load (tap for more info):</string>
<string name="device_menu_plugins">Plugin settings</string>
<string name="device_menu_unpair">Unpair</string>
<string name="device_not_reachable">Paired device not reachable</string>
......
......@@ -83,7 +83,6 @@ public class Device implements BaseLink.PacketReceiver {
private List<String> m_supportedPlugins = new ArrayList<>();
private final ConcurrentHashMap<String, Plugin> plugins = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Plugin> failedPlugins = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Plugin> pluginsWithoutPermissions = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Plugin> pluginsWithoutOptionalPermissions = new ConcurrentHashMap<>();
private Map<String, ArrayList<String>> pluginsByIncomingInterface = new HashMap<>();
......@@ -686,23 +685,12 @@ public class Device implements BaseLink.PacketReceiver {
//
public <T extends Plugin> T getPlugin(Class<T> pluginClass) {
return (T) getPlugin(Plugin.getPluginKey(pluginClass));
Plugin plugin = getPlugin(Plugin.getPluginKey(pluginClass));
return (T) plugin;
}
public <T extends Plugin> T getPlugin(Class<T> pluginClass, boolean includeFailed) {
return (T) getPlugin(Plugin.getPluginKey(pluginClass), includeFailed);
}
private Plugin getPlugin(String pluginKey) {
return getPlugin(pluginKey, false);
}
public Plugin getPlugin(String pluginKey, boolean includeFailed) {
Plugin plugin = plugins.get(pluginKey);
if (includeFailed && plugin == null) {
plugin = failedPlugins.get(pluginKey);
}
return plugin;
public Plugin getPlugin(String pluginKey) {
return plugins.get(pluginKey);
}
private synchronized boolean addPlugin(final String pluginKey) {
......@@ -728,8 +716,6 @@ public class Device implements BaseLink.PacketReceiver {
final Plugin plugin = PluginFactory.instantiatePluginForDevice(context, pluginKey, this);
if (plugin == null) {
Log.e("KDE/addPlugin", "could not instantiate plugin: " + pluginKey);
//Can't put a null
//failedPlugins.put(pluginKey, null);
return false;
}
......@@ -744,19 +730,13 @@ public class Device implements BaseLink.PacketReceiver {
} catch (Exception e) {
success = false;
e.printStackTrace();
Log.e("KDE/addPlugin", "Exception loading plugin " + pluginKey);
}
if (success) {
//Log.e("addPlugin","added " + pluginKey);
failedPlugins.remove(pluginKey);
plugins.put(pluginKey, plugin);
} else {
if (!success) {
Log.e("KDE/addPlugin", "plugin failed to load " + pluginKey);
plugins.remove(pluginKey);
failedPlugins.put(pluginKey, plugin);
}
plugins.put(pluginKey, plugin);
if (!plugin.checkRequiredPermissions()) {
Log.e("KDE/addPlugin", "No permission " + pluginKey);
plugins.remove(pluginKey);
......@@ -781,14 +761,9 @@ public class Device implements BaseLink.PacketReceiver {
private synchronized boolean removePlugin(String pluginKey) {
Plugin plugin = plugins.remove(pluginKey);
Plugin failedPlugin = failedPlugins.remove(pluginKey);
if (plugin == null) {
if (failedPlugin == null) {
//Not found
return false;
}
plugin = failedPlugin;
return false;
}
try {
......@@ -814,8 +789,6 @@ public class Device implements BaseLink.PacketReceiver {
public void reloadPluginsFromSettings() {
failedPlugins.clear();
HashMap<String, ArrayList<String>> newPluginsByIncomingInterface = new HashMap<>();
for (String pluginKey : m_supportedPlugins) {
......@@ -860,10 +833,6 @@ public class Device implements BaseLink.PacketReceiver {
return plugins;
}
public ConcurrentHashMap<String, Plugin> getFailedPlugins() {
return failedPlugins;
}
public ConcurrentHashMap<String, Plugin> getPluginsWithoutPermissions() {
return pluginsWithoutPermissions;
}
......
......@@ -208,7 +208,13 @@ public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.O
}
@Override
public AlertDialogFragment getErrorDialog() {
public boolean checkRequiredPermissions() {
//Notifications use a different kind of permission, because it was added before the current runtime permissions model
return hasPermission();
}
@Override
public AlertDialogFragment getPermissionExplanationDialog(int requestCode) {
return new StartActivityAlertDialogFragment.Builder()
.setTitle(R.string.pref_plugin_mpris)
.setMessage(R.string.no_permission_mprisreceiver)
......@@ -216,7 +222,7 @@ public class MprisReceiverPlugin extends Plugin implements MediaSessionManager.O
.setNegativeButton(R.string.cancel)
.setIntentAction("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")
.setStartForResult(true)
.setRequestCode(MainActivity.RESULT_NEEDS_RELOAD)
.setRequestCode(requestCode)
.create();
}
......
......@@ -101,6 +101,12 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
return null;
}
@Override
public boolean checkRequiredPermissions() {
//Notifications use a different kind of permission, because it was added before the current runtime permissions model
return hasPermission();
}
private boolean hasPermission() {
String notificationListenerList = Settings.Secure.getString(context.getContentResolver(), "enabled_notification_listeners");
return (notificationListenerList != null && notificationListenerList.contains(context.getPackageName()));
......@@ -492,7 +498,7 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
}
@Override
public AlertDialogFragment getErrorDialog() {
public AlertDialogFragment getPermissionExplanationDialog(int requestCode) {
return new StartActivityAlertDialogFragment.Builder()
.setTitle(R.string.pref_plugin_notifications)
.setMessage(R.string.no_permissions)
......@@ -500,7 +506,7 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
.setNegativeButton(R.string.cancel)
.setIntentAction("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")
.setStartForResult(true)
.setRequestCode(MainActivity.RESULT_NEEDS_RELOAD)
.setRequestCode(requestCode)
.create();
}
......
......@@ -239,10 +239,6 @@ public abstract class Plugin {
* the problem (and how to fix it, if possible) to the user.
*/
public AlertDialogFragment getErrorDialog() {
return null;
}
public AlertDialogFragment getPermissionExplanationDialog(int requestCode) {
return requestPermissionDialog(getRequiredPermissions(), permissionExplanation, requestCode);
}
......
......@@ -54,6 +54,7 @@ import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
......@@ -81,7 +82,7 @@ class SimpleSftpServer {
private final SshServer sshd = SshServer.setUpDefaultServer();
void init(Context context, Device device) throws Exception {
void init(Context context, Device device) throws GeneralSecurityException {
sshd.setKeyExchangeFactories(Arrays.asList(
new DHG14.Factory(),
......
......@@ -330,22 +330,14 @@ public class DeviceFragment extends Fragment {
pluginListItems.add(new PluginItem(p, v -> p.startMainActivity(mActivity)));
}
DeviceFragment.this.createPluginsList(device.getFailedPlugins(), R.string.plugins_failed_to_load, (plugin) -> {
AlertDialogFragment dialog = plugin.getErrorDialog();
if (dialog != null) {
dialog.show(getChildFragmentManager(), null);
}
});
DeviceFragment.this.createPluginsList(device.getPluginsWithoutPermissions(), R.string.plugins_need_permission, (plugin) -> {
AlertDialogFragment dialog = plugin.getPermissionExplanationDialog(0);
AlertDialogFragment dialog = plugin.getPermissionExplanationDialog(MainActivity.RESULT_NEEDS_RELOAD);
if (dialog != null) {
dialog.show(getChildFragmentManager(), null);
}
});
DeviceFragment.this.createPluginsList(device.getPluginsWithoutOptionalPermissions(), R.string.plugins_need_optional_permission, (plugin) -> {
AlertDialogFragment dialog = plugin.getOptionalPermissionExplanationDialog(0);
AlertDialogFragment dialog = plugin.getOptionalPermissionExplanationDialog(MainActivity.RESULT_NEEDS_RELOAD);
if (dialog != null) {
dialog.show(getChildFragmentManager(), null);
......
......@@ -71,7 +71,7 @@ public class DeviceSettingsActivity
fragment = DeviceSettingsFragment.newInstance(deviceId);
} else {
Device device = BackgroundService.getInstance().getDevice(deviceId);
Plugin plugin = device.getPlugin(pluginKey, true);
Plugin plugin = device.getPlugin(pluginKey);
fragment = plugin.getSettingsFragment();
}
......
......@@ -360,15 +360,20 @@ public class MainActivity extends AppCompatActivity implements SharedPreferences
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
boolean grantedPermission = false;
for (int result : grantResults) {
if (result == PackageManager.PERMISSION_GRANTED) {
//New permission granted, reload plugins
BackgroundService.RunCommand(this, service -> {
Device device = service.getDevice(mCurrentDevice);
device.reloadPluginsFromSettings();
});
grantedPermission = true;
break;
}
}
if (grantedPermission) {
//New permission granted, reload plugins
BackgroundService.RunCommand(this, service -> {
Device device = service.getDevice(mCurrentDevice);
device.reloadPluginsFromSettings();
});
}
}
@Override
......
......@@ -32,10 +32,10 @@ class PluginPreference extends CheckBoxPreference {
setIcon(android.R.color.transparent);
setChecked(device.isPluginEnabled(pluginKey));
Plugin plugin = device.getPlugin(pluginKey, true);
Plugin plugin = device.getPlugin(pluginKey);
if (info.hasSettings() && plugin != null) {
this.listener = v -> {
Plugin plugin1 = device.getPlugin(pluginKey, true);
Plugin plugin1 = device.getPlugin(pluginKey);
if (plugin1 != null) {
callback.onStartPluginSettingsFragment(plugin1);
} else { //Could happen if the device is not connected anymore
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment