File indexing completed on 2024-12-22 04:41:40
0001 /* 0002 * SPDX-FileCopyrightText: 2015 Aleix Pol Gonzalez <aleixpol@kde.org> 0003 * SPDX-FileCopyrightText: 2015 Albert Vaca Cintora <albertvaka@gmail.com> 0004 * 0005 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0006 */ 0007 0008 package org.kde.kdeconnect.Plugins.RunCommandPlugin; 0009 0010 import android.content.ClipboardManager; 0011 import android.os.Bundle; 0012 import android.util.Log; 0013 import android.view.ContextMenu; 0014 import android.view.MenuInflater; 0015 import android.view.MenuItem; 0016 import android.view.View; 0017 import android.widget.AdapterView; 0018 import android.widget.Toast; 0019 0020 import androidx.appcompat.app.AlertDialog; 0021 import androidx.appcompat.app.AppCompatActivity; 0022 import androidx.core.content.ContextCompat; 0023 0024 import org.json.JSONException; 0025 import org.json.JSONObject; 0026 import org.kde.kdeconnect.Device; 0027 import org.kde.kdeconnect.KdeConnect; 0028 import org.kde.kdeconnect.UserInterface.List.ListAdapter; 0029 import org.kde.kdeconnect_tp.R; 0030 import org.kde.kdeconnect_tp.databinding.ActivityRunCommandBinding; 0031 0032 import java.util.ArrayList; 0033 import java.util.Collections; 0034 import java.util.Comparator; 0035 import java.util.List; 0036 import java.util.Objects; 0037 0038 public class RunCommandActivity extends AppCompatActivity { 0039 private ActivityRunCommandBinding binding; 0040 private String deviceId; 0041 private final RunCommandPlugin.CommandsChangedCallback commandsChangedCallback = () -> runOnUiThread(this::updateView); 0042 private List<CommandEntry> commandItems; 0043 0044 private void updateView() { 0045 RunCommandPlugin plugin = KdeConnect.getInstance().getDevicePlugin(deviceId, RunCommandPlugin.class); 0046 if (plugin == null) { 0047 finish(); 0048 return; 0049 } 0050 0051 registerForContextMenu(binding.runCommandsList); 0052 0053 commandItems = new ArrayList<>(); 0054 for (JSONObject obj : plugin.getCommandList()) { 0055 try { 0056 commandItems.add(new CommandEntry(obj)); 0057 } catch (JSONException e) { 0058 Log.e("RunCommand", "Error parsing JSON", e); 0059 } 0060 } 0061 0062 Collections.sort(commandItems, Comparator.comparing(CommandEntry::getName)); 0063 0064 ListAdapter adapter = new ListAdapter(RunCommandActivity.this, commandItems); 0065 0066 binding.runCommandsList.setAdapter(adapter); 0067 binding.runCommandsList.setOnItemClickListener((adapterView, view1, i, l) -> 0068 plugin.runCommand(commandItems.get(i).getKey())); 0069 0070 String text = getString(R.string.addcommand_explanation); 0071 if (!plugin.canAddCommand()) { 0072 text += "\n" + getString(R.string.addcommand_explanation2); 0073 } 0074 binding.addCommandExplanation.setText(text); 0075 binding.addCommandExplanation.setVisibility(commandItems.isEmpty() ? View.VISIBLE : View.GONE); 0076 } 0077 0078 @Override 0079 protected void onCreate(Bundle savedInstanceState) { 0080 super.onCreate(savedInstanceState); 0081 0082 binding = ActivityRunCommandBinding.inflate(getLayoutInflater()); 0083 setContentView(binding.getRoot()); 0084 0085 setSupportActionBar(binding.toolbarLayout.toolbar); 0086 Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true); 0087 getSupportActionBar().setDisplayShowHomeEnabled(true); 0088 0089 deviceId = getIntent().getStringExtra("deviceId"); 0090 Device device = KdeConnect.getInstance().getDevice(deviceId); 0091 if (device != null) { 0092 getSupportActionBar().setSubtitle(device.getName()); 0093 RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class); 0094 if (plugin != null) { 0095 if (plugin.canAddCommand()) { 0096 binding.addCommandButton.show(); 0097 } else { 0098 binding.addCommandButton.hide(); 0099 } 0100 binding.addCommandButton.setOnClickListener(v -> { 0101 plugin.sendSetupPacket(); 0102 new AlertDialog.Builder(RunCommandActivity.this) 0103 .setTitle(R.string.add_command) 0104 .setMessage(R.string.add_command_description) 0105 .setPositiveButton(R.string.ok, null) 0106 .show(); 0107 }); 0108 } 0109 } 0110 updateView(); 0111 } 0112 0113 @Override 0114 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 0115 super.onCreateContextMenu(menu, v, menuInfo); 0116 MenuInflater inflater = getMenuInflater(); 0117 inflater.inflate(R.menu.runcommand_context, menu); 0118 } 0119 0120 @Override 0121 public boolean onContextItemSelected(MenuItem item) { 0122 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); 0123 if (item.getItemId() == R.id.copy_url_to_clipboard) { 0124 CommandEntry entry = (CommandEntry) commandItems.get(info.position); 0125 String url = "kdeconnect://runcommand/" + deviceId + "/" + entry.getKey(); 0126 ClipboardManager cm = ContextCompat.getSystemService(this, ClipboardManager.class); 0127 cm.setText(url); 0128 Toast toast = Toast.makeText(this, R.string.clipboard_toast, Toast.LENGTH_SHORT); 0129 toast.show(); 0130 return true; 0131 } 0132 return false; 0133 } 0134 0135 @Override 0136 protected void onResume() { 0137 super.onResume(); 0138 0139 RunCommandPlugin plugin = KdeConnect.getInstance().getDevicePlugin(deviceId, RunCommandPlugin.class); 0140 if (plugin == null) { 0141 finish(); 0142 return; 0143 } 0144 plugin.addCommandsUpdatedCallback(commandsChangedCallback); 0145 } 0146 0147 @Override 0148 protected void onPause() { 0149 super.onPause(); 0150 0151 RunCommandPlugin plugin = KdeConnect.getInstance().getDevicePlugin(deviceId, RunCommandPlugin.class); 0152 if (plugin == null) { 0153 return; 0154 } 0155 plugin.removeCommandsUpdatedCallback(commandsChangedCallback); 0156 } 0157 0158 @Override 0159 public boolean onSupportNavigateUp() { 0160 super.onBackPressed(); 0161 return true; 0162 } 0163 }