Skip to content

Commit 4a09189

Browse files
committed
Deleting old code and adding checks.
1 parent 8957223 commit 4a09189

4 files changed

Lines changed: 68 additions & 148 deletions

File tree

-321 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
3-
package="plugin.library">
3+
package="plugin.pasteboard">
44
<application>
55
</application>
66
</manifest>

src/android/plugin/src/main/java/plugin/pasteboard/ClipboardListener.java

Lines changed: 66 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -8,157 +8,77 @@
88
// Package name
99
package plugin.pasteboard;
1010

11-
// Java Imports
12-
import java.util.*;
13-
import java.lang.*;
14-
15-
// Android Imports
1611
import android.content.Context;
1712

18-
// Corona Imports
1913
import com.ansca.corona.CoronaActivity;
2014
import com.ansca.corona.CoronaEnvironment;
2115

2216

2317
// Clipboard listener class
24-
public class ClipboardListener
25-
{
26-
// Private vars
27-
private Timer timer;
28-
29-
// Functions for API Level 11 and above
30-
private static class ApiLevel11
31-
{
32-
/** Constructor made private to prevent instances from being made. */
33-
private ApiLevel11() { }
34-
35-
// Private vars
36-
private static android.content.ClipboardManager clipboardManager;
37-
private static android.content.ClipboardManager.OnPrimaryClipChangedListener primaryClipChangedListener;
38-
39-
// Function to add the clipChanged listener
40-
public static boolean addClipChangedListener( final Context context )
41-
{
42-
// Verify environment
43-
CoronaActivity coronaActivity = CoronaEnvironment.getCoronaActivity();
44-
if ( coronaActivity == null ) { return false; }
45-
46-
// Create a new runnable object to invoke our activity
47-
Runnable activityRunnable = new Runnable()
48-
{
49-
public void run()
50-
{
51-
// Grab the initial clipboard contents and put them in pasteboard, if any.
52-
setNewPasteboardItem( context );
53-
54-
// Clip changed listener
55-
primaryClipChangedListener = new android.content.ClipboardManager.OnPrimaryClipChangedListener()
56-
{
57-
public void onPrimaryClipChanged()
58-
{
59-
setNewPasteboardItem( context );
60-
}
61-
};
62-
63-
// Add the clip listener
64-
clipboardManager.addPrimaryClipChangedListener( primaryClipChangedListener );
65-
}
66-
};
67-
68-
// Run the activity on the uiThread
69-
coronaActivity.runOnUiThread( activityRunnable );
70-
71-
return true;
72-
}
73-
74-
// Function to remove the clipChanged listener
75-
public static boolean removeClipChangedListener()
76-
{
77-
// Remove the clip listener
78-
clipboardManager.removePrimaryClipChangedListener( primaryClipChangedListener );
79-
return true;
80-
}
81-
82-
// Function to set a new pasteboard item.
83-
// Can be called from lua thread or UI thread.
84-
private static void setNewPasteboardItem( Context context ) {
85-
86-
// Assign the clipboard manager
87-
clipboardManager = ( android.content.ClipboardManager )context.getSystemService( context.CLIPBOARD_SERVICE );
88-
89-
// If the Clipboard contains data
90-
if ( clipboardManager.hasPrimaryClip() )
91-
{
92-
// Get the primary clip
93-
android.content.ClipData clipData = clipboardManager.getPrimaryClip();
94-
95-
// Set the clipdata item
96-
android.content.ClipData.Item item = clipData.getItemAt( 0 );
97-
98-
// Set the currentPasteboard item to the new text.
99-
shared.setCurrentPasteboardItem(shared.ApiLevel11.coerceToString(context, item));
100-
}
101-
}
102-
}
103-
104-
105-
106-
// Function to add the clipboard listener
107-
public boolean addClipChangedListener()
108-
{
109-
// Verify environment
110-
Context context = CoronaEnvironment.getApplicationContext();
111-
if ( context == null ) { return false; }
112-
113-
// Api levels above or equal to 11
114-
if ( android.os.Build.VERSION.SDK_INT >= 11 )
115-
{
116-
ApiLevel11.addClipChangedListener( context );
117-
}
118-
// Api's older than 11
119-
else
120-
{
121-
// Create a new timer
122-
timer = new Timer();
123-
// Setup a Clipboard manager instance
124-
final android.text.ClipboardManager clipboardManager;
125-
clipboardManager = ( android.text.ClipboardManager )context.getSystemService( Context.CLIPBOARD_SERVICE );
126-
// Set the currentPasteboard item to the new text
127-
shared.setCurrentPasteboardItem(clipboardManager.getText().toString());
128-
129-
// Start the timer
130-
timer.scheduleAtFixedRate( new java.util.TimerTask()
131-
{
132-
@Override
133-
public void run()
134-
{
135-
// Set the currentPasteboard item to the new text
136-
shared.setCurrentPasteboardItem(clipboardManager.getText().toString());
137-
}
138-
}, 0, 100 );
139-
}
140-
141-
return true;
142-
}
143-
144-
// Function to remove the clipboard listener
145-
public boolean removeClipChangedListener()
146-
{
147-
// Verify environment
148-
Context context = CoronaEnvironment.getApplicationContext();
149-
if ( context == null ) { return false; }
150-
151-
// Api levels above or equal to 11
152-
if ( android.os.Build.VERSION.SDK_INT >= 11 )
153-
{
154-
ApiLevel11.removeClipChangedListener();
155-
}
156-
// Api's older than 11
157-
else
158-
{
159-
timer.cancel();
160-
}
161-
162-
return true;
163-
}
18+
public class ClipboardListener {
19+
private android.content.ClipboardManager clipboardManager;
20+
private android.content.ClipboardManager.OnPrimaryClipChangedListener primaryClipChangedListener;
21+
22+
private void setNewPasteboardItem(Context context) {
23+
24+
if (clipboardManager == null) {
25+
clipboardManager = (android.content.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
26+
}
27+
28+
// If the Clipboard contains data, save it to shared
29+
if (clipboardManager.hasPrimaryClip()) {
30+
android.content.ClipData clipData = clipboardManager.getPrimaryClip();
31+
android.content.ClipData.Item item = clipData.getItemAt(0);
32+
shared.setCurrentPasteboardItem(shared.ApiLevel11.coerceToString(context, item));
33+
}
34+
}
35+
36+
37+
// Function to add the clipboard listener
38+
public void addClipChangedListener() {
39+
// Verify environment
40+
final Context context = CoronaEnvironment.getApplicationContext();
41+
if (context == null) return;
42+
43+
if (clipboardManager == null) {
44+
clipboardManager = (android.content.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
45+
}
46+
if (clipboardManager == null) return;
47+
48+
// Verify environment
49+
CoronaActivity coronaActivity = CoronaEnvironment.getCoronaActivity();
50+
if (coronaActivity == null) return;
51+
52+
if (primaryClipChangedListener == null) {
53+
primaryClipChangedListener = new android.content.ClipboardManager.OnPrimaryClipChangedListener() {
54+
public void onPrimaryClipChanged() {
55+
setNewPasteboardItem(context);
56+
}
57+
};
58+
}
59+
60+
coronaActivity.runOnUiThread(new Runnable() {
61+
public void run() {
62+
// Grab the initial clipboard contents and put them in pasteboard, if any.
63+
setNewPasteboardItem(context);
64+
clipboardManager.addPrimaryClipChangedListener(primaryClipChangedListener);
65+
}
66+
});
67+
}
68+
69+
// Function to remove the clipboard listener
70+
public void removeClipChangedListener() {
71+
// Verify environment
72+
Context context = CoronaEnvironment.getApplicationContext();
73+
if (context == null) return;
74+
75+
// Remove the clip listener
76+
if (clipboardManager == null || primaryClipChangedListener == null) {
77+
return;
78+
}
79+
try {
80+
clipboardManager.removePrimaryClipChangedListener(primaryClipChangedListener);
81+
} catch (Throwable ignore) {
82+
}
83+
}
16484
}

src/android/settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
rootProject.name = "Corona Native Android"
1+
rootProject.name = "Pasteboard plugin"
22
include("app", "plugin")

0 commit comments

Comments
 (0)