Commit 1b5604e6 authored by Gang Wu's avatar Gang Wu Committed by Commit Bot

[Clipboard] Rename clipboard image API

change the order of the set/get api to Text/HTMLText/Image.

also change image set/get api to setImageUri/getImageUri

Change-Id: If9af9c205abe1a4e56f14ee6ef397374c3ba13a2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2070138Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Commit-Queue: Gang Wu <gangwu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#744221}
parent 280440fb
......@@ -201,7 +201,7 @@ public class ShareHelper {
* @return The file name if system clipboard contains a Uri from Chrome, otherwise return null.
*/
private static String getClipboardCurrentFilepath() throws IOException {
Uri clipboardUri = Clipboard.getInstance().getUri();
Uri clipboardUri = Clipboard.getInstance().getImageUri();
if (isUriInDirectory(clipboardUri, getSharedFilesDirectory())) {
return clipboardUri.getPath();
}
......
......@@ -104,7 +104,7 @@ public class TabContextMenuItemDelegate implements ContextMenuItemDelegate {
@Override
public void onSaveImageToClipboard(Uri uri) {
Clipboard.getInstance().setImage(uri);
Clipboard.getInstance().setImageUri(uri);
}
@Override
......
......@@ -120,7 +120,7 @@ public class ShareHelperTest {
ShareHelper.generateUriFromData(
mActivityTestRule.getActivity(), TEST_IMAGE_DATA, imageCallback);
imageCallback.waitForCallback(0, 1, WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
Clipboard.getInstance().setImage(imageCallback.getImageUri());
Clipboard.getInstance().setImageUri(imageCallback.getImageUri());
return imageCallback.getImageUri();
}
......
......@@ -125,19 +125,6 @@ public class Clipboard implements ClipboardManager.OnPrimaryClipChangedListener
return null;
}
/**
* Gets the Uri of top item on the primary clip on the Android clipboard.
*
* @return an Uri if any, or null if there is no Uri or no entries on the primary clip.
*/
public Uri getUri() {
ClipData clipData = mClipboardManager.getPrimaryClip();
if (clipData == null) return null;
if (clipData.getItemCount() == 0) return null;
return clipData.getItemAt(0).getUri();
}
/**
* Gets the HTML text of top item on the primary clip on the Android clipboard.
*
......@@ -156,6 +143,25 @@ public class Clipboard implements ClipboardManager.OnPrimaryClipChangedListener
}
}
/**
* Gets the Uri of top item on the primary clip on the Android clipboard if the mime type is
* image.
*
* @return an Uri if mime type is image type, or null if there is no Uri or no entries on the
* primary clip.
*/
public Uri getImageUri() {
ClipData clipData = mClipboardManager.getPrimaryClip();
if (clipData == null || clipData.getItemCount() == 0) return null;
ClipDescription description = clipData.getDescription();
if (description == null || !description.hasMimeType("image/*")) {
return null;
}
return clipData.getItemAt(0).getUri();
}
/**
* Emulates the behavior of the now-deprecated
* {@link android.text.ClipboardManager#setText(CharSequence)}, setting the
......@@ -168,11 +174,23 @@ public class Clipboard implements ClipboardManager.OnPrimaryClipChangedListener
setPrimaryClipNoException(ClipData.newPlainText("text", text));
}
/**
* Writes HTML to the clipboard, together with a plain-text representation
* of that very data.
*
* @param html The HTML content to be pasted to the clipboard.
* @param text Plain-text representation of the HTML content.
*/
@CalledByNative
private void setHTMLText(final String html, final String text) {
setPrimaryClipNoException(ClipData.newHtmlText("html", text, html));
}
/**
* Setting the clipboard's current primary clip to an image.
* @param Uri The {@link Uri} will become the content of the clipboard's primary clip.
*/
public void setImage(final Uri uri) {
public void setImageUri(final Uri uri) {
if (uri == null) {
showCopyToClipboardFailureMessage();
return;
......@@ -186,18 +204,6 @@ public class Clipboard implements ClipboardManager.OnPrimaryClipChangedListener
setPrimaryClipNoException(clip);
}
/**
* Writes HTML to the clipboard, together with a plain-text representation
* of that very data.
*
* @param html The HTML content to be pasted to the clipboard.
* @param text Plain-text representation of the HTML content.
*/
@CalledByNative
private void setHTMLText(final String html, final String text) {
setPrimaryClipNoException(ClipData.newHtmlText("html", text, html));
}
/**
* Clears the Clipboard Primary clip.
*
......
......@@ -18,7 +18,14 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.chromium.base.ContentUriUtils;
import org.chromium.base.StreamUtil;
import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.base.test.util.UrlUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Tests logic in the Clipboard class.
......@@ -28,7 +35,29 @@ import org.chromium.base.test.BaseRobolectricTestRunner;
public class ClipboardTest {
private static final String PLAIN_TEXT = "plain";
private static final String HTML_TEXT = "<span style=\"color: red;\">HTML</span>";
private static final Uri IMAGE_URI = Uri.parse("content://test.image");
private static final byte[] TEST_IMAGE_DATA = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
private Uri generateUriFromImage(final byte[] jpegImageData) {
FileOutputStream fOut = null;
try {
File path = new File(UrlUtils.getIsolatedTestFilePath("test_image"));
if (path.exists() || path.mkdir()) {
File saveFile = File.createTempFile(
String.valueOf(System.currentTimeMillis()), ".jpg", path);
fOut = new FileOutputStream(saveFile);
fOut.write(jpegImageData);
fOut.flush();
return ContentUriUtils.getContentUriFromFile(saveFile);
}
} catch (IOException ie) {
// Ignore exception.
} finally {
StreamUtil.closeQuietly(fOut);
}
return null;
}
@Test
public void testClipDataToHtmlText() {
......@@ -61,11 +90,12 @@ public class ClipboardTest {
Clipboard clipboard = Clipboard.getInstance();
// simple set a null, check if there is no crash.
clipboard.setImage(null);
clipboard.setImageUri(null);
// Set actually data.
clipboard.setImage(IMAGE_URI);
Uri imageUri = generateUriFromImage(TEST_IMAGE_DATA);
clipboard.setImageUri(imageUri);
assertEquals(IMAGE_URI, clipboard.getUri());
assertEquals(imageUri, clipboard.getImageUri());
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment