OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.ui.base; | 5 package org.chromium.ui.base; |
6 | 6 |
| 7 import android.annotation.TargetApi; |
7 import android.content.ClipData; | 8 import android.content.ClipData; |
8 import android.content.ClipboardManager; | 9 import android.content.ClipboardManager; |
9 import android.content.Context; | 10 import android.content.Context; |
| 11 import android.os.Build; |
10 import android.widget.Toast; | 12 import android.widget.Toast; |
11 | 13 |
12 import org.chromium.base.ApiCompatibilityUtils; | |
13 import org.chromium.base.CalledByNative; | 14 import org.chromium.base.CalledByNative; |
14 import org.chromium.base.JNINamespace; | 15 import org.chromium.base.JNINamespace; |
15 import org.chromium.ui.R; | 16 import org.chromium.ui.R; |
16 | 17 |
17 /** | 18 /** |
18 * Simple proxy that provides C++ code with an access pathway to the Android | 19 * Simple proxy that provides C++ code with an access pathway to the Android |
19 * clipboard. | 20 * clipboard. |
20 */ | 21 */ |
21 @JNINamespace("ui") | 22 @JNINamespace("ui") |
22 public class Clipboard { | 23 public class Clipboard { |
| 24 |
| 25 private static final boolean IS_HTML_CLIPBOARD_SUPPORTED = |
| 26 Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN; |
| 27 |
23 // Necessary for coercing clipboard contents to text if they require | 28 // Necessary for coercing clipboard contents to text if they require |
24 // access to network resources, etceteras (e.g., URI in clipboard) | 29 // access to network resources, etceteras (e.g., URI in clipboard) |
25 private final Context mContext; | 30 private final Context mContext; |
26 | 31 |
27 private final ClipboardManager mClipboardManager; | 32 private final ClipboardManager mClipboardManager; |
28 | 33 |
29 /** | 34 /** |
30 * Use the factory constructor instead. | 35 * Use the factory constructor instead. |
31 * | 36 * |
32 * @param context for accessing the clipboard | 37 * @param context for accessing the clipboard |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 } | 81 } |
77 return null; | 82 return null; |
78 } | 83 } |
79 | 84 |
80 /** | 85 /** |
81 * Gets the HTML text of top item on the primary clip on the Android clipboa
rd. | 86 * Gets the HTML text of top item on the primary clip on the Android clipboa
rd. |
82 * | 87 * |
83 * @return a Java string with the html text if any, or null if there is no h
tml | 88 * @return a Java string with the html text if any, or null if there is no h
tml |
84 * text or no entries on the primary clip. | 89 * text or no entries on the primary clip. |
85 */ | 90 */ |
| 91 @TargetApi(Build.VERSION_CODES.JELLY_BEAN) |
86 @CalledByNative | 92 @CalledByNative |
87 private String getHTMLText() { | 93 private String getHTMLText() { |
88 if (isHTMLClipboardSupported()) { | 94 if (IS_HTML_CLIPBOARD_SUPPORTED) { |
89 final ClipData clip = mClipboardManager.getPrimaryClip(); | 95 final ClipData clip = mClipboardManager.getPrimaryClip(); |
90 if (clip != null && clip.getItemCount() > 0) { | 96 if (clip != null && clip.getItemCount() > 0) { |
91 return clip.getItemAt(0).getHtmlText(); | 97 return clip.getItemAt(0).getHtmlText(); |
92 } | 98 } |
93 } | 99 } |
94 return null; | 100 return null; |
95 } | 101 } |
96 | 102 |
97 /** | 103 /** |
98 * Emulates the behavior of the now-deprecated | 104 * Emulates the behavior of the now-deprecated |
(...skipping 23 matching lines...) Expand all Loading... |
122 | 128 |
123 /** | 129 /** |
124 * Writes HTML to the clipboard, together with a plain-text representation | 130 * Writes HTML to the clipboard, together with a plain-text representation |
125 * of that very data. This API is only available in Android JellyBean+ and | 131 * of that very data. This API is only available in Android JellyBean+ and |
126 * will be a no-operation in older versions. | 132 * will be a no-operation in older versions. |
127 * | 133 * |
128 * @param html The HTML content to be pasted to the clipboard. | 134 * @param html The HTML content to be pasted to the clipboard. |
129 * @param label The Plain-text label for the HTML content. | 135 * @param label The Plain-text label for the HTML content. |
130 * @param text Plain-text representation of the HTML content. | 136 * @param text Plain-text representation of the HTML content. |
131 */ | 137 */ |
| 138 @TargetApi(Build.VERSION_CODES.JELLY_BEAN) |
132 public void setHTMLText(final String html, final String label, final String
text) { | 139 public void setHTMLText(final String html, final String label, final String
text) { |
133 if (isHTMLClipboardSupported()) { | 140 if (IS_HTML_CLIPBOARD_SUPPORTED) { |
134 setPrimaryClipNoException(ClipData.newHtmlText(label, text, html)); | 141 setPrimaryClipNoException(ClipData.newHtmlText(label, text, html)); |
135 } | 142 } |
136 } | 143 } |
137 | 144 |
138 /** | 145 /** |
139 * Writes HTML to the clipboard, together with a plain-text representation | 146 * Writes HTML to the clipboard, together with a plain-text representation |
140 * of that very data. This API is only available in Android JellyBean+ and | 147 * of that very data. This API is only available in Android JellyBean+ and |
141 * will be a no-operation in older versions. | 148 * will be a no-operation in older versions. |
142 * | 149 * |
143 * @param html The HTML content to be pasted to the clipboard. | 150 * @param html The HTML content to be pasted to the clipboard. |
144 * @param text Plain-text representation of the HTML content. | 151 * @param text Plain-text representation of the HTML content. |
145 */ | 152 */ |
146 @CalledByNative | 153 @CalledByNative |
147 public void setHTMLText(final String html, final String text) { | 154 public void setHTMLText(final String html, final String text) { |
148 setHTMLText(html, null, text); | 155 setHTMLText(html, null, text); |
149 } | 156 } |
150 | 157 |
151 @CalledByNative | 158 @CalledByNative |
152 private static boolean isHTMLClipboardSupported() { | 159 private static boolean isHTMLClipboardSupported() { |
153 return ApiCompatibilityUtils.isHTMLClipboardSupported(); | 160 return IS_HTML_CLIPBOARD_SUPPORTED; |
154 } | 161 } |
155 | 162 |
156 private void setPrimaryClipNoException(ClipData clip) { | 163 private void setPrimaryClipNoException(ClipData clip) { |
157 try { | 164 try { |
158 mClipboardManager.setPrimaryClip(clip); | 165 mClipboardManager.setPrimaryClip(clip); |
159 } catch (Exception ex) { | 166 } catch (Exception ex) { |
160 // Ignore any exceptions here as certain devices have bugs and will
fail. | 167 // Ignore any exceptions here as certain devices have bugs and will
fail. |
161 String text = mContext.getString(R.string.copy_to_clipboard_failure_
message); | 168 String text = mContext.getString(R.string.copy_to_clipboard_failure_
message); |
162 Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show(); | 169 Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show(); |
163 } | 170 } |
164 } | 171 } |
165 } | 172 } |
OLD | NEW |