Archive

Archive for the ‘android’ Category

Google CodeSearch: your best friend to Reverse Engineer Android Code to your Android app

September 30, 2010 4 comments

I’m on my 4th week developing my first Android Application: a client to the Discovery API for the CollabNet Subversion Edge Server, which I started writing to validate the implementation of the Discovery API I had written based on a previous implementation using jmDNS for the Subversion Edge server. The first version was definitely exciting because I wanted to see how the Discovery API, which uses the Bonjour Protocol (ZeroConf/mDNS from Apple), behaved on Android. Since I only have some evenings and weekends to work on it, it has become difficult to keep track of all the topics learned so I decided to do a crash course and watched the video “Beyond Helloworld” and get an insight of what is to be developing on Android. I got hooked!!! For a Java/Linux lover myself, it is very addicting to develop on it because I was once a JavaME developer and did develop a few applications at my Motorola Brazil Test Center internship. Anyway, here’s the YouTube video I did when I first finished the version 0.0.1 😀 There are still technical Strings exposed to the UI but, as you can see, I had fun producing a video using the Android Screencast and Mac iMovie.

After getting familiar with the terminology with Activity and Service Classes, etc, I started developing different features one by one. Considering each application has its own unique requirements, you definitely need to understand the API, read the Java Docs and the fundamental documentation provided by Google and others via blog posts. However, the more you start developing a customized application with icons, colors, etc, the more addicted you get 🙂

Google Android - Add to Home screen Dialog with images

So, customizing components sometimes is tricky because of the different ways to implement something. However, there are similarities on UI components and behavior you want to use in your application to maintain the standard behavior of you application. That’s when I had a requirement that add a custom Dialog to an item of a list of Subversion Edge servers that are discovered in the network, showing the options for that type of server. The Dialog needs to have the associated icon that is displayed in the list, and the options with associated icons. The section Creating Dialogs does not offer the implementation of customized dialog I wanted, not an example of an Adapter that does the trick, and I did try different ways to create a layout with icons, but I couldn’t get it right. Maybe that’s the things a novice Android developer does…

Considering another options to get the implementation of an a Dialog Adapter for my requirement, I thought there might be an example of what I wanted. That’s when I remembered that the long press event of the home screen shows a Dialog with menu options with the icons, as shown in screenshot below:

I thought to myself, “Wait, Android is Open-Source!”, and it’s not the iPHONE! 🙂 That moment I went to Google Code Search, a product still branded as to be in “Google Labs” that I use to look for open-source code (Eclipse, Subversion, etc, etc). So, I started using the menu title “Add to Home screen”  as my search key and I could only find it when I used the exact value found in the source-code sufixing the string with “</string>”, found in the strings.xml. Here’s the URL of the search.

http://www.google.com/codesearch/p?hl=en#4r7JaNM0EqE/res/values/strings.xml&q=%22Add%20to%20Home%20screen%3C/string%3E%22&sa=N&cd=1&ct=rc

<!-- Shortcuts -->
    <skip />
    <!-- Title of dialog box -->
    <string name="menu_item_add_item">Add to Home screen</string>

Bingo! I found the String with its associated  key “menu_item_add_item” and the only thing to do now is to find the implementation of the method that creates the menu item and reuse that on my own customized version for my long item press. So, I did another search for the key “menu_item_add_item” and another success!!! Here’s the implementation of the dialog showed in the screenshot above.

http://www.google.com/codesearch/p?hl=pt-BR#4r7JaNM0EqE/src/com/android/launcher/Launcher.java&q=menu_item_add_item&sa=N&cd=1&ct=rc

    /**
     * Displays the shortcut creation dialog and launches, if necessary, the
     * appropriate activity.
     */
    private class CreateShortcut implements DialogInterface.OnClickListener,
            DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
            DialogInterface.OnShowListener {

        private AddAdapter mAdapter;

        Dialog createDialog() {
            mWaitingForResult = true;

            mAdapter = new AddAdapter(Launcher.this);

            final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
            builder.setTitle(getString(R.string.menu_item_add_item));
            builder.setAdapter(mAdapter, this);

            builder.setInverseBackgroundForced(true);

            AlertDialog dialog = builder.create();
            dialog.setOnCancelListener(this);
            dialog.setOnDismissListener(this);
            dialog.setOnShowListener(this);

            return dialog;
        }

Great, note that the implementation of the dialog has its own implementation of the Adapter called “AddAdapter”, a second piece of implementation where the icons and titles might be defined. So, I looked for the Adapter class “AddAdapter.java” and I was happy to find everything needed there, and for my surprise, no layout implementation in XML. I guess sometimes hard-coded layouts are all you need anyway if you don’t reuse the same thing in a different place. I then looked for the class and I found the implementation of the menu items.

http://www.google.com/codesearch/p?hl=pt-BR#4r7JaNM0EqE/src/com/android/launcher/AddAdapter.java&q=AddAdapter.java%20LayoutInflater&l=63

    public AddAdapter(Launcher launcher) {
        super();

        mInflater = (LayoutInflater) launcher.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // Create default actions
        Resources res = launcher.getResources();

        mItems.add(new ListItem(res, R.string.group_shortcuts,
                R.drawable.ic_launcher_shortcut, ITEM_SHORTCUT));

        mItems.add(new ListItem(res, R.string.group_widgets,
                R.drawable.ic_launcher_appwidget, ITEM_APPWIDGET));

        mItems.add(new ListItem(res, R.string.group_live_folders,
                R.drawable.ic_launcher_add_folder, ITEM_LIVE_FOLDER));

        mItems.add(new ListItem(res, R.string.group_wallpapers,
                R.drawable.ic_launcher_wallpaper, ITEM_WALLPAPER));

    }

As I mentioned earlier, each application is implemented differently and the problem I had now was just a Java refactoring, as I had implemented my events differently. In the end, it payed off and I could implement my own custom dialog. That shows how awesome Google Code Search is! I will take that route the next time I need to implement similar functionalities to my Subversion Edge Android Discovery Client and other future apps.

Subversion Edge Discovery Android Client - Custom Dialog with Icons

Advertisements
%d bloggers like this: