Differential D12507 Diff 38792 src/org/kde/kdeconnect/Plugins/RunCommandPlugin/RunCommandPlugin.java
Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/RunCommandPlugin/RunCommandPlugin.java
Show All 27 Lines | |||||
28 | 28 | | |||
29 | import org.json.JSONException; | 29 | import org.json.JSONException; | ||
30 | import org.json.JSONObject; | 30 | import org.json.JSONObject; | ||
31 | import org.kde.kdeconnect.NetworkPacket; | 31 | import org.kde.kdeconnect.NetworkPacket; | ||
32 | import org.kde.kdeconnect.Plugins.Plugin; | 32 | import org.kde.kdeconnect.Plugins.Plugin; | ||
33 | import org.kde.kdeconnect_tp.R; | 33 | import org.kde.kdeconnect_tp.R; | ||
34 | 34 | | |||
35 | import java.util.ArrayList; | 35 | import java.util.ArrayList; | ||
36 | import java.util.Collections; | ||||
36 | import java.util.Iterator; | 37 | import java.util.Iterator; | ||
37 | 38 | | |||
38 | public class RunCommandPlugin extends Plugin { | 39 | public class RunCommandPlugin extends Plugin { | ||
39 | 40 | | |||
40 | public final static String PACKET_TYPE_RUNCOMMAND = "kdeconnect.runcommand"; | 41 | public final static String PACKET_TYPE_RUNCOMMAND = "kdeconnect.runcommand"; | ||
41 | public final static String PACKET_TYPE_RUNCOMMAND_REQUEST = "kdeconnect.runcommand.request"; | 42 | public final static String PACKET_TYPE_RUNCOMMAND_REQUEST = "kdeconnect.runcommand.request"; | ||
42 | 43 | | |||
43 | private ArrayList<JSONObject> commandList = new ArrayList<>(); | 44 | private ArrayList<JSONObject> commandList = new ArrayList<>(); | ||
44 | private ArrayList<CommandsChangedCallback> callbacks = new ArrayList<>(); | 45 | private ArrayList<CommandsChangedCallback> callbacks = new ArrayList<>(); | ||
46 | private final ArrayList<CommandEntry> commandItems = new ArrayList<>(); | ||||
45 | 47 | | |||
46 | private boolean canAddCommand; | 48 | private boolean canAddCommand; | ||
nicolasfella: Coding style, no spaces inside <>.
Apply everywhere | |||||
47 | 49 | | |||
48 | public void addCommandsUpdatedCallback(CommandsChangedCallback newCallback) { | 50 | public void addCommandsUpdatedCallback(CommandsChangedCallback newCallback) { | ||
49 | callbacks.add(newCallback); | 51 | callbacks.add(newCallback); | ||
50 | } | 52 | } | ||
51 | 53 | | |||
52 | public void removeCommandsUpdatedCallback(CommandsChangedCallback theCallback) { | 54 | public void removeCommandsUpdatedCallback(CommandsChangedCallback theCallback) { | ||
53 | callbacks.remove(theCallback); | 55 | callbacks.remove(theCallback); | ||
54 | } | 56 | } | ||
55 | 57 | | |||
56 | interface CommandsChangedCallback { | 58 | interface CommandsChangedCallback { | ||
57 | void update(); | 59 | void update(); | ||
58 | } | 60 | } | ||
59 | 61 | | |||
60 | public ArrayList<JSONObject> getCommandList() { | 62 | public ArrayList<JSONObject> getCommandList() { | ||
61 | return commandList; | 63 | return commandList; | ||
62 | } | 64 | } | ||
63 | 65 | | |||
66 | public ArrayList<CommandEntry> getCommandItems() { | ||||
67 | return commandItems; | ||||
68 | } | ||||
69 | | ||||
64 | @Override | 70 | @Override | ||
65 | public String getDisplayName() { | 71 | public String getDisplayName() { | ||
66 | return context.getResources().getString(R.string.pref_plugin_runcommand); | 72 | return context.getResources().getString(R.string.pref_plugin_runcommand); | ||
67 | } | 73 | } | ||
68 | 74 | | |||
69 | @Override | 75 | @Override | ||
70 | public String getDescription() { | 76 | public String getDescription() { | ||
71 | return context.getResources().getString(R.string.pref_plugin_runcommand_desc); | 77 | return context.getResources().getString(R.string.pref_plugin_runcommand_desc); | ||
Show All 11 Lines | |||||
83 | } | 89 | } | ||
84 | 90 | | |||
85 | @Override | 91 | @Override | ||
86 | public boolean onPacketReceived(NetworkPacket np) { | 92 | public boolean onPacketReceived(NetworkPacket np) { | ||
87 | 93 | | |||
88 | if (np.has("commandList")) { | 94 | if (np.has("commandList")) { | ||
89 | commandList.clear(); | 95 | commandList.clear(); | ||
90 | try { | 96 | try { | ||
97 | commandItems.clear(); | ||||
91 | JSONObject obj = new JSONObject(np.getString("commandList")); | 98 | JSONObject obj = new JSONObject(np.getString("commandList")); | ||
92 | Iterator<String> keys = obj.keys(); | 99 | Iterator<String> keys = obj.keys(); | ||
93 | while (keys.hasNext()) { | 100 | while (keys.hasNext()) { | ||
94 | String s = keys.next(); | 101 | String s = keys.next(); | ||
95 | JSONObject o = obj.getJSONObject(s); | 102 | JSONObject o = obj.getJSONObject(s); | ||
96 | o.put("key", s); | 103 | o.put("key", s); | ||
97 | commandList.add(o); | 104 | commandList.add(o); | ||
105 | | ||||
106 | try { | ||||
107 | commandItems.add( | ||||
108 | new CommandEntry( | ||||
109 | o.getString("name"), | ||||
110 | o.getString("command"), | ||||
111 | o.getString("key") | ||||
112 | ) | ||||
113 | ); | ||||
114 | } catch (JSONException e) { | ||||
115 | e.printStackTrace(); | ||||
116 | } | ||||
98 | } | 117 | } | ||
118 | | ||||
119 | Collections.sort(commandItems, (lhs, rhs) -> lhs.getName().compareTo(rhs.getName()) ); | ||||
120 | | ||||
121 | Intent updateWidget = new Intent(context, RunCommandWidget.class); | ||||
122 | context.sendBroadcast(updateWidget); | ||||
nicolasfella: Lambda | |||||
123 | | ||||
99 | } catch (JSONException e) { | 124 | } catch (JSONException e) { | ||
100 | e.printStackTrace(); | 125 | e.printStackTrace(); | ||
101 | } | 126 | } | ||
102 | 127 | | |||
103 | for (CommandsChangedCallback aCallback : callbacks) { | 128 | for (CommandsChangedCallback aCallback : callbacks) { | ||
104 | aCallback.update(); | 129 | aCallback.update(); | ||
105 | } | 130 | } | ||
106 | 131 | | |||
▲ Show 20 Lines • Show All 59 Lines • Show Last 20 Lines |
Coding style, no spaces inside <>.
Apply everywhere