mobile/android/base/util/Clipboard.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/util/Clipboard.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,138 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.util;
     1.9 +
    1.10 +import android.content.ClipData;
    1.11 +import android.content.Context;
    1.12 +import android.os.Build;
    1.13 +import android.util.Log;
    1.14 +
    1.15 +import org.mozilla.gecko.mozglue.generatorannotations.WrapElementForJNI;
    1.16 +
    1.17 +import java.util.concurrent.SynchronousQueue;
    1.18 +
    1.19 +public final class Clipboard {
    1.20 +    private static Context mContext;
    1.21 +    private final static String LOGTAG = "GeckoClipboard";
    1.22 +    private final static SynchronousQueue<String> sClipboardQueue = new SynchronousQueue<String>();
    1.23 +
    1.24 +    private Clipboard() {
    1.25 +    }
    1.26 +
    1.27 +    public static void init(final Context c) {
    1.28 +        if (mContext != null) {
    1.29 +            Log.w(LOGTAG, "Clipboard.init() called twice!");
    1.30 +            return;
    1.31 +        }
    1.32 +        mContext = c.getApplicationContext();
    1.33 +    }
    1.34 +
    1.35 +    @WrapElementForJNI(stubName = "GetClipboardTextWrapper")
    1.36 +    public static String getText() {
    1.37 +        // If we're on the UI thread or the background thread, we have a looper on the thread
    1.38 +        // and can just call this directly. For any other threads, post the call to the
    1.39 +        // background thread.
    1.40 +
    1.41 +        if (ThreadUtils.isOnUiThread() || ThreadUtils.isOnBackgroundThread()) {
    1.42 +            return getClipboardTextImpl();
    1.43 +        }
    1.44 +
    1.45 +        ThreadUtils.postToBackgroundThread(new Runnable() {
    1.46 +            @Override
    1.47 +            public void run() {
    1.48 +                String text = getClipboardTextImpl();
    1.49 +                try {
    1.50 +                    sClipboardQueue.put(text != null ? text : "");
    1.51 +                } catch (InterruptedException ie) {}
    1.52 +            }
    1.53 +        });
    1.54 +        try {
    1.55 +            return sClipboardQueue.take();
    1.56 +        } catch (InterruptedException ie) {
    1.57 +            return "";
    1.58 +        }
    1.59 +    }
    1.60 +
    1.61 +    @WrapElementForJNI(stubName = "SetClipboardText")
    1.62 +    public static void setText(final CharSequence text) {
    1.63 +        ThreadUtils.postToBackgroundThread(new Runnable() {
    1.64 +            @Override
    1.65 +            @SuppressWarnings("deprecation")
    1.66 +            public void run() {
    1.67 +                if (Build.VERSION.SDK_INT >= 11) {
    1.68 +                    android.content.ClipboardManager cm = getClipboardManager(mContext);
    1.69 +                    ClipData clip = ClipData.newPlainText("Text", text);
    1.70 +                    try {
    1.71 +                        cm.setPrimaryClip(clip);
    1.72 +                    } catch (NullPointerException e) {
    1.73 +                        // Bug 776223: This is a Samsung clipboard bug. setPrimaryClip() can throw
    1.74 +                        // a NullPointerException if Samsung's /data/clipboard directory is full.
    1.75 +                        // Fortunately, the text is still successfully copied to the clipboard.
    1.76 +                    }
    1.77 +                } else {
    1.78 +                    android.text.ClipboardManager cm = getDeprecatedClipboardManager(mContext);
    1.79 +                    cm.setText(text);
    1.80 +                }
    1.81 +            }
    1.82 +        });
    1.83 +    }
    1.84 +
    1.85 +    /**
    1.86 +     * Returns true if the clipboard is nonempty, false otherwise.
    1.87 +     *
    1.88 +     * @return true if the clipboard is nonempty, false otherwise.
    1.89 +     */
    1.90 +    @WrapElementForJNI
    1.91 +    public static boolean hasText() {
    1.92 +        if (Build.VERSION.SDK_INT >= 11) {
    1.93 +            android.content.ClipboardManager cm = getClipboardManager(mContext);
    1.94 +            return cm.hasPrimaryClip();
    1.95 +        }
    1.96 +
    1.97 +        android.text.ClipboardManager cm = getDeprecatedClipboardManager(mContext);
    1.98 +        return cm.hasText();
    1.99 +    }
   1.100 +
   1.101 +    /**
   1.102 +     * Deletes all text from the clipboard.
   1.103 +     */
   1.104 +    @WrapElementForJNI
   1.105 +    public static void clearText() {
   1.106 +        setText(null);
   1.107 +    }
   1.108 +
   1.109 +    private static android.content.ClipboardManager getClipboardManager(Context context) {
   1.110 +        // In API Level 11 and above, CLIPBOARD_SERVICE returns android.content.ClipboardManager,
   1.111 +        // which is a subclass of android.text.ClipboardManager.
   1.112 +        return (android.content.ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
   1.113 +    }
   1.114 +
   1.115 +    private static android.text.ClipboardManager getDeprecatedClipboardManager(Context context) {
   1.116 +        return (android.text.ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
   1.117 +    }
   1.118 +
   1.119 +    /* On some devices, access to the clipboard service needs to happen
   1.120 +     * on a thread with a looper, so this function requires a looper is
   1.121 +     * present on the thread. */
   1.122 +    @SuppressWarnings("deprecation")
   1.123 +    private static String getClipboardTextImpl() {
   1.124 +        if (Build.VERSION.SDK_INT >= 11) {
   1.125 +            android.content.ClipboardManager cm = getClipboardManager(mContext);
   1.126 +            if (cm.hasPrimaryClip()) {
   1.127 +                ClipData clip = cm.getPrimaryClip();
   1.128 +                if (clip != null) {
   1.129 +                    ClipData.Item item = clip.getItemAt(0);
   1.130 +                    return item.coerceToText(mContext).toString();
   1.131 +                }
   1.132 +            }
   1.133 +        } else {
   1.134 +            android.text.ClipboardManager cm = getDeprecatedClipboardManager(mContext);
   1.135 +            if (cm.hasText()) {
   1.136 +                return cm.getText().toString();
   1.137 +            }
   1.138 +        }
   1.139 +        return null;
   1.140 +    }
   1.141 +}

mercurial