Created
February 8, 2019 15:20
-
-
Save krisai/8e289a55e25e5e46930fde80b34a2ed5 to your computer and use it in GitHub Desktop.
Permanently listen to Clipboard changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.Calendar; | |
import android.app.Service; | |
import android.content.ClipData; | |
import android.content.ClipDescription; | |
import android.content.ClipboardManager; | |
import android.content.ClipboardManager.OnPrimaryClipChangedListener; | |
import android.content.Intent; | |
import android.os.IBinder; | |
public class CBWatcherService extends Service { | |
private final String tag = "[[ClipboardWatcherService]] "; | |
private OnPrimaryClipChangedListener listener = new OnPrimaryClipChangedListener() { | |
public void onPrimaryClipChanged() { | |
performClipboardCheck(); | |
} | |
}; | |
@Override | |
public void onCreate() { | |
((ClipboardManager) getSystemService(CLIPBOARD_SERVICE)).addPrimaryClipChangedListener(listener); | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
File folder = new File(ClipboardCacheFolderPath); | |
// ClipboardCacheFolderPath is a predefined constant with the path | |
// where the clipboard contents will be written | |
if (!folder.exists()) { folder.mkdir(); } | |
return START_STICKY; | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
return null; | |
} | |
private void performClipboardCheck() { | |
ClipboardManager cb = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); | |
if (cb.hasPrimaryClip()) { | |
ClipData cd = cb.getPrimaryClip(); | |
if (cd.getDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) { | |
try { | |
File folder = new File(ClipboardCacheFolderPath); | |
if (!folder.exists()) { folder.mkdir(); } | |
Calendar cal = Calendar.getInstance(); | |
String newCachedClip = | |
cal.get(Calendar.YEAR) + "-" + | |
cal.get(Calendar.MONTH) + "-" + | |
cal.get(Calendar.DAY_OF_MONTH) + "-" + | |
cal.get(Calendar.HOUR_OF_DAY) + "-" + | |
cal.get(Calendar.MINUTE) + "-" + | |
cal.get(Calendar.SECOND); | |
// The name of the file acts as the timestamp (ingenious, uh?) | |
File file = new File(ClipboardCacheFolderPath + newCachedClip); | |
file.createNewFile(); | |
BufferedWriter bWriter = new BufferedWriter(new FileWriter(file)); | |
bWriter.write((cd.getItemAt(0).getText()).toString()); | |
bWriter.close(); | |
} | |
catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment