mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-29 13:17:43 +00:00
Fixed clipboard propagation loop bug
Now ClipboardListener is a singleton BUG: 362596
This commit is contained in:
parent
901440f78a
commit
62bf2ccab7
@ -28,12 +28,15 @@ import android.os.Build;
|
|||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
|
|
||||||
import org.kde.kdeconnect.Device;
|
import java.util.HashSet;
|
||||||
import org.kde.kdeconnect.NetworkPackage;
|
|
||||||
|
|
||||||
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
||||||
public class ClipboardListener {
|
public class ClipboardListener {
|
||||||
|
|
||||||
|
public interface ClipboardObserver {
|
||||||
|
void clipboardChanged(String content);
|
||||||
|
}
|
||||||
|
private HashSet<ClipboardObserver> observers = new HashSet<>();
|
||||||
|
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private String currentContent;
|
private String currentContent;
|
||||||
@ -41,9 +44,26 @@ public class ClipboardListener {
|
|||||||
private ClipboardManager cm = null;
|
private ClipboardManager cm = null;
|
||||||
private ClipboardManager.OnPrimaryClipChangedListener listener;
|
private ClipboardManager.OnPrimaryClipChangedListener listener;
|
||||||
|
|
||||||
ClipboardListener(final Context ctx, final Device device) {
|
private static ClipboardListener _instance = null;
|
||||||
|
public static ClipboardListener instance(Context context) {
|
||||||
|
if (_instance == null) {
|
||||||
|
_instance = new ClipboardListener(context);
|
||||||
|
}
|
||||||
|
return _instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerObserver(ClipboardObserver observer) {
|
||||||
|
observers.add(observer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeObserver(ClipboardObserver observer) {
|
||||||
|
observers.remove(observer);
|
||||||
|
}
|
||||||
|
|
||||||
|
ClipboardListener(final Context ctx) {
|
||||||
context = ctx;
|
context = ctx;
|
||||||
if(android.os.Build.VERSION.SDK_INT < 11) {
|
|
||||||
|
if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,11 +79,14 @@ public class ClipboardListener {
|
|||||||
ClipData.Item item = cm.getPrimaryClip().getItemAt(0);
|
ClipData.Item item = cm.getPrimaryClip().getItemAt(0);
|
||||||
String content = item.coerceToText(context).toString();
|
String content = item.coerceToText(context).toString();
|
||||||
|
|
||||||
if (!content.equals(currentContent)) {
|
if (content.equals(currentContent)) {
|
||||||
NetworkPackage np = new NetworkPackage(ClipboardPlugin.PACKAGE_TYPE_CLIPBOARD);
|
return;
|
||||||
np.set("content", content);
|
}
|
||||||
device.sendPackage(np);
|
|
||||||
currentContent = content;
|
currentContent = content;
|
||||||
|
|
||||||
|
for (ClipboardObserver observer : observers) {
|
||||||
|
observer.clipboardChanged(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -76,18 +99,10 @@ public class ClipboardListener {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
|
||||||
if(android.os.Build.VERSION.SDK_INT < 11) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
cm.removePrimaryClipChangedListener(listener);
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
public void setText(String text) {
|
public void setText(String text) {
|
||||||
currentContent = text;
|
currentContent = text;
|
||||||
if(android.os.Build.VERSION.SDK_INT < 11) {
|
if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
|
||||||
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
|
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||||
clipboard.setText(text);
|
clipboard.setText(text);
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,8 @@
|
|||||||
|
|
||||||
package org.kde.kdeconnect.Plugins.ClibpoardPlugin;
|
package org.kde.kdeconnect.Plugins.ClibpoardPlugin;
|
||||||
|
|
||||||
|
import android.os.Build;
|
||||||
|
|
||||||
import org.kde.kdeconnect.NetworkPackage;
|
import org.kde.kdeconnect.NetworkPackage;
|
||||||
import org.kde.kdeconnect.Plugins.Plugin;
|
import org.kde.kdeconnect.Plugins.Plugin;
|
||||||
import org.kde.kdeconnect_tp.R;
|
import org.kde.kdeconnect_tp.R;
|
||||||
@ -41,28 +43,34 @@ public class ClipboardPlugin extends Plugin {
|
|||||||
@Override
|
@Override
|
||||||
public boolean isEnabledByDefault() {
|
public boolean isEnabledByDefault() {
|
||||||
//Disabled by default due to just one direction sync(incoming clipboard change) in early version of android.
|
//Disabled by default due to just one direction sync(incoming clipboard change) in early version of android.
|
||||||
return (android.os.Build.VERSION.SDK_INT >= 11);
|
return (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ClipboardListener listener;
|
@Override
|
||||||
|
public boolean onPackageReceived(NetworkPackage np) {
|
||||||
|
String content = np.getString("content");
|
||||||
|
ClipboardListener.instance(context).setText(content);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ClipboardListener.ClipboardObserver observer = new ClipboardListener.ClipboardObserver() {
|
||||||
|
@Override
|
||||||
|
public void clipboardChanged(String content) {
|
||||||
|
NetworkPackage np = new NetworkPackage(ClipboardPlugin.PACKAGE_TYPE_CLIPBOARD);
|
||||||
|
np.set("content", content);
|
||||||
|
device.sendPackage(np);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreate() {
|
public boolean onCreate() {
|
||||||
listener = new ClipboardListener(context, device);
|
ClipboardListener.instance(context).registerObserver(observer);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
listener.stop();
|
ClipboardListener.instance(context).removeObserver(observer);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onPackageReceived(NetworkPackage np) {
|
|
||||||
|
|
||||||
String content = np.getString("content");
|
|
||||||
listener.setText(content);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user