OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 org.chromium.base.ApiCompatibilityUtils; |
7 import org.chromium.base.CalledByNative; | 8 import org.chromium.base.CalledByNative; |
8 import org.chromium.base.JNINamespace; | 9 import org.chromium.base.JNINamespace; |
9 | 10 |
10 import android.content.ClipData; | 11 import android.content.ClipData; |
11 import android.content.ClipboardManager; | 12 import android.content.ClipboardManager; |
12 import android.content.Context; | 13 import android.content.Context; |
13 import android.os.Build; | 14 import android.os.Build; |
14 import android.text.TextUtils; | 15 import android.text.TextUtils; |
15 | 16 |
16 /** | 17 /** |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 if (clip != null && clip.getItemCount() > 0) { | 71 if (clip != null && clip.getItemCount() > 0) { |
71 final CharSequence sequence = clip.getItemAt(0).coerceToText(mContex
t); | 72 final CharSequence sequence = clip.getItemAt(0).coerceToText(mContex
t); |
72 if (sequence != null) { | 73 if (sequence != null) { |
73 return sequence.toString(); | 74 return sequence.toString(); |
74 } | 75 } |
75 } | 76 } |
76 return null; | 77 return null; |
77 } | 78 } |
78 | 79 |
79 /** | 80 /** |
| 81 * Gets the HTML text of top item on the primary clip on the Android clipboa
rd. |
| 82 * |
| 83 * @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. |
| 85 */ |
| 86 @CalledByNative |
| 87 private String getHTMLText() { |
| 88 if (isHTMLClipboardSupported()) { |
| 89 final ClipData clip = mClipboardManager.getPrimaryClip(); |
| 90 if (clip != null && clip.getItemCount() > 0) { |
| 91 return clip.getItemAt(0).getHtmlText(); |
| 92 } |
| 93 } |
| 94 return null; |
| 95 } |
| 96 |
| 97 /** |
80 * Emulates the behavior of the now-deprecated | 98 * Emulates the behavior of the now-deprecated |
81 * {@link android.text.ClipboardManager#setText(CharSequence)}, setting the | 99 * {@link android.text.ClipboardManager#setText(CharSequence)}, setting the |
82 * clipboard's current primary clip to a plain-text clip that consists of | 100 * clipboard's current primary clip to a plain-text clip that consists of |
83 * the specified string. | 101 * the specified string. |
84 * | 102 * |
85 * @param text will become the content of the clipboard's primary clip | 103 * @param text will become the content of the clipboard's primary clip |
86 */ | 104 */ |
87 @SuppressWarnings("javadoc") | 105 @SuppressWarnings("javadoc") |
88 @CalledByNative | 106 @CalledByNative |
89 private void setText(final String text) { | 107 private void setText(final String text) { |
90 mClipboardManager.setPrimaryClip(ClipData.newPlainText(null, text)); | 108 mClipboardManager.setPrimaryClip(ClipData.newPlainText(null, text)); |
91 } | 109 } |
92 | 110 |
93 /** | 111 /** |
94 * Writes HTML to the clipboard, together with a plain-text representation | 112 * 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 | 113 * of that very data. This API is only available in Android JellyBean+ and |
96 * will be a no-operation in older versions. | 114 * will be a no-operation in older versions. |
97 * | 115 * |
98 * @param html The HTML content to be pasted to the clipboard. | 116 * @param html The HTML content to be pasted to the clipboard. |
99 * @param text Plain-text representation of the HTML content. | 117 * @param text Plain-text representation of the HTML content. |
100 */ | 118 */ |
101 @CalledByNative | 119 @CalledByNative |
102 private void setHTMLText(final String html, final String text) { | 120 private void setHTMLText(final String html, final String text) { |
103 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { | 121 if (isHTMLClipboardSupported()) { |
104 mClipboardManager.setPrimaryClip( | 122 mClipboardManager.setPrimaryClip( |
105 ClipData.newHtmlText(null, text, html)); | 123 ClipData.newHtmlText(null, text, html)); |
106 } | 124 } |
107 } | 125 } |
108 | 126 |
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 | 127 @CalledByNative |
120 private boolean hasPlainText() { | 128 private static boolean isHTMLClipboardSupported() { |
121 final ClipData clip = mClipboardManager.getPrimaryClip(); | 129 return ApiCompatibilityUtils.isHTMLClipboardSupported(); |
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 } | 130 } |
128 } | 131 } |
OLD | NEW |