Modifying the search widget application (Should know)

In this recipe, we will modify the default search text in the quick search widget. The aim of this recipe is to make the reader comfortable with making changes, compiling them, and testing those changes.

Getting ready

The search widget's code is in the directory named QuickSearchBox under ANDROID_SRC/packages/apps/. Navigate to this directory and open up ANDROID_SRC/packages/apps/QuickSearchBox/res/drawable/text_field_search_empty_google.xml.

How to do it...

  1. Edit the XML layout file by replacing:
    <item android:drawable="@drawable/hint_google" />

    with

    <item android:drawable="@drawable/packt" />
  2. Now, use a popular image editor to create an icon of your choice with dimensions of 97 x 40 pixels. We create a Packt logo variant for this task in GIMP. Make sure you save the image as a PNG type. Copy this image to three locations: drawable-hdpi, drawable-ldpi, and drawable-mdpi.

    Note

    For each screen density, an image with different parameters should be stored. Otherwise, you might notice problems in rendering; images may not appear to be clear. As we are simply demonstrating a concept here, I do not focus on creating differently sized images.

  3. Now we will build the new QuickSearchWidget application. In a terminal, as usual, navigate to the Android source directory, include the environment setup and lunch the emulator target. Issue the following command:
    mmm packages/app/QuickSearchWidget
    
  4. This will build only the search widget. After building is complete, start the emulator and drop to an adb shell. We need to remount the system partition as read/write, since system applications are installed on to this partition which is mounted read-only. To be able to write a new executable of the QuickSearchWidget application, and without rebuilding and reflashing the entire operating system, we will simply "push" the generated APK onto the remounted directory.
  5. We need to find out where the /system partition is mounted. In an adb shell, execute the following command:
    cat /proc/mtd
    

    The output will be similar to the following:

    dev:    size   erasesize  name
    
    mtd0: 0c200000 00020000 "system"
    
    mtd1: 0c200000 00020000 "userdata"
    
    mtd2: 04000000 00020000 "cache"

    Note

    On a real device, this will appear differently. The MTD mounts vary based on the manufacturer of the device. The output seen here is on an emulator, which is where you should be doing initial development and debugging before trying anything on a real device.

  6. We see that /system exists in mtd0. To remount as read/write, in an adb shell:
    mount -o rw,remount -t yaffs2 /dev/block/mtd0 /system
    chmod 777 /system/app
    
  7. Now, exit the adb shell and navigate to ANDROID_SRC/out/target/product/generic/system/app. This is where the built APK is stored.
  8. In a terminal, execute the following command:
    adb push QuickSearchWidget.apk /system/app
    

    This will install the new widget. The change should be visible on the emulator:

    How to do it...

How it works...

The mmm command selectively builds code. The argument to it is the path to the source directory containing an Android.mk file. The path should end with the name of a target that can be built. For example, if you open the Android.mk file, the target is identified by the LOCAL_PACKAGE_NAME tag.

As we have seen in earlier recipes, the system partition is mounted as read-only for security reasons. To install a system application, we need to remount this. The remount can only be done by a root user. On the emulator, ADB runs as root by default.

The side effect of simply pushing an APK to /system/app results in its installation.

The different drawable directories are used based on different screen densities. This has the same meaning as the directories used when writing SDK-based apps.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.111.24