Peter_vdL

With a whirr of gears, and a whiff of code, the little robot trundled on...

by Peter_vdL Motorola 06-20-2012 03:34 PM - edited 06-20-2012 06:46 PM

Everyone who's read a few of my blog posts knows that I have a secret passion for tracked vehicles, like bulldozers.    And, like a lot of guys, I'm keen on robots, especially ones with LEDs or lasers.   Also, I just love remote-controlled stuff, and Android is lots of fun to interact with.   So what do you think my reaction would be, if someone launched a product that was

    • a noisy robot with LEDs for eyes
    • that runs on tracks, not wheels
    • and is remotely-controlled
    • from your Android phone
    • by an open source app!

Let me tell you, I have a grin all over my face that is so wide, I have to walk sideways to get on the elevator!  I can eat pizza without cutting it into slices!  Having this much fun is probably outlawed in three states!  I've been this way since I discovered DeskPets - a Hong Kong based toy company, with just 7 employees, but world class products.   Most of their current products are remote-controlled toy robots of various form factors.  I have a blue Tankbot, like the one shown in the picture.  Youtube has a bunch of videos about them. You can find them at your local toystore or by a search at Amazon.com.

 

Screen shot 2012-06-20 at 11.17.35 AM.png

Figure 1: android-controlled Tankbots - crush your cubicle enemies, then recharge from USB ports

 

These amazing robots can operate autonomously, or be remote controlled from your android phone through a dongle that plugs into the audio socket.  There's an app that runs on the phone, allowing you to remotely steer the tankbot.    Best of all, the control app is open source!


The app source code can be downloaded from http://sourceforge.net/projects/deskpets/.  I know what you're thinking - why didn't they use github, instead of sourceforge? - well, the Deskpet guys are brilliant inventors, formidable engineers, ok soccer players, but somewhat ready for help in the coding department.   And that's where we come in!  With open source code, Android developers can make all the improvements we want, test them on our own desks, and generally take this robot product family to amazing new places.

 

With that in mind, the rest of this blog post is about how you code review and do maintenance on an existing Android app.  Modifying Android apps is often easier than working on equivalent desktop or server apps, because Android breaks the code up into Activity-sized chunks for you.  So you never have to bite off more than you can chew.  Here are the steps to follow to get into someone else's code base, and start improving their code.

 

1.  Import the zip file into Studio or Eclipse.  If this was a project exported from Eclipse, use File > Import ... > Existing projects... (not "> Archive File", as you would expect!)
 
2.  Review the AndroidManifest.xml file.  Look at the permissions it uses, the number of activities, services, receivers, or content providers.   Look at the names chosen - how meaningful are they?  The manifest file also lists the minimum API level, and any libraries that the app needs to run.  Now is a good time to enable debugging by adding to the application element in the manifest, like this


<application
             
android:debuggable="true"

 

3.  Build the app, install it on a phone, and run it.  Go through all the screens.  Identify the flow between activities, and relate these to the Activity names you saw in the previous step.  Get a feel for what the app does overall. 

 
4.  In Eclipse, in the manifest file, identify the app entry point, which will be marked like this:

<activity android:name=".myMain"
          android:label="@string/app_name">
     <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>
</activity>

The main activity usually has a name with "main" in it, and here it is ".myMain".   That means we can find the associated source file under src/packagename/myMain.java . Look inside that java file to get the layout filename.   The R.layout.splash name in the java source tells us the layout file is under res/layout/splash.xml (or an alternative resource).

 

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
        
    Thread logoTimer = new Thread(){
	public void run(){
	  try{
	    int logoTimer = 0;
	    while(logoTimer < 3000){
	    sleep(100);
	    logoTimer = logoTimer + 100;
	}
        startActivity(new Intent("com.deskpets.android.deskpets.CLEARSCREEN"));

Here, unusually, the code sets a splash screen (just an ImageView in the layout XML file), times it out after 3 seconds, and then fires an intent for another activity.  The intent tells us control will go to whoever the manifest says handles a CLEARSCREEN intent. Quickly checking the manifest, we can see the activity that handles that is src/packagename/myController.java

 

That immediately suggests a useful improvement.  Since most splash screens have no value for the user, let's update this part of the manifest file to promote the myController activity to be the main entry point, and go directly there on launching, instead of going through the splash screen.   We can remove the MAIN attributes from myMain, and add them to myController, like this:

<activity android:name=".myController"
          android:label="@string/app_name">
    <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

 Test to make sure there are no side effects.  Ah!  That's better already.


5.  Repeat the review process for all activities.   Look at how you get to that Activity, what layout it uses, what it does, and what other Activities it can lead to.  Look briefly through the code to get the "big picture", but also flag any obvious problems for later follow-up.

 

6.  Be alert for code smells.  These are things like large chunks of code commented out, dead code, uncommented code, or comments that are wrong.  When the comments disagree with the code, it's wiser to go with what the code says.    Form opinions about code style, data use, naming conventions, etc, and share them with fellow developers.

7.  Find and fix the smallest, easiest bug in the program (every program has at least one bug).   We were lucky to find an easy one in the first place we looked.  Share the code back with the owner.   Congratulations, in the eyes of fellow developers, you are now officially a maintainer of that code!  The Deskpets are pretty awesome, and I hope to blog about their Android app some more in future.  I plan to code in a variable speed control for my tankbot. 

This is the way to do an Android app code review.  And this is the way to get into working on any code, from the Android open source framework to the Linux kernel.   Congratulations again, the world is your oyster!

 

So.... comments, corrections, complaints, compliments, source diffs, all welcome.  What is your favorite technique for getting into code written by someone else?

Peter van der Linden

Android Technology Evangelist

Comments
by Community Manager on 06-20-2012 04:19 PM
I want one of these! Here is a video I found about the making of the deskpets from one of the founders:
Post a Comment
Be sure to enter a unique name. You can't reuse a name that's already in use.
Be sure to enter a unique email address. You can't reuse an email address that's already in use.
reCAPTCHA challenge image
Type the characters you see in the picture above.Type the words you hear.
About MOTODEV Blog
The MOTODEV blog keeps you updated on mobile app development news from MOTODEV and the Android developer community.

Subscribe to our RSS feed Subscribe via RSS

Follow Us:
Fan MOTODEV on Facebook Join the MOTODEV LinkedIn Group MOTODEV on YouTube

motodev profile

motodev It's not too late to join our Google+ Hangout: Migrating your Legacy Systems to the Cloud & Mobile: moto.ly/l2cloud #l2cloud 6 days ago · reply · retweet · favorite

motodev profile

motodev Very interesting info about using StackMob & force.com tools 2 migrate legacy systems to cloud & mobile. moto.ly/l2cloud 6 days ago · reply · retweet · favorite

motodev profile

motodev At #AppsForChange today in SF helping non-profits build & market apps. are u a non-profit w/ a great mobile app? share with us a link. 5 days ago · reply · retweet · favorite

Our Blog & Comment Policy
Opinions expressed here and in any corresponding comments are the personal opinions of the original authors, not of Motorola. The content is provided for informational purposes only and is not meant to be an endorsement or representation by Motorola or any other party.

Remember, when you comment, please stay on topic and avoid spam, profanity, and anything else that violates our user guidelines. All comments require approval by Motorola and can be rejected for any reason.

For customer support issues with your Motorola phone go to the Motorola customer support website.