diff --git a/AndroidManifest.xml b/AndroidManifest.xml --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -232,6 +232,10 @@ android:value="org.kde.kdeconnect.UserInterface.PluginSettingsActivity" /> + diff --git a/res/drawable/ic_brightness.xml b/res/drawable/ic_brightness.xml new file mode 100644 --- /dev/null +++ b/res/drawable/ic_brightness.xml @@ -0,0 +1,13 @@ + + + + + + diff --git a/res/layout/activity_brightness.xml b/res/layout/activity_brightness.xml new file mode 100644 --- /dev/null +++ b/res/layout/activity_brightness.xml @@ -0,0 +1,12 @@ + + + + + \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -235,5 +235,7 @@ You will need to confirm the command on the desktop There are no commands registered You can add new commands in the KDE Connect System Settings + Brightness + Control the brightness of your connected device diff --git a/src/org/kde/kdeconnect/Plugins/BrightnessPlugin/BrightnessActivity.java b/src/org/kde/kdeconnect/Plugins/BrightnessPlugin/BrightnessActivity.java new file mode 100644 --- /dev/null +++ b/src/org/kde/kdeconnect/Plugins/BrightnessPlugin/BrightnessActivity.java @@ -0,0 +1,116 @@ +/* + * Copyright 2018 Nicolas Fella + * + * 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 . +*/ + +package org.kde.kdeconnect.Plugins.BrightnessPlugin; + +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v7.app.AppCompatActivity; +import android.util.Log; +import android.widget.SeekBar; + +import org.kde.kdeconnect.Backends.BaseLink; +import org.kde.kdeconnect.Backends.BaseLinkProvider; +import org.kde.kdeconnect.BackgroundService; +import org.kde.kdeconnect.Device; +import org.kde.kdeconnect.NetworkPacket; +import org.kde.kdeconnect_tp.R; + +public class BrightnessActivity extends AppCompatActivity implements BrightnessPlugin.BrightnessListener, SeekBar.OnSeekBarChangeListener { + + private SeekBar slider; + private String deviceId; + private BrightnessPlugin plugin; + private final BaseLinkProvider.ConnectionReceiver connectionReceiver = new BaseLinkProvider.ConnectionReceiver() { + @Override + public void onConnectionReceived(NetworkPacket identityPacket, BaseLink link) { + connectToPlugin(); + } + + @Override + public void onConnectionLost(BaseLink link) { + + } + }; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.activity_brightness); + + deviceId = getIntent().getStringExtra("deviceId"); + slider = (SeekBar) findViewById(R.id.brightnessSlider); + + BackgroundService.RunCommand(BrightnessActivity.this, new BackgroundService.InstanceCallback() { + @Override + public void onServiceStart(BackgroundService service) { + service.addConnectionListener(connectionReceiver); + } + }); + connectToPlugin(); + + } + + private void connectToPlugin() { + + BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() { + @Override + public void onServiceStart(BackgroundService service) { + final Device device = service.getDevice(deviceId); + plugin = device.getPlugin(BrightnessPlugin.class); + if (plugin == null) { + Log.e("BrightnessActivity", "device has no brightness plugin!"); + return; + } + + plugin.addListener(BrightnessActivity.this); + + slider.setMax(plugin.getBrightnessMax()); + slider.setProgress(plugin.getBrightness()); + slider.setOnSeekBarChangeListener(BrightnessActivity.this); + + } + }); + } + + @Override + public void brightnessChanged() { + slider.setMax(plugin.getBrightnessMax()); + slider.setProgress(plugin.getBrightness()); + } + + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + if (fromUser) { + plugin.sendBrightness(progress); + } + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + + } +} diff --git a/src/org/kde/kdeconnect/Plugins/BrightnessPlugin/BrightnessPlugin.java b/src/org/kde/kdeconnect/Plugins/BrightnessPlugin/BrightnessPlugin.java new file mode 100644 --- /dev/null +++ b/src/org/kde/kdeconnect/Plugins/BrightnessPlugin/BrightnessPlugin.java @@ -0,0 +1,138 @@ +/* + * Copyright 2018 Nicolas Fella + * + * 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 . +*/ + +package org.kde.kdeconnect.Plugins.BrightnessPlugin; + +import android.app.Activity; +import android.content.Intent; +import android.graphics.drawable.Drawable; +import android.support.v4.content.ContextCompat; + +import org.kde.kdeconnect.NetworkPacket; +import org.kde.kdeconnect.Plugins.Plugin; +import org.kde.kdeconnect_tp.R; + +import java.util.ArrayList; +import java.util.List; + + +public class BrightnessPlugin extends Plugin { + + private final static String PACKET_TYPE_BRIGHTNESS = "kdeconnect.brightness"; + private final static String PACKET_TYPE_BRIGHTNESS_REQUEST = "kdeconnect.brightness.request"; + private int brightness; + private int brightnessMax; + private int brightnessSteps; + private List listeners = new ArrayList<>(); + + @Override + public boolean onPacketReceived(NetworkPacket np) { + + if (np.has("brightness")) { + brightness = np.getInt("brightness"); + } + + if (np.has("brightnessMax")) { + brightnessMax = np.getInt("brightnessMax"); + } + + if (np.has("brightness")) { + brightnessSteps = np.getInt("brightness"); + } + + for (BrightnessListener l : listeners) { + l.brightnessChanged(); + } + + return true; + + } + + @Override + public boolean onCreate() { + NetworkPacket np = new NetworkPacket(PACKET_TYPE_BRIGHTNESS_REQUEST); + np.set("requestBrightness", true); + device.sendPacket(np); + return true; + } + + @Override + public void startMainActivity(Activity parentActivity) { + Intent intent = new Intent(parentActivity, BrightnessActivity.class); + intent.putExtra("deviceId", device.getDeviceId()); + parentActivity.startActivity(intent); + } + + void sendBrightness(int brightness) { + NetworkPacket np = new NetworkPacket(PACKET_TYPE_BRIGHTNESS_REQUEST); + np.set("setBrightness", brightness); + device.sendPacket(np); + } + + int getBrightness() { + return brightness; + } + + int getBrightnessMax() { + return brightnessMax; + } + + public int getBrightnessSteps() { + return brightnessSteps; + } + + void addListener(BrightnessListener listener) { + listeners.add(listener); + } + + @Override + public boolean hasMainActivity() { + return true; + } + + @Override + public String[] getSupportedPacketTypes() { + return new String[]{PACKET_TYPE_BRIGHTNESS}; + } + + @Override + public String[] getOutgoingPacketTypes() { + return new String[]{PACKET_TYPE_BRIGHTNESS_REQUEST}; + } + + @Override + public Drawable getIcon() { + return ContextCompat.getDrawable(context, R.drawable.ic_brightness); + } + + @Override + public String getDisplayName() { + return context.getResources().getString(R.string.pref_plugin_brightness); + } + + @Override + public String getDescription() { + return context.getResources().getString(R.string.pref_plugin_brightness_desc); + } + + interface BrightnessListener { + void brightnessChanged(); + } +} diff --git a/src/org/kde/kdeconnect/Plugins/PluginFactory.java b/src/org/kde/kdeconnect/Plugins/PluginFactory.java --- a/src/org/kde/kdeconnect/Plugins/PluginFactory.java +++ b/src/org/kde/kdeconnect/Plugins/PluginFactory.java @@ -26,6 +26,7 @@ import org.kde.kdeconnect.Device; import org.kde.kdeconnect.Plugins.BatteryPlugin.BatteryPlugin; +import org.kde.kdeconnect.Plugins.BrightnessPlugin.BrightnessPlugin; import org.kde.kdeconnect.Plugins.ClibpoardPlugin.ClipboardPlugin; import org.kde.kdeconnect.Plugins.FindMyPhonePlugin.FindMyPhonePlugin; import org.kde.kdeconnect.Plugins.MousePadPlugin.MousePadPlugin; @@ -128,6 +129,7 @@ PluginFactory.registerPlugin(FindMyPhonePlugin.class); PluginFactory.registerPlugin(RunCommandPlugin.class); PluginFactory.registerPlugin(RemoteKeyboardPlugin.class); + PluginFactory.registerPlugin(BrightnessPlugin.class); } public static PluginInfo getPluginInfo(Context context, String pluginKey) {