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