Category Archives: Android/Java

Posts about Java and Android

Recovering Android KeyStore password

Sometime back, I helped a friend of mine to launch an app in the Android market. As most of the Android developers know, one of the requirements to upload your APK file into the android market is that you have to sign it using a KeyStore.

I created a new KeyStore for him and I signed the app and he uploaded it to Android market. All this happened a couple of years back and now when he wanted to update the app again, he had to sign the apk file again with the same KeyStore.

I had remembered to backup the keystore file, but somehow couldn’t remember the exact password I used while creating the KeyStore.

After a quick search, I realized that I am not the only one who has done that before and also realized that there is no other way to upload the app again, without recovering the password 🙁

I started my search to find ways to recover the password and luckily found a tool, which took a smart list of words and then tried to guess the password by brute force. I gave it a couple of probable words that I would have used for the password and luckily I was able to recover the password with-in a couple of hours. 🙂

Some major take aways for me from this entire episode.

  • Always, always and always make sure you have more than one copy of backup
  • Don’t ever forget the password for your KeyStore. If you do then you might have to abandon your current app in the market.
  • As @pareshmayani pointed out below, make sure you store the keystore file as well in your app’s repo. God be merciful on you, if you don’t version your code
  • Sometimes keeping a simple password helps 😉

Posted in Android/Java | Tagged , , , , | 7 Comments

Using WebSockets with Android to control Arduino robots

I just came back from DroidCon 2012 by hasgeek, where I gave a talk about using WebSockets with Android to control Arduino based robots. I talked about WebSockets, how it can be used with Android and then gave a demo that I made using WebSockets, Android and the node.js server which I wrote for jsFoo. The demo allowed anyone from the audience to control an Arduino based robot which I placed in the stage.

I didn’t just talk about making Android and Arduino talk to each other, since I have already covered that in last year’s DroidCon.

Continue reading »

Posted in Android/Java, Arduino, Events/Conferences | Tagged , , , | 18 Comments

10 more open source Android apps which every developer must look into

Sometime back, I listed top 10 Open source Android apps, which every developer must look into. It’s been quite sometime since I wrote it and now I am adding a few more to the list.

Ubuntu One

Ubuntu One is a cloud storage service by Ubuntu. They have released an Android app which allows you to upload files to the cloud from your mobile and also manage your Ubuntu One account from your phone.

You can learn how to upload files, make Rest API calls, store auth tokens, detect WIFI activation etc by browsing the source code.

Source code Url: https://code.launchpad.net/ubuntuone-android-files

Mixare

Mixare is an open source Augmented Reality Engine for Android. It is pretty well build and complete.

You will learn how to draw custom shapes on top of the live video feed captures by the camera by browsing the source code. You can also learn about creating Plugin models in Android apps.

Source code Url: http://code.google.com/p/mixare/

Desksms

DeskSMS is an android app that forwards all your text messages and call history to your GTalk, GMail, or a convenient website and browser extension. It written by koushikdutta.com, the author of Rom Manager

You can learn how to read, send and forward sms and call log by browsing the source code. In addition to the android app source code, checkout the other browser extension source code as well.

Source code Url: https://github.com/ClockworkMod/DeskSMS

Call Meter NG

Call Meter NG/3G is an Android application summing up your call and text logs.

You can learn how to read call logs, SMS logs, MMS logs and data logs by browsing the source code. In addition to it, you can also learn how to create variable length widgets, which is one of the cool things about the app.

Source code Url: http://code.google.com/p/callmeter/

Scrobble Droid

Scrobble Droid is an Android app, that keeps track of music you listen and sends the details to Last.fm

You can learn how to track music that is currently playing in Android by browsing the source code. In addition to it, you can also learn how to consume Last.fm API from ndroid.

Source code Url: http://code.google.com/p/scrobbledroid/

Gmote

Gmote is an Android app that turns your Android phone into a remote control for a computer, allowing users to run movies and music at a distance.

You can learn how to transfer touch and other gestures from phone to computer and also about music streaming between phone and computer by browsing the source code.

Source code Url: http://code.google.com/p/gmote/

Ema Personal Wiki

Ema Personal Wiki is a notebook with linkable pages for tracking your ideas, todo lists, tasks, bookmarks, brainstorms etc. The notes are stored in markdown format and can be synchronized using Dropbox account.

