Sunday, July 20, 2014

Release build optimization - Part 1

The goal of this article is to remove calls to debug log for release builds. Debug logs, which is done using Log.d(tag, msg) often contains more information that you want exposed to the public. It also takes up valuable CPU cycles.

Remove Log.d calls via Proguard

Proguard is a tool on Android that shrinks, optimize, and obfuscate your code. We want to use this on our release builds. In Gradle, we enable this either in the buildType or product flavor closure. The details can be found in the Android Tools User Guide

Example build.gradle:
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
defaultConfig {
minSdkVersion 10
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.txt'
}
}
}
view raw build.gradle hosted with ❤ by GitHub
Note that there are two default rules files. When you first create a project in Android Studio, you usually get a proguard-android.txt file. In order for us to tell Proguard to remove the debug log, we need it to run the optimization phase, so we need to tell it to use the proguard-android-optimization.txt file instead.

The second thing to notice here is the inclusion of a second txt file, the proguard-rules.txt. This text file is how we tell Proguard to remove calls to Log.d, Log.v, Log.i, and whatever else you think is safe to remove.

Example proguard-rules.txt
-assumenosideeffects class android.util.Log {
public static *** d(...);
public static *** v(...);
public static *** i(...);
}
The assumenosideaffects flag is an optimization option that tells Proguard to treat the declared method as an empty method and thus is safe to remove. Details can be found in the Proguard Usage Manual.

Please note that if for whatever reason you decide to use the returned integer from Log.d and assign it to a variable, Proguard will think that it maybe a useful method and won't remove it. So make sure that whatever method you decided to use this option with does not return a value that's being used, if your intention is to remove the call.

So now if we compile the release build, we would effectively remove the Log.d. So lets take the following example set of calls

Before Proguard:
Log.v(TAG, "I'm verbose");
Log.d(TAG, "I like to debug");
Log.d(TAG, new UserInfo(mUserId).getDisplayName());
Log.w(TAG, "warning!");
Log.e(TAG, "ERROR!");
view raw gistfile1.java hosted with ❤ by GitHub
After Proguard:
new u(i).g();
Log.w(t, "warning!");
Log.e(t, "ERROR!");
view raw gistfile1.txt hosted with ❤ by GitHub
As we can see, it did a decent job of removing the debug logs. But it also left something behind. It created a new class and called some function. This is the obfuscated version of the code. So what we are seeing here is that the UserInfo class is still being created. So while we no longer take up I/O cycles by logging, we are still taking up CPU and memory by creating a class that we otherwise didn't need to create. So how can we change the code such that Proguard can safely remove the creation of the UserInfo class? It turns out that if we wrap it around a final boolean of FALSE, then Proguard will remove the code since there's no way the code can be executed. So we might consider changing our code to something like this:
if (BuildConfig.DEBUG) {
Log.v(TAG, "I'm verbose");
Log.d(TAG, "I like to debug");
Log.d(TAG, new UserInfo(mUserId).getDisplayName());
}
Log.w(TAG, "warning!");
Log.e(TAG, "ERROR!");
view raw gistfile1.java hosted with ❤ by GitHub
This is a working solution. But because it requires that you add these "if (BuildConfig.DEBUG)" statements everywhere, it may not be acceptable, depending on the size of your project.

to be continued...

Wednesday, July 9, 2014

Quick & Dirty Tips: How to remove preloaded android apps

You need to have a rooted phone. Rooting of a phone is different for every device model, so it's important to find the right tools and procedure for your particular phone. Once your phone is rooted, connect your computer to your phone via USB and enable debugging via the developer options. You can now use the following set of commands in your computer terminal.

leo-osx:src_folder leo.kwong$ adb shell
shell@d2spr:/ $ su
root@d2spr:/ # mount -o rw,remount /system
root@d2spr:/ # cd system/priv-app/                                             
root@d2spr:/system/priv-app # ls MyTestApp*
MyTestAppForBlog.apk
root@d2spr:rm MyTestAppForBlog.apk
root@d2spr:/system/priv-app # exit
shell@d2spr:/ $ exit
leo-osx:src_folder leo.kwong$ 


What we're doing here is making the system folder read and write-able. Some of the preload apps will be in the system/priv-app and some will be in system/app. Only apks in /system/priv-app can use "system"-level permissions. Prior to Kitkat, all apks on the system partition could use those permissions. Uninstalling the app is just a matter of removing the APK file.

Thursday, April 3, 2014

ModernUI guideline on Windows Phone 7/8

I was cleaning up my laptop today and found an old memo that I thought was worth saving. I wanted to post it here in case anyone thought it was useful.


Sunday, March 2, 2014

Hiring your first Android developer

As a seasoned mobile application developer, I'm regularly approached by recruiters for new exciting opportunities, many of them from startup companies that are ready to jump into the mobile app world with existing web services.

Most companies start breaking into the mobile app development with iPhone/iPad app. This is influenced by reports that iOS users are more willing to pay. CNN reported in 2011 that Apple users buy 61% more apps! What's more, it's my personal experience that Android app simply takes longer to develop. Here are my reasons for that:
  1. Apple's XCode has a wonderful emulator & profiling tool for performance.
    1. Android's emulator is slow; really really slow. So slow that every developer must obtain a physical device.
  2. Apple's Xcode is a mature IDE
    1. Most developer still develop their Android app using the poorly executed Eclipse IDE.
    2. If you're lucky and have some bravery, you've switched to Android Studio, which is based on the rock solid IntelliJ IDEA. Even though it's in beta, it will improve your life dramatically.
  3. Apple's adoption rate is extremely high, so you code against only the latest SDK
    1. Example: Adoption rate of iOS 7 vs Adoption rate of Android KitKat
    2. In Android, you have to draw a line where your minimum SDK would be.
    3. This version will have consequence in terms of how complicated your code becomes and what open source libraries you maybe able to take advantage of.
  4. As an illustration, the project I'm currently involved in has an iOS and an Android team. We're building roughly the same app. Adjusted for developers dedicated to features specific to a platform, the iOS team has only 2 person, but the Android team has 4. I've been involved in developing a dozen mobile apps, and time and again, the iOS app has less bugs, jank, and requires less people.

