Book Image

Instant Android Systems Development How-to

By : Earlence Fernandes
Book Image

Instant Android Systems Development How-to

By: Earlence Fernandes

Overview of this book

<p>Android is by far the most popular open source mobile operating system. Learning to write high quality code at the platform level and learning how the systems works internally is a vital skill. This book teaches you these skills with clear and concise explanations and code examples.</p> <p>Instant Android Systems Development How-to provides a gentle introduction to the platform internals without sacrificing depth. Source code examples are designed to be meaningful, but at the same time, do not disguise their real purpose, which is to illustrate systems development techniques and common design patterns in android systems programming. Readers will be guided through several examples that give a hands-on experience.</p> <p>Readers begin by downloading the android source code, which is a topic of much discussion on android forums. They are then guided through the android boot process, and later on learn various common android systems development paradigms. More importantly, the book provides advice on when to use certain techniques which is often a mystery for the novice developer. Readers who complete the book will have high confidence in developing good systems code for Android.</p> <p>The book discusses how to setup a development machine and how to obtain the android source code and kernel code. It describes the source code organization and how the system boots up with precise references to various points in the source code. It highlights the common systems design patterns followed and how to create a custom system service. It then covers the all important flashing of phones. This is a topic of much confusion and the book provides direct steps to achieve safe flashing of developer phones. It describes the user application library mechanism and the platform library mechanism. Native code is needed for certain operations and an example service utilizing native code is explained. Modification of core system applications is explained and useful tips are provided on how to speed up the build-test cycle. The book concludes with a case study of two real world android platform extensions which give the user a reference while developing their own extensions.</p> <p>Instant Android Systems Development How-to is a well rounded book on platform internals that provides simple explanations without sacrificing depth and rigor.</p>
Table of Contents (7 chapters)
Instant Android Systems Development How-to
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Instant Android Systems Development How-to

Writing the Java driver (Should know)


To test our native library, we need to write some Java code that will call the native function. I will re-use code from an earlier example, where we created and tested the PacktCrypto system service.

We need to call the native code from the Java-based PacktCrypto service. Hence, in this recipe, we will modify the PacktCrypto system service so that it can call a native method.

Getting ready

Open up the PacktCrypto.java file that is located in the services/java/com/android/packt directory and change it by adding the following lines towards the end of the file.

How to do it...

  1. To test the sort method, I simply call the sort() method inside our existing getMD5 method. I am doing this so that you can quickly test whether the native code works as expected without making too many changes. If you recall, we have already written a test harness for getMD5 inside SystemServer.java. The first step is to modify the PacktCrypto.java file by adding the following lines towards the end of the file.

  2. This is a fragment of the PacktCrypto.java file that tests our native function:

    void sort(int [] data) 
      { 
        int [] sorted = quickSort(data); 
        for(int i = 0; i < data.length; i++) 
          Log.i(TAG, "s: " + sorted[i]); 
      } 
       
      native int [] quickSort(int [] data);
  3. By adding the following lines to the getMD5 method towards the end, we will be able to quickly test our native code. The new version of getMD5() is as follows:

    public byte [] getMD5(String data) 
      { 
        byte [] dataBytes = null; 
        MessageDigest md5 = null; 
         
        try { 
         
          dataBytes = data.getBytes("UTF-8"); 
          md5 = MessageDigest.getInstance("MD5"); 
          Log.i(TAG, "MD5 digestion invoked"); 
           
        } catch(java.io.UnsupportedEncodingException uee) 
        { 
          Log.e(TAG, "Unsupported Encoding UTF8"); 
        } 
        catch(java.security.NoSuchAlgorithmException nsae) 
        { 
          Log.e(TAG, "No Algorithm MD5"); 
        } 
        //quick hack to test our native method 
        int toSort [] = { 5, -1, 0, 30, 4, 0, 1, 3, 15, 3 }; 
        sort(toSort); 
                 
        if(md5 != null) 
          return md5.digest(dataBytes); 
        else 
          return null; 
      }

    Notice the call to sort().

  4. Now, we can build our code. In a terminal, as usual, issue the make command. Once the build is complete, run the emulator and observe the logs:

    I/SystemServer(   67): PacktCrypto service 
    I/PacktCrypto(   67): MD5 digestion invoked 
    I/PacktCrypto(   67): s: -1 
    I/PacktCrypto(   67): s: 0 
    I/PacktCrypto(   67): s: 0 
    I/PacktCrypto(   67): s: 1 
    I/PacktCrypto(   67): s: 3 
    I/PacktCrypto(   67): s: 3 
    I/PacktCrypto(   67): s: 4 
    I/PacktCrypto(   67): s: 5 
    I/PacktCrypto(   67): s: 15 
    I/PacktCrypto(   67): s: 30 
    I/PacktTest(   67): MD5 sum: 422164113c2fc595dd0ab44a18925ac5

How it works...

The Java code contains a native method definition. The framework loads the native code and during the onload event, our native implementation is loaded and registered. When getMD5() is called, our native code is executed and the output is visible on the logger.

In the next recipe, we will learn how some of the most important core Android services are organized and how to make changes to these services. We will also learn how to secure our changes with custom permissions added to the framework.

The Internet infrastructure on Android requires discussion as its design is non-conventional as compared to other services such as the Location Manager service.