2014-11-16 23:14:06 -08:00
|
|
|
/*
|
2020-08-17 16:17:20 +02:00
|
|
|
* SPDX-FileCopyrightText: 2014 Albert Vaca Cintora <albertvaka@gmail.com>
|
2014-11-16 23:14:06 -08:00
|
|
|
*
|
2020-08-17 16:17:20 +02:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2014-11-16 23:14:06 -08:00
|
|
|
*/
|
|
|
|
|
2013-09-05 01:37:59 +02:00
|
|
|
package org.kde.kdeconnect.Plugins;
|
2013-08-16 10:31:01 +02:00
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
import android.app.Activity;
|
2013-08-16 10:31:01 +02:00
|
|
|
import android.content.Context;
|
2020-03-20 20:30:58 +00:00
|
|
|
import android.content.SharedPreferences;
|
2017-05-31 15:51:07 +02:00
|
|
|
import android.content.pm.PackageManager;
|
2013-08-19 19:57:29 +02:00
|
|
|
import android.graphics.drawable.Drawable;
|
2018-01-03 20:49:51 +01:00
|
|
|
import android.os.Build;
|
2013-08-16 10:31:01 +02:00
|
|
|
|
2020-03-20 20:30:58 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.annotation.StringRes;
|
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
import androidx.fragment.app.DialogFragment;
|
|
|
|
|
2020-07-12 13:37:24 +05:30
|
|
|
import org.apache.commons.lang3.ArrayUtils;
|
2013-09-05 01:37:59 +02:00
|
|
|
import org.kde.kdeconnect.Device;
|
2018-03-04 11:31:37 +01:00
|
|
|
import org.kde.kdeconnect.NetworkPacket;
|
2019-02-02 11:38:13 +00:00
|
|
|
import org.kde.kdeconnect.UserInterface.AlertDialogFragment;
|
2020-01-14 22:57:48 +01:00
|
|
|
import org.kde.kdeconnect.UserInterface.MainActivity;
|
2019-02-02 11:38:13 +00:00
|
|
|
import org.kde.kdeconnect.UserInterface.PermissionsAlertDialogFragment;
|
2019-01-06 12:26:05 +01:00
|
|
|
import org.kde.kdeconnect.UserInterface.PluginSettingsFragment;
|
2017-05-31 15:51:07 +02:00
|
|
|
import org.kde.kdeconnect_tp.R;
|
2013-08-16 10:31:01 +02:00
|
|
|
|
|
|
|
public abstract class Plugin {
|
|
|
|
protected Device device;
|
|
|
|
protected Context context;
|
2017-05-31 15:51:07 +02:00
|
|
|
protected int permissionExplanation = R.string.permission_explanation;
|
2017-07-11 13:50:40 +02:00
|
|
|
protected int optionalPermissionExplanation = R.string.optional_permission_explanation;
|
2020-03-20 20:30:58 +00:00
|
|
|
@Nullable
|
|
|
|
protected SharedPreferences preferences;
|
2013-08-16 10:31:01 +02:00
|
|
|
|
2020-03-20 20:30:58 +00:00
|
|
|
public final void setContext(@NonNull Context context, @Nullable Device device) {
|
2013-08-16 10:31:01 +02:00
|
|
|
this.device = device;
|
|
|
|
this.context = context;
|
2020-03-20 20:30:58 +00:00
|
|
|
|
|
|
|
if (device != null) {
|
|
|
|
this.preferences = this.context.getSharedPreferences(this.getSharedPreferencesName(), Context.MODE_PRIVATE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getSharedPreferencesName() {
|
|
|
|
if (device == null) {
|
|
|
|
throw new RuntimeException("You have to call setContext() before you can call getSharedPreferencesName()");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.supportsDeviceSpecificSettings())
|
|
|
|
return this.device.getDeviceId() + "_" + this.getPluginKey() + "_preferences";
|
|
|
|
else
|
|
|
|
return this.getPluginKey() + "_preferences";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
public SharedPreferences getPreferences() {
|
|
|
|
return this.preferences;
|
2013-08-16 10:31:01 +02:00
|
|
|
}
|
|
|
|
|
2015-08-19 21:27:27 +05:30
|
|
|
/**
|
2015-08-27 13:06:24 +05:30
|
|
|
* To receive the network package from the unpaired device, override
|
|
|
|
* listensToUnpairedDevices to return true and this method.
|
2015-08-19 21:27:27 +05:30
|
|
|
*/
|
2018-03-04 11:31:37 +01:00
|
|
|
public boolean onUnpairedDevicePacketReceived(NetworkPacket np) {
|
2015-08-19 21:27:27 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-27 13:06:24 +05:30
|
|
|
/**
|
2018-03-04 11:31:37 +01:00
|
|
|
* Returns whether this plugin should be loaded or not, to listen to NetworkPackets
|
2015-08-27 13:06:24 +05:30
|
|
|
* from the unpaired devices. By default, returns false.
|
|
|
|
*/
|
|
|
|
public boolean listensToUnpairedDevices() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
/**
|
|
|
|
* Return the internal plugin name, that will be used as a
|
2015-06-06 00:38:51 -07:00
|
|
|
* unique key to distinguish it. Use the class name as key.
|
2013-08-19 19:57:29 +02:00
|
|
|
*/
|
2015-06-06 00:38:51 -07:00
|
|
|
public String getPluginKey() {
|
|
|
|
return getPluginKey(this.getClass());
|
|
|
|
}
|
2018-03-03 16:06:52 +01:00
|
|
|
|
2015-06-06 00:38:51 -07:00
|
|
|
public static String getPluginKey(Class<? extends Plugin> p) {
|
|
|
|
return p.getSimpleName();
|
|
|
|
}
|
2013-08-19 19:57:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the human-readable plugin name. This function can
|
|
|
|
* access this.context to provide translated text.
|
|
|
|
*/
|
|
|
|
public abstract String getDisplayName();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the human-readable description of this plugin. This
|
|
|
|
* function can access this.context to provide translated text.
|
|
|
|
*/
|
|
|
|
public abstract String getDescription();
|
|
|
|
|
2015-04-12 00:11:30 -07:00
|
|
|
/**
|
|
|
|
* Return the action name displayed in the main activity, that
|
|
|
|
* will call startMainActivity when clicked
|
|
|
|
*/
|
|
|
|
public String getActionName() {
|
|
|
|
return getDisplayName();
|
|
|
|
}
|
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
/**
|
|
|
|
* Return an icon associated to this plugin. This function can
|
|
|
|
* access this.context to load the image from resources.
|
|
|
|
*/
|
2015-04-12 00:11:30 -07:00
|
|
|
public Drawable getIcon() {
|
|
|
|
return null;
|
|
|
|
}
|
2013-08-19 19:57:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if this plugin should be enabled on new devices.
|
|
|
|
* This function can access this.context and perform compatibility
|
|
|
|
* checks with the Android version, but can not access this.device.
|
|
|
|
*/
|
2015-04-12 00:11:30 -07:00
|
|
|
public boolean isEnabledByDefault() {
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-19 19:57:29 +02:00
|
|
|
|
2014-09-16 15:45:31 +02:00
|
|
|
/**
|
|
|
|
* Return true if this plugin needs an specific UI settings.
|
|
|
|
*/
|
2015-04-12 00:11:30 -07:00
|
|
|
public boolean hasSettings() {
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-16 15:45:31 +02:00
|
|
|
|
2020-03-20 20:30:58 +00:00
|
|
|
/**
|
|
|
|
* Called to find out if a plugin supports device specific settings.
|
|
|
|
* If you return true your PluginSettingsFragment will use the device
|
|
|
|
* specific SharedPreferences to store the settings.
|
|
|
|
*
|
|
|
|
* @return true if this plugin supports device specific settings
|
|
|
|
*/
|
|
|
|
public boolean supportsDeviceSpecificSettings() { return false; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when it's time to move the plugin settings from the global preferences
|
|
|
|
* to device specific preferences
|
|
|
|
*
|
|
|
|
* @param globalSharedPreferences The global Preferences to copy the settings from
|
|
|
|
*/
|
|
|
|
public void copyGlobalToDeviceSpecificSettings(SharedPreferences globalSharedPreferences) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the plugin should remove it's settings from the provided ShardPreferences
|
|
|
|
*
|
|
|
|
* @param sharedPreferences The SharedPreferences to remove the settings from
|
|
|
|
*/
|
|
|
|
public void removeSettings(SharedPreferences sharedPreferences) {}
|
|
|
|
|
2015-01-10 00:31:07 -08:00
|
|
|
/**
|
|
|
|
* If hasSettings returns true, this will be called when the user
|
2019-01-06 12:26:05 +01:00
|
|
|
* wants to access this plugin's preferences. The default implementation
|
|
|
|
* will return a PluginSettingsFragment with content from "yourplugin"_preferences.xml
|
|
|
|
*
|
|
|
|
* @return The PluginSettingsFragment used to display this plugins settings
|
2015-01-10 00:31:07 -08:00
|
|
|
*/
|
2019-02-22 20:08:21 +01:00
|
|
|
public PluginSettingsFragment getSettingsFragment(Activity activity) {
|
2019-01-06 12:26:05 +01:00
|
|
|
return PluginSettingsFragment.newInstance(getPluginKey());
|
2015-01-10 00:31:07 -08:00
|
|
|
}
|
|
|
|
|
2015-04-12 00:11:30 -07:00
|
|
|
/**
|
|
|
|
* Return true if the plugin should display something in the Device main view
|
|
|
|
*/
|
|
|
|
public boolean hasMainActivity() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement here what your plugin should do when clicked
|
|
|
|
*/
|
2018-03-03 16:06:52 +01:00
|
|
|
public void startMainActivity(Activity parentActivity) {
|
|
|
|
}
|
2015-04-12 00:11:30 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if the entry for this app should appear in the context menu instead of the main view
|
|
|
|
*/
|
|
|
|
public boolean displayInContextMenu() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
/**
|
|
|
|
* Initialize the listeners and structures in your plugin.
|
|
|
|
* Should return true if initialization was successful.
|
|
|
|
*/
|
2015-04-12 00:11:30 -07:00
|
|
|
public boolean onCreate() {
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-19 19:57:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Finish any ongoing operations, remove listeners... so
|
2013-09-03 17:58:59 +02:00
|
|
|
* this object could be garbage collected.
|
2013-08-19 19:57:29 +02:00
|
|
|
*/
|
2018-03-03 16:06:52 +01:00
|
|
|
public void onDestroy() {
|
|
|
|
}
|
2013-08-19 19:57:29 +02:00
|
|
|
|
|
|
|
/**
|
2016-12-11 16:40:11 +01:00
|
|
|
* Called when a plugin receives a package. By convention we return true
|
|
|
|
* when we have done something in response to the package or false
|
|
|
|
* otherwise, even though that value is unused as of now.
|
2013-08-19 19:57:29 +02:00
|
|
|
*/
|
2018-03-04 11:31:37 +01:00
|
|
|
public boolean onPacketReceived(NetworkPacket np) {
|
2018-03-03 16:06:52 +01:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-16 10:31:01 +02:00
|
|
|
|
2015-09-08 14:54:04 -07:00
|
|
|
/**
|
2018-03-04 11:31:37 +01:00
|
|
|
* Should return the list of NetworkPacket types that this plugin can handle
|
2015-09-08 14:54:04 -07:00
|
|
|
*/
|
2018-03-04 11:31:37 +01:00
|
|
|
public abstract String[] getSupportedPacketTypes();
|
2015-09-08 14:54:04 -07:00
|
|
|
|
|
|
|
/**
|
2018-03-04 11:31:37 +01:00
|
|
|
* Should return the list of NetworkPacket types that this plugin can send
|
2015-09-08 14:54:04 -07:00
|
|
|
*/
|
2018-03-04 11:31:37 +01:00
|
|
|
public abstract String[] getOutgoingPacketTypes();
|
2015-09-08 14:54:04 -07:00
|
|
|
|
2018-10-26 23:53:58 +02:00
|
|
|
protected String[] getRequiredPermissions() {
|
2020-07-12 13:37:24 +05:30
|
|
|
return ArrayUtils.EMPTY_STRING_ARRAY;
|
2017-05-31 15:51:07 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 23:53:58 +02:00
|
|
|
protected String[] getOptionalPermissions() {
|
2020-07-12 13:37:24 +05:30
|
|
|
return ArrayUtils.EMPTY_STRING_ARRAY;
|
2017-05-31 15:51:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Permission from Manifest.permission.*
|
|
|
|
protected boolean isPermissionGranted(String permission) {
|
|
|
|
int result = ContextCompat.checkSelfPermission(context, permission);
|
|
|
|
return (result == PackageManager.PERMISSION_GRANTED);
|
|
|
|
}
|
|
|
|
|
2018-10-26 23:53:58 +02:00
|
|
|
private boolean arePermissionsGranted(String[] permissions) {
|
2018-03-03 16:06:52 +01:00
|
|
|
for (String permission : permissions) {
|
|
|
|
if (!isPermissionGranted(permission)) {
|
2017-05-31 15:51:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-14 22:57:48 +01:00
|
|
|
private PermissionsAlertDialogFragment requestPermissionDialog(final String[] permissions, @StringRes int reason) {
|
2019-02-02 11:38:13 +00:00
|
|
|
return new PermissionsAlertDialogFragment.Builder()
|
2017-05-31 15:51:07 +02:00
|
|
|
.setTitle(getDisplayName())
|
|
|
|
.setMessage(reason)
|
2019-02-02 11:38:13 +00:00
|
|
|
.setPositiveButton(R.string.ok)
|
|
|
|
.setNegativeButton(R.string.cancel)
|
|
|
|
.setPermissions(permissions)
|
2020-01-14 22:57:48 +01:00
|
|
|
.setRequestCode(MainActivity.RESULT_NEEDS_RELOAD)
|
2017-05-31 15:51:07 +02:00
|
|
|
.create();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If onCreate returns false, should create a dialog explaining
|
|
|
|
* the problem (and how to fix it, if possible) to the user.
|
|
|
|
*/
|
|
|
|
|
2020-01-14 22:57:48 +01:00
|
|
|
public DialogFragment getPermissionExplanationDialog() {
|
|
|
|
return requestPermissionDialog(getRequiredPermissions(), permissionExplanation);
|
2017-05-31 15:51:07 +02:00
|
|
|
}
|
|
|
|
|
2020-01-14 22:57:48 +01:00
|
|
|
public AlertDialogFragment getOptionalPermissionExplanationDialog() {
|
|
|
|
return requestPermissionDialog(getOptionalPermissions(), optionalPermissionExplanation);
|
2017-07-11 13:50:40 +02:00
|
|
|
}
|
|
|
|
|
2018-03-03 16:06:52 +01:00
|
|
|
public boolean checkRequiredPermissions() {
|
2017-07-11 13:50:40 +02:00
|
|
|
return arePermissionsGranted(getRequiredPermissions());
|
|
|
|
}
|
|
|
|
|
2018-03-03 16:06:52 +01:00
|
|
|
public boolean checkOptionalPermissions() {
|
|
|
|
return arePermissionsGranted(getOptionalPermissions());
|
2017-05-31 15:51:07 +02:00
|
|
|
}
|
|
|
|
|
2018-01-03 20:49:51 +01:00
|
|
|
public int getMinSdk() {
|
|
|
|
return Build.VERSION_CODES.BASE;
|
|
|
|
}
|
2013-08-16 10:31:01 +02:00
|
|
|
}
|