Thursday, October 2, 2014

iOS and Android - Communication between JavaScript and Native

Many of us work on hybrid applications where the core part of the functionality is written in HTML / JavaScript, loaded on a WebView. And we write native code to perform things that JS cannot do, like accessing the Gallery, writing files to the system, creating a native database, transacting with it and much more. For the application to perform seemlessly we need a bridge to call JS methods from native code and Native methods from JavaScript. PhoneGap plugins work on the same principle. In this post, I will cover ways on both Android and iOS to communicate with JavaScript.

Let us first create an HTML file, which we will load on the UI Webview and will use to interact with native
<html>
  <head>
    <script type="text/javascript">
      function loadURLInIframe(url)
      {
        document.getElementById("myFrame").src = url
      }
      function sayHello()
      {
        nativeInterface.say("Hello");
      }
</script>
</head>
  <body style="width:100%; height:100%; margin:0px;">
    <iframe id="myFrame" src="about:blank" style="width:100%; height:400px;"></iframe>
    <a onclick="sayHello()" href="#">Say Hello to Native</a>
  </body>
</html>

In the HTML code above, we have one iFrame with a blank URL. We've defined a method in JS named "loadURLInIframe", which accepts one parameter "URL" that will change the URL of the iFrame. We'd call this method from the native code. There is another span, which calls the other JS method "sayHelloToNative", which we will use to call the native methods.

Now let us write the native code to communicate with the JavaScript in the HTML above. We will start with the easier one i.e. Android. Android provides a very easy mechanism to communicate with the JS loaded on the WebView. Let us first look at a way to invoke JS method from native Java.
webView.loadUrl("javascript:loadURLInIframe(' " + "http://techiepulkit.blogspot.in" + " ');");

What are we doing here? We asked webview to loadUrl("javascript:"), which means that we are asking webView to execute a JS method. The name of the function is followed by ":" and then the parameter in brackets. Note the single quote (') on both sides of the parameter. This is important to pass a string parameter. That's it. The JS method will get called and it will load the URL in the iFrame.

Now, coming to the second part, calling native method from JavaScript. For JavaScript to be able to call native methods, we need to attach a JavaScript interface to the Webview. Let us see, how.

First, create a new class that would contain the methods to be invoked from Native.
public class JSInterfaceManager
{
    @JavascriptInterface
    public String say(String message)
        {
            Log.d("MyLogs", "Message from JS: " + message);
        }
}
In the class above, we have created one method "say" which accepts one parameter as String. Notice the @JavascriptInterface declaration on top of the method. This declaration exposes the method to JavaScript. Let us now associated this class with the interface.
webView.addJavascriptInterface(new JSInterfaceManager(), "nativeInterface");

Now that the JSInterfaceManager has been associated with the Webview, a new Object with the name "nativeInterface" will be available to the JavaScript now. Now when you click on the span "Say hello to native" in the HTML loaded in Webview, it will call nativeInterface.say method, which will invoke the say method inside the JSInterfaceManager class. Now each time you click on that span, you will get a log saying "Message from JS: Hello" in the Logcat. Note that you can also return a value in the native method and JS method will be able to receive it. That is it! We have now covered the 2 way communication with JS on Android.

Let us now repeat the same steps for iOS

First we will see calling JS method from ObjectiveC. The syntax to call a JS method from ObjectiveC looks as follows:
NSString *jsScript = [NSString stringWithFormat:@"loadURLInIframe('%@')", @"http://techiepulkit.blogspot.in"];
[webView stringByEvaluatingJavaScriptFromString: jsScript];

Pretty much similar to how it worked on Android. The only notable difference is that we don't need to prefix "javascript:" in the script to invoke the JS Method. Once this code is executed, it will load the URL in the iFrame. Simple ain't it! Well the second part i.e. calling native methods from JS is not that simple. Let's see how that works.

Before iOS 7, there was no direct way of calling native methods from JavaScript, all we had were workarounds like changing the URL of the Webview and listening the change on the delegate method. This was both slow and tedious. With iOS7, there came JavaScriptCore. JavaScript core allows the developers to create a JSContext and use it for direct communication. There are multiple uses of JavaScriptCore but I'd cover only the communication with WebView. In principle, this works quite similar to the way JavaScriptInterface on Android does but the implementation in native is quite different.
We will first define a Protocol as follows:
@protocol MyJSExport <JSExport>
  - (NSString *)say:(NSString *)message;
@end
};
We have defined a protocol which implements JSExport interface. Implementing it makes all the methods and properties in the protocol visible to JS. Now we will create a class "MyNativeInterface" that implements this protocol and has the actual implementation of the method.
@interface NativeInterface : NSObject
-- Any method not visible to JS can be defined here.
@end

