2015-09-12 12:28:27 +02:00
|
|
|
/*
|
2020-08-17 16:17:20 +02:00
|
|
|
* SPDX-FileCopyrightText: 2015 Aleix Pol Gonzalez <aleixpol@kde.org>
|
|
|
|
* SPDX-FileCopyrightText: 2015 Albert Vaca Cintora <albertvaka@gmail.com>
|
2015-09-12 12:28:27 +02:00
|
|
|
*
|
2020-08-17 16:17:20 +02:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2018-05-10 19:46:47 +02:00
|
|
|
*/
|
2015-09-12 12:28:27 +02:00
|
|
|
|
|
|
|
package org.kde.kdeconnect.Plugins.RunCommandPlugin;
|
|
|
|
|
2018-07-27 07:55:30 +02:00
|
|
|
import android.content.ClipboardManager;
|
2015-09-12 12:28:27 +02:00
|
|
|
import android.os.Bundle;
|
2019-03-31 20:09:44 +02:00
|
|
|
import android.util.Log;
|
2018-07-27 07:55:30 +02:00
|
|
|
import android.view.ContextMenu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2015-09-12 12:28:27 +02:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.AdapterView;
|
2018-07-27 07:55:30 +02:00
|
|
|
import android.widget.Toast;
|
2015-09-12 12:28:27 +02:00
|
|
|
|
2020-07-07 16:45:02 +05:30
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
|
2015-09-12 12:28:27 +02:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2023-05-24 19:26:54 +02:00
|
|
|
import org.kde.kdeconnect.KdeConnect;
|
2015-09-12 12:28:27 +02:00
|
|
|
import org.kde.kdeconnect.UserInterface.List.ListAdapter;
|
|
|
|
import org.kde.kdeconnect_tp.R;
|
2020-07-08 05:34:01 +05:30
|
|
|
import org.kde.kdeconnect_tp.databinding.ActivityRunCommandBinding;
|
2015-09-12 12:28:27 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2016-09-29 11:08:25 +02:00
|
|
|
import java.util.Collections;
|
2020-07-10 16:55:35 +05:30
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.List;
|
2021-03-07 10:55:47 +00:00
|
|
|
import java.util.Objects;
|
2015-09-12 12:28:27 +02:00
|
|
|
|
2017-07-25 18:12:18 +02:00
|
|
|
public class RunCommandActivity extends AppCompatActivity {
|
2020-07-08 05:34:01 +05:30
|
|
|
private ActivityRunCommandBinding binding;
|
2015-09-12 12:28:27 +02:00
|
|
|
private String deviceId;
|
2023-05-24 19:26:54 +02:00
|
|
|
private final RunCommandPlugin.CommandsChangedCallback commandsChangedCallback = () -> runOnUiThread(this::updateView);
|
2020-07-10 16:55:35 +05:30
|
|
|
private List<CommandEntry> commandItems;
|
2015-09-12 12:28:27 +02:00
|
|
|
|
|
|
|
private void updateView() {
|
2023-05-24 19:26:54 +02:00
|
|
|
RunCommandPlugin plugin = KdeConnect.getInstance().getDevicePlugin(deviceId, RunCommandPlugin.class);
|
|
|
|
if (plugin == null) {
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
registerForContextMenu(binding.runCommandsList);
|
|
|
|
|
|
|
|
commandItems = new ArrayList<>();
|
|
|
|
for (JSONObject obj : plugin.getCommandList()) {
|
|
|
|
try {
|
|
|
|
commandItems.add(new CommandEntry(obj.getString("name"),
|
|
|
|
obj.getString("command"), obj.getString("key")));
|
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.e("RunCommand", "Error parsing JSON", e);
|
2019-03-30 17:27:26 +01:00
|
|
|
}
|
2023-05-24 19:26:54 +02:00
|
|
|
}
|
2018-05-09 14:02:56 +02:00
|
|
|
|
2023-05-24 19:26:54 +02:00
|
|
|
Collections.sort(commandItems, Comparator.comparing(CommandEntry::getName));
|
2018-05-09 14:02:56 +02:00
|
|
|
|
2023-05-24 19:26:54 +02:00
|
|
|
ListAdapter adapter = new ListAdapter(RunCommandActivity.this, commandItems);
|
2018-05-09 14:02:56 +02:00
|
|
|
|
2023-05-24 19:26:54 +02:00
|
|
|
binding.runCommandsList.setAdapter(adapter);
|
|
|
|
binding.runCommandsList.setOnItemClickListener((adapterView, view1, i, l) ->
|
|
|
|
plugin.runCommand(commandItems.get(i).getKey()));
|
2018-05-09 14:02:56 +02:00
|
|
|
|
2023-05-24 19:26:54 +02:00
|
|
|
String text = getString(R.string.addcommand_explanation);
|
|
|
|
if (!plugin.canAddCommand()) {
|
|
|
|
text += "\n" + getString(R.string.addcommand_explanation2);
|
|
|
|
}
|
|
|
|
binding.addComandExplanation.setText(text);
|
|
|
|
binding.addComandExplanation.setVisibility(commandItems.isEmpty() ? View.VISIBLE : View.GONE);
|
2015-09-12 12:28:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2020-07-08 05:34:01 +05:30
|
|
|
|
|
|
|
binding = ActivityRunCommandBinding.inflate(getLayoutInflater());
|
|
|
|
setContentView(binding.getRoot());
|
2015-09-12 12:28:27 +02:00
|
|
|
|
2021-03-07 10:55:47 +00:00
|
|
|
setSupportActionBar(binding.toolbarLayout.toolbar);
|
|
|
|
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
|
|
|
|
getSupportActionBar().setDisplayShowHomeEnabled(true);
|
|
|
|
|
2015-09-12 12:28:27 +02:00
|
|
|
deviceId = getIntent().getStringExtra("deviceId");
|
2023-05-24 19:26:54 +02:00
|
|
|
RunCommandPlugin plugin = KdeConnect.getInstance().getDevicePlugin(deviceId,RunCommandPlugin.class);
|
|
|
|
if (plugin != null) {
|
|
|
|
if (plugin.canAddCommand()) {
|
|
|
|
binding.addCommandButton.show();
|
|
|
|
} else {
|
|
|
|
binding.addCommandButton.hide();
|
|
|
|
}
|
|
|
|
binding.addCommandButton.setOnClickListener(v -> {
|
|
|
|
plugin.sendSetupPacket();
|
|
|
|
new AlertDialog.Builder(RunCommandActivity.this)
|
|
|
|
.setTitle(R.string.add_command)
|
|
|
|
.setMessage(R.string.add_command_description)
|
|
|
|
.setPositiveButton(R.string.ok, null)
|
|
|
|
.show();
|
|
|
|
});
|
2019-03-20 22:07:02 +01:00
|
|
|
}
|
2015-09-12 12:28:27 +02:00
|
|
|
updateView();
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:55:30 +02:00
|
|
|
@Override
|
|
|
|
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
|
|
|
|
super.onCreateContextMenu(menu, v, menuInfo);
|
|
|
|
MenuInflater inflater = getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.runcommand_context, menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onContextItemSelected(MenuItem item) {
|
|
|
|
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
|
|
|
|
if (item.getItemId() == R.id.copy_url_to_clipboard) {
|
|
|
|
CommandEntry entry = (CommandEntry) commandItems.get(info.position);
|
|
|
|
String url = "kdeconnect://runcommand/" + deviceId + "/" + entry.getKey();
|
2020-07-07 16:45:02 +05:30
|
|
|
ClipboardManager cm = ContextCompat.getSystemService(this, ClipboardManager.class);
|
2018-07-27 07:55:30 +02:00
|
|
|
cm.setText(url);
|
|
|
|
Toast toast = Toast.makeText(this, R.string.clipboard_toast, Toast.LENGTH_SHORT);
|
|
|
|
toast.show();
|
2022-01-22 12:03:53 +01:00
|
|
|
return true;
|
2018-07-27 07:55:30 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-12 12:28:27 +02:00
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
|
2023-05-24 19:26:54 +02:00
|
|
|
RunCommandPlugin plugin = KdeConnect.getInstance().getDevicePlugin(deviceId, RunCommandPlugin.class);
|
|
|
|
if (plugin == null) {
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
plugin.addCommandsUpdatedCallback(commandsChangedCallback);
|
2015-09-12 12:28:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
|
2023-05-24 19:26:54 +02:00
|
|
|
RunCommandPlugin plugin = KdeConnect.getInstance().getDevicePlugin(deviceId, RunCommandPlugin.class);
|
|
|
|
if (plugin == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
plugin.removeCommandsUpdatedCallback(commandsChangedCallback);
|
2018-03-04 14:38:10 +01:00
|
|
|
}
|
2021-10-17 11:32:41 +05:30
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onSupportNavigateUp() {
|
|
|
|
super.onBackPressed();
|
|
|
|
return true;
|
|
|
|
}
|
2015-09-12 12:28:27 +02:00
|
|
|
}
|