How long does it take to make a mobile app? Answer: 18 weeks!

Kinvey Backend as a Service
If you've started with an iOS app, you have at least one mobile developer. She is going to understand the challenges presented to her like no other kinds of developer. She understand that you are going to be running code in someone's personal phone, which means the user care about battery life, bandwidth usage, privacy, and most obviously, the user need that phone to be a phone first, and an application second. Because of that your application can be interrupted any time. And because of these limitations, there are apps that simply doesn't belong in the mobile world, or at least not in the same form as you would on a desktop.

So what if your version 1.0 app is going to be an Android app because market research suggest that those are your primary customers? Here's what I would look for in a candidate:

Understanding application life cycle
  • What type of objects would be appropriate to store in the Application?
  • How would I handle the app being interrupted by a phone call in an Activity or Fragment?
  • What special care to take if the app is to support orientation change?
Proper usage of thread
  • What are Executors and how do you choose which Executor to use?
  • When is it appropriate to use runOnUiThread and when would you use Handler instead?
  • In which thread are the method in AsyncTask executed in and how would you make it safe to use if your app handles both portrait and landscape orientation?
  • What things are appropriate to run in the UI thread?
Understanding Services
  • What type of task is appropriate to be done in a Service?
  • How does a Service notify the application UI of progress or result?
  • What are the ways to start/stop a Service?
Know how to compose the UI
Know how to write test cases in Android

And then depending on the type of application you are making, there are some area of topics that she should have experience with:
  • If you plan to use a database
  • If you plan to have authenticated accounts
  • Know something about caching such as the HttpResponseCache
  • Familiar with popular open source libraries and can make suggestions as to which library maybe appropriate for your application.
  • If you need real time update, she should know about Google Cloud Messaging
  • and many many more topics, that not everyone is going to know
    • Adding maps to your app
    • Writing a widget
    • Chrome casting
    • Media playback
    • Ads integration
    • Facebook and Twitter integration
Lastly, you need to be able to build and distribute the app, which initially may be very simple, but soon enough you'll need someone who knows how to build, sign, and submit the app. You may want someone who's familiar with Maven, Gradle, and Google Play

I think if you're in a startup and wants to hire just one Android developer, she needs to have a good amount of diverse and deep understanding of Android. In my opinion, it's worthwhile to plan out what features you want to build so that you can ask the right questions. There's no point in hiring someone who has tons of experience in Map related apps if you're not going to be integrating maps. That's not to say you shouldn't hire a good generalist. But since this is your first and only Android developer in your startup, you need someone who is already an expert in Android, so in this case, you need someone who has already done it.

The other part of the hiring process that I find extremely flawed in the software industry is how we interview people. Most typically, we revert to CS 101 questions. While most of those questions are legitimate, many of them are useless. For example, a now popular question is about how to detect infinite loop in a linked list. This is popularized by an interview book that both interviewer and interviewee reads, and thus offer no insight today. If she ace the question right away, I'm pretty sure she just read the book. And honestly, how often do you encounter most of the things we ask in these interviews anyway.

The best interview that I've personally had was when I was asked to bring my own laptop, my own IDE, and pair program together a very simple app. For example, write an app that downloads a JSON file and display it in a ListView. Another type of question would be to design the app that you want to build. What component would we need. what open source library can we utilize. Perhaps most importantly, IMHO, is to make the interviewing process worthwhile for both parties. Both person should walk away learning something.

I realize my opinion doesn't align exact with Joel Spolsky's view on interviewing. I just think his views can be adjusted for different types of developers. And as Google admitted,  even their hiring process is flawed. And perhaps the technical interview is dead. One thing is certain, hiring and getting hired as a software developer is hard!

So godspeed to all who look for the right developer! and godspeed to those of us who strive to widen our knowledge and to sharpen our skills everyday. May we encounter good people and produce great product that enriches our society.

EDIT: I recently found an article that's similar to mine but he went much further to claim that Android development cost 2-3x than iOS. http://stevecheney.com/why-android-first-is-a-myth/

Saturday, March 1, 2014

Builder pattern in Java

I've always liked how nice the StringBuilder API is. But I have not found the perfect opportunity to use it until today when I wanted to create a clean API for a Request object for searching OnDemand videos. The server API has been defined to accept a bunch of parameters. And depending on what you want to search for, you may supply one or more of these parameters.

What I wanted to create was a request object that's immutable once it's created. I have plans to use this request as a key to retrieve cached response, so it would be messy if someone was allowed to modify the attributes of the original request. So I've decided to have no setter methods.

I could create a constructor with all the parameters, but that would mean that most of the time the developer may have to create a SearchRequest object with lots of nulls. It's also very easy to misplace the arguments. If argument 1 is the rating and argument 2 is the offset, and they're both integers, I can imagine someone accidentally putting the value for offset in the ratings position and the value for ratings in the offset position.

The Builder pattern can be helpful in my situation. So I've prototyped the class here. Let's take a look at how I use this class first. Here's an example of a SearchRequest that query for high rating sports TV shows and episodes.

SearchRequest request = new SearchRequest.SearchRequestBuilder()
.ofType(SearchRequest.MediaType.TV_SERIES)
.ofType(SearchRequest.MediaType.TV_EPISODE)
.inCategory(SearchRequest.MediaCategory.SPORTS)
.withRatingsHigherThan(4)
.toSearchRequest();
System.out.println(request.getURL());
view raw gistfile1.java hosted with ❤ by GitHub