@implementation NativeInterface
  - (NSString *)say:(NSString *)message
{
    NSString *printMessage = [NSString stringWithFormat:@"Message from JS: %@", message];
    NSLog(printMessage);
}
@end
Now, we have the class implementation ready and we just need to hook it with the Webview's context.
//First get the JSContext from JS.
JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//
MyNativeInterface *nativeInterface = [[NativeInterface alloc] init];
context["nativeInterface"] = nativeInterface

So, we created an instance of the class that exposes the methods to JS and set it as nativeInterface on Webview's JSContext. This would mean that the JS now has a variable named "nativeInterface" and it can call it's methods. So, now when the user clicks on the span, it would invoke native method, which will log the message sent by JS. And that's it, now we have covered 2 way communication between JS and ObjectiveC on iOS as well.

Before I conclude, I would like to share a few tips, which you should keep in mind while working with JavaScriptCore:
  • JSContext can be used to invoke JS methods also. Using [context evaluateJavascript], you can call JS methods but it is erratic in nature. The application crashes randomly when using this approach so one should continue using [webView stringByEvaluatingString]
  • We generally tend to set webview's URL to "about:blank" before destroying it to force it to release memory but when used with JavaScriptCore, it works the other way round. It doesn't release the instance of Webview at all and with each instantiation, it keeps accumulating memory.
  • Do not forget to set the context references to nil on destroy. A strong reference of these objects is created and is important to be destroyed. In this case, context["nativeInterface"] = nil should be called
  • When calling native methods from JS, always wrap the call in setTimeout of 0. This ensures that the calling is done in a thread safe manner
  • When calling methods with multiple arguments, the function name gets modified when exposed to iOS. So, a method -(void) doSomething (NSString *)param withOption:(NSString *), the methodName that gets exposed will be doSomethingWithOption(arg1, arg2) instead of doSomething.

The JavaScriptCore is still in nascent stages on iOS and has a lot of quirks, still it is the fastest and the most convenient way to communicate with JS. The best part is that we are able to reuse the same approach on iOS and Android. In this post, we have just scratched the surface of JavaScriptCore and the possibilities are immense.

Do pass on any feedback or questions that you may have here.

Sunday, September 28, 2014

Gradle - The next generation ANT (Build management tool adopted by Google)

All those who've worked on automating the builds must have worked on ANT. ANT was a simple tool but extremely effective in making the builds automated. It had a simple XML based structure, allowed you to break building pieces into tasks and easily use external JARS. You could also create your own tasks.

However, I think Ant has not evolved as we would have wanted. The XML syntax, despite being easy to use is tedious. Time to analyze the other players on the block.

A few popular ones out today are:
  • Maven
  • Gulp
  • Grunt
  • NodeJS
  • Gradle
Among the ones listed above, Maven has been around for long and is quite close to Ant and has similar limitations. Among others, I found Gradle to be the most impressive. The best part about Gradle is that it doesn't reinvent the wheel, it makes use of the existing systems and provides a simple language, similar to Groovy to write your build scripts. Gradle readily adds support for or has plugins available for the following:
  • Ant Tasks
  • RequireJS
  • Google Closure Compiler
  • GZipJs
  • ...and many more

The biggest endorsement for Gradle comes from Google itself who've based their new Android IDE, Android studio completely on Gradle, need I say more? The flexibility that Gradle provides is amazing, you can use Gradle to:
  • Build JS Projects
  • Build Android projects
  • .. And also build XCode Projects
This is amazing as I can rely on one tool for all my build management. In this post I will share the approach to automate the builds of a JS project and share some basic info about Android Studio and XCode support.

To understand the examples better, you can read basics about gradle here:
Gradle Homepage
Gradle JS Plugin

Download Gradle at: Gradle 2.1

Let's get started! First create an empty text file and name it "build.gradle". Second thing we will do is define the plugins we are going to use for this
buildscript {
  repositories {
      jcenter()
  }
  dependencies {
      classpath "com.eriwen:gradle-js-plugin:1.12.1"
  }
}
That's all we need, Gradle will itself download and install all plugins defined when build script is run. As we move ahead, some of the things will seem strikingly similar to Ant. Gradle leverages on ANT and the terminology it uses is also quite close. The methods are called tasks and variables are accessed using $(var). Just like Ant, Gradle allows defining configurations in a properties file. The name of the default properties file is gradle.properties. Any property defined in the properties file can be used using the varName syntax. Let's see an example:
Create a new text file "gradle.properties" and add a variable as follows:
message=Hello World

