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.os.Bundle;
|
2018-03-04 14:38:10 +01:00
|
|
|
import android.support.design.widget.FloatingActionButton;
|
2017-07-25 18:12:18 +02:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
2015-09-12 12:28:27 +02:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.ListView;
|
2018-03-04 14:38:10 +01:00
|
|
|
import android.widget.TextView;
|
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.Device;
|
|
|
|
import org.kde.kdeconnect.UserInterface.List.ListAdapter;
|
|
|
|
import org.kde.kdeconnect_tp.R;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2016-09-29 11:08:25 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
2015-09-12 12:28:27 +02:00
|
|
|
|
2017-07-25 18:12:18 +02:00
|
|
|
public class RunCommandActivity extends AppCompatActivity {
|
2015-09-12 12:28:27 +02:00
|
|
|
|
|
|
|
private String deviceId;
|
2018-03-04 14:38:10 +01:00
|
|
|
private final RunCommandPlugin.CommandsChangedCallback commandsChangedCallback = new RunCommandPlugin.CommandsChangedCallback() {
|
|
|
|
@Override
|
|
|
|
public void update() {
|
|
|
|
updateView();
|
|
|
|
}
|
|
|
|
};
|
2015-09-12 12:28:27 +02:00
|
|
|
|
|
|
|
private void updateView() {
|
|
|
|
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
|
|
|
|
@Override
|
|
|
|
public void onServiceStart(final BackgroundService service) {
|
|
|
|
|
|
|
|
final Device device = service.getDevice(deviceId);
|
|
|
|
final RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class);
|
|
|
|
if (plugin == null) {
|
|
|
|
Log.e("RunCommandActivity", "device has no runcommand plugin!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
runOnUiThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2018-03-04 14:38:10 +01:00
|
|
|
ListView view = (ListView) findViewById(R.id.runcommandslist);
|
2015-09-12 12:28:27 +02:00
|
|
|
|
2016-09-29 11:08:25 +02:00
|
|
|
final ArrayList<ListAdapter.Item> commandItems = new ArrayList<>();
|
|
|
|
for (JSONObject obj : plugin.getCommandList()) {
|
2015-09-12 12:28:27 +02:00
|
|
|
try {
|
2016-09-29 11:08:25 +02:00
|
|
|
commandItems.add(new CommandEntry(obj.getString("name"),
|
|
|
|
obj.getString("command"), obj.getString("key")));
|
2015-09-12 12:28:27 +02:00
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 11:08:25 +02:00
|
|
|
Collections.sort(commandItems, new Comparator<ListAdapter.Item>() {
|
|
|
|
@Override
|
|
|
|
public int compare(ListAdapter.Item lhs, ListAdapter.Item rhs) {
|
|
|
|
String lName = ((CommandEntry) lhs).getName();
|
|
|
|
String rName = ((CommandEntry) rhs).getName();
|
|
|
|
return lName.compareTo(rName);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-09-12 12:28:27 +02:00
|
|
|
ListAdapter adapter = new ListAdapter(RunCommandActivity.this, commandItems);
|
|
|
|
|
|
|
|
view.setAdapter(adapter);
|
|
|
|
view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
2016-09-29 11:08:25 +02:00
|
|
|
CommandEntry entry = (CommandEntry) commandItems.get(i);
|
|
|
|
plugin.runCommand(entry.getKey());
|
2015-09-12 12:28:27 +02:00
|
|
|
}
|
|
|
|
});
|
2018-03-04 14:38:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
TextView explanation = (TextView) findViewById(R.id.addcomand_explanation);
|
|
|
|
String text = getString(R.string.addcommand_explanation);
|
|
|
|
if (!plugin.canAddCommand()) {
|
|
|
|
text += "\n" + getString(R.string.addcommand_explanation2);
|
|
|
|
}
|
|
|
|
explanation.setText(text);
|
|
|
|
explanation.setVisibility(commandItems.isEmpty() ? View.VISIBLE : View.GONE);
|
2015-09-12 12:28:27 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2018-03-04 14:38:10 +01:00
|
|
|
setContentView(R.layout.activity_runcommand);
|
2015-09-12 12:28:27 +02:00
|
|
|
|
|
|
|
deviceId = getIntent().getStringExtra("deviceId");
|
|
|
|
|
2018-03-04 14:38:10 +01:00
|
|
|
boolean canAddCommands = BackgroundService.getInstance().getDevice(deviceId).getPlugin(RunCommandPlugin.class).canAddCommand();
|
|
|
|
|
|
|
|
FloatingActionButton addCommandButton = (FloatingActionButton) findViewById(R.id.add_command_button);
|
|
|
|
addCommandButton.setVisibility(canAddCommands ? View.VISIBLE : View.GONE);
|
|
|
|
addCommandButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
new AddCommandDialog().show(getSupportFragmentManager(), "addcommanddialog");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-09-12 12:28:27 +02:00
|
|
|
updateView();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
|
|
|
|
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
|
|
|
|
@Override
|
|
|
|
public void onServiceStart(final BackgroundService service) {
|
|
|
|
|
|
|
|
final Device device = service.getDevice(deviceId);
|
|
|
|
final RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class);
|
|
|
|
if (plugin == null) {
|
|
|
|
Log.e("RunCommandActivity", "device has no runcommand plugin!");
|
|
|
|
return;
|
|
|
|
}
|
2018-03-04 14:38:10 +01:00
|
|
|
plugin.addCommandsUpdatedCallback(commandsChangedCallback);
|
2015-09-12 12:28:27 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
|
|
|
|
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
|
|
|
|
@Override
|
|
|
|
public void onServiceStart(final BackgroundService service) {
|
|
|
|
|
|
|
|
final Device device = service.getDevice(deviceId);
|
|
|
|
final RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class);
|
|
|
|
if (plugin == null) {
|
|
|
|
Log.e("RunCommandActivity", "device has no runcommand plugin!");
|
|
|
|
return;
|
|
|
|
}
|
2018-03-04 14:38:10 +01:00
|
|
|
plugin.removeCommandsUpdatedCallback(commandsChangedCallback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void dialogResult(final String cmdName, final String cmdCmd) {
|
|
|
|
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
|
|
|
|
@Override
|
|
|
|
public void onServiceStart(BackgroundService service) {
|
|
|
|
Device device = service.getDevice(deviceId);
|
|
|
|
RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class);
|
|
|
|
if(!cmdName.isEmpty() && !cmdCmd.isEmpty()) {
|
|
|
|
plugin.addCommand(cmdName, cmdCmd);
|
|
|
|
}
|
2015-09-12 12:28:27 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|