1 | // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | package org.chromium.ui; |
6 | |
7 | import org.chromium.base.CalledByNative; |
8 | import org.chromium.base.JNINamespace; |
9 | |
10 | import android.content.ClipData; |
11 | import android.content.ClipboardManager; |
12 | import android.content.Context; |
13 | import android.text.TextUtils; |
14 | |
15 | /** |
16 | * Simple proxy that provides C++ code with an access pathway to the Android |
17 | * clipboard. |
18 | */ |
19 | @JNINamespace("ui") |
20 | public class Clipboard { |
21 | // Necessary for coercing clipboard contents to text if they require |
22 | // access to network resources, etceteras (e.g., URI in clipboard) |
23 | private final Context mContext; |
24 | |
25 | private final ClipboardManager mClipboardManager; |
26 | |
27 | /** |
28 | * Use the factory constructor instead. |
29 | * |
30 | * @param context for accessing the clipboard |
31 | */ |
32 | private Clipboard(final Context context) { |
33 | mContext = context; |
34 | mClipboardManager = (ClipboardManager) |
35 | context.getSystemService(Context.CLIPBOARD_SERVICE); |
36 | } |
37 | |
38 | /** |
39 | * Returns a new Clipboard object bound to the specified context. |
40 | * |
41 | * @param context for accessing the clipboard |
42 | * @return the new object |
43 | */ |
44 | @CalledByNative |
45 | private static Clipboard create(final Context context) { |
46 | return new Clipboard(context); |
47 | } |
48 | |
49 | /** |
50 | * Emulates the behavior of the now-deprecated |
51 | * {@link android.text.ClipboardManager#getText()} by invoking |
52 | * {@link android.content.ClipData.Item#coerceToText(Context)} on the first |
53 | * item in the clipboard (if any) and returning the result as a string. |
54 | * <p> |
55 | * This is quite different than simply calling {@link Object#toString()} on |
56 | * the clip; consumers of this API should familiarize themselves with the |
57 | * process described in |
58 | * {@link android.content.ClipData.Item#coerceToText(Context)} before using |
59 | * this method. |
60 | * |
61 | * @return a string representation of the first item on the clipboard, if |
62 | * the clipboard currently has an item and coercion of the item into |
63 | * a string is possible; otherwise, <code>null</code> |
64 | */ |
65 | @SuppressWarnings("javadoc") |
66 | @CalledByNative |
67 | private String getCoercedText() { |
68 | final ClipData clip = mClipboardManager.getPrimaryClip(); |
69 | if (clip != null && clip.getItemCount() > 0) { |
70 | final CharSequence sequence = clip.getItemAt(0).coerceToText(mContext); |
71 | if (sequence != null) { |
72 | return sequence.toString(); |
73 | } |
74 | } |
75 | return null; |
76 | } |
77 | |
78 | /** |
79 | * Emulates the behavior of the now-deprecated |
80 | * {@link android.text.ClipboardManager#setText(CharSequence)}, setting the |
81 | * clipboard's current primary clip to a plain-text clip that consists of |
82 | * the specified string. |
83 | * |
84 | * @param text will become the content of the clipboard's primary clip |
85 | */ |
86 | @SuppressWarnings("javadoc") |
87 | @CalledByNative |
88 | private void setText(final String text) { |
89 | mClipboardManager.setPrimaryClip(ClipData.newPlainText(null, text)); |
90 | } |
91 | |
92 | /** |
93 | * Approximates the behavior of the now-deprecated |
94 | * {@link android.text.ClipboardManager#hasText()}, returning true if and |
95 | * only if the clipboard has a primary clip and that clip contains a plain |
96 | * non-empty text entry (without attempting coercion - URLs and intents |
97 | * will cause this method to return false). |
98 | * |
99 | * @return as described above |
100 | */ |
101 | @SuppressWarnings("javadoc") |
102 | @CalledByNative |
103 | private boolean hasPlainText() { |
104 | final ClipData clip = mClipboardManager.getPrimaryClip(); |
105 | if (clip != null && clip.getItemCount() > 0) { |
106 | final CharSequence text = clip.getItemAt(0).getText(); |
107 | return !TextUtils.isEmpty(text); |
108 | } |
109 | return false; |
110 | } |
111 | } |