2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-22 09:58:08 +00:00

Make it possible to add commands for the RunCommandsPlugin from Android

Summary: Add an ActionBar entry for adding a command to the list. The current way to add commands is quite hidden.

Reviewers: #kde_connect, mtijink

Reviewed By: #kde_connect, mtijink

Subscribers: philipc, mtijink, #kde_connect

Tags: #kde_connect

Differential Revision: https://phabricator.kde.org/D7959
This commit is contained in:
Nicolas Fella 2018-03-04 14:38:10 +01:00
parent f615a0f7f9
commit ae0538ae0c
7 changed files with 211 additions and 12 deletions

13
res/drawable/ic_add.xml Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#ffffff"
android:pathData="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" />
<path
android:pathData="M0 0h24v24H0z" />
</vector>

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/runcommandslist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:addStatesFromChildren="true"
android:orientation="vertical" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/add_command_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/ic_add"
app:backgroundTint="@color/primary"
app:layout_anchor="@id/runcommandslist"
app:layout_anchorGravity="bottom|end" />
<TextView
android:id="@+id/addcomand_explanation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="30dp"
android:text="@string/addcommand_explanation"
android:textAlignment="center"
android:textSize="16sp" />
</android.support.design.widget.CoordinatorLayout>

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/command_confirm_needed"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<EditText
android:id="@+id/addcommand_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="@string/addcommand_name"
android:inputType="text" />
<EditText
android:id="@+id/addcommand_command"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="@string/addcommand_command"
android:inputType="text" />
</LinearLayout>

View File

@ -229,4 +229,11 @@
<string name="device_icon_description">Device icon</string>
<string name="settings_icon_description">Settings icon</string>
<string name="add_command">Add a command</string>
<string name="addcommand_name">Name</string>
<string name="addcommand_command">Command</string>
<string name="command_confirm_needed">You will need to confirm the command on the desktop</string>
<string name="addcommand_explanation">There are no commands registered</string>
<string name="addcommand_explanation2">You can add new commands in the KDE Connect System Settings</string>
</resources>

View File

@ -0,0 +1,57 @@
package org.kde.kdeconnect.Plugins.RunCommandPlugin;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import org.kde.kdeconnect_tp.R;
public class AddCommandDialog extends DialogFragment {
private EditText nameField;
private EditText commandField;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.add_command);
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.addcommanddialog, null);
nameField = (EditText) view.findViewById(R.id.addcommand_name);
commandField = (EditText) view.findViewById(R.id.addcommand_command);
builder.setView(view);
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (getActivity() instanceof RunCommandActivity) {
String name = nameField.getText().toString();
String command = commandField.getText().toString();
((RunCommandActivity) getActivity()).dialogResult(name, command);
}
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
}

View File

@ -22,11 +22,13 @@
package org.kde.kdeconnect.Plugins.RunCommandPlugin;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
@ -42,6 +44,12 @@ import java.util.Comparator;
public class RunCommandActivity extends AppCompatActivity {
private String deviceId;
private final RunCommandPlugin.CommandsChangedCallback commandsChangedCallback = new RunCommandPlugin.CommandsChangedCallback() {
@Override
public void update() {
updateView();
}
};
private void updateView() {
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@ -58,7 +66,7 @@ public class RunCommandActivity extends AppCompatActivity {
runOnUiThread(new Runnable() {
@Override
public void run() {
ListView view = (ListView) findViewById(R.id.listView1);
ListView view = (ListView) findViewById(R.id.runcommandslist);
final ArrayList<ListAdapter.Item> commandItems = new ArrayList<>();
for (JSONObject obj : plugin.getCommandList()) {
@ -89,26 +97,39 @@ public class RunCommandActivity extends AppCompatActivity {
plugin.runCommand(entry.getKey());
}
});
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);
}
});
}
});
}
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);
setContentView(R.layout.activity_runcommand);
deviceId = getIntent().getStringExtra("deviceId");
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");
}
});
updateView();
}
@ -126,7 +147,7 @@ public class RunCommandActivity extends AppCompatActivity {
Log.e("RunCommandActivity", "device has no runcommand plugin!");
return;
}
plugin.addCommandsUpdatedCallback(theCallback);
plugin.addCommandsUpdatedCallback(commandsChangedCallback);
}
});
}
@ -145,7 +166,20 @@ public class RunCommandActivity extends AppCompatActivity {
Log.e("RunCommandActivity", "device has no runcommand plugin!");
return;
}
plugin.removeCommandsUpdatedCallback(theCallback);
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);
}
}
});
}

View File

@ -39,10 +39,13 @@ public class RunCommandPlugin extends Plugin {
public final static String PACKAGE_TYPE_RUNCOMMAND = "kdeconnect.runcommand";
public final static String PACKAGE_TYPE_RUNCOMMAND_REQUEST = "kdeconnect.runcommand.request";
public final static String PACKAGE_TYPE_RUNCOMMAND_ADD = "kdeconnect.runcommand.add";
private ArrayList<JSONObject> commandList = new ArrayList<>();
private ArrayList<CommandsChangedCallback> callbacks = new ArrayList<>();
private boolean canAddCommand;
public void addCommandsUpdatedCallback(CommandsChangedCallback newCallback) {
callbacks.add(newCallback);
}
@ -104,6 +107,8 @@ public class RunCommandPlugin extends Plugin {
device.onPluginsChanged();
canAddCommand = np.getBoolean("canAddCommand", false);
return true;
}
return false;
@ -133,7 +138,7 @@ public class RunCommandPlugin extends Plugin {
@Override
public boolean hasMainActivity() {
return !commandList.isEmpty();
return true;
}
@Override
@ -148,4 +153,17 @@ public class RunCommandPlugin extends Plugin {
return context.getString(R.string.pref_plugin_runcommand);
}
public void addCommand(String name, String command){
NetworkPackage np = new NetworkPackage(PACKAGE_TYPE_RUNCOMMAND_ADD);
np.set("name", name);
np.set("command", command);
device.sendPackage(np);
}
public boolean canAddCommand(){
return canAddCommand;
}
}