mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-28 04:37:40 +00:00
Moved non-retrocompatible clipboard code to a new class so it wont crash
This commit is contained in:
parent
436e3f9335
commit
4d1608d68e
@ -0,0 +1,52 @@
|
|||||||
|
package org.kde.kdeconnect.Plugins.ClibpoardPlugin;
|
||||||
|
|
||||||
|
import android.content.ClipData;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.ClipboardManager;
|
||||||
|
|
||||||
|
import org.kde.kdeconnect.Device;
|
||||||
|
import org.kde.kdeconnect.NetworkPackage;
|
||||||
|
|
||||||
|
public class ClipboardListener {
|
||||||
|
|
||||||
|
|
||||||
|
private String currentContent;
|
||||||
|
|
||||||
|
private ClipboardManager cm = null;
|
||||||
|
ClipboardManager.OnPrimaryClipChangedListener listener;
|
||||||
|
|
||||||
|
ClipboardListener(final Context context, final Device device) {
|
||||||
|
cm = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||||
|
listener = new ClipboardManager.OnPrimaryClipChangedListener() {
|
||||||
|
@Override
|
||||||
|
public void onPrimaryClipChanged() {
|
||||||
|
try {
|
||||||
|
|
||||||
|
ClipData.Item item = cm.getPrimaryClip().getItemAt(0);
|
||||||
|
String content = item.coerceToText(context).toString();
|
||||||
|
|
||||||
|
if (!content.equals(currentContent)) {
|
||||||
|
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_CLIPBOARD);
|
||||||
|
np.set("content", content);
|
||||||
|
device.sendPackage(np);
|
||||||
|
currentContent = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch(Exception e) {
|
||||||
|
//Probably clipboard was not text
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
cm.addPrimaryClipChangedListener(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
cm.removePrimaryClipChangedListener(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setText(String text) {
|
||||||
|
currentContent = text;
|
||||||
|
cm.setText(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -2,8 +2,6 @@ package org.kde.kdeconnect.Plugins.ClibpoardPlugin;
|
|||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.content.ClipData;
|
|
||||||
import android.content.ClipboardManager;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
@ -16,8 +14,6 @@ import org.kde.kdeconnect_tp.R;
|
|||||||
|
|
||||||
public class ClipboardPlugin extends Plugin {
|
public class ClipboardPlugin extends Plugin {
|
||||||
|
|
||||||
private String currentContent;
|
|
||||||
|
|
||||||
/*static {
|
/*static {
|
||||||
PluginFactory.registerPlugin(ClipboardPlugin.class);
|
PluginFactory.registerPlugin(ClipboardPlugin.class);
|
||||||
}*/
|
}*/
|
||||||
@ -46,38 +42,16 @@ public class ClipboardPlugin extends Plugin {
|
|||||||
return (Build.VERSION.SDK_INT >= 11);
|
return (Build.VERSION.SDK_INT >= 11);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ClipboardManager cm;
|
private ClipboardListener listener;
|
||||||
private ClipboardManager.OnPrimaryClipChangedListener listener = new ClipboardManager.OnPrimaryClipChangedListener() {
|
|
||||||
@Override
|
|
||||||
public void onPrimaryClipChanged() {
|
|
||||||
try {
|
|
||||||
|
|
||||||
ClipData.Item item = cm.getPrimaryClip().getItemAt(0);
|
|
||||||
String content = item.coerceToText(context).toString();
|
|
||||||
|
|
||||||
if (!content.equals(currentContent)) {
|
|
||||||
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_CLIPBOARD);
|
|
||||||
np.set("content", content);
|
|
||||||
device.sendPackage(np);
|
|
||||||
currentContent = content;
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch(Exception e) {
|
|
||||||
//Probably clipboard was not text
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreate() {
|
public boolean onCreate() {
|
||||||
|
|
||||||
cm = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
|
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT < 11) {
|
if (Build.VERSION.SDK_INT < 11) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
cm.addPrimaryClipChangedListener(listener);
|
listener = new ClipboardListener(context, device);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
@ -86,7 +60,9 @@ public class ClipboardPlugin extends Plugin {
|
|||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
|
|
||||||
cm.removePrimaryClipChangedListener(listener);
|
if (Build.VERSION.SDK_INT < 11) return;
|
||||||
|
|
||||||
|
listener.stop();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,8 +73,8 @@ public class ClipboardPlugin extends Plugin {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
currentContent = np.getString("content");
|
String content = np.getString("content");
|
||||||
cm.setText(currentContent);
|
listener.setText(content);
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user