Running this code generates the following print:
03-01 20:00:59.849    1321-1321/com.example.app I/System.out﹕ http://my.server.com/search.json?offset=0&max_result=100&rating=4&categories=SPORTS&types=TV_EPISODE,TV_SERIES

I've restricted the creation of the SearchRequest object directly by making the constructor private. Instead the developer need to create a new SearchRequestBuilder. The builder then has small methods that are clearly named to add attributes. Once you have composed the request you want, you call the toSearchRequest method and you get the SearchRequest object.

And here's the source code for SearchRequest. I've seen variations of this pattern, but I've implemented it to satisfy my own requirements. I'd love to hear how you've used this pattern in the course of your career and how it differs from my interpretation.

package com.example.app;
import java.util.HashSet;
import java.util.Set;
/**
* SearchRequest is an object to help create a valid REST URL for searching TV Shows and movies...
*/
public class SearchRequest {
public static enum MediaType {
VIDEO_CLIP,
TV_SERIES,
TV_EPISODE,
MOVIE
}
public static enum MediaCategory {
NEWS,
SPORTS,
KIDS
}
public static class SearchRequestBuilder {
private SearchRequest request;
public SearchRequestBuilder() {
request = new SearchRequest();
}
public SearchRequestBuilder ofType(SearchRequest.MediaType mediaType) {
request.mTypes.add(mediaType);
return this;
}
public SearchRequestBuilder inRange(int offset, int maxResult) {
request.mOffset = offset;
request.mMaxResult = maxResult;
return this;
}
public SearchRequestBuilder matchingTitle(String title) {
request.mTitleQuery = title;
return this;
}
public SearchRequestBuilder inCategory(SearchRequest.MediaCategory categories) {
request.mCategory.add(categories);
return this;
}
public SearchRequestBuilder withRatingsHigherThan(int rating) {
request.mRating = rating;
return this;
}
public SearchRequest toSearchRequest() {
return request;
}
}
private Set<MediaType> mTypes = new HashSet<MediaType>();
private Set<MediaCategory> mCategory = new HashSet<MediaCategory>();
private String mTitleQuery;
private int mRating = 0;
private int mOffset = 0;
private int mMaxResult = 100;
//constructors only accessible by the Builder
private SearchRequest() {}
public String getURL() {
StringBuilder builder = new StringBuilder();
builder.append("http://my.server.com/search.json?");
builder.append("offset=").append(mOffset);
builder.append("&max_result=").append(mMaxResult);
builder.append("&rating=").append(mRating);
if (mTitleQuery != null) builder.append("&titleQ=").append(this.mTitleQuery);
if (mCategory.size() > 0) builder.append("&categories=").append(toQueryValue(mCategory));
if (mTypes.size() > 0) builder.append("&types=").append(toQueryValue(mTypes));
return builder.toString();
}
private String toQueryValue(Set<?> items) {
StringBuilder builder = new StringBuilder();
for (Object item : items) {
if (builder.length() > 0) builder.append(",");
builder.append(item.toString());
}
return builder.toString();
}
}

Thursday, February 27, 2014

Intro to SQLlite in Android


It has been a while since I've dealt with SQL. Recently we wanted to see if having an SQLlite database in Android would provide a performance boost. The idea is to prefetch the day's TV guide and load it into an SQL database during the night. And then when the user starts the app, we would first look at the database to see if we already have the data. If so we would load that instead of going through a network request. There may also be a performance boost if the query was written properly. Another benefit over network request is that the REST resource is in JSON format and needs to be parsed, whereas translating from a database row into a POJO potentially is faster. It also takes us later into using CusorAdapter and load data on demand. For those reason it's worth trying it out.

By the way, great site to practice your SQL is http://www.w3schools.com/sql/trysql.asp

The first thing I need to do is store all my TV Program POJO into the database. And the first step is to create a table. To create a table, I execute an SQL statement such as this:
CREATE TABLE program (id INTEGER PRIMARY KEY, name TEXT NOT NULL, start_time NUMERIC, end_time NUMERIC)

It reads that we're going to be creating a new table with 4 fields: an ID, name, start and stop time. This table will have the name program. Each row in this table must have at least an ID and a display name.

In Android, we do this by creating a class that extends SQLiteOpenHelper and overriding the method
public void onCreate(SQLiteDatabase db);

In that function we can execute the query
db.execSQL(createTableQueryStr);

The SQLiteOpenHelper is a class that manage database creation and help with version control.

When you extends SQLiteOpenHelper, you are also required to implement onUpgrade method. Here, we'll simply delete the table and erase all previous data. The query is:
DROP TABLE IF EXISTS program

