OLD | NEW |
| (Empty) |
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.os.Build; | |
14 import android.text.TextUtils; | |
15 | |
16 /** | |
17 * Simple proxy that provides C++ code with an access pathway to the Android | |
18 * clipboard. | |
19 */ | |
20 @JNINamespace("ui") | |
21 public class Clipboard { | |
22 // Necessary for coercing clipboard contents to text if they require | |
23 // access to network resources, etceteras (e.g., URI in clipboard) | |
24 private final Context mContext; | |
25 | |
26 private final ClipboardManager mClipboardManager; | |
27 | |
28 /** | |
29 * Use the factory constructor instead. | |
30 * | |
31 * @param context for accessing the clipboard | |
32 */ | |
33 private Clipboard(final Context context) { | |
34 mContext = context; | |
35 mClipboardManager = (ClipboardManager) | |
36 context.getSystemService(Context.CLIPBOARD_SERVICE); | |
37 } | |
38 | |
39 /** | |
40 * Returns a new Clipboard object bound to the specified context. | |
41 * | |
42 * @param context for accessing the clipboard | |
43 * @return the new object | |
44 */ | |
45 @CalledByNative | |
46 private static Clipboard create(final Context context) { | |
47 return new Clipboard(context); | |
48 } | |
49 | |
50 /** | |
51 * Emulates the behavior of the now-deprecated | |
52 * {@link android.text.ClipboardManager#getText()} by invoking | |
53 * {@link android.content.ClipData.Item#coerceToText(Context)} on the first | |
54 * item in the clipboard (if any) and returning the result as a string. | |
55 * <p> | |
56 * This is quite different than simply calling {@link Object#toString()} on | |
57 * the clip; consumers of this API should familiarize themselves with the | |
58 * process described in | |
59 * {@link android.content.ClipData.Item#coerceToText(Context)} before using | |
60 * this method. | |
61 * | |
62 * @return a string representation of the first item on the clipboard, if | |
63 * the clipboard currently has an item and coercion of the item into | |
64 * a string is possible; otherwise, <code>null</code> | |
65 */ | |
66 @SuppressWarnings("javadoc") | |
67 @CalledByNative | |
68 private String getCoercedText() { | |
69 final ClipData clip = mClipboardManager.getPrimaryClip(); | |
70 if (clip != null && clip.getItemCount() > 0) { | |
71 final CharSequence sequence = clip.getItemAt(0).coerceToText(mContex
t); | |
72 if (sequence != null) { | |
73 return sequence.toString(); | |
74 } | |
75 } | |
76 return null; | |
77 } | |
78 | |
79 /** | |
80 * Emulates the behavior of the now-deprecated | |
81 * {@link android.text.ClipboardManager#setText(CharSequence)}, setting the | |
82 * clipboard's current primary clip to a plain-text clip that consists of | |
83 * the specified string. | |
84 * | |
85 * @param text will become the content of the clipboard's primary clip | |
86 */ | |
87 @SuppressWarnings("javadoc") | |
88 @CalledByNative | |
89 private void setText(final String text) { | |
90 mClipboardManager.setPrimaryClip(ClipData.newPlainText(null, text)); | |
91 } | |
92 | |
93 /** | |
94 * Writes HTML to the clipboard, together with a plain-text representation | |
95 * of that very data. This API is only available in Android JellyBean+ and | |
96 * will be a no-operation in older versions. | |
97 * | |
98 * @param html The HTML content to be pasted to the clipboard. | |
99 * @param text Plain-text representation of the HTML content. | |
100 */ | |
101 @CalledByNative | |
102 private void setHTMLText(final String html, final String text) { | |
103 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { | |
104 mClipboardManager.setPrimaryClip( | |
105 ClipData.newHtmlText(null, text, html)); | |
106 } | |
107 } | |
108 | |
109 /** | |
110 * Approximates the behavior of the now-deprecated | |
111 * {@link android.text.ClipboardManager#hasText()}, returning true if and | |
112 * only if the clipboard has a primary clip and that clip contains a plain | |
113 * non-empty text entry (without attempting coercion - URLs and intents | |
114 * will cause this method to return false). | |
115 * | |
116 * @return as described above | |
117 */ | |
118 @SuppressWarnings("javadoc") | |
119 @CalledByNative | |
120 private boolean hasPlainText() { | |
121 final ClipData clip = mClipboardManager.getPrimaryClip(); | |
122 if (clip != null && clip.getItemCount() > 0) { | |
123 final CharSequence text = clip.getItemAt(0).getText(); | |
124 return !TextUtils.isEmpty(text); | |
125 } | |
126 return false; | |
127 } | |
128 } | |
OLD | NEW |