And this is how we use it in the script:
println "Just want to say ${message}";

Simple, ain't it! That's not all. Gradle also provides a powerful mechanism of creating custom properties files and using both the default one and the custom one. This is particularly useful when you want to customize build according to themes but don't want to specify the common properties repeatedly. We will see it in action in the tutorial that follows:

In this tutorial, I will create a Task, which will first combine a list of JS files, combine them and then Obfuscate the same using Google Closure compiler.

First thing we will do is load a properties file
Properties props = new Properties() props.load(new FileInputStream("${theme}.properties"));

Here, we loaded a custom properties file based on the theme parameter. Now, I will create the tasks to Combine the JS files. We will create 2 tasks, just to demonstrate use of custom properties.

task combineCommonJS(type: com.eriwen.gradle.js.tasks.CombineJsTask)  {
    List list = new ArrayList();
    if(flavor == "mobile")
    {
        mobile_common_scripts.split(",").each{String f -> list.add(f)}
    }
    else
    {
        common_scripts.split(",").each{String f -> list.add(f)}
    }
    source = list;
    dest = file("${buildDir}/common.js")
}

task combineCustomJS(type: com.eriwen.gradle.js.tasks.CombineJsTask) {
    List list = new ArrayList();
    if(flavor == "mobile")
    {
        props.mobile_custom_scripts.split(",").each{String f -> list.add(f)}
    }
    else
    {
         props.custom_scripts.split(",").each{String f -> list.add(f)}
    }
    source = list;
    dest = file("${buildDir}/custom.js")
}

So, here, we created 2 tasks, both of type CombineJsTask. The idea here is to fetch a list of JS files from properties and combine them. In the first task, the list of JS files was picked from the default properties and in the second one, it was picked from the custom properties. In the second one, we also used a condition to check whether we are making a build for mobile flavor or not. Here flavor is just another variable which can be anything but when working with Android studio, flavor is special but will talk about that later. Variables can also be provided as input parameters, to a gradle script, but that for later.

By now, we have written code to combine all JS files into 2 files common.js and custom.js. Now, we'd create tasks to minify these using Google Closure Compiler.
task minifyCommonJS(type: com.eriwen.gradle.js.tasks.MinifyJsTask) {
    source = file("${buildDir}/common.js")
    dest = file("${buildDir}/common.min.js")
    closure {
        warningLevel = 'QUIET'
    }
}

task minifyCustomJS(type: com.eriwen.gradle.js.tasks.MinifyJsTask) {
    source = file("${buildDir}/custom.js")
    dest = file("${buildDir}/custom.min.js")
    closure {
        warningLevel = 'QUIET'
    }
}

So, now we have all the tasks ready but we need to define the dependencies so that they execute in the correct order. For that we'd add a few more lines to the gradle script:

minifyCommonJS.dependsOn combineCommonJS minifyCustomJS.dependsOn combineCustomJS combineCommonJS.dependsOn minifyReaderJS

We have defined dependencies in a way that when we call minifyCommonJS task, it will execute all other tasks due to the defined dependencies. That's it, we have the script ready. Before we execute, let us see how to define JS files in the properties file.

gradle.properties:
common_scripts=lib/a.js,lib/b.js,lib/c.js
mobile_common_scripts=lib/a.js,lib/b.js,lib/mobile.js

defaulttheme.properties:
custom_scripts=defaulttheme/1.js,defaulttheme/2.js

customtheme.properties:
custom_scripts=customtheme/1.js,custom=theme/2.js

Here, we have created 3 properties files, one default one and 2 for the themes. Take special care that there is no space between the file names and no trailing spaces. Now let us see how to execute this gradle script.

Assuming you've added gradle to the Path. Open command line, locate to the folder containing the build.gradle file and type:
gradle minifyCommonJS -Ptheme=defaulttheme -Pflavor=mobile

Here we have instructed gradle to run the task minifyCommonJS and provide 2 properties as input, theme and flavor. The properties need to be defined using the -P syntax. Note there is no space after -P.

