From 1d25d2ff40e7db531d713ea41efbcb84555fc2a6 Mon Sep 17 00:00:00 2001 From: cketti Date: Sun, 1 Apr 2012 21:08:31 +0200 Subject: [PATCH] Added helper class to use the most recent ClipboardManager --- src/com/fsck/k9/helper/ClipboardManager.java | 63 +++++++++++++++++++ .../fsck/k9/helper/ClipboardManagerApi1.java | 22 +++++++ .../fsck/k9/helper/ClipboardManagerApi11.java | 23 +++++++ 3 files changed, 108 insertions(+) create mode 100644 src/com/fsck/k9/helper/ClipboardManager.java create mode 100644 src/com/fsck/k9/helper/ClipboardManagerApi1.java create mode 100644 src/com/fsck/k9/helper/ClipboardManagerApi11.java diff --git a/src/com/fsck/k9/helper/ClipboardManager.java b/src/com/fsck/k9/helper/ClipboardManager.java new file mode 100644 index 000000000..7751b3b89 --- /dev/null +++ b/src/com/fsck/k9/helper/ClipboardManager.java @@ -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); +} diff --git a/src/com/fsck/k9/helper/ClipboardManagerApi1.java b/src/com/fsck/k9/helper/ClipboardManagerApi1.java new file mode 100644 index 000000000..80bfe714c --- /dev/null +++ b/src/com/fsck/k9/helper/ClipboardManagerApi1.java @@ -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); + } +} diff --git a/src/com/fsck/k9/helper/ClipboardManagerApi11.java b/src/com/fsck/k9/helper/ClipboardManagerApi11.java new file mode 100644 index 000000000..cae25fa0f --- /dev/null +++ b/src/com/fsck/k9/helper/ClipboardManagerApi11.java @@ -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); + } +}