public class ProgramsDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "programs.db";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_PROGRAM = "program";
public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_START_TIME = "start_time";
public static final String COLUMN_END_TIME = "end_time";
public ProgramsDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
StringBuilder createTableQuery = new StringBuilder();
createTableQuery.append("CREATE TABLE ").append(TABLE_PROGRAM).append(" (");
createTableQuery.append(COLUMN_ID).append(" INTEGER PRIMARY KEY, ");
createTableQuery.append(COLUMN_NAME).append(" TEXT NOT NULL, ");
createTableQuery.append(COLUMN_START_TIME).append(" NUMERIC, ");
createTableQuery.append(COLUMN_END_TIME).append(" NUMERIC");
createTableQuery.append(")");
db.execSQL(createTableQuery.toString());
}
/** Uncomment this if we use foreign key constraint in any of our table
@Override
public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
db.setForeignKeyConstraintsEnabled(true);
}
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROGRAM);
onCreate(db);
}
}
The Program POJO actually has more data than the 4 things, but for simplicity of this blog, we'll pretend there's only 4 fields. But I want to mention that in reality, the POJO also includes a List of Strings that denotes which genre(s) the program belongs to. In order to include this information, I needed to create a 2nd table of genres. I decided to use Foreign key contraint to better insure data integrity. This way I don't accidentally add a Genre that doesn't belong to any program. The query for that table looks like this:
CREATE TABLE genre (program_id INTEGER, genre_name TEXT, FOREIGN KEY (program_id) REFERENCES program(id))

A database query always return a Cursor. It's the mechanism for which you retrieve the result. It doesn't load all the results in memory, only what the cursor is pointing at. This is good if you pass this cursor to your view so that you only need to get the data that is displayed in the UI, but it's not always possible depending on how the app is structured. So for convinence, I've created some method to translate between table row and POJO and vise versa.
public class Program {
public String id;
public String name;
public long start_time;
public long end_time;
public static Program toProgram(Cursor cursor) {
Program p = new Program();
p.id = cursor.getString(cursor.getColumnIndex(ProgramsDbHelper.COLUMN_ID));
p.name = cursor.getString(cursor.getColumnIndex(ProgramsDbHelper.COLUMN_NAME));
p.start_time = cursor.getLong(cursor.getColumnIndex(ProgramsDbHelper.COLUMN_START_TIME));
p.end_time = cursor.getLong(cursor.getColumnIndex(ProgramsDbHelper.COLUMN_END_TIME));
return p;
}
public static ContentValues toDbValues(Program program) {
ContentValues values = new ContentValues();
values.put(ProgramsDbHelper.COLUMN_ID, program.id);
values.put(ProgramsDbHelper.COLUMN_NAME, program.name);
values.put(ProgramsDbHelper.COLUMN_START_TIME, program.start_time);
values.put(ProgramsDbHelper.COLUMN_END_TIME, program.end_time);
return values;
}
}
view raw Program.java hosted with ❤ by GitHub
Before you can insert, query, or delete anything in the database, you need to open it. This is usually done either than your app is created or when your Service is connected. Here I've created a ProgramsDataSource class that'll help me insert query and remove programs based on the start_time of the TV program.
Note the use of the insertWithOnConflict method where if a row already exist, I'll replace the values instead of throwing an exception.
The other thing to note is that if you need to use the DISTINCT function of SQL to query all unique names for example. Let's say I want to know programs of my entire TV Guide during prime time during the week, but I don't want to see the evening news show up 5 times, I would do a query with DISTINCT. To do that in SQLiteDatabase in Android, you use the overloaded method for query with the boolean as first argument.

public class ProgramsDataSource {
private ProgramsDbHelper dbHelper;
private SQLiteDatabase database;
private String[] allColumns = {
ProgramsDbHelper.COLUMN_ID,
ProgramsDbHelper.COLUMN_NAME,
ProgramsDbHelper.COLUMN_START_TIME,
ProgramsDbHelper.COLUMN_END_TIME,
};
public ProgramsDataSource(Context context) {
dbHelper = new ProgramsDbHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public synchronized void insertPrograms(List<Program> programs) {
try {
database.beginTransaction();
for (Program program : programs) {
ContentValues values = ProgramsMapper.toDbValues(program);
database.insertWithOnConflict(ProgramsDbHelper.TABLE_PROGRAM, null, values, SQLiteDatabase.CONFLICT_REPLACE);
}
database.setTransactionSuccessful();
} catch (Exception e) {
e.printStackTrace(System.err);
} finally {
database.endTransaction();
}
}
public synchronized void removePrograms(long olderThan) {
try {
database.beginTransaction();
//TODO: remove entry in genre table
database.delete(ProgramsDbHelper.TABLE_PROGRAM, ProgramsDbHelper.COLUMN_START_TIME + " < " + olderThan, null);
database.setTransactionSuccessful();
} catch (Exception e) {
e.printStackTrace(System.err);
} finally {
database.endTransaction();
}
}
public synchronized List<Program> getPrograms(long start_time, long end_time) {
assert(end_time > start_time);
StringBuilder selection = new StringBuilder();
selection.append(ProgramsDbHelper.COLUMN_START_TIME).append(" < ").append(end_time);
selection.append(" AND ");
selection.append(ProgramsDbHelper.COLUMN_END_TIME).append(" > ").append(start_time);
Cursor cursor = database.query(ProgramsDbHelper.TABLE_PROGRAM, allColumns,
selection.toString(), null, null, null, ProgramsDbHelper.COLUMN_START_TIME );
cursor.moveToFirst();
List<Program> programs = new ArrayList<Program>();
while (!cursor.isAfterLast()) {
Program program = ProgramsMapper.toProgram(cursor);
programs.add(program);
cursor.moveToNext();
}
return programs;
}
}

WebRep
currentVote
noRating
noWeight

Thursday, February 13, 2014

Quick & Dirty Tips: Splitting words

First thing to note is a StringTokenzier is depreciated.
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
Instead use String.split(String regex), which takes regular expression. So to use the method we have to look at how regular express can split words.

[^abc] Any character except a, b, or c (negation)
\s A whitespace character: [ \t\n\x0B\f\r]
\S A non-whitespace character: [^\s]

So to split words in a String, I use the following:

String line = "Lorem ipsum dolor sit amet, consectetur adipisicing elit...";
String[] words = line.split("\\s*[^a-zA-Z]+\\s*");


Links:

Tuesday, February 4, 2014

Creating a REST library using Retrofit & OkHttp

I've been tasked to modernize our application's networking layer. We have been writing our own network manager to deal with http 302 redirect and caching responses, but there are many opensource project that already does these things very well. I've looked into RoboSpice and Volley as the more popular choices for doing networking in Android. I found that they're a good candidate as an improved AsyncTask as well as providing caching. However both framework are closely related to the Activity context and doesn't work as well when put into a Service such as ones that I intent to make with a SyncAdapter. We needed a lower level framework. To figure out what fits our bill, we wrote down some of our requirements:

  1. It needs to support Https
  2. We want to translate server JSON into client POJO
  3. The framework needs to allow us to edit the header and post body for authentication
  4. We want the ability to pass arguments to methods and insert them as part of the URL or the query
  5. We want to save bandwidth with Gzip
  6. We want efficient communication with parallel requests
  7. we want Http caching that follows the server's http cache directive
  8. It needs to follow http 302 redirect
  9. It needs to work well with Android

I stumbled across Square's Retrofit and OkHttp. Here's how it fits.

Retrofit

Supports Https
In Retrofit's home page, their example uses https, so clearly it supports it.
RestAdapter restAdapter = new RestAdapter.Builder() .setServer("https://api.github.com") .build();

Converts JSON to POJO
Retrofit use GSON by default to convert Http bodies to and from JSON.

Edit request header and body
Retrofit allow header information to be set statically using annotation or at runtime using a RequestIntercepter.

Edit URL and query
Retrofit uses annotated method arguments to replace PATH or query placeholders. You can also change it at runtime using RequestIntercepter.

Working with Android
The generated implementation GET and POST can be made synchronously outside of UI thread, or asynchronously callback on the UI thread, or in background thread via Netflix's RxJava rx.Observable. So it's very flexible in what threading model we need. We can also put RoboSpice on top of Retrofit later if we want.

OkHttp

Supports GZIP
"Transparent GZIP shrinks download sizes."

Efficient Networking
  • "SPDY support allows all requests to the same host to share a socket."
  • "Connection pooling reduces request latency (if SPDY isn’t available)."
  • "it will silently recover from common connection problems"

Local Caching
OkHttp's HttpResponseCache allows you to set aside a specific amount of space in cache directory, very similar to Google's HttpResponseCache.


First create an OkHttpClient and set 10MB of space in the cache director for caching
OkHttpClient okHttpClient = new OkHttpClient();
File cacheDir = context.getCacheDir();
HttpResponseCache cache = new HttpResponseCache(cacheDir, 10 * 1024 * 1024);
okHttpClient.setResponseCache(cache);
view raw gistfile1.java hosted with ❤ by GitHub

Then in order to add some standard PATH and query parameters I create a RequestInterceptor
private class BasicRequestInterceptor implements RequestInterceptor {
@Override
public void intercept(RequestFacade requestFacade) {
requestFacade.addEncodedPathParam("product_name", mProductName);
requestFacade.addQueryParam("device", android.os.Build.MODEL);
requestFacade.addQueryParam("firmware", android.os.Build.VERSION.RELEASE);
}
}
view raw gistfile1.java hosted with ❤ by GitHub

Then we can build the RestAdapter and set the OkHttpClient as the networking component and set the request interceptor so that every request will be modified to include those Path and query.
Note that for debugging purpose I've turned on debugging so I can see the actual request and response in the log.
Also note that the http scheme is set as part of the most.
new RestAdapter.Builder()
.setServer("http://" + this.mHost)
.setRequestInterceptor(requestInterceptor)
.setClient(httpClient)
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
view raw gistfile1.java hosted with ❤ by GitHub

Then with the RestAdapter, you call create with the Interface that you want to create. But before we do that, we need to define the Interface.

GET
Notice that synchronous call throws RetrofitError at runtime, so I've declared it in the interface explicitly here. The caller should try/catch the request in case an error occurred.
public class Program {
public String id;
public String name;
public String description;
public long start_time;
public long duration;
public String channel_id;
public List<String> genre_list;
public long original_air_date;
}
@GET("/{product_name}/guide/v3/programs")
List<Program> getPrograms(
@Path("product_name") String productName
@Query("channel_id") String channelId,
@Query("start-time") long startTime,
@Query("timeslice") long timeSlice) throws RetrofitError;
view raw gistfile1.java hosted with ❤ by GitHub


POST
public class AuthTokenPostData {
public String grant_type = "password";
public String client_id;
public String username;
public String password;
}
public class AuthTokenResponse {
public String token_type;
public String access_token;
public String scope;
public Long expires_in;
public String refresh_token;
}
@POST("/{product_name}/oauth2/tokens.json")
@Headers({
HTTP.CONN_DIRECTIVE + ":" + HTTP.CONN_CLOSE,
})
AuthTokenResponse getAuthtoken(@Body AuthTokenPostData userNamePassword) throws RetrofitError;
view raw gistfile1.java hosted with ❤ by GitHub

So now, we can create POJO data for all the data that our server returns, create Interface files for all the APIs that the server has available, annotate the URL, the header, the post body, and the return type. And then I should have a brand new and robust REST library for my application!
For more information, visit the

Saturday, January 25, 2014

Learning about Android Loaders: AsyncTaskLoader

Android introduced Loaders in 3.0 to ease asynchronous tasks on Activity and Fragments. It monitor the source of the data and automatically deliver new result when content changes. It's primary usage is to work with a database utilizing the CursorLoader, but it can also be used to do other asynchronous tasks. I want to take a look at how Loaders could be used as a replacement for AsyncTask.

The issue with AsyncTask is well known. If you rotate the device, you have to cancel the AsyncTask or the app will crash. Loaders handles rotation better, it automatically reconnect to the last loader’s cursor when being recreated after a configuration change avoiding the need to re-query their data.

There is an instance of LoaderManager in every Activity and Fragment. You simply call getLoaderManager(). Note that if you're using the support library, you will call getSupportLoaderManager() instead.

To do the actual task, create a class that extends the AsyncTaskLoader and implement the loadInBackground method. The return type will be whatever data you want to send back to the main app. In my example, the work is just to sleep for some time. And my return value is just how many milliseconds I slept.

To trigger the work, the first thing to do is to implement the LoaderCallbacks interface in your Activity or Fragment. You can then call initLoader in the Activity's onCreate method. In my test app, I'm actually going to start the task after clicking a button, so I call initLoader() and forceLoad(). The onCreateLoader method will then be triggered. Notice that it uses an integer ID to identify the Loader. I'm not sure, but my guess is that typically you only have one Loader to handle all the asynchronously tasks for an Activity or Fragment.

Finally, to get feedback on the UI, the onLoadFinished method will be called in the UI thread where you can show the result. In my example, I'm simply making a Toast message, but you could imagine where we can update a table with rows of values or show a text.

Note that while AsyncTask has onProgressUpdate(Integer progress) to show how much data has been fetched, a Loader doesn't have this capability out of the box.

I still have a lot to learn about Loaders after this short prototype. So far, it seems like a lot of work to avoid canceling and restarting tasks. If data fetch performance is a concern, which is always the case to a certain degree, then I would consider using AsyncTaskLoader instead of AsyncTask. But there are alternate solutions, such as RoboSpice, that maybe a better solution. My guess is that Loaders are much more effective when dealing with database, which I intent to try out soon.

public static class Sleeper extends AsyncTaskLoader<Integer> {
private int mSleepTime;
public Sleeper(Context context, int sleepTime) {
super(context);
mSleepTime = sleepTime;
}
@Override
public Integer loadInBackground() {
try {
Thread.sleep(mSleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
return mSleepTime;
}
}
public static class MyFragment extends Fragment
implements LoaderManager.LoaderCallbacks<Integer>{
private final static int TASK_ID_A = 1;
private final static int TASK_ID_B = 2;
private final static int TASK_ID_C = 3;
public MyFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button submitButton = (Button) rootView.findViewById(R.id.submit_button);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getLoaderManager().initLoader(TASK_ID_A, null, MyFragment.this).forceLoad();
getLoaderManager().initLoader(TASK_ID_B, null, MyFragment.this).forceLoad();
getLoaderManager().initLoader(TASK_ID_C, null, MyFragment.this).forceLoad();
}
});
return rootView;
}
@Override
public Loader<Integer> onCreateLoader(int i, Bundle bundle) {
switch(i) {
case TASK_ID_A:
return new Sleeper(getActivity(), 100);
case TASK_ID_B:
return new Sleeper(getActivity(), 500);
case TASK_ID_C:
return new Sleeper(getActivity(), 250);
default:
return new Sleeper(getActivity(), 1);
}
}
@Override
public void onLoadFinished(Loader<Integer> integerLoader, Integer integer) {
Toast.makeText(getActivity().getApplicationContext(), "onLoadFinished for " + integerLoader, 1000);
}
@Override
public void onLoaderReset(Loader<Integer> integerLoader) {
}
}
view raw gistfile1.java hosted with ❤ by GitHub

Tuesday, January 14, 2014

Java's CountDownLatch

In our Android phone TV application, we need a few server resources before meaningful data can be fetched. For example, we need to know the template for the playback URL so that when user select a channel, we can construct a full URL to play from the channel ID, the device dimension, etc. We also need to fetch another table that tells us which settings to use when the app is connected to a WiFi spot versus when it's on a cellular signal. We need these and a few other essential resources before the app can start.

All of these resources are asynchronous fetches from the server. And we don't want to navigate to the home page until all resources have been retrieved. A handy class to help track that we've got all those resources is the CountDownLatch. It blocks the calling thread until the count reaches zero. You create the latch, fire off your asynchronous tasks in separate threads. Then when each of the task is finished, you decrement the count by calling countDown().

To block the calling thread, you call the await() method. One mistake I made the first time was call wait() instead of await(), so make sure you call the right method. If you call wait, you'll get an IllegalMonitorException.

Warning: NEVER block the UI/main thread!


It's obvious, but still worth noting. You should not use this on the main thread. Instead I would use this to patch several tasks and make it act like a single operation, then notify the UI that all tasks are done via a handler or broadcast receiver.

To see how it all works, I wrote a simple function and run it to see how it looks:

Sample code


private static class WorkerTask implements Runnable {
private String name;
private long taskLen;
private CountDownLatch lock;
private WorkerTask(String name, long taskLen, CountDownLatch countDownLatch) {
this.name = name;
this.taskLen = taskLen;
this.lock = countDownLatch;
}
@Override
public void run() {
//do some work here
SystemClock.sleep(this.taskLen);
System.out.println("DEMO - Completed: " + this.name);
this.lock.countDown();
}
}
public void execWorkerTasksBlocking() {
try {
System.out.println("DEMO - execWorkerTasksBlocking <<");
CountDownLatch lock = new CountDownLatch(5);
ExecutorService pool = Executors.newFixedThreadPool(2);
pool.submit(new WorkerTask("task A", 250, lock));
pool.submit(new WorkerTask("task B", 500, lock));
pool.submit(new WorkerTask("task C", 100, lock));
pool.submit(new WorkerTask("task D", 750, lock));
pool.submit(new WorkerTask("task E", 100, lock));
lock.await();
System.out.println("DEMO - execWorkerTasksBlocking >>");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
view raw gistfile1.java hosted with ❤ by GitHub

Sample output


01-07 10:50:53.758    7809-7809/com.mobitv.refapp I/System.out﹕ DEMO - execWorkerTasksBlocking <<
01-07 10:50:54.018    7809-8129/com.mobitv.refapp I/System.out﹕ DEMO - Completed: task A
01-07 10:50:54.118    7809-8129/com.mobitv.refapp I/System.out﹕ DEMO - Completed: task C
01-07 10:50:54.269    7809-8130/com.mobitv.refapp I/System.out﹕ DEMO - Completed: task B
01-07 10:50:54.369    7809-8130/com.mobitv.refapp I/System.out﹕ DEMO - Completed: task E
01-07 10:50:54.879    7809-8129/com.mobitv.refapp I/System.out﹕ DEMO - Completed: task D
01-07 10:50:54.879    7809-7809/com.mobitv.refapp I/System.out﹕ DEMO - execWorkerTasksBlocking >>

Wednesday, January 8, 2014

Quick & Dirty Tips: JavaDoc gradle task

I was trying to publish a JavaDoc for my Android Library. Being an absolute novice in Gradle, I searched the net and found a piece of gem that just worked. The Magic is from line 31 to line 46. It creates 2 Gradle tasks for me:

  • generatedebugjavadoc &
  • generatereleasejavadoc

And then the JavaDoc files can be found in <ProjectDir>/build/docs

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android-library'
repositories {
mavenCentral()
}
android {
compileSdkVersion 19
buildToolsVersion "18.1.1"
defaultConfig {
minSdkVersion 11
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
android.libraryVariants.all { variant ->
task("generate${variant.name}Javadoc", type: Javadoc) {
title = "$name $version API"
description "Generates Javadoc for $variant.name."
source = variant.javaCompile.source
ext.androidJar =
"${android.plugin.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
classpath = files(variant.javaCompile.classpath.files) + files(ext.androidJar)
options.links("http://docs.oracle.com/javase/7/docs/api/");
options.links("http://d.android.com/reference/");
exclude '**/BuildConfig.java'
exclude '**/R.java'
}
}
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile 'com.octo.android.robospice:robospice:1.4.9'
compile 'com.octo.android.robospice:robospice-spring-android:1.4.9'
compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
compile 'org.codehaus.jackson:jackson-core-asl:1.9.13'
}
view raw build.gradle hosted with ❤ by GitHub