When this task runs, the defaulttheme.properties file will be loaded and from the scripts, mobile_common_scripts will be compiled in the combineCommonJS task. We can change the input properties to change the behavior as follows:
gradle minifyCommonJS -Ptheme=customtheme -Pflavor=web

Once the task executes, we will have the minimized and obfuscated output for the JS files. That's it! We have successfully created and executed a gradle build.

This concludes the tutorial but we have just about touched Gradle. The possibilities are endless. Gradle for Android brings an interesting concept of flavor. With the help of flavor, you can maintain different assets per flavor and at the time of compilation, it will only copy the chosen flavor. Makes life easier. To explore the full powers of Gradle on Android, use Android Studio.

Gradle for XCODE is something I am yet to touch but you can read about it here: Gradle XCode Plugin

I hope this post helps you in getting familiar with Gradle. For any queries, feel free to email me

Sunday, September 21, 2014

SQLite Full Text Search on Mobile devices (FTS3)

When we talk about Full Text Search, the libraries that come to mind are Solr, ElasticSearch etc. However, all of these are back end libraries and require the indexes of the files to be created first.

When we talk about searching inside the mobile apps (without hitting the server), either these libraries do not have the client side implementations or are too heavy to be included in an app.

This is where Full Text Search engine of SQLite (FTS3) comes to rescue. FTS3 provides a lot of methods to perform full text search on the database using SQL statements. Although, both iOS and Android support SQLite out of the box, however, even using the same with libraries like SQLCipher is quite easy and doesn't require embedding huge libraries into the app either. I am not going to cover the differences between FTS3 and FTS4 as all that information is available on the link I share below.

All the methods provided by FTS3 with examples are listed here http://www.sqlite.org/fts3.html
. I recommend that you do keep referring to the link if anything is not clear as I will not cover the definition in detail but only the basics of FTS3 and share a code snippet for Android.

The FTS3 and FTS4 extension modules allows users to create special tables with a built-in full-text index, "FTS tables". The full-text index allows the user to efficiently query the database for all rows that contain one or more words also known as "tokens", even if the table contains many large documents.

To create a virtual table, the following statement can be used.
    CREATE VIRTUAL TABLE enrondata1 USING fts3(content TEXT);
This makes the table eligible for Full text query

When the WHERE clause of the SELECT statement contains a sub-clause of the form " MATCH ?", FTS is able to use the built-in full-text index to restrict the search to those documents that match the full-text query string specified as the right-hand operand of the MATCH clause.

The fast full text query looks as follows:     SELECT * FROM mail WHERE subject match 'database';

FTS3 and FTS4 provides three special auxiliary functions that are very useful to the developers:"snippet", "offsets" and "matchinfo". As the SQLite portal states: "The purpose of the "snippet" and "offsets" functions is to allow the user to identify the location of queried terms in the returned documents. The "matchinfo" function provides the user with metrics that may be useful for filtering or sorting query results according to relevance."

In this post, I am going to talk about the Offsets method only as that is the one I found to be most effective if you have to perform a full text search and fetch an excerpt. Although, the snippets function seems to be the one to fetch excerpts, it actually doesn't work as per its name.
The offsets() function returns a text value containing a series of space-separated integers. For each term in each phrase match of the current row, there are four integers in the returned list. Each set of four integers is interpreted as follows:
Integer Interpretation
0 - The column number that the term instance occurs in (0 for the leftmost column of the FTS table, 1 for the next leftmost, etc.).
1 - The term number of the matching term within the full-text query expression. Terms within a query expression are numbered starting from 0 in the order that they occur.
2 - The *byte offset* of the matching term within the column.
3 - The *size* of the matching term in bytes.
Important thing to note here is that the offset is a byte offset and not a character offset.
More details on the same can be read here: http://www.sqlite.org/fts3.html#section_4_1

Let us see an example of Offsets function being used in Android.

First we will create a new virtual table:
database.execSQL("CREATE VIRTUAL TABLE mail USING fts3(subject, body);");
ContentValues contentValues = new ContentValues();
contentValues.put("subject", "Subject");
contentValues.put("body", textContent);
database.insert("mail", null, contentValues);

Now that the table is ready and has one record, we will write the FTS query as follows:
Cursor myCursor = database.rawQuery("SELECT offsets(mail), body FROM mail WHERE mail MATCH 'Republic';", null);

