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;
|
|
|
|
import android.os.Build;
|
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.annotation.RequiresApi;
|
|
|
|
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;
|
|
|
|
import org.kde.kdeconnect.BackgroundService;
|
|
|
|
import org.kde.kdeconnect.UserInterface.List.ListAdapter;
|
2020-07-16 06:28:33 +00:00
|
|
|
import org.kde.kdeconnect.UserInterface.ThemeUtil;
|
2015-09-12 12:28:27 +02:00
|
|
|
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;
|
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;
|
2018-05-09 14:02:56 +02:00
|
|
|
private final RunCommandPlugin.CommandsChangedCallback commandsChangedCallback = 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() {
|
2019-03-30 17:27:26 +01:00
|
|
|
BackgroundService.RunWithPlugin(this, deviceId, RunCommandPlugin.class, plugin -> runOnUiThread(() -> {
|
2020-07-08 05:34:01 +05:30
|
|
|
registerForContextMenu(binding.runCommandsList);
|
2019-03-30 17:27:26 +01:00
|
|
|
|
|
|
|
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) {
|
2019-03-31 20:09:44 +02:00
|
|
|
Log.e("RunCommand", "Error parsing JSON", e);
|
2018-05-09 14:02:56 +02:00
|
|
|
}
|
2019-03-30 17:27:26 +01:00
|
|
|
}
|
2018-05-09 14:02:56 +02:00
|
|
|
|
2020-07-10 16:55:35 +05:30
|
|
|
Collections.sort(commandItems, Comparator.comparing(CommandEntry::getName));
|
2018-05-09 14:02:56 +02:00
|
|
|
|
2019-03-30 17:27:26 +01:00
|
|
|
ListAdapter adapter = new ListAdapter(RunCommandActivity.this, commandItems);
|
2018-05-09 14:02:56 +02:00
|
|
|
|
2020-07-08 05:34:01 +05:30
|
|
|
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
|
|
|
|
2019-03-30 17:27:26 +01:00
|
|
|
String text = getString(R.string.addcommand_explanation);
|
|
|
|
if (!plugin.canAddCommand()) {
|
|
|
|
text += "\n" + getString(R.string.addcommand_explanation2);
|
|
|
|
}
|
2020-07-08 05:34:01 +05:30
|
|
|
binding.addComandExplanation.setText(text);
|
|
|
|
binding.addComandExplanation.setVisibility(commandItems.isEmpty() ? View.VISIBLE : View.GONE);
|
2019-03-30 17:27:26 +01:00
|
|
|
}));
|
2015-09-12 12:28:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2020-07-16 06:28:33 +00:00
|
|
|
ThemeUtil.setUserPreferredTheme(this);
|
2020-07-08 05:34:01 +05:30
|
|
|
|
|
|
|
binding = ActivityRunCommandBinding.inflate(getLayoutInflater());
|
|
|
|
setContentView(binding.getRoot());
|
2015-09-12 12:28:27 +02:00
|
|
|
|
|
|
|
deviceId = getIntent().getStringExtra("deviceId");
|
|
|
|
|
2019-11-25 14:32:27 +01:00
|
|
|
boolean canAddCommands = false;
|
|
|
|
try {
|
|
|
|
canAddCommands = BackgroundService.getInstance().getDevice(deviceId).getPlugin(RunCommandPlugin.class).canAddCommand();
|
|
|
|
} catch (Exception ignore) {
|
|
|
|
}
|
2018-03-04 14:38:10 +01:00
|
|
|
|
2019-03-20 22:07:02 +01:00
|
|
|
if (canAddCommands) {
|
2020-07-08 05:34:01 +05:30
|
|
|
binding.addCommandButton.show();
|
2019-03-20 22:07:02 +01:00
|
|
|
} else {
|
2020-07-08 05:34:01 +05:30
|
|
|
binding.addCommandButton.hide();
|
2019-03-20 22:07:02 +01:00
|
|
|
}
|
2018-05-10 19:46:47 +02:00
|
|
|
|
2020-07-08 05:34:01 +05:30
|
|
|
binding.addCommandButton.setOnClickListener(v -> BackgroundService.RunWithPlugin(RunCommandActivity.this, deviceId, RunCommandPlugin.class, plugin -> {
|
2019-03-30 17:27:26 +01:00
|
|
|
plugin.sendSetupPacket();
|
2019-10-27 23:43:13 +01:00
|
|
|
new AlertDialog.Builder(RunCommandActivity.this)
|
2019-03-30 17:27:26 +01:00
|
|
|
.setTitle(R.string.add_command)
|
|
|
|
.setMessage(R.string.add_command_description)
|
|
|
|
.setPositiveButton(R.string.ok, null)
|
2019-10-27 23:43:13 +01:00
|
|
|
.show();
|
2019-03-30 17:27:26 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
|
|
|
|
@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();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-12 12:28:27 +02:00
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
|
2019-03-30 17:27:26 +01:00
|
|
|
BackgroundService.RunWithPlugin(this, deviceId, RunCommandPlugin.class, plugin -> plugin.addCommandsUpdatedCallback(commandsChangedCallback));
|
2015-09-12 12:28:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
|
2019-03-30 17:27:26 +01:00
|
|
|
BackgroundService.RunWithPlugin(this, deviceId, RunCommandPlugin.class, plugin -> plugin.removeCommandsUpdatedCallback(commandsChangedCallback));
|
2018-03-04 14:38:10 +01:00
|
|
|
}
|
2015-09-12 12:28:27 +02:00
|
|
|
}
|