1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-12-24 08:38:51 -05:00

Added helper class to use the most recent ClipboardManager

This commit is contained in:
cketti 2012-04-01 21:08:31 +02:00
parent 240f7ea9ac
commit 1d25d2ff40
3 changed files with 108 additions and 0 deletions

View File

@ -0,0 +1,63 @@
package com.fsck.k9.helper;
import android.content.Context;
import android.os.Build;
/**
* Helper class to access the system clipboard
*
* @see ClipboardManagerApi1
* @see ClipboardManagerApi11
*/
public abstract class ClipboardManager {
/**
* Instance of the API-specific class that interfaces with the clipboard API.
*/
private static ClipboardManager sInstance = null;
/**
* Get API-specific instance of the {@code ClipboardManager} class
*
* @param context
* A {@link Context} instance.
*
* @return Appropriate {@link ClipboardManager} instance for this device.
*/
public static ClipboardManager getInstance(Context context) {
Context appContext = context.getApplicationContext();
if (sInstance == null) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
sInstance = new ClipboardManagerApi1(appContext);
} else {
sInstance = new ClipboardManagerApi11(appContext);
}
}
return sInstance;
}
protected Context mContext;
/**
* Constructor
*
* @param context
* A {@link Context} instance.
*/
protected ClipboardManager(Context context) {
mContext = context;
}
/**
* Copy a text string to the system clipboard
*
* @param label
* User-visible label for the content.
* @param text
* The actual text to be copied to the clipboard.
*/
public abstract void setText(String label, String text);
}

View File

@ -0,0 +1,22 @@
package com.fsck.k9.helper;
import android.content.Context;
import android.text.ClipboardManager;
/**
* Access the system clipboard using the now deprecated {@link ClipboardManager}
*/
@SuppressWarnings("deprecation")
public class ClipboardManagerApi1 extends com.fsck.k9.helper.ClipboardManager {
public ClipboardManagerApi1(Context context) {
super(context);
}
@Override
public void setText(String label, String text) {
ClipboardManager clipboardManager =
(ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setText(text);
}
}

View File

@ -0,0 +1,23 @@
package com.fsck.k9.helper;
import android.content.ClipData;
import android.content.Context;
import android.content.ClipboardManager;
/**
* Access the system clipboard using the new {@link ClipboardManager} introduced with API 11
*/
public class ClipboardManagerApi11 extends com.fsck.k9.helper.ClipboardManager {
public ClipboardManagerApi11(Context context) {
super(context);
}
@Override
public void setText(String label, String text) {
ClipboardManager clipboardManager =
(ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
clipboardManager.setPrimaryClip(clip);
}
}