2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-22 01:51:47 +00:00
Nicolas Fella e0fde16525 Added runcommandplugin widget
Summary: A simple widget to execute commands from android desktop

Test Plan:
Add the widget and execute commands from it.
By default, the selected device is the first device in the device list.
The selected device can be changed via the widget title.
If there is no commands, the widget show an empty list
If there is no connected device, show the message located in @string/unreachable_description

Reviewers: albertvaka, nicolasfella, #kde_connect

Reviewed By: albertvaka

Subscribers: kdeconnect, nicolasfella

Tags: #kde_connect

Differential Revision: https://phabricator.kde.org/D12507
2018-07-30 18:15:25 +02:00

191 lines
5.9 KiB
Java

/*
* Copyright 2015 Aleix Pol Gonzalez <aleixpol@kde.org>
* Copyright 2015 Albert Vaca Cintora <albertvaka@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.kde.kdeconnect.Plugins.RunCommandPlugin;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import org.json.JSONException;
import org.json.JSONObject;
import org.kde.kdeconnect.NetworkPacket;
import org.kde.kdeconnect.Plugins.Plugin;
import org.kde.kdeconnect_tp.R;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class RunCommandPlugin extends Plugin {
public final static String PACKET_TYPE_RUNCOMMAND = "kdeconnect.runcommand";
public final static String PACKET_TYPE_RUNCOMMAND_REQUEST = "kdeconnect.runcommand.request";
private ArrayList<JSONObject> commandList = new ArrayList<>();
private ArrayList<CommandsChangedCallback> callbacks = new ArrayList<>();
private final ArrayList<CommandEntry> commandItems = new ArrayList<>();
private boolean canAddCommand;
public void addCommandsUpdatedCallback(CommandsChangedCallback newCallback) {
callbacks.add(newCallback);
}
public void removeCommandsUpdatedCallback(CommandsChangedCallback theCallback) {
callbacks.remove(theCallback);
}
interface CommandsChangedCallback {
void update();
}
public ArrayList<JSONObject> getCommandList() {
return commandList;
}
public ArrayList<CommandEntry> getCommandItems() {
return commandItems;
}
@Override
public String getDisplayName() {
return context.getResources().getString(R.string.pref_plugin_runcommand);
}
@Override
public String getDescription() {
return context.getResources().getString(R.string.pref_plugin_runcommand_desc);
}
@Override
public Drawable getIcon() {
return ContextCompat.getDrawable(context, R.drawable.runcommand_plugin_icon);
}
@Override
public boolean onCreate() {
requestCommandList();
return true;
}
@Override
public boolean onPacketReceived(NetworkPacket np) {
if (np.has("commandList")) {
commandList.clear();
try {
commandItems.clear();
JSONObject obj = new JSONObject(np.getString("commandList"));
Iterator<String> keys = obj.keys();
while (keys.hasNext()) {
String s = keys.next();
JSONObject o = obj.getJSONObject(s);
o.put("key", s);
commandList.add(o);
try {
commandItems.add(
new CommandEntry(
o.getString("name"),
o.getString("command"),
o.getString("key")
)
);
} catch (JSONException e) {
e.printStackTrace();
}
}
Collections.sort(commandItems, (lhs, rhs) -> lhs.getName().compareTo(rhs.getName()) );
Intent updateWidget = new Intent(context, RunCommandWidget.class);
context.sendBroadcast(updateWidget);
} catch (JSONException e) {
e.printStackTrace();
}
for (CommandsChangedCallback aCallback : callbacks) {
aCallback.update();
}
device.onPluginsChanged();
canAddCommand = np.getBoolean("canAddCommand", false);
return true;
}
return false;
}
@Override
public String[] getSupportedPacketTypes() {
return new String[]{PACKET_TYPE_RUNCOMMAND};
}
@Override
public String[] getOutgoingPacketTypes() {
return new String[]{PACKET_TYPE_RUNCOMMAND_REQUEST};
}
public void runCommand(String cmdKey) {
NetworkPacket np = new NetworkPacket(PACKET_TYPE_RUNCOMMAND_REQUEST);
np.set("key", cmdKey);
device.sendPacket(np);
}
private void requestCommandList() {
NetworkPacket np = new NetworkPacket(PACKET_TYPE_RUNCOMMAND_REQUEST);
np.set("requestCommandList", true);
device.sendPacket(np);
}
@Override
public boolean hasMainActivity() {
return true;
}
@Override
public void startMainActivity(Activity parentActivity) {
Intent intent = new Intent(parentActivity, RunCommandActivity.class);
intent.putExtra("deviceId", device.getDeviceId());
parentActivity.startActivity(intent);
}
@Override
public String getActionName() {
return context.getString(R.string.pref_plugin_runcommand);
}
public boolean canAddCommand(){
return canAddCommand;
}
void sendSetupPacket() {
NetworkPacket np = new NetworkPacket(RunCommandPlugin.PACKET_TYPE_RUNCOMMAND_REQUEST);
np.set("setup", true);
device.sendPacket(np);
}
}