Here along with the offsets, I am also fetching the full text so that I can extract excerpts from the same.
myCursor.moveToFirst(); // Move cursor to first location
String[] offsets = myCursor.getString(0).split(" "); // Split to " " to read integers
String text = myCursor.getString(1); //Store complete body in a variable
byte[] textBytes = text.getBytes(); // Convert text to bytes
ByteArrayInputStream ba = new ByteArrayInputStream(textBytes);
int i = 0;
ArrayList results = new ArrayList();
int textLength = textBytes.length;
ExcerptFinder excerptFinder = new ExcerptFinder(ba); // Provide the stream containing text bytes to ExcerptFinder
while (i < offsets.length)
{
  //Term and column index are ignored because we've searched for a single term only.
  int startOffset = Integer.parseInt(offsets[i + 2]);// Find the start index of searched term
  int endOffset = startOffset + Integer.parseInt(offsets[i + 3]);// Find the end index of searched term

if(startOffset < 0)
{
    startOffset = 0;
}

if(endOffset >= textLength - 1)
{
     endOffset = textLength - 1;
}
  String excerpt = excerptFinder.readFullWords(startOffset, endOffset);
  results.add(excerpt);
  i += 4;
}

So, here we extract excerpts for the searched term by first identifying its start index and end index in bytes. The same process continues for all the results for the row. In this example, we are working with a single row of data, otherwise there would be one more loop for the rows. Since we need to run forwards and backwards in the ByteArray Stream, the ExcerptFinder class I created extends the RandomAccessStream class provided at: RandomAccessStream.java

The relevant code in the Excerpt Finder class goes as follows:
public final String readFullWords(int start, int end) throws IOException
{
  int startOffset = start;
   int endOffset = end;
  byte[] singleByte = new byte[1];
  int currentCharacter = 0;
  int i = 0;
  int spaceCount = 0;
  while(currentCharacter != RETURN_DELIMITER && spaceCount < NUMBER_OF_WORDS)
  {
    startOffset = start - i;
    seek(startOffset);
    if(startOffset <= 0)
    {
      startOffset = -1;
      break;
    }
    read(singleByte, 0, 1);
    currentCharacter = singleByte[0];
    if(currentCharacter == 32)
    {
      spaceCount++;
    }
    i++;
  }
  currentCharacter = 0;
  i = 0;
  spaceCount = 0;
  int readBytes = 0;
  while(currentCharacter != RETURN_DELIMITER && spaceCount < NUMBER_OF_WORDS)
  {
    endOffset = end + i;
    seek(endOffset);
    readBytes = read(singleByte, 0, 1);
    if(readBytes < 0)
    { //
      endOffset = endOffset - 1;
      break;
    }
  currentCharacter = singleByte[0];
  if(currentCharacter == 32)
  {
    spaceCount++;
  }
  i++;
    }
  seek(startOffset + 1);
  byte[] result = new byte[endOffset - startOffset - 1];
  readFully(result, result.length);
  return new String(result);
}

In the code above, the rule for an excerpt is defined by a RETURN delimiter or NUMBER_OF_WORDS before and after the term. The logic can be tweaked to anything that you want. The key here is to play with the bytes offsets returned by the FTS3 query. Although, we are performing operations at a byte level, the code executes much faster then a RegEx performed on plain text. The search can be made faster by simply fetching a predefined number of bytes before and after the search term without worrying about the number of words.

FTS can be very handy when it comes to providing offline search to mobile apps. You can create the DB at the backend and simply download it on the app to run the FTS query on it. This concludes the post and I look forward to the comments.

If you need the source for the Android app to help you get started, feel free to email me.

iFrame & iOS 7 Safari (Issues, workarounds & limitations)

