diff --git a/src/org/kde/kdeconnect/UserInterface/DeviceFragment.java b/src/org/kde/kdeconnect/UserInterface/DeviceFragment.java --- a/src/org/kde/kdeconnect/UserInterface/DeviceFragment.java +++ b/src/org/kde/kdeconnect/UserInterface/DeviceFragment.java @@ -15,8 +15,8 @@ * 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 . -*/ + * along with this program. If not, see . + */ package org.kde.kdeconnect.UserInterface; @@ -265,72 +265,53 @@ //Once in-app, there is no point in keep displaying the notification if any device.hidePairingNotification(); - mActivity.runOnUiThread(new Runnable() { - @Override - public void run() { + mActivity.runOnUiThread(() -> { - if (device.isPairRequestedByPeer()) { - ((TextView) rootView.findViewById(R.id.pair_message)).setText(R.string.pair_requested); - rootView.findViewById(R.id.pairing_buttons).setVisibility(View.VISIBLE); - rootView.findViewById(R.id.pair_progress).setVisibility(View.GONE); - rootView.findViewById(R.id.pair_button).setVisibility(View.GONE); - rootView.findViewById(R.id.pair_request).setVisibility(View.VISIBLE); - } else { + if (device.isPairRequestedByPeer()) { + ((TextView) rootView.findViewById(R.id.pair_message)).setText(R.string.pair_requested); + rootView.findViewById(R.id.pairing_buttons).setVisibility(View.VISIBLE); + rootView.findViewById(R.id.pair_progress).setVisibility(View.GONE); + rootView.findViewById(R.id.pair_button).setVisibility(View.GONE); + rootView.findViewById(R.id.pair_request).setVisibility(View.VISIBLE); + } else { + + boolean paired = device.isPaired(); + boolean reachable = device.isReachable(); + boolean onData = NetworkHelper.isOnMobileNetwork(getContext()); + + rootView.findViewById(R.id.pairing_buttons).setVisibility(paired ? View.GONE : View.VISIBLE); + rootView.findViewById(R.id.not_reachable_message).setVisibility((paired && !reachable && !onData) ? View.VISIBLE : View.GONE); + rootView.findViewById(R.id.on_data_message).setVisibility((paired && !reachable && onData) ? View.VISIBLE : View.GONE); + + try { + pluginListItems = new ArrayList<>(); - boolean paired = device.isPaired(); - boolean reachable = device.isReachable(); - boolean onData = NetworkHelper.isOnMobileNetwork(getContext()); - - rootView.findViewById(R.id.pairing_buttons).setVisibility(paired ? View.GONE : View.VISIBLE); - rootView.findViewById(R.id.not_reachable_message).setVisibility((paired && !reachable && !onData) ? View.VISIBLE : View.GONE); - rootView.findViewById(R.id.on_data_message).setVisibility((paired && !reachable && onData) ? View.VISIBLE : View.GONE); - - try { - pluginListItems = new ArrayList<>(); - - //Plugins button list - final Collection plugins = device.getLoadedPlugins().values(); - for (final Plugin p : plugins) { - if (!p.hasMainActivity()) continue; - if (p.displayInContextMenu()) continue; - - pluginListItems.add(new PluginItem(p, v -> p.startMainActivity(mActivity))); - } - - createPluginsList(device.getFailedPlugins(), R.string.plugins_failed_to_load, new PluginClickListener() { - @Override - void action() { - plugin.getErrorDialog(mActivity).show(); - } - }); - createPluginsList(device.getPluginsWithoutPermissions(), R.string.plugins_need_permission, new PluginClickListener() { - @Override - void action() { - plugin.getPermissionExplanationDialog(mActivity).show(); - } - }); - createPluginsList(device.getPluginsWithoutOptionalPermissions(), R.string.plugins_need_optional_permission, new PluginClickListener() { - @Override - void action() { - plugin.getOptionalPermissionExplanationDialog(mActivity).show(); - } - }); - - ListView buttonsList = (ListView) rootView.findViewById(R.id.buttons_list); - ListAdapter adapter = new ListAdapter(mActivity, pluginListItems); - buttonsList.setAdapter(adapter); - - mActivity.invalidateOptionsMenu(); - - } catch (IllegalStateException e) { - e.printStackTrace(); - //Ignore: The activity was closed while we were trying to update it - } catch (ConcurrentModificationException e) { - Log.e("DeviceActivity", "ConcurrentModificationException"); - this.run(); //Try again + //Plugins button list + final Collection plugins = device.getLoadedPlugins().values(); + for (final Plugin p : plugins) { + if (!p.hasMainActivity()) continue; + if (p.displayInContextMenu()) continue; + + pluginListItems.add(new PluginItem(p, v -> p.startMainActivity(mActivity))); } + createPluginsList(device.getFailedPlugins(), R.string.plugins_failed_to_load, (plugin) -> plugin.getErrorDialog(mActivity).show()); + createPluginsList(device.getPluginsWithoutPermissions(), R.string.plugins_need_permission, (plugin) -> plugin.getPermissionExplanationDialog(mActivity).show()); + createPluginsList(device.getPluginsWithoutOptionalPermissions(), R.string.plugins_need_optional_permission, (plugin) -> plugin.getOptionalPermissionExplanationDialog(mActivity).show()); + + ListView buttonsList = (ListView) rootView.findViewById(R.id.buttons_list); + ListAdapter adapter = new ListAdapter(mActivity, pluginListItems); + buttonsList.setAdapter(adapter); + + mActivity.invalidateOptionsMenu(); + + } catch (IllegalStateException e) { + e.printStackTrace(); + //Ignore: The activity was closed while we were trying to update it + } catch (ConcurrentModificationException e) { + Log.e("DeviceActivity", "ConcurrentModificationException"); } + } }); @@ -420,7 +401,7 @@ }); } - void createPluginsList(ConcurrentHashMap plugins, int headerText, PluginClickListener onClickListener) { + void createPluginsList(ConcurrentHashMap plugins, int headerText, PluginSmallEntryItem.Action action) { if (!plugins.isEmpty()) { TextView header = new TextView(mActivity); @@ -442,35 +423,10 @@ if (plugin == null) { pluginListItems.add(new SmallEntryItem(pluginKey)); } else { - PluginClickListener listener = onClickListener.clone(); - listener.plugin = plugin; - pluginListItems.add(new SmallEntryItem(plugin.getDisplayName(), listener)); + pluginListItems.add(new PluginSmallEntryItem(plugin, action)); } } } } } - - private abstract class PluginClickListener implements View.OnClickListener, Cloneable { - - Plugin plugin; - - @Override - public void onClick(View v) { - action(); - } - - @Override - public PluginClickListener clone() { - try { - return (PluginClickListener) super.clone(); - } catch (CloneNotSupportedException e) { - e.printStackTrace(); - } - return null; - } - - abstract void action(); - } - } diff --git a/src/org/kde/kdeconnect/UserInterface/PluginSmallEntryItem.java b/src/org/kde/kdeconnect/UserInterface/PluginSmallEntryItem.java new file mode 100644 --- /dev/null +++ b/src/org/kde/kdeconnect/UserInterface/PluginSmallEntryItem.java @@ -0,0 +1,34 @@ +/* + * Copyright 2018 Nicolas Fella + * + * 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 org.kde.kdeconnect.Plugins.Plugin; +import org.kde.kdeconnect.UserInterface.List.SmallEntryItem; + +class PluginSmallEntryItem extends SmallEntryItem { + + interface Action { + void action(Plugin plugin); + } + + PluginSmallEntryItem(Plugin plugin, Action action) { + super(plugin.getDisplayName(), (view) -> action.action(plugin)); + } +}