Sunday, January 5, 2014

Android's IntentService

When I ask my colleagues about IntentService as a way to execute asynchronous tasks, most seems to be unaware of it existence. Perhaps it is because it doesn't interact with the UI/main thread like AsyncTask does, so it's use is limited to background processes that doesn't require UI feedback, and so there are less scenarios where it is appropriate. IntentService is just one of many ways Android provide asynchronous operations. Others include
  • runOnUIThread
  • Executor
  • Handler
  • AsyncTask
  • Service
  • Loader

Service vs IntentService


The first question that most developer ask is what's the difference between a normal Android Service and an IntentService. Most folks understand that a Service is an application component that can perform long running tasks in the background without UI feedback. If you read the Class Overview of IntentService, you would read that
"IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand."
The first time I read this quote, I interpreted to mean that Service extends IntentService. But the opposite is true.
public abstract class IntentService extends Service
IntentService does a little bit more than what Service does for you.
  • While a regular Service runs in the caller's thread, which in most cases is the main thread, IntentService always run on a background thread.
  • IntentService uses a message queue to process requests, so only one intent is handled at any time. 
    • if you need parallel tasks such as downloading multiple images at the same time, IntentService may not be a good choice. 
    • If one of the tasks is stuck, then all the pending tasks in the queue will not execute. 
  • Calling stopService is optional; The IntentService will stop itself once there's no more work on the queue. 
  • A broadcast receiver is needed if you want to provide UI feedback.