You can learn about using markdown and syncing files using Dropbox Api by browsing the source code.

Source code Url: https://github.com/janwillemb/Ema-Personal-Wiki

Chrome to Phone

Google Chrome to Phone Extension is a project consisting of a Chrome Extension, Android App, and supporting AppEngine server that enables users to send links from their Chrome desktop browser to their Android device using Android’s Cloud to Device Messaging service.

You will learn about using C2DM and sharing url from browser by browsing the source code.

Source code Url: http://code.google.com/p/chrometophone/

Android Tetris

It is a Tetris clone for Android.

You will learn about drawing on the canvas by browsing the source code.

Source code Url: http://code.google.com/p/androidtetris/

NFC Tic Tac Toe

This is a sample Tic Tac Toe game implemented using NFC.

You will learn about transfering data using NFC by browsing the source code.

Source code Url: https://github.com/iBadrinath/NFC-TicTacToe

Posted in Android/Java | Tagged , , , | 17 Comments

Building Robots using Android and Arduino in Google Devfestx

I just came back from Google Devfestx which happened in Bangalore today. The event was very well organized and I gave a talk on building robots using Android and Arduino. This is a slightly modified version of the talk which I gave at Droidcon. I have added some additional information, especially about NFC and communicating Audio Jacket.

Continue reading »

Posted in Android/Java, Arduino, Events/Conferences | Tagged , , , , | 7 Comments

Transferring data from Android using Audio

Just came back from monthly Bangalore Android meetup. Today I gave a talk about transferring data from in and out of Android using the audio jacket.

About the talk

In the talk I mainly discussed about the way by which you can transfer data in and out of Android using the audio.

I also discussed about the ways by which you can read this data in Arduino from Android.

Continue reading »

Posted in Android/Java, Events/Conferences | Tagged , , | 6 Comments

Building Robots using Arduino and Android at DroidCon

I just came back from yet another amazing event by HasGeek. This time it is DroidCon.

DroidCon

DroidCon is a developer conference and is part of world’s largest series of Android conferences. It happened at Bangalore on 18th and 19th of November.

Continue reading »

Posted in Android/Java, Arduino, Events/Conferences | Tagged , , , , , | 19 Comments

Using standard Arduino board as Google’s ADK kit

When Google announced about the Open Accessory Development kit (ADK) in the last Google IO conference, I was really excited since it was based on Arduino boards.

But after the initial bliss went off, I was disappointed for two reasons. First you need an Android phone running Android version 2.3.4 or above. The second reason is that the original ADK kit is not compatible with the plain Arduino board. The downside of this is that all your existing Arduino Shields will not work with that board and not to mention you have to buy a rather expensive ADK kit. Now there is also an Arduino compatible ADK kit, but it is again expensive and it didn’t made sense for me to buy another Arduino board when I already had a bunch of them lying around.

Since it is based on Oleg’s USB Host Shield, I was trying to see if I can build a board with just plain Arduino board and Oleg’s USB Host Shield. It turns out it is possible and with the release of Oleg’s second version of the USB Host Library, it is much simpler.

Things you need

Any Arduino board.

You can use either UNO or Duemilanove. You can also use Arduino Mega, if you have one lying around. The advantage of using Mega is that you will have additional I/O pins.

USB Host Shield

You need a USB Host shield for Arduino. You can use the original shield by Oleg for $40, or you can buy a cheaper clone from sparkfun for $25. I recommend the original one by Oleg, since it doesn’t need an external power supply. If you are going to use Sparkfun’s version you need an external power supply. Also make sure you don’t buy the older version of Sparkfun shield. It has pins marked wrongly and you need to make changes to the library to make it work.

ADK compatible Android phone

You need an Android Phone which is ADK compatible, which means you need a phone which is running 2.3.4 or above. I tested this setup in Nexus S and it is working properly.

Sensors, LED’s, buttons

You would need additional sensors, LEDs or buttons to test the flow. To start with, you can just use some LED’s

Connections

Stack the USB Host shield on top of the Arduino board. Connect an LED on pin number 3. Connect your phone’s cable to the USB port of the shield.

Arduino Firmware

