Differential D12229 Diff 32204 src/org/kde/kdeconnect/Plugins/RunCommandPlugin/RunCommandActivity.java
Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/RunCommandPlugin/RunCommandActivity.java
Show All 38 Lines | |||||
39 | 39 | | |||
40 | import java.util.ArrayList; | 40 | import java.util.ArrayList; | ||
41 | import java.util.Collections; | 41 | import java.util.Collections; | ||
42 | import java.util.Comparator; | 42 | import java.util.Comparator; | ||
43 | 43 | | |||
44 | public class RunCommandActivity extends AppCompatActivity { | 44 | public class RunCommandActivity extends AppCompatActivity { | ||
45 | 45 | | |||
46 | private String deviceId; | 46 | private String deviceId; | ||
47 | private final RunCommandPlugin.CommandsChangedCallback commandsChangedCallback = new RunCommandPlugin.CommandsChangedCallback() { | 47 | private final RunCommandPlugin.CommandsChangedCallback commandsChangedCallback = this::updateView; | ||
48 | @Override | | |||
49 | public void update() { | | |||
50 | updateView(); | | |||
51 | } | | |||
52 | }; | | |||
53 | 48 | | |||
54 | private void updateView() { | 49 | private void updateView() { | ||
55 | BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { | 50 | BackgroundService.RunCommand(this, service -> { | ||
56 | @Override | | |||
57 | public void onServiceStart(final BackgroundService service) { | | |||
58 | 51 | | |||
59 | final Device device = service.getDevice(deviceId); | 52 | final Device device = service.getDevice(deviceId); | ||
60 | final RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class); | 53 | final RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class); | ||
61 | if (plugin == null) { | 54 | if (plugin == null) { | ||
62 | Log.e("RunCommandActivity", "device has no runcommand plugin!"); | 55 | Log.e("RunCommandActivity", "device has no runcommand plugin!"); | ||
63 | return; | 56 | return; | ||
64 | } | 57 | } | ||
65 | 58 | | |||
66 | runOnUiThread(new Runnable() { | 59 | runOnUiThread(() -> { | ||
67 | @Override | | |||
68 | public void run() { | | |||
69 | ListView view = (ListView) findViewById(R.id.runcommandslist); | 60 | ListView view = (ListView) findViewById(R.id.runcommandslist); | ||
70 | 61 | | |||
71 | final ArrayList<ListAdapter.Item> commandItems = new ArrayList<>(); | 62 | final ArrayList<ListAdapter.Item> commandItems = new ArrayList<>(); | ||
72 | for (JSONObject obj : plugin.getCommandList()) { | 63 | for (JSONObject obj : plugin.getCommandList()) { | ||
73 | try { | 64 | try { | ||
74 | commandItems.add(new CommandEntry(obj.getString("name"), | 65 | commandItems.add(new CommandEntry(obj.getString("name"), | ||
75 | obj.getString("command"), obj.getString("key"))); | 66 | obj.getString("command"), obj.getString("key"))); | ||
76 | } catch (JSONException e) { | 67 | } catch (JSONException e) { | ||
77 | e.printStackTrace(); | 68 | e.printStackTrace(); | ||
78 | } | 69 | } | ||
79 | } | 70 | } | ||
80 | 71 | | |||
81 | Collections.sort(commandItems, new Comparator<ListAdapter.Item>() { | 72 | Collections.sort(commandItems, (lhs, rhs) -> { | ||
82 | @Override | | |||
83 | public int compare(ListAdapter.Item lhs, ListAdapter.Item rhs) { | | |||
84 | String lName = ((CommandEntry) lhs).getName(); | 73 | String lName = ((CommandEntry) lhs).getName(); | ||
85 | String rName = ((CommandEntry) rhs).getName(); | 74 | String rName = ((CommandEntry) rhs).getName(); | ||
86 | return lName.compareTo(rName); | 75 | return lName.compareTo(rName); | ||
87 | } | | |||
88 | }); | 76 | }); | ||
89 | 77 | | |||
90 | ListAdapter adapter = new ListAdapter(RunCommandActivity.this, commandItems); | 78 | ListAdapter adapter = new ListAdapter(RunCommandActivity.this, commandItems); | ||
91 | 79 | | |||
92 | view.setAdapter(adapter); | 80 | view.setAdapter(adapter); | ||
93 | view.setOnItemClickListener(new AdapterView.OnItemClickListener() { | 81 | view.setOnItemClickListener((adapterView, view1, i, l) -> { | ||
94 | @Override | | |||
95 | public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { | | |||
96 | CommandEntry entry = (CommandEntry) commandItems.get(i); | 82 | CommandEntry entry = (CommandEntry) commandItems.get(i); | ||
97 | plugin.runCommand(entry.getKey()); | 83 | plugin.runCommand(entry.getKey()); | ||
98 | } | | |||
99 | }); | 84 | }); | ||
100 | 85 | | |||
101 | 86 | | |||
102 | TextView explanation = (TextView) findViewById(R.id.addcomand_explanation); | 87 | TextView explanation = (TextView) findViewById(R.id.addcomand_explanation); | ||
103 | String text = getString(R.string.addcommand_explanation); | 88 | String text = getString(R.string.addcommand_explanation); | ||
104 | if (!plugin.canAddCommand()) { | 89 | if (!plugin.canAddCommand()) { | ||
105 | text += "\n" + getString(R.string.addcommand_explanation2); | 90 | text += "\n" + getString(R.string.addcommand_explanation2); | ||
106 | } | 91 | } | ||
107 | explanation.setText(text); | 92 | explanation.setText(text); | ||
108 | explanation.setVisibility(commandItems.isEmpty() ? View.VISIBLE : View.GONE); | 93 | explanation.setVisibility(commandItems.isEmpty() ? View.VISIBLE : View.GONE); | ||
109 | } | | |||
110 | }); | 94 | }); | ||
111 | } | | |||
112 | }); | 95 | }); | ||
113 | } | 96 | } | ||
114 | 97 | | |||
115 | @Override | 98 | @Override | ||
116 | protected void onCreate(Bundle savedInstanceState) { | 99 | protected void onCreate(Bundle savedInstanceState) { | ||
117 | super.onCreate(savedInstanceState); | 100 | super.onCreate(savedInstanceState); | ||
118 | setContentView(R.layout.activity_runcommand); | 101 | setContentView(R.layout.activity_runcommand); | ||
119 | 102 | | |||
120 | deviceId = getIntent().getStringExtra("deviceId"); | 103 | deviceId = getIntent().getStringExtra("deviceId"); | ||
121 | 104 | | |||
122 | boolean canAddCommands = BackgroundService.getInstance().getDevice(deviceId).getPlugin(RunCommandPlugin.class).canAddCommand(); | 105 | boolean canAddCommands = BackgroundService.getInstance().getDevice(deviceId).getPlugin(RunCommandPlugin.class).canAddCommand(); | ||
123 | 106 | | |||
124 | FloatingActionButton addCommandButton = (FloatingActionButton) findViewById(R.id.add_command_button); | 107 | FloatingActionButton addCommandButton = (FloatingActionButton) findViewById(R.id.add_command_button); | ||
125 | addCommandButton.setVisibility(canAddCommands ? View.VISIBLE : View.GONE); | 108 | addCommandButton.setVisibility(canAddCommands ? View.VISIBLE : View.GONE); | ||
126 | addCommandButton.setOnClickListener(new View.OnClickListener() { | 109 | addCommandButton.setOnClickListener(view -> new AddCommandDialog().show(getSupportFragmentManager(), "addcommanddialog")); | ||
127 | @Override | | |||
128 | public void onClick(View view) { | | |||
129 | new AddCommandDialog().show(getSupportFragmentManager(), "addcommanddialog"); | | |||
130 | } | | |||
131 | }); | | |||
132 | 110 | | |||
133 | updateView(); | 111 | updateView(); | ||
134 | } | 112 | } | ||
135 | 113 | | |||
136 | @Override | 114 | @Override | ||
137 | protected void onResume() { | 115 | protected void onResume() { | ||
138 | super.onResume(); | 116 | super.onResume(); | ||
139 | 117 | | |||
140 | BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { | 118 | BackgroundService.RunCommand(this, service -> { | ||
141 | @Override | | |||
142 | public void onServiceStart(final BackgroundService service) { | | |||
143 | 119 | | |||
144 | final Device device = service.getDevice(deviceId); | 120 | final Device device = service.getDevice(deviceId); | ||
145 | final RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class); | 121 | final RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class); | ||
146 | if (plugin == null) { | 122 | if (plugin == null) { | ||
147 | Log.e("RunCommandActivity", "device has no runcommand plugin!"); | 123 | Log.e("RunCommandActivity", "device has no runcommand plugin!"); | ||
148 | return; | 124 | return; | ||
149 | } | 125 | } | ||
150 | plugin.addCommandsUpdatedCallback(commandsChangedCallback); | 126 | plugin.addCommandsUpdatedCallback(commandsChangedCallback); | ||
151 | } | | |||
152 | }); | 127 | }); | ||
153 | } | 128 | } | ||
154 | 129 | | |||
155 | @Override | 130 | @Override | ||
156 | protected void onPause() { | 131 | protected void onPause() { | ||
157 | super.onPause(); | 132 | super.onPause(); | ||
158 | 133 | | |||
159 | BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { | 134 | BackgroundService.RunCommand(this, service -> { | ||
160 | @Override | | |||
161 | public void onServiceStart(final BackgroundService service) { | | |||
162 | 135 | | |||
163 | final Device device = service.getDevice(deviceId); | 136 | final Device device = service.getDevice(deviceId); | ||
164 | final RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class); | 137 | final RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class); | ||
165 | if (plugin == null) { | 138 | if (plugin == null) { | ||
166 | Log.e("RunCommandActivity", "device has no runcommand plugin!"); | 139 | Log.e("RunCommandActivity", "device has no runcommand plugin!"); | ||
167 | return; | 140 | return; | ||
168 | } | 141 | } | ||
169 | plugin.removeCommandsUpdatedCallback(commandsChangedCallback); | 142 | plugin.removeCommandsUpdatedCallback(commandsChangedCallback); | ||
170 | } | | |||
171 | }); | 143 | }); | ||
172 | } | 144 | } | ||
173 | 145 | | |||
174 | public void dialogResult(final String cmdName, final String cmdCmd) { | 146 | public void dialogResult(final String cmdName, final String cmdCmd) { | ||
175 | BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { | 147 | BackgroundService.RunCommand(this, service -> { | ||
176 | @Override | | |||
177 | public void onServiceStart(BackgroundService service) { | | |||
178 | Device device = service.getDevice(deviceId); | 148 | Device device = service.getDevice(deviceId); | ||
179 | RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class); | 149 | RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class); | ||
180 | if(!cmdName.isEmpty() && !cmdCmd.isEmpty()) { | 150 | if(!cmdName.isEmpty() && !cmdCmd.isEmpty()) { | ||
181 | plugin.addCommand(cmdName, cmdCmd); | 151 | plugin.addCommand(cmdName, cmdCmd); | ||
182 | } | 152 | } | ||
183 | } | | |||
184 | }); | 153 | }); | ||
185 | } | 154 | } | ||
186 | } | 155 | } |