diff --git a/mobile/android/base/util/Clipboard.java b/mobile/android/base/util/Clipboard.java index 9182458fc383..ca1548398540 100644 --- a/mobile/android/base/util/Clipboard.java +++ b/mobile/android/base/util/Clipboard.java @@ -14,7 +14,10 @@ import android.content.Context; import android.util.Log; public final class Clipboard { - private static Context mContext; + // Volatile but not synchronized: we don't care about the race condition in + // init, because both app contexts will be the same, but we do care about a + // thread having a stale null value of mContext. + /* inner-access */ volatile static Context mContext; private final static String LOGTAG = "GeckoClipboard"; private final static SynchronousQueue sClipboardQueue = new SynchronousQueue(); @@ -48,6 +51,7 @@ public final class Clipboard { } catch (InterruptedException ie) {} } }); + try { return sClipboardQueue.take(); } catch (InterruptedException ie) { @@ -61,9 +65,11 @@ public final class Clipboard { @Override @SuppressWarnings("deprecation") public void run() { + // In API Level 11 and above, CLIPBOARD_SERVICE returns android.content.ClipboardManager, + // which is a subclass of android.text.ClipboardManager. if (Versions.feature11Plus) { - android.content.ClipboardManager cm = getClipboardManager(mContext); - ClipData clip = ClipData.newPlainText("Text", text); + final android.content.ClipboardManager cm = (android.content.ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE); + final ClipData clip = ClipData.newPlainText("Text", text); try { cm.setPrimaryClip(clip); } catch (NullPointerException e) { @@ -71,27 +77,28 @@ public final class Clipboard { // a NullPointerException if Samsung's /data/clipboard directory is full. // Fortunately, the text is still successfully copied to the clipboard. } - } else { - android.text.ClipboardManager cm = getDeprecatedClipboardManager(mContext); - cm.setText(text); + return; } + + // Deprecated. + android.text.ClipboardManager cm = (android.text.ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE); + cm.setText(text); } }); } /** - * Returns true if the clipboard is nonempty, false otherwise. - * * @return true if the clipboard is nonempty, false otherwise. */ @WrapElementForJNI public static boolean hasText() { if (Versions.feature11Plus) { - android.content.ClipboardManager cm = getClipboardManager(mContext); + android.content.ClipboardManager cm = (android.content.ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE); return cm.hasPrimaryClip(); } - android.text.ClipboardManager cm = getDeprecatedClipboardManager(mContext); + // Deprecated. + android.text.ClipboardManager cm = (android.text.ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE); return cm.hasText(); } @@ -103,23 +110,15 @@ public final class Clipboard { setText(null); } - private static android.content.ClipboardManager getClipboardManager(Context context) { - // In API Level 11 and above, CLIPBOARD_SERVICE returns android.content.ClipboardManager, - // which is a subclass of android.text.ClipboardManager. - return (android.content.ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE); - } - - private static android.text.ClipboardManager getDeprecatedClipboardManager(Context context) { - return (android.text.ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE); - } - - /* On some devices, access to the clipboard service needs to happen + /** + * On some devices, access to the clipboard service needs to happen * on a thread with a looper, so this function requires a looper is - * present on the thread. */ + * present on the thread. + */ @SuppressWarnings("deprecation") - private static String getClipboardTextImpl() { + /* inner-access */ static String getClipboardTextImpl() { if (Versions.feature11Plus) { - android.content.ClipboardManager cm = getClipboardManager(mContext); + android.content.ClipboardManager cm = (android.content.ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE); if (cm.hasPrimaryClip()) { ClipData clip = cm.getPrimaryClip(); if (clip != null) { @@ -128,7 +127,7 @@ public final class Clipboard { } } } else { - android.text.ClipboardManager cm = getDeprecatedClipboardManager(mContext); + android.text.ClipboardManager cm = (android.text.ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE); if (cm.hasText()) { return cm.getText().toString(); }