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

Analyzing the ActivityManagerService class (Should know)


The ActivityManagerService class is probably one of the most important of all services that exist in an Android system. In this recipe, we will go through its major functions and highlight peculiarities of the service.

Getting ready

ActivityManagerService.java is the source file and is located at ANDROID_SRC/frameworks/base/services/java/com/android/server/am.

How to do it...

  1. Open this file in a code editor of your choice. We will move through the ActivityManagerService file and understand its various components.

  2. The Activity Manager extends a class known as ActivityManagerNative, which implements the binder protocol for remote invocation. This code is written manually like we've written in the recipe on using IPC with our system service code. This was done as the AIDL compiler did not exist the time the Activity Manager code was written by Google employees.

  3. In the beginning of the file, there are various control variables for different aspects related to activities. One such variable is:

    static final int MAX_ACTIVITIES = 20;

    This defines the maximum number of activities that can be in the stack. It means the maximum number of activities that could be running at a time.

  4. Another interesting variable is the duration the system waits for a process to be launched.

    static final int PROC_START_TIMEOUT = 10*1000;
  5. SDK developers will know that when using a broadcast receiver, the onReceive() method should finish fast. The timeout is governed by:

    static final int BROADCAST_TIMEOUT = 10*1000;

    which is around 10 seconds.

  6. Scroll down in the file, there is a handler which does the majority of the work that does not require immediate return. As you may know, Android has the concept of a handler, which basically is a mechanism to execute code in a separate thread and have it return results later. The handler is defined as:

    final Handler mHandler = new Handler() {
  7. Some example tasks performed are the ANR dialogs, indicated by case SHOW_NOT_RESPONDING_MSG and the sending of broadcasts to registered receivers.

  8. Other functionality is implemented as methods that return synchronously. An asynchronous return means that the results of a request are returned to the caller at a later time, and not immediately, like a synchronous call.

  9. There is a separate section in the sources for permissions manipulation and verification. The only public entry point for permissions checking is the method:

    public int checkPermission(String permission, int pid, int uid)
  10. All permission checks that are executed in the system go through this one method. Hence it serves as a reliable checkpoint to understand what processes are doing with respect to protected data and operations.

  11. Following this, the Activity Manager has a group of methods that manage tasks by manipulating the activity stack. The stack is a data structure defined in the ActivityStack.java file in the same directory as the activity manager service.

  12. The following method is executed when the system has completely booted and is ready to start launching user processes:

    public void systemReady(final Runnable goingCallback)

    There are separate sections managing services, content providers, and broadcasts. At the start of each section, a comment indicates what type of methods follow. Various API methods such as registerReceiver, startActivity, and startService are implemented by the ActivityManagerService class in the same file.

How it works...

The Activity Manager service is started early during the bootup process. At the end, when all the initialization tasks are complete, the systemReady method is executed and the system boot complete broadcast is fired. This method is called by the SystemServer service. It is used to notify the ActivityManagerService that the system is at a point where it is possible to start running third-party code. While the ActivityManagerService does not immediately do this, it gets itself ready when it receives the systemReady call.

There's more...

Whenever you make changes to the ActivityManagerService class, make sure that you don't introduce code paths that make the service vulnerable or leak sensitive data to malicious applications. Always follow the principles of secure coding and protect your methods by permission checks. If you don't want third-party developers to access your functionality, assign a signature permission to that method. The keen reader will have noticed that the ActivityManagerService class is like any other system service and executes in the context of the SystemServer service. Hence, now you realize that your custom system service executes alongside such important services. Any bugs in your system service have the potential to bring down the whole framework. So be careful when writing such code!