Page MenuHomePhabricator

Android app doesn't allow text selection for cut-n-paste
Closed, ResolvedPublic

Description

Cut-n-paste from articles seems like something we'd want to support. :)

Nexus 1, Android 2.3.6


Version: 1.0.0 (Android)
Severity: normal

Details

Reference
bz31484

Event Timeline

bzimport raised the priority of this task from to Low.Nov 21 2014, 11:54 PM
bzimport set Reference to bz31484.
brion created this task.Oct 7 2011, 2:49 AM

herman.wong wrote:

We need to access the following native API: http://developer.android.com/reference/android/webkit/WebView.html#emulateShiftHeld()

Would suggest writing a PhoneGap plugin to allow us to access this API.

Is there anything else that could do that? That function is marked as 'Do not rely on this functionality; it will be deprecated in the future.'

herman.wong wrote:

Perhaps creating a plugin to emulate this functionality may to do the trick for us.

http://devemat-androidprogramming.blogspot.com/2011/06/selecting-text-with-webview.html

This menu item doesn't appear to do anything on my Nexus 1, Android 2.3.6. No highlight or selection markers appear; tapping or dragging around after selecting the option does nothing; using the roller ball after selecting the option does nothing.

Same goes for me. I'm not seeing any changes within the UI after a key press hold.

Ok, my first result was from Eclipse seeing the new *web asset files* but not the new *plugin files*. :P

After forcing it to refresh, it now builds the actual selection plugin.

But, it's... not very stable:

[17:25] <brion> it intermittently mostly-works or crashes
[17:25] <brion> when it mostly-works, it first shows a tiny mouse cursor in the center of my screen (i shit you not)
[17:25] <brion> then i can select some stuff and copy it
[17:25] <brion> when it crashes, it just crashes as soon as i select the menu item.

anis wrote:

It's crashing for me too intermittently. I will look into it tomorrow.

anis wrote:

Actually I looked into it nao. Try with this:

https://github.com/nitobi/Wikipedia/commit/856043fd84e1bfa41812703cf24753d299ff7cd0

and let me know how it goes

brion added a comment.Oct 25 2011, 5:34 PM

Ok, that version doesn't seem to crash anymore on my Nexus 1. :)

Here's a test branch that also triggers text selection mode after a tap-and-hold in the web view; it doesn't _quite_ feel natural (it's still doing the weird-ass mouse cursor thing) but on my phone it's closer to the expected behavior.

https://github.com/brion/Wikipedia/tree/hold-to-select

brion added a comment.Oct 25 2011, 8:21 PM

So the ultimate confusion with all this is that as far as we can tell, long touch to activate text selection mode *is baked in to the WebView* and should already work, without us doing anything at all special.

I've checked out the Android 2.3.6r1 source and packaged it up for Eclipse (per http://android.opensourceror.org/2010/01/18/android-source/) so I could step through the code and see what's going on.

(Note: I had to remove a directory tree containing 'mock' interfaces so the real code would show up in Eclipse when debugging.)

WebView.performLongClick() gets called when I've been holding on the screen for half a second or so.

This function does some checks (all of which pass), including a call to super.performLongClick() which should call any standard event handlers or context menu popups that are attached. This doesn't actually trigger anything, so we keep on executing.

It then starts the text selection by calling its own WebView.setUpSelect() function (the same function that emulateLeftShiftClick() calls) and if I understand then attempts to adjust the selection to match the word you were clicking on.

Finally, it calls notifySelectDialogDismissed() and returns false to continue on to other things.

So it *really looks like* it's setting up text selection.... but it doesn't appear to actually happen. What the heck? A minimal test case might be nice here.

brion added a comment.Oct 25 2011, 8:54 PM

Here's a minimal test case to confirm whether text selection works:
https://github.com/brion/WebViewTest

This is a stub Eclipse Android project with a WebView tossed in. Only added code is to load up http://en.m.wikipedia.org/ into the view.

Text selection works when I tap and hold for about a half second on a portion of the text in the web view; tested on my Nexus 1 running 2.3.6, built with Android 4.0 SDK.

So at a minimal level, we know text selection *should be able to work* in a WebView, with no special magic performed by the calling application.

brion added a comment.Oct 25 2011, 9:08 PM

Ok if I call setWebChromeClient() on the WebView, it breaks! Copied a bunch of stuff out of the phonegap DroidGap class, and that was the only one to cause this effect.

// from DroidGap:
//
//this.appView.setWebChromeClient(new GapClient(DroidGap.this));
//this.setWebViewClient(this.appView, new GapViewClient(this));
//
// Including this line kills selection mode activation.
webby.setWebChromeClient(new WebChromeClient());

The WebChromeClient gets called during WebView.performLongClick():

if (mNativeClass != 0 && nativeWordSelection(x, y)) {
    nativeSetExtendSelection();
    WebChromeClient client = getWebChromeClient();
    if (client != null) client.onSelectionStart(this);
    return true;
}

perhaps we're swapping it from a handler that performs the cut-n-paste logic to one that's ignoring it?

brion added a comment.Oct 25 2011, 9:12 PM

in WebChromeClient:

/**
 * Tell the client that the selection has been initiated.
 * @hide
 */
public void onSelectionStart(WebView view) {
    // By default we cancel the selection again, thus disabling
    // text selection unless the chrome client supports it.
    view.notifySelectDialogDismissed();
}

Haha! Another magic hidden public method that's not in the documentation. THANKS, ANDROID.

Here's a branch of callback-android (the new home for phonegap-android) which builds with current Android SDK and lets select-copy work in the WebView:

https://github.com/brion/callback-android/tree/select-tweak

herman.wong wrote:

You'll need to merge the select-tweak into https://github.com/hermwong/phonegap-android since we have some custom changes for the menu plugin that have not been pulled into the main callback-android repo.

Are those the Honeycomb changes that cause the app to crash on Honeycomb? Not sure we want that... :)

bowserj wrote:

Upgraded PhoneGap by merging the menu code with the selection tweak code. These two commits have the upgrading of the JAR.

https://github.com/nitobi/Wikipedia/commit/b615af6b9f3a90c2a804da8abed482b2b272817a
https://github.com/nitobi/Wikipedia/commit/2c0f9294996672fa055984c360a46b0dba262b75

brion added a comment.Nov 2 2011, 9:55 PM

The app no longer builds, as the .classpath still refers to phonegap-1.1.0.jar which no longer exists.

Fix in pull request: https://github.com/nitobi/Wikipedia/pull/52

With this it seems to work reasonably nice on my Nexus 1 / Android 2.3.6 -- tap-and-hold on text selects a word or a few, more as I expect. Yay!

brion added a comment.Nov 2 2011, 10:06 PM

this has been merged, all is well :D

I am not able to tap and hold on my Sony Xperia Neo.

Sorry, found how to do it.