Differential D13447 Diff 38546 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 15 Lines | |||||
16 | * GNU General Public License for more details. | 16 | * GNU General Public License for more details. | ||
17 | * | 17 | * | ||
18 | * You should have received a copy of the GNU General Public License | 18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
20 | */ | 20 | */ | ||
21 | 21 | | |||
22 | package org.kde.kdeconnect.Plugins.RunCommandPlugin; | 22 | package org.kde.kdeconnect.Plugins.RunCommandPlugin; | ||
23 | 23 | | |||
24 | import android.content.ClipboardManager; | ||||
25 | import android.content.Context; | ||||
26 | import android.os.Build; | ||||
24 | import android.os.Bundle; | 27 | import android.os.Bundle; | ||
28 | import android.support.annotation.RequiresApi; | ||||
25 | import android.support.design.widget.FloatingActionButton; | 29 | import android.support.design.widget.FloatingActionButton; | ||
26 | import android.support.v7.app.AlertDialog; | 30 | import android.support.v7.app.AlertDialog; | ||
27 | import android.support.v7.app.AppCompatActivity; | 31 | import android.support.v7.app.AppCompatActivity; | ||
28 | import android.util.Log; | 32 | import android.util.Log; | ||
33 | import android.view.ContextMenu; | ||||
34 | import android.view.MenuInflater; | ||||
35 | import android.view.MenuItem; | ||||
29 | import android.view.View; | 36 | import android.view.View; | ||
30 | import android.widget.AdapterView; | 37 | import android.widget.AdapterView; | ||
31 | import android.widget.ListView; | 38 | import android.widget.ListView; | ||
32 | import android.widget.TextView; | 39 | import android.widget.TextView; | ||
40 | import android.widget.Toast; | ||||
33 | 41 | | |||
34 | import org.json.JSONException; | 42 | import org.json.JSONException; | ||
35 | import org.json.JSONObject; | 43 | import org.json.JSONObject; | ||
36 | import org.kde.kdeconnect.BackgroundService; | 44 | import org.kde.kdeconnect.BackgroundService; | ||
37 | import org.kde.kdeconnect.Device; | 45 | import org.kde.kdeconnect.Device; | ||
38 | import org.kde.kdeconnect.UserInterface.List.ListAdapter; | 46 | import org.kde.kdeconnect.UserInterface.List.ListAdapter; | ||
39 | import org.kde.kdeconnect.UserInterface.ThemeUtil; | 47 | import org.kde.kdeconnect.UserInterface.ThemeUtil; | ||
40 | import org.kde.kdeconnect_tp.R; | 48 | import org.kde.kdeconnect_tp.R; | ||
41 | 49 | | |||
42 | import java.util.ArrayList; | 50 | import java.util.ArrayList; | ||
43 | import java.util.Collections; | 51 | import java.util.Collections; | ||
44 | import java.util.Comparator; | | |||
45 | 52 | | |||
46 | public class RunCommandActivity extends AppCompatActivity { | 53 | public class RunCommandActivity extends AppCompatActivity { | ||
47 | 54 | | |||
48 | private String deviceId; | 55 | private String deviceId; | ||
49 | private final RunCommandPlugin.CommandsChangedCallback commandsChangedCallback = this::updateView; | 56 | private final RunCommandPlugin.CommandsChangedCallback commandsChangedCallback = this::updateView; | ||
57 | private ArrayList<ListAdapter.Item> commandItems; | ||||
50 | 58 | | |||
51 | private void updateView() { | 59 | private void updateView() { | ||
52 | BackgroundService.RunCommand(this, service -> { | 60 | BackgroundService.RunCommand(this, service -> { | ||
53 | 61 | | |||
54 | final Device device = service.getDevice(deviceId); | 62 | final Device device = service.getDevice(deviceId); | ||
55 | final RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class); | 63 | final RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class); | ||
56 | if (plugin == null) { | 64 | if (plugin == null) { | ||
57 | Log.e("RunCommandActivity", "device has no runcommand plugin!"); | 65 | Log.e("RunCommandActivity", "device has no runcommand plugin!"); | ||
58 | return; | 66 | return; | ||
59 | } | 67 | } | ||
60 | 68 | | |||
61 | runOnUiThread(() -> { | 69 | runOnUiThread(() -> { | ||
62 | ListView view = (ListView) findViewById(R.id.runcommandslist); | 70 | ListView view = (ListView) findViewById(R.id.runcommandslist); | ||
63 | 71 | | |||
64 | final ArrayList<ListAdapter.Item> commandItems = new ArrayList<>(); | 72 | if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) { | ||
73 | registerForContextMenu(view); | ||||
74 | } | ||||
75 | | ||||
76 | commandItems = new ArrayList<>(); | ||||
65 | for (JSONObject obj : plugin.getCommandList()) { | 77 | for (JSONObject obj : plugin.getCommandList()) { | ||
66 | try { | 78 | try { | ||
67 | commandItems.add(new CommandEntry(obj.getString("name"), | 79 | commandItems.add(new CommandEntry(obj.getString("name"), | ||
68 | obj.getString("command"), obj.getString("key"))); | 80 | obj.getString("command"), obj.getString("key"))); | ||
69 | } catch (JSONException e) { | 81 | } catch (JSONException e) { | ||
70 | e.printStackTrace(); | 82 | e.printStackTrace(); | ||
71 | } | 83 | } | ||
72 | } | 84 | } | ||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Line(s) | 125 | addCommandButton.setOnClickListener(view -> BackgroundService.RunCommand(RunCommandActivity.this, service -> { | |||
129 | dialog.show(); | 141 | dialog.show(); | ||
130 | 142 | | |||
131 | })); | 143 | })); | ||
132 | 144 | | |||
133 | updateView(); | 145 | updateView(); | ||
134 | } | 146 | } | ||
135 | 147 | | |||
136 | @Override | 148 | @Override | ||
149 | public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { | ||||
150 | super.onCreateContextMenu(menu, v, menuInfo); | ||||
151 | MenuInflater inflater = getMenuInflater(); | ||||
152 | inflater.inflate(R.menu.runcommand_context, menu); | ||||
153 | } | ||||
154 | | ||||
155 | @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB) | ||||
156 | @Override | ||||
157 | public boolean onContextItemSelected(MenuItem item) { | ||||
158 | AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); | ||||
159 | if (item.getItemId() == R.id.copy_url_to_clipboard) { | ||||
160 | CommandEntry entry = (CommandEntry) commandItems.get(info.position); | ||||
161 | String url = "kdeconnect://runcommand/" + deviceId + "/" + entry.getKey(); | ||||
162 | ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); | ||||
163 | cm.setText(url); | ||||
164 | Toast toast = Toast.makeText(this, R.string.clipboard_toast, Toast.LENGTH_SHORT); | ||||
165 | toast.show(); | ||||
166 | } | ||||
167 | return false; | ||||
168 | } | ||||
169 | | ||||
170 | @Override | ||||
137 | protected void onResume() { | 171 | protected void onResume() { | ||
138 | super.onResume(); | 172 | super.onResume(); | ||
139 | 173 | | |||
140 | BackgroundService.RunCommand(this, service -> { | 174 | BackgroundService.RunCommand(this, service -> { | ||
141 | 175 | | |||
142 | final Device device = service.getDevice(deviceId); | 176 | final Device device = service.getDevice(deviceId); | ||
143 | final RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class); | 177 | final RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class); | ||
144 | if (plugin == null) { | 178 | if (plugin == null) { | ||
Show All 23 Lines |