Implementing an IntentSevice


There are only a few things you have to do:
  1. Declare a few constants for communication between application and IntentService.
  2. Create a class that extends IntentService; Implements onHandleIntent method.
  3. Create a BroadcastReciever; Implement onReceive method.
  4. Trigger the IntentService by calling startService, with input arguments wrapped in an Intent.

Example:


public class MyBackgroundService extends IntentService {
public static final String BROADCAST_ACTION = "com.mobitv.demo.MyBackgroundService.BROADCAST";
public static final String INPUT_STOCK_SYM = "com.mobitv.demo.MyBackgroundService.INPUT";
public static final String OUTPUT_PRICE = "com.mobitv.demo.MyBackgroundService.OUTPUT";
public MyBackgroundService() {
super("MyBackgroundService");
}
@Override
protected void onHandleIntent(Intent intent) {
String stockSym = intent.getStringExtra(INPUT_STOCK_SYM);
float price = predictStockPrice(stockSym);
Intent localIntent = new Intent(BROADCAST_ACTION);
localIntent.putExtra(OUTPUT_PRICE, price);
LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
}
private float predictStockPrice(String stockSym) {
//here we query historic prices of this stock and similar stock
//and make comparisons and use some fancy want to find a price
return result;
}
}
private class MyBkgSvcResponseReciever extends BroadcastReceiver {
private Activity mActivity;
//prevent instantiation
private MyBkgSvcResponseReciever(Activity activity) {
mActivity = activity;
}
@Override
public void onReceive(Context context, Intent intent) {
float price = intent.getLongExtra(MyBackgroundService.OUTPUT_PRICE, -1);
textView.setText("Predicted price is " + price);
}
}
mServiceIntent = new Intent(this, MyBackgroundService.class);
mBroadcastReciever = new MyBkgSvcResponseReciever(this);
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReciever, new IntentFilter(MyBackgroundService.BROADCAST_ACTION));
//trigger the background service work
mServiceIntent.putExtra(MyBackgroundService.INPUT_STOCK_SYM, "GOOG");
startService(mServiceIntent);
view raw gistfile1.java hosted with ❤ by GitHub

