Saturday, October 1, 2016

Presentation @ Google Developer Group LA

Finally got to put all bits of information together and present on connecting JavaScript to Java on Android. It so happens that the hosts, Victorious, talked to me afterwards and indicated that they had to solve the same problem! They added a very valuable hint: when deploying to the Google Play store you have to exclude any HTML/JS files from being processed (obfuscated) by ProGuard, otherwise any reference to injected Java objects will be rewritten causing obvious problems.

Slides can be found here.

Sunday, September 25, 2016

Integrating Third-Party JavaScript Widgets with Angular

I was working on integrating a gauge widget into an Angular directive. The widget requires to be setup in JavaScript that looks something as follows:


       var gauge = new JustGage({             
              id : "the-element-id",             
              min: 0,             
              max: 100,             
              title: "Gauge Title"        
       });

It makes sense to represent the gauge widget as its own directive, so I created a directive for that purpose. Then, this imperative/procedural code that manipulates the DOM should go in the 'link' function of the directive. So far so good.

The gauge requires an element to render itself. The 'id' attribute of the gauge configuration specifies the ID of that element, which. In my case, I needed to specify the ID dynamically. The ID of the gauge element was supplied to the directive via an attribute:

      <my-gauge id="{{model.id}}" ... ></my-gauge>

On first attempt to run, I got an error saying that the element was not found. After debugging a bit and Googling a lot, I found out that the problem is mentioned in this post:

     "Notice that when the link function runs, any attributes that contain {{}}'s are 
     not evaluated yet (so if you try to examine the attributes, you'll get undefined)."

Then came $observe to the rescue. $observe, as opposed to $watch, allows you to listen on changes in the attributes of the directive element via the 'attrs' object passed to the link function. So in my link function I did: 

      attrs.$observe('id', function(newValue) {
           // initialize the gauge here
      };
   
And that solved the problem.

Sunday, June 19, 2016

Conditional Injection - Testing Angular App on Android

I was developing an Angular-based app inside an Android app. That is, my Android Activity incorporated a WebView that loaded HTML/JavaScript pages that use Angular.

The Java code in the Andorid app communicated to with the JavaScript running inside the WebView by injecting a Java object into JavaScript as discussed in a previous post.

Cool! So?

The problem arose when I wanted to test the app. One could simply test the app within the Android IDE (Eclipse in this case), but the development cycle is much faster if I could just use a browser as one would normally do when building a web app. But the JavaScript depends on the injected Java objects in order to function, so how can I test my app in the browser in absence of the Java code that injects these objects. 

I embarked on the following solution:

  • Encapsulate each of the injected Java objects in a "wrapper" Angular service.
  • Also create Angular services that mock each of the wrapper services.
  • When running in Android instantiate the wrapper services, otherwise instantiate the mock ones.
The trick here is that both services, the wrapper and corresponding mock, have the same name and only one of them can be instantiated in a particular run. Since there is no built in conditional compilation/inclusion directive akin to that of C, I came up with my own using a directive that looks like this:

       <script conditional-load
                  if-defined="<JavaObjectReference>" 
                  else-load="<MockService.js>" 
                  then-load="<WrapperService.js>">




The core code for the directive checks whether the specified reference is defined and loads either script accordingly.


.directive("conditionalLoad", function($window, ScriptLoaderService) {

    return {
        link: function(scope, element, attrs, controller) {
            ...
            if ($window[attrs.ifDefined]) {
                ScriptLoaderService.loadScript(attrs.thenLoad);
            }
            else if (attrs.elseLoad) {
                ScriptLoaderService.loadScript(attrs.elseLoad);
            }
        }
    }
})

With an instance of this directive for every injected Java object we are left with writing the ScriptLoaderService, which turns out to be non-trivial, and I will leave this to a separate post.

Monday, February 15, 2016

Twitter Broken GeoCode API

Using the GeoCode parameter with the Twitter Search API did not work for me (returned no results). A quick search turned up this post on the Twitter Community pages. It seems that issues were reported with this API over a year ago. Since then, it has not been fixed and now they say it may never be fixed!  I am not going to rant about how shameful it is to have a public API that you know does not work and just leave out there, but ...

Sunday, January 24, 2016

Angular Multiple ng-transclude

Quick post that may save someone a lot of time and frustration. Transclusion is a great feature in Angular (see the documentation). The ability to use multiple ng-transclude bits in your template makes it even greater. However, it took me a long time to get the simplest example to work. The problem was that each occurrence of ng-transclude in my template, even though assigned to a specific slot, did end up including everything from the HTML and not just the specified slot.

I compared my work to other examples that did work and found no difference at all in the template, the HTML, nor the code.

Long story short, I noticed in some article on the subject that the multiple transclusion feature was added to Angular starting 1.5. I went to check my bower.json and indeed, I was on angular 1.4.8. Upping the version to 1.5 did solve the problem.

Thursday, January 7, 2016

Android: From Java to JavaScript and Back


After the fight with Android Studio and Eclipse yesterday, I finally got to try what I was planning to do all day:

  • From within JavaScript running in a WebView, Invoke Android Java code , and  
  • From Android Java code, inject JavaScript code into an HTML page in a WebView. 

There are a few samples that can be found for both routes, either in the official docs or in blog posts. However, there were some tricks to get even the simplest example running.

Other than the standard issues of having to enable JavaScript in the WebView and annotate the callback method, here are a couple of interesting ones that I faced trying the first route:

  • Default WebViewClient: Attempted to run some trivial JavaScript inside the WebView to make sure it is enabled. The simplest of examples (popping an alert) did not work. After looking around, it turns out that you must set a WebViewClient on the WebView, even if it is simply the default one from the SDK. Apparently, it provides a method that handles showing alerts to make them fit with the theme, etc.
  • Method Signature: The simplest of examples for calling Java from JavaScript did not work for me. I was getting a "Error calling method on NPObject" message. After trying a couple of things I decided to post on StackOverflow, but I had to double check my code before doing so. And guess what I found? I found that the method signature called from JavaScript did not match the one declared in Java. Fixing that got the sample to work.

The second route was not a smooth ride either. Here are a few issues I encountered:


  • Threading: Apparently, any interaction with a WebView has to happen on the 'same' thread (as the error stated). I figured that this meant the event thread, and indeed scheduling the injection of JavaScript using runOnUiThread solved this.
  • Navigation: Upon successful execution of JavaScript code, the original page content was replaced by the result of the JavaScript statement that just executed. Apparently, this is to be expected and others have solved this by appending a void(0); to the JavaScript code. The reason is explained here in detail.
  • Arguments: When passing JavaScript code as a String do not forget to quote values that are supposed to be String valued. This simple advice might save you considerable time if you are passing around element IDs. Apparently, when an element ID appeared in the JavaScript unquoted the interpreter took at it as a reference to the element!


Wednesday, January 6, 2016

Android Studio and ADT on Ubuntu

This was not a fun day.

I spent hours trying to set up Android Studio on Ubuntu. I followed the instructions given here (after eliminating the extra spaces in the URL) and things seemed to be going. After the install, I went through some gyrations to get the Studio to update and run. I imported a project and tried to run it and the IDE started throwing exceptions in the logs. I fought with it a bit, trying to update the SDK, tools, and installing the missing 32-bit C libraries as suggested in this post (only a few are actually needed, it seems, but I installed all just in case). However, new errors kept popping up and I finally decided to go back to what I know; the Android Eclipse plugin.

It was a surprise to me that Google removed the installation instructions for the Eclipse plugin from their site in an attempt to push the Android Studio. And indeed they tout it as the official IDE now and urge people to migrate.

Anyways, I got the plugin from the Eclipse Marketplace. After installing the SDK (again) and trying to run a sample, Eclipse complained about inability to connect to adb, and kept retrying. I went ahead and manually started the adb server from the command line and this did work, at least the first time. Thereafter, Eclipse complained that the connection was lost and prompted me to start both adb as well as Eclipse itself! After having to do this a couple of times, I realized that the SDK path pointed to the first one I installed, which did seem to be broken in some way. Fixing the path got things working again.

Next, I went back to my project but it had compile errors. Apparently, in the initial SDK install I picked (somehow) just API level 17, whereas the feature I wanted required API level 19. I went back and downloaded both 19 and 23 (the latest) which took forever. Finally, I was up and running!