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;
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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() {
|
|
|
|
ListView view = (ListView) findViewById(R.id.listView1);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private final RunCommandPlugin.CommandsChangedCallback theCallback = new RunCommandPlugin.CommandsChangedCallback() {
|
|
|
|
@Override
|
|
|
|
public void update() {
|
|
|
|
updateView();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_list);
|
|
|
|
|
|
|
|
deviceId = getIntent().getStringExtra("deviceId");
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
plugin.addCommandsUpdatedCallback(theCallback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
|
|
|
plugin.removeCommandsUpdatedCallback(theCallback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|