| 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.ApiCompatibilityUtils; |
| 8 import org.chromium.base.CalledByNative; | 8 import org.chromium.base.CalledByNative; |
| 9 import org.chromium.base.JNINamespace; | 9 import org.chromium.base.JNINamespace; |
| 10 import org.chromium.ui.R; |
| 10 | 11 |
| 11 import android.content.ClipData; | 12 import android.content.ClipData; |
| 12 import android.content.ClipboardManager; | 13 import android.content.ClipboardManager; |
| 13 import android.content.Context; | 14 import android.content.Context; |
| 14 import android.os.Build; | 15 import android.os.Build; |
| 15 import android.text.TextUtils; | 16 import android.text.TextUtils; |
| 17 import android.widget.Toast; |
| 16 | 18 |
| 17 /** | 19 /** |
| 18 * Simple proxy that provides C++ code with an access pathway to the Android | 20 * Simple proxy that provides C++ code with an access pathway to the Android |
| 19 * clipboard. | 21 * clipboard. |
| 20 */ | 22 */ |
| 21 @JNINamespace("ui") | 23 @JNINamespace("ui") |
| 22 public class Clipboard { | 24 public class Clipboard { |
| 23 // Necessary for coercing clipboard contents to text if they require | 25 // Necessary for coercing clipboard contents to text if they require |
| 24 // access to network resources, etceteras (e.g., URI in clipboard) | 26 // access to network resources, etceteras (e.g., URI in clipboard) |
| 25 private final Context mContext; | 27 private final Context mContext; |
| 26 | 28 |
| 27 private final ClipboardManager mClipboardManager; | 29 private final ClipboardManager mClipboardManager; |
| 28 | 30 |
| 29 /** | 31 /** |
| 30 * Use the factory constructor instead. | 32 * Use the factory constructor instead. |
| 31 * | 33 * |
| 32 * @param context for accessing the clipboard | 34 * @param context for accessing the clipboard |
| 33 */ | 35 */ |
| 34 private Clipboard(final Context context) { | 36 public Clipboard(final Context context) { |
| 35 mContext = context; | 37 mContext = context; |
| 36 mClipboardManager = (ClipboardManager) | 38 mClipboardManager = (ClipboardManager) |
| 37 context.getSystemService(Context.CLIPBOARD_SERVICE); | 39 context.getSystemService(Context.CLIPBOARD_SERVICE); |
| 38 } | 40 } |
| 39 | 41 |
| 40 /** | 42 /** |
| 41 * Returns a new Clipboard object bound to the specified context. | 43 * Returns a new Clipboard object bound to the specified context. |
| 42 * | 44 * |
| 43 * @param context for accessing the clipboard | 45 * @param context for accessing the clipboard |
| 44 * @return the new object | 46 * @return the new object |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 } | 95 } |
| 94 return null; | 96 return null; |
| 95 } | 97 } |
| 96 | 98 |
| 97 /** | 99 /** |
| 98 * Emulates the behavior of the now-deprecated | 100 * Emulates the behavior of the now-deprecated |
| 99 * {@link android.text.ClipboardManager#setText(CharSequence)}, setting the | 101 * {@link android.text.ClipboardManager#setText(CharSequence)}, setting the |
| 100 * clipboard's current primary clip to a plain-text clip that consists of | 102 * clipboard's current primary clip to a plain-text clip that consists of |
| 101 * the specified string. | 103 * the specified string. |
| 102 * | 104 * |
| 105 * @param label will become the label of the clipboard's primary clip |
| 106 * @param text will become the content of the clipboard's primary clip |
| 107 */ |
| 108 public void setText(final String label, final String text) { |
| 109 setPrimaryClipNoException(ClipData.newPlainText(label, text)); |
| 110 } |
| 111 |
| 112 /** |
| 113 * Emulates the behavior of the now-deprecated |
| 114 * {@link android.text.ClipboardManager#setText(CharSequence)}, setting the |
| 115 * clipboard's current primary clip to a plain-text clip that consists of |
| 116 * the specified string. |
| 117 * |
| 103 * @param text will become the content of the clipboard's primary clip | 118 * @param text will become the content of the clipboard's primary clip |
| 104 */ | 119 */ |
| 105 @SuppressWarnings("javadoc") | |
| 106 @CalledByNative | 120 @CalledByNative |
| 107 private void setText(final String text) { | 121 public void setText(final String text) { |
| 108 mClipboardManager.setPrimaryClip(ClipData.newPlainText(null, text)); | 122 setText(null, text); |
| 109 } | 123 } |
| 110 | 124 |
| 111 /** | 125 /** |
| 126 * Writes HTML to the clipboard, together with a plain-text representation |
| 127 * of that very data. This API is only available in Android JellyBean+ and |
| 128 * will be a no-operation in older versions. |
| 129 * |
| 130 * @param html The HTML content to be pasted to the clipboard. |
| 131 * @param label The Plain-text label for the HTML content. |
| 132 * @param text Plain-text representation of the HTML content. |
| 133 */ |
| 134 public void setHTMLText(final String html, final String label, final String
text) { |
| 135 if (isHTMLClipboardSupported()) { |
| 136 setPrimaryClipNoException(ClipData.newHtmlText(label, text, html)); |
| 137 } |
| 138 } |
| 139 |
| 140 /** |
| 112 * Writes HTML to the clipboard, together with a plain-text representation | 141 * Writes HTML to the clipboard, together with a plain-text representation |
| 113 * of that very data. This API is only available in Android JellyBean+ and | 142 * of that very data. This API is only available in Android JellyBean+ and |
| 114 * will be a no-operation in older versions. | 143 * will be a no-operation in older versions. |
| 115 * | 144 * |
| 116 * @param html The HTML content to be pasted to the clipboard. | 145 * @param html The HTML content to be pasted to the clipboard. |
| 117 * @param text Plain-text representation of the HTML content. | 146 * @param text Plain-text representation of the HTML content. |
| 118 */ | 147 */ |
| 119 @CalledByNative | 148 @CalledByNative |
| 120 private void setHTMLText(final String html, final String text) { | 149 public void setHTMLText(final String html, final String text) { |
| 121 if (isHTMLClipboardSupported()) { | 150 setHTMLText(html, null, text); |
| 122 mClipboardManager.setPrimaryClip( | |
| 123 ClipData.newHtmlText(null, text, html)); | |
| 124 } | |
| 125 } | 151 } |
| 126 | 152 |
| 127 @CalledByNative | 153 @CalledByNative |
| 128 private static boolean isHTMLClipboardSupported() { | 154 private static boolean isHTMLClipboardSupported() { |
| 129 return ApiCompatibilityUtils.isHTMLClipboardSupported(); | 155 return ApiCompatibilityUtils.isHTMLClipboardSupported(); |
| 130 } | 156 } |
| 157 |
| 158 private void setPrimaryClipNoException(ClipData clip) { |
| 159 try { |
| 160 mClipboardManager.setPrimaryClip(clip); |
| 161 } catch (Exception ex) { |
| 162 // Ignore any exceptions here as certain devices have bugs and will
fail. |
| 163 String text = mContext.getString(R.string.copy_to_clipboard_failure_
message); |
| 164 Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show(); |
| 165 } |
| 166 } |
| 131 } | 167 } |
| OLD | NEW |