Sunday, March 12, 2017

Forwarding GoDaddy domain to Google app engine

If you purchased your domain through GoDaddy and want to forward its traffic to an application hosted on Google App Engine, don't start at GoDaddy domains settings, it'll just complicate the process.

Start at Google cloud console. Here is a link to the only instructions you need. The process is simple: Click Add Custom Domain under  {my app} -> App Engine -> Settings -> Custom Domains and follow the prompts.  Google will verify that you own the domain and then allow you to configure domain forwarding.  After configuration is saved, it'll take up to 24 hours for the changes to take effect, but it's usually faster than that.

At this point sub-domain forwarding should work.  To forward your naked domain, you need to login to GoDaddy, go to My Account -> Domains -> {domain name} -> DNS Zone File and add all A and AAAA records listed on Google cloud console.  Use @ as a host name when adding the records. Give it some time before testing.  In my case it took about 5 minutes for the changes to take effect, but it may take longer.

Saturday, March 11, 2017

Deploying a Polymer app to Google App Engine

The steps that are required to deploy a Polymer app to Google App Engine are documented and there is no need to repeat them here.  However, some changes are required to make everything work end to end.

First, polymer build command creates an unbundled output and places it to build/default folder. GAE seems to serve resources via HTTP/2 with server push just fine, so there is no need to create create a bundled output.

Second, since all output files by default are in build/default folder, you need to change static_files in app.yaml file  from build/bundled/... to build/default/....  Another option is to simply rename build/default folder to build/bundled folder.

Third, appcfg.py -A {project name} update app.yaml will more likely fail.  I had to add --oauth2 option for deployment to succeed:  appcfg.py --oauth2 -A {project name} update app.yaml

Wednesday, March 8, 2017

Tips to reduce mobile data usage

After switching to Project Fi, I started tracking my mobile data usage.  Below is the summary of what I've done so far to reduce my bills.

Google Play Music

Turn on Stream only on Wi-Fi in Settings
If you want to stream over mobile network, set Quality on mobile network to Low.

YouTube

Turn on Limit mobile data usage in Settings -> General.  This will only stream HD videos on Wi-Fi.

Google Photos

Turn off Cellular data back up in Settings -> Back up & sync

Google Play

In Settings (General) set Auto-update apps to Auto-update apps over Wi-Fi only. 

Google Plus

Turn on Conserve data usage in Settings (General)

Google Maps

Turn on Wi-Fi only in Settings.  Click on your offline areas link and save the area around your home.  Google Maps can save areas larger than a half of the state of Washington in size.

I'll update this post when I learn about other apps that consume a lot of data.  Project Fi app reports data usage per app, so it's easy to track apps that abuse mobile data.

Friday, March 3, 2017

Using CKEditor in WebView on Android

CKEditor is a powerful rich text editor for web applications.  This post shows how to integrate the editor into an Android application and load a sample page into a WebView.

  1. Download CKEditor.
  2. Unpack Zip file and move ckeditor folder to $project.projectDir/src/main/assets folder.
  3. Create a WebView in your layout file.
  4. After inflating the layout file, find WebView and enable JavaScript: webView.getSettings().setJavaScriptEnabled(true);
  5. Load sample app into the WebView: webView.loadUrl("file:///android_asset/ckeditor/samples/index.html");
When the application is launched, it'll look something like this:




Thursday, March 2, 2017

Loading WebView from a resource file on Android

This pattern worked for me:

final Uri uri = Uri.parse("file:///android_res/raw/maptest");
webView.loadUrl(uri.toString());

where maptest.html is a file in res/raw folder.  Replacing maptest with maptest.html also works.

The following works to load a file from assets directory:

final Uri uri = Uri.parse("file:///android_asset/maptest.html");
webView.loadUrl(uri.toString());

Note the use of .html extension when loading from assets directory.  When the extension is removed from the URI, the file won't load.

Tuesday, February 28, 2017

Antlr4, gradle, -package, outputDirectory

First, we enabled antlr4 in IntelliJ/gradle. Next we need to find a location for antlr4 files and correctly place its output files.  There is a couple of ways to configure antlr4 output directory in gradle.

The simplest way is specify package name in @header:

@header {
  package com.example.parser;
}

Then, if we place our grammar files to "$project.projectDir/src/main/antlr/com/example/parser", antlr4 will place its output files to "$project.buildDir/generated-src/antlr/main/com/example/parser".

The problem with specifying package name in the @header is that it ties the grammar to the target language.  There is another way to configure output directory.

First, we remove @header from grammar file to untie the grammar from the target language.
Second, we add the following to build.gradle:

generateGrammarSource {
    outputDirectory file("${project.buildDir}/generated-src/antlr/main/com/example/parser")
    arguments << '-package' << 'com.example.parser'
}

An optional step is to move the grammar files from "$project.projectDir/src/main/antlr/com/example/parser" to "$project.projectDir/src/main/antlr" so that we don't have to look for the files deep down the directory hierarchy.

When all this is setup, antlr4 will place its output files into the same directory as in the first example.

Sunday, February 26, 2017

Testing Content Providers

ProviderTestCase2 and Testing Your Content Provider documents seems not complete.  I followed them and getMockContentResolver() always returned null. The reason for null resolver is that setUp() method is not called when tests run.  Adding the following method to my sub-class of ProviderTestCase2 fixed the problem.

@Before
public void mySetUp() throws Exception {
    setUp();
}

I'm not sure if this is the best solution though.