2015-09-12 12:28:27 +02:00
|
|
|
/*
|
|
|
|
* 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;
|
2018-03-04 11:31:37 +01:00
|
|
|
import org.kde.kdeconnect.NetworkPacket;
|
2015-09-12 12:28:27 +02:00
|
|
|
import org.kde.kdeconnect.Plugins.Plugin;
|
|
|
|
import org.kde.kdeconnect_tp.R;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2018-07-30 18:15:00 +02:00
|
|
|
import java.util.Collections;
|
2015-09-12 12:28:27 +02:00
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
public class RunCommandPlugin extends Plugin {
|
|
|
|
|
2018-03-04 11:31:37 +01:00
|
|
|
public final static String PACKET_TYPE_RUNCOMMAND = "kdeconnect.runcommand";
|
|
|
|
public final static String PACKET_TYPE_RUNCOMMAND_REQUEST = "kdeconnect.runcommand.request";
|
2016-05-31 17:19:39 +02:00
|
|
|
|
2015-09-12 12:28:27 +02:00
|
|
|
private ArrayList<JSONObject> commandList = new ArrayList<>();
|
|
|
|
private ArrayList<CommandsChangedCallback> callbacks = new ArrayList<>();
|
2018-07-30 18:15:00 +02:00
|
|
|
private final ArrayList<CommandEntry> commandItems = new ArrayList<>();
|
2015-09-12 12:28:27 +02:00
|
|
|
|
2018-03-04 14:38:10 +01:00
|
|
|
private boolean canAddCommand;
|
|
|
|
|
2015-09-12 12:28:27 +02:00
|
|
|
public void addCommandsUpdatedCallback(CommandsChangedCallback newCallback) {
|
|
|
|
callbacks.add(newCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeCommandsUpdatedCallback(CommandsChangedCallback theCallback) {
|
|
|
|
callbacks.remove(theCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface CommandsChangedCallback {
|
|
|
|
void update();
|
2018-03-03 16:06:52 +01:00
|
|
|
}
|
2015-09-12 12:28:27 +02:00
|
|
|
|
|
|
|
public ArrayList<JSONObject> getCommandList() {
|
|
|
|
return commandList;
|
|
|
|
}
|
|
|
|
|
2018-07-30 18:15:00 +02:00
|
|
|
public ArrayList<CommandEntry> getCommandItems() {
|
|
|
|
return commandItems;
|
|
|
|
}
|
|
|
|
|
2015-09-12 12:28:27 +02:00
|
|
|
@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
|
2018-03-04 11:31:37 +01:00
|
|
|
public boolean onPacketReceived(NetworkPacket np) {
|
2015-09-12 12:28:27 +02:00
|
|
|
|
|
|
|
if (np.has("commandList")) {
|
|
|
|
commandList.clear();
|
|
|
|
try {
|
2018-07-30 18:15:00 +02:00
|
|
|
commandItems.clear();
|
2015-09-12 12:28:27 +02:00
|
|
|
JSONObject obj = new JSONObject(np.getString("commandList"));
|
|
|
|
Iterator<String> keys = obj.keys();
|
2018-03-03 16:06:52 +01:00
|
|
|
while (keys.hasNext()) {
|
2015-09-12 12:28:27 +02:00
|
|
|
String s = keys.next();
|
|
|
|
JSONObject o = obj.getJSONObject(s);
|
|
|
|
o.put("key", s);
|
|
|
|
commandList.add(o);
|
2018-07-30 18:15:00 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
commandItems.add(
|
|
|
|
new CommandEntry(
|
|
|
|
o.getString("name"),
|
|
|
|
o.getString("command"),
|
|
|
|
o.getString("key")
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2015-09-12 12:28:27 +02:00
|
|
|
}
|
2018-07-30 18:15:00 +02:00
|
|
|
|
|
|
|
Collections.sort(commandItems, (lhs, rhs) -> lhs.getName().compareTo(rhs.getName()) );
|
|
|
|
|
|
|
|
Intent updateWidget = new Intent(context, RunCommandWidget.class);
|
|
|
|
context.sendBroadcast(updateWidget);
|
|
|
|
|
2015-09-12 12:28:27 +02:00
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (CommandsChangedCallback aCallback : callbacks) {
|
|
|
|
aCallback.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
device.onPluginsChanged();
|
|
|
|
|
2018-03-04 14:38:10 +01:00
|
|
|
canAddCommand = np.getBoolean("canAddCommand", false);
|
|
|
|
|
2015-09-12 12:28:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-04 11:31:37 +01:00
|
|
|
public String[] getSupportedPacketTypes() {
|
|
|
|
return new String[]{PACKET_TYPE_RUNCOMMAND};
|
2015-09-12 12:28:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-04 11:31:37 +01:00
|
|
|
public String[] getOutgoingPacketTypes() {
|
|
|
|
return new String[]{PACKET_TYPE_RUNCOMMAND_REQUEST};
|
2015-09-12 12:28:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void runCommand(String cmdKey) {
|
2018-03-04 11:31:37 +01:00
|
|
|
NetworkPacket np = new NetworkPacket(PACKET_TYPE_RUNCOMMAND_REQUEST);
|
2015-09-12 12:28:27 +02:00
|
|
|
np.set("key", cmdKey);
|
2018-03-04 11:31:37 +01:00
|
|
|
device.sendPacket(np);
|
2015-09-12 12:28:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void requestCommandList() {
|
2018-03-04 11:31:37 +01:00
|
|
|
NetworkPacket np = new NetworkPacket(PACKET_TYPE_RUNCOMMAND_REQUEST);
|
2015-09-12 12:28:27 +02:00
|
|
|
np.set("requestCommandList", true);
|
2018-03-04 11:31:37 +01:00
|
|
|
device.sendPacket(np);
|
2015-09-12 12:28:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasMainActivity() {
|
2018-03-04 14:38:10 +01:00
|
|
|
return true;
|
2015-09-12 12:28:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
2018-03-04 14:38:10 +01:00
|
|
|
public boolean canAddCommand(){
|
|
|
|
return canAddCommand;
|
|
|
|
}
|
|
|
|
|
2018-05-10 19:46:47 +02:00
|
|
|
void sendSetupPacket() {
|
|
|
|
NetworkPacket np = new NetworkPacket(RunCommandPlugin.PACKET_TYPE_RUNCOMMAND_REQUEST);
|
|
|
|
np.set("setup", true);
|
|
|
|
device.sendPacket(np);
|
|
|
|
}
|
|
|
|
|
2015-09-12 12:28:27 +02:00
|
|
|
}
|