From a299686f47294c20ef04e818585f1d27cf069876 Mon Sep 17 00:00:00 2001 From: Maxime Biais Date: Fri, 4 Mar 2016 11:30:34 +0100 Subject: [PATCH 1/2] Squashed 'libs/editor/' changes from 57802bf..2b066be 2b066be Merge pull request #306 from wordpress-mobile/issue/268-link-dialog-field-text 56b5dfa Prepend http scheme if there is no URI in the link text f5f5aa3 Replacing inline String with getSimpleName to future-proof name changes 4e51cf8 Select all text in Link dialog URL field for quick deletion e6366e6 Editor passes clipboard URL to Link dialog when able 6082015 Adding utility method (with tests) to get URL text from the clipboard d6d5868 Showing soft keyboard when entering Link dialog 4d13977 Removing default URL text from Link dialog field e9d2bc2 Creating fields for Link dialog arguments git-subtree-dir: libs/editor git-subtree-split: 2b066be2de3a0d2b142ffc25c1450efd084faba5 --- .../android/editor/EditorFragment.java | 24 ++++++--- .../android/editor/LinkDialogFragment.java | 19 ++++--- .../org/wordpress/android/editor/Utils.java | 17 +++++++ .../src/main/res/layout/dialog_link.xml | 1 - .../wordpress/android/editor/UtilsTest.java | 51 +++++++++++++++++++ 5 files changed, 98 insertions(+), 14 deletions(-) diff --git a/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java b/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java index 930653cda181..d5145c6679e5 100755 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java @@ -7,6 +7,7 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.res.Configuration; +import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Looper; @@ -16,6 +17,7 @@ import android.text.Editable; import android.text.SpannableString; import android.text.Spanned; +import android.text.TextUtils; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -485,6 +487,12 @@ public void onClick(View v) { Bundle dialogBundle = new Bundle(); + // Pass potential URL from user clipboard + String clipboardUri = Utils.getUrlFromClipboard(getActivity()); + if (clipboardUri != null) { + dialogBundle.putString(LinkDialogFragment.LINK_DIALOG_ARG_URL, clipboardUri); + } + // Pass selected text to dialog if (mSourceView.getVisibility() == View.VISIBLE) { // HTML mode @@ -492,14 +500,14 @@ public void onClick(View v) { mSelectionEnd = mSourceViewContent.getSelectionEnd(); String selectedText = mSourceViewContent.getText().toString().substring(mSelectionStart, mSelectionEnd); - dialogBundle.putString("linkText", selectedText); + dialogBundle.putString(LinkDialogFragment.LINK_DIALOG_ARG_TEXT, selectedText); } else { // Visual mode mGetSelectedTextCountDownLatch = new CountDownLatch(1); mWebView.execJavaScriptFromString("ZSSEditor.execFunctionForResult('getSelectedText');"); try { if (mGetSelectedTextCountDownLatch.await(1, TimeUnit.SECONDS)) { - dialogBundle.putString("linkText", mJavaScriptResult); + dialogBundle.putString(LinkDialogFragment.LINK_DIALOG_ARG_TEXT, mJavaScriptResult); } } catch (InterruptedException e) { AppLog.d(T.EDITOR, "Failed to obtain selected text from JS editor."); @@ -507,7 +515,7 @@ public void onClick(View v) { } linkDialogFragment.setArguments(dialogBundle); - linkDialogFragment.show(getFragmentManager(), "LinkDialogFragment"); + linkDialogFragment.show(getFragmentManager(), LinkDialogFragment.class.getSimpleName()); } else { if (v instanceof ToggleButton) { onFormattingButtonClicked((ToggleButton) v); @@ -561,13 +569,15 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { return; } - String linkUrl = extras.getString("linkURL"); - String linkText = extras.getString("linkText"); + String linkUrl = extras.getString(LinkDialogFragment.LINK_DIALOG_ARG_URL); + String linkText = extras.getString(LinkDialogFragment.LINK_DIALOG_ARG_TEXT); if (linkText == null || linkText.equals("")) { linkText = linkUrl; } + if (TextUtils.isEmpty(Uri.parse(linkUrl).getScheme())) linkUrl = "http://" + linkUrl; + if (mSourceView.getVisibility() == View.VISIBLE) { Editable content = mSourceViewContent.getText(); if (content == null) { @@ -1174,8 +1184,8 @@ public void onLinkTapped(String url, String title) { Bundle dialogBundle = new Bundle(); - dialogBundle.putString("linkURL", url); - dialogBundle.putString("linkText", title); + dialogBundle.putString(LinkDialogFragment.LINK_DIALOG_ARG_URL, url); + dialogBundle.putString(LinkDialogFragment.LINK_DIALOG_ARG_TEXT, title); linkDialogFragment.setArguments(dialogBundle); linkDialogFragment.show(getFragmentManager(), "LinkDialogFragment"); diff --git a/WordPressEditor/src/main/java/org/wordpress/android/editor/LinkDialogFragment.java b/WordPressEditor/src/main/java/org/wordpress/android/editor/LinkDialogFragment.java index 59386a33a870..9be36cb33a77 100644 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/LinkDialogFragment.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/LinkDialogFragment.java @@ -8,6 +8,7 @@ import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; +import android.view.WindowManager; import android.widget.EditText; public class LinkDialogFragment extends DialogFragment { @@ -16,6 +17,9 @@ public class LinkDialogFragment extends DialogFragment { public static final int LINK_DIALOG_REQUEST_CODE_UPDATE = 2; public static final int LINK_DIALOG_REQUEST_CODE_DELETE = 3; + public static final String LINK_DIALOG_ARG_URL = "linkURL"; + public static final String LINK_DIALOG_ARG_TEXT = "linkText"; + @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); @@ -31,8 +35,8 @@ public Dialog onCreateDialog(Bundle savedInstanceState) { @Override public void onClick(DialogInterface dialog, int id) { Intent intent = new Intent(); - intent.putExtra("linkURL", urlEditText.getText().toString()); - intent.putExtra("linkText", linkEditText.getText().toString()); + intent.putExtra(LINK_DIALOG_ARG_URL, urlEditText.getText().toString()); + intent.putExtra(LINK_DIALOG_ARG_TEXT, linkEditText.getText().toString()); getTargetFragment().onActivityResult(getTargetRequestCode(), getTargetRequestCode(), intent); } }) @@ -55,15 +59,18 @@ public void onClick(DialogInterface dialog, int which) { // Prepare initial state of EditTexts Bundle bundle = getArguments(); if (bundle != null) { - linkEditText.setText(bundle.getString("linkText")); + linkEditText.setText(bundle.getString(LINK_DIALOG_ARG_TEXT)); - String url = bundle.getString("linkURL"); + String url = bundle.getString(LINK_DIALOG_ARG_URL); if (url != null) { urlEditText.setText(url); } - urlEditText.setSelection(urlEditText.length()); + urlEditText.selectAll(); } - return builder.create(); + AlertDialog dialog = builder.create(); + dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); + + return dialog; } } diff --git a/WordPressEditor/src/main/java/org/wordpress/android/editor/Utils.java b/WordPressEditor/src/main/java/org/wordpress/android/editor/Utils.java index 62297005e542..61868fdc9b77 100644 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/Utils.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/Utils.java @@ -1,9 +1,12 @@ package org.wordpress.android.editor; import android.app.Activity; +import android.content.ClipData; +import android.content.ClipboardManager; import android.content.Context; import android.content.res.AssetManager; import android.net.Uri; +import android.util.Patterns; import org.wordpress.android.util.AppLog; import org.wordpress.android.util.HTTPUtils; @@ -222,4 +225,18 @@ public static Uri downloadExternalMedia(Context context, Uri imageUri, Map diff --git a/WordPressEditor/src/test/java/org/wordpress/android/editor/UtilsTest.java b/WordPressEditor/src/test/java/org/wordpress/android/editor/UtilsTest.java index f4ae62167a81..b7bc70fe884f 100644 --- a/WordPressEditor/src/test/java/org/wordpress/android/editor/UtilsTest.java +++ b/WordPressEditor/src/test/java/org/wordpress/android/editor/UtilsTest.java @@ -1,5 +1,9 @@ package org.wordpress.android.editor; +import android.content.ClipData; +import android.content.ClipboardManager; +import android.content.Context; + import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; @@ -14,12 +18,16 @@ import java.util.Set; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import static org.wordpress.android.editor.Utils.buildMapFromKeyValuePairs; import static org.wordpress.android.editor.Utils.decodeHtml; import static org.wordpress.android.editor.Utils.escapeHtml; import static org.wordpress.android.editor.Utils.getChangeMapFromSets; import static org.wordpress.android.editor.Utils.splitDelimitedString; import static org.wordpress.android.editor.Utils.splitValuePairDelimitedString; +import static org.wordpress.android.editor.Utils.getUrlFromClipboard; @Config(sdk = 18) @RunWith(RobolectricTestRunner.class) @@ -161,4 +169,47 @@ public void testGetChangeMapFromSets() { // Test empty sets assertEquals(Collections.emptyMap(), getChangeMapFromSets(Collections.emptySet(), Collections.emptySet())); } + + @Test + public void testClipboardUrlWithNullContext() { + assertNull(getUrlFromClipboard(null)); + } + + @Test + public void testClipboardUrlWithNoClipData() { + assertNull(getClipboardUrlHelper(0, null)); + } + + @Test + public void testClipboardUrlWithNonUriData() { + assertNull(getClipboardUrlHelper(1, "not a URL")); + } + + @Test + public void testClipboardUrlWithLocalUriData() { + assertNull(getClipboardUrlHelper(1, "file://test.png")); + } + + @Test + public void testClipboardWithUrlData() { + String testUrl = "google.com"; + assertEquals(testUrl, getClipboardUrlHelper(1, testUrl)); + } + + private String getClipboardUrlHelper(int itemCount, String clipText) { + ClipData.Item mockItem = mock(ClipData.Item.class); + when(mockItem.getText()).thenReturn(clipText); + + ClipData mockPrimary = mock(ClipData.class); + when(mockPrimary.getItemCount()).thenReturn(itemCount); + when(mockPrimary.getItemAt(0)).thenReturn(mockItem); + + ClipboardManager mockManager = mock(ClipboardManager.class); + when(mockManager.getPrimaryClip()).thenReturn(mockPrimary); + + Context mockContext = mock(Context.class); + when(mockContext.getSystemService(Context.CLIPBOARD_SERVICE)).thenReturn(mockManager); + + return getUrlFromClipboard(mockContext); + } } From 8db246f15ce6f4d2c7f7f7ec51c68b87e9a66c2f Mon Sep 17 00:00:00 2001 From: Maxime Biais Date: Tue, 8 Mar 2016 13:59:39 +0100 Subject: [PATCH 2/2] Squashed 'libs/editor/' changes from 2b066be..eb8d070 eb8d070 Merge pull request #311 from wordpress-mobile/issue/310-add-labels-to-link-edittext 6c99e24 wrap DialogLink's EditText inside TextInputLayout git-subtree-dir: libs/editor git-subtree-split: eb8d070e013e9c9ce30d20ba28825831b4689e79 --- WordPressEditor/build.gradle | 1 + .../src/main/res/layout/dialog_link.xml | 40 ++++++++++++------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/WordPressEditor/build.gradle b/WordPressEditor/build.gradle index d13b34740cec..a8109cc0c5e3 100644 --- a/WordPressEditor/build.gradle +++ b/WordPressEditor/build.gradle @@ -48,6 +48,7 @@ android { dependencies { compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:support-v4:23.1.1' + compile 'com.android.support:design:23.1.1' compile 'org.wordpress:utils:1.9.0' // Test libraries diff --git a/WordPressEditor/src/main/res/layout/dialog_link.xml b/WordPressEditor/src/main/res/layout/dialog_link.xml index 910dc40c213d..c303163d0632 100644 --- a/WordPressEditor/src/main/res/layout/dialog_link.xml +++ b/WordPressEditor/src/main/res/layout/dialog_link.xml @@ -1,31 +1,41 @@ + android:layout_height="wrap_content" + android:orientation="vertical"> + + + android:inputType="textUri" + tools:ignore="HardcodedText" /> + + + - \ No newline at end of file + android:hint="@string/link_enter_url_text" + android:inputType="text" /> + +