Handling device rotation


In order to handle configuration change properly, when the device is rotated for example, the broadcast receiver should be bind and unbinded along with the lifecycle of the Activity or Fragment

@Override
public void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReciever, new IntentFilter(MyBackgroundService.BROADCAST_ACTION));
}
@Override
public void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReciever);
}

Sample output


Note that the requests are handled serially such that if startService was called by mistake more than once with the same Intent, the work will be done twice, so it's important to either prevent that from happening in the calling side or write the IntentService such that it recognize a duplicate request. This can be accomplished by a runtime cache or input and output. So if the service receive the Intent and it already cached the result, then just broadcast the result again. Finally, if you call stopService all the pending tasks will be lost, which maybe what you want.


Calling startService just once


12-13 15:44:11.300 19913-19913/com.mobitv.demo I/System.out﹕ Button.setOnClickListener calls startService
12-13 15:44:11.370 19913-19913/com.mobitv.demo I/System.out﹕ MyBackgroundService.onCreate
12-13 15:44:11.380 19913-20063/com.mobitv.demo I/System.out﹕ MyBackgroundService.onHandleIntent; Find price for GOOG
12-13 15:44:13.242 19913-20063/com.mobitv.demo I/System.out﹕ MyBackgroundService.onHandleIntent; Found price: $1200.45
12-13 15:44:13.252 19913-19913/com.mobitv.demo I/System.out﹕ MyBkgSvcResponseReciever.onRecieve
12-13 15:44:13.272 19913-19913/com.mobitv.demo I/System.out﹕ MyBackgroundService.onDestory