As a developer, I must admit that iFrame does make developer's life easy but they are not browsers' best friends. We are using iFrame in one of the ebook reader products we are creating and really struggled to get it working on iOS 7. There are multiple ways to work with iFrame on iOS 7:
  1. Setting the iScroll's scrollable property to "yes" doesn't make the iFrame scrollable but actually increases the height of the iFrame to match it's content. So, if you specified the height of iFrame to be "600px" but the content is 12000 px, the iFrame's height actually becomes 12000px which means the parent document's body scrolls and not the iFrame itself, which may defeat the purpose of using an iFrame.

  2. Another approach that I discovered by reading blogs was to wrap the iFrame inside a container div with a fixed height and set the iFrame's height to match it's content and set it up as follows:

    <div style="overflow:scroll; position:absolute; -webkit-overflow-scrolling: touch; width:100%; height:100%;">
        <iframe scrolling="no" style="width:1000px; height:6000px;" src="my_page_url.html"></iframe>
    </div>
    As you can notice, the container div has a fixed height where as the iFrame is larger to match its content. The most important thing here is the style applied on the parent div i.e. "style="overflow:scroll; -webkit-overflow-scrolling: touch;". This is what informs the webkit browser that the div is supposed to scroll and should scroll on touch. Unless you specify this style on the container div, the div will NOT scroll on iOS. You can follow the same approach on all WebKit compatible browsers. This approach will work and make the container div only scroll, however, this approach has it's caveats and we will talk about it later in the article.

  3. The third and final approach I discovered is only feasible when you've control over the document that is loading inside the iFrame. This involves modifying iFrame document's DOM which is possible only when loading documents from the same domain as the parent. In this approach we'd turn the iFrame's scrolling to "no" and also keep it to a fixed size so that the containing div or the parent body doesn't scroll. To achieve the scroll we'd need to inject a container div into the iFrame body and make it overflow. The div can be inject using a code snippet like this:

    iframeDocumentBody.innerHTML = "<div style="overflow:scroll; position:absolute; -webkit-overflow-scrolling: touch; width:100%; height:100%;"> + iframeDocumentBody.innerHTML + </div>

    This will ensure that you actually have a scrolling iFrame.

Now that we have seen 3 different approaches to get the iFrame to work, let us understand the caveats that each one of the above possesses:

  • Approach #1 has the biggest caveat that the iFrame itself doesn't scroll and the container document scrolls.
  • Both approach #1 and #2 present memory issues on iOS Safari. As the iFrame goes longer, the iOS Safari consumes more virtual memory to load the same. So, if you have a 20000 px long iFrame and it contains an HTML video or audio element, the page will CRASH on load. Not certain what happens here but it seems that iOS tries to create a huge place holder for the iFrame and crashes while allocating memory. The memory management works better in case of approach #3 and the page doesn't crash with the same content.
  • Although, following approach #3 solves the problem with crashing on load, however, with all 3 approaches, the iFrame with huge overflow and a media element (audio / video) will crash when you scroll at a very high speed. So audio or video inside the iFrame are not liked by Safari and should be avoided. Best if they can be added to the main document instead.
  • Text Selection: The selection of text works fine with all the three approaches, but with approach #2 and #3 when you scroll the page, the selection doesn't move along with the scroll. This is a bug with iOS safari, which means that it is unable to move selection with any overflowing element doesn't move with the scroll. Sadly, I haven't found any workaround for the same yet. If anyone knows about any work around, please let me know too :D

  • So, as I covered above, there are several issues with the iFrame on iOS 7. I have not got my hands dirty on iOS 8 so can't say what will change. Till that time, I would recommend NOT using iFrame till it is absolutely important. If you still do, I hope the tricks above will help you. If you have anything to add or want to suggest a better approach, please comment and I will modify the post.

Friday, February 7, 2014

Android: Overriding the default contextual action bar for text selection in WebView (Android 4.1+)

We had to create an Android app where user annotates using the text selected in an HTML page. We used the standard Webview component of Android to render the web page.

By default Android's Webview displays the standard ActionBar with Copy, Paste and other buttons when the text is selected. I wanted to either disable the Contextual Action Bar (CAB) or display a CAB with my own set of buttons. Android Webview doesn't provide ability to do either of these.

People had commented on various StackOverflow posts to either use LongPress listener or use registerContextMenu. If we setup the LongClick listener, the selection stops working and registerContextMenu doesn't work in the desired manner either.

After delving deep into the Webview code, I found out that WebViewClassic class was invoking startActionMode method of the Webview. This is all we needed. I created a new class by subclassing the Webview class and overrided the startActionMode method. After that, I just used the extended Webview instead of the default one and that was it.

The overridden method looks like this:

        public ActionMode startActionMode(ActionMode.Callback callback)
          {
                 actionModeCallback = new SelectActionModeCallback();
                return super.startActionMode(actionModeCallback);
          }



As you can note in the code above, instead of sending the ActionMode.Callback sent by Webview classic, I simply created a custom ActionMode.Callback with desired items and sent it to the subclass. This ensured that the selection worked perfectly fine and my custom CAB showed up in place of Android's default menu, each time text was selected. Works like a charm.

However, there is a small caveat in this solution. The CAB doesn't get closed by itself as the Android's default CAB and I had to write code to close it at appropriate places. Hope this helps someone.