Download v2 of the USB Host Shield Library and place it in the library folder of your Arduino setup. Open the demokit_20 example sketch from the /examples folder and upload it to your Arduino board.

Android App

The Android demo app for ADK, can be found under the /app folder of the ADK package. Upload the app to your Android phone.

Testing it

Power on Arduino and connect your Android phone to the other side of the cable. You should see a dialog box, which asks you whether you want to launch the app. Click yes. Once the app is launched you can control the LED connected on pin 3, by moving the slider in the Android app.

Now enjoy your new cheaper ADK kit 😉

Posted in Android/Java, Arduino | Tagged , , , | 17 Comments

Conditional code compilation in Java

In C/C++ there is a construct which allows you to conditionally compile code. Yes I am talking about the #ifdef... #endif Java doesn’t have an equivalent. But you can use the following clever trick to achieve the same functionality.

Why conditionally compile code?

So before we proceed, you might ask me, why we need to do conditionally compiling? There are couple of reasons, but the main reason which motivated me to look for the solution is to conditionally enable debug statements. You can say that we can probably use a boolean variable or a function call, to determine whether we need to output the debug statement or not. But if the number of debug statements is high then they could add up.

Setting up final boolean variable

So the trick is simple. You have to create a if statement with a boolean private variable and if you  make the boolean private variable as final and set the value to false, then at compile time, the compiler will be able to determine that these code branches are unreachable and will not include them as part of the compiled code.

Sample Code

The following sample code explains this trick.

Posted in Android/Java | Tagged , | 5 Comments

Finding the song/track which is currently playing in Android

For my iAndroidRemote project, I had to find the currently playing song/track in Android phone and the ability to change to next/previous song/track.

After some digging, I found that there is no documented way to do it. It depends on an undocumented way which may not work well in all Android phones.

I found an undocumented way which works for HTC phones and some other stock Android phones that are using the default music player.

I thought of sharing the code here, so that it would be useful for others and also I would know where to look for it when I need it for the next time.

Copying the aidl files

Any Android app, can play music by using the com.android.music.MediaPlaybackService class. In order to find the currently playing song, we need to create a Service which will interact with this class

In order to do that, we need to copy the IMediaPlaybackService.aidl file from the source code of the default music app. For HTC phones we need to copy the IMediaPlaybackService.aidl file inside the com.htc.music package and for other Android phones we have to copy it from the com.android.music package.

Create the following packages to your android project.

  • com.htc.music
  • com.android.music

Copy the com.htc.music.IMediaPlaybackService.aidl and com.android.music.IMediaPlaybackService.aidl files to the newly created packages.

Creating the ServiceConnection class

The next step is to create the ServiceConnection class which will allow us to interact with the MediaPlaybackService class.

Copy the following code and create the MediaPlayerServiceConnection inside your activity as an inner class.

We are using the boolean isHtc to determine if we are on an HTC phone. We would be using this flag before invoking the methods on the ServiceConnection class.

Getting the current song

Now we can call the getTrackName() method on the corresponding object and we can get the track information.

Playing next song

To play the next song in the track we have to just call the next() method on the corresponding object

Playing Previous song

To play the previous song in the album we have to just call the prev() method on the correct object

I have created a small sample project to show the entire flow in action. You can download the project from my Github page.

Posted in Android/Java | Tagged , , | 16 Comments

Adjusting the volume in Android through code

For my iAndroidRemote project, I had to adjust the volume of my Android phone. I found the code after digging around a bit.

I thought of sharing the code here, so that it would be useful for others and also I would know where to look for it when I need it for the next time.

Changing the volume in Android is pretty easy. You just need to know which class and method to call.

Getting the AudioManager Instance

First you have to get an instance of the AudioManager. You can get it by calling the getSystemService method of the Context object. If you are inside an Activity, you can get the instance by making the following code

Once you have the AudioManager instance, all you have to do is to call the adjustVolume method with appropriate arguments.

Increase Volume

To increase the volume you have to pass the AudioManager.ADJUST_RAISE constant.

Reduce Volume

And to reduce the volume you have to pass the AudioManager.ADJUST_LOWER constant.

I have created a small sample project to show the entire flow in action. You can download the project from my Github page.

Posted in Android/Java | Tagged , | 7 Comments