Calling startService many times in rapid succession.


12-13 15:46:28.568 20388-20388/com.mobitv.demo I/System.out﹕ Button.setOnClickListener calls startService
12-13 15:46:28.628 20388-20388/com.mobitv.demo I/System.out﹕ MyBackgroundService.onCreate
12-13 15:46:28.638 20388-20494/com.mobitv.demo I/System.out﹕ MyBackgroundService.onHandleIntent; Find price for GOOG
12-13 15:46:28.688 20388-20388/com.mobitv.demo I/System.out﹕ Button.setOnClickListener calls startService
12-13 15:46:28.838 20388-20388/com.mobitv.demo I/System.out﹕ Button.setOnClickListener calls startService
12-13 15:46:28.989 20388-20388/com.mobitv.demo I/System.out﹕ Button.setOnClickListener calls startService
12-13 15:46:29.129 20388-20388/com.mobitv.demo I/System.out﹕ Button.setOnClickListener calls startService
12-13 15:46:30.520 20388-20494/com.mobitv.demo I/System.out﹕ MyBackgroundService.onHandleIntent; Found price: $1200.45
12-13 15:46:30.530 20388-20494/com.mobitv.demo I/System.out﹕ MyBackgroundService.onHandleIntent; Find price for GOOG
12-13 15:46:30.550 20388-20388/com.mobitv.demo I/System.out﹕ MyBkgSvcResponseReciever.onRecieve
12-13 15:46:32.422 20388-20494/com.mobitv.demo I/System.out﹕ MyBackgroundService.onHandleIntent; Found price: $1200.45
12-13 15:46:32.422 20388-20388/com.mobitv.demo I/System.out﹕ MyBkgSvcResponseReciever.onRecieve
12-13 15:46:32.442 20388-20494/com.mobitv.demo I/System.out﹕ MyBackgroundService.onHandleIntent; Find price for GOOG
12-13 15:46:34.324 20388-20494/com.mobitv.demo I/System.out﹕ MyBackgroundService.onHandleIntent; Found price: $1200.45
12-13 15:46:34.324 20388-20388/com.mobitv.demo I/System.out﹕ MyBkgSvcResponseReciever.onRecieve
12-13 15:46:34.354 20388-20494/com.mobitv.demo I/System.out﹕ MyBackgroundService.onHandleIntent; Find price for GOOG
12-13 15:46:36.246 20388-20494/com.mobitv.demo I/System.out﹕ MyBackgroundService.onHandleIntent; Found price: $1200.45
12-13 15:46:36.256 20388-20388/com.mobitv.demo I/System.out﹕ MyBkgSvcResponseReciever.onRecieve
12-13 15:46:36.286 20388-20494/com.mobitv.demo I/System.out﹕ MyBackgroundService.onHandleIntent; Find price for GOOG
12-13 15:46:38.168 20388-20494/com.mobitv.demo I/System.out﹕ MyBackgroundService.onHandleIntent; Found price: $1200.45
12-13 15:46:38.168 20388-20388/com.mobitv.demo I/System.out﹕ MyBkgSvcResponseReciever.onRecieve
12-13 15:46:38.248 20388-20388/com.mobitv.demo I/System.out﹕ MyBackgroundService.onDestory


Calling startService many times in rapid succession, then calling stopService once.


12-13 15:47:19.849 20807-20807/com.mobitv.demo I/System.out﹕ Button.setOnClickListener calls startService
12-13 15:47:19.909 20807-20807/com.mobitv.demo I/System.out﹕ MyBackgroundService.onCreate
12-13 15:47:19.919 20807-20952/com.mobitv.demo I/System.out﹕ MyBackgroundService.onHandleIntent; Find price for GOOG
12-13 15:47:20.009 20807-20807/com.mobitv.demo I/System.out﹕ Button.setOnClickListener calls startService
12-13 15:47:20.209 20807-20807/com.mobitv.demo I/System.out﹕ Button.setOnClickListener calls startService
12-13 15:47:20.409 20807-20807/com.mobitv.demo I/System.out﹕ Button.setOnClickListener calls startService
12-13 15:47:20.590 20807-20807/com.mobitv.demo I/System.out﹕ Button.setOnClickListener calls startService
12-13 15:47:20.940 20807-20807/com.mobitv.demo I/System.out﹕ MyBackgroundService.onDestory
12-13 15:47:21.791 20807-20952/com.mobitv.demo I/System.out﹕ MyBackgroundService.onHandleIntent; Found price: $1200.45
12-13 15:47:21.801 20807-20807/com.mobitv.demo I/System.out﹕ MyBkgSvcResponseReciever.onRecieve



Example Use in projects

To see how others have used IntentService, I went on Github and did a search and found a few examples of open source project that uses IntentService as a background task.
  1. RingerMuteService uses IntentService to change the volume level of the device
  2. VBox's EventIntentService polls VirtualBox for events and publish them in a local broadcaster
  3. WidgetCreatorService uses IntentService to create app shortcut on the device home page
  4. WotdWidget updates data in an Android widget.
  5. UploadToJBossServerService upload file to a JBoss Server.