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 PackageManagerService class (Should know)


The Package Manager service is a central service in the Android system and is one of the most important. It manages all the packages in the system and is central to platform security. It maintains a mapping between user/group identifiers and higher-level permission strings. In this recipe, we will see how permissions are managed and granted by the Package Manager service. As you may know, the fundamental aspect of the Android Security Model is the permissions system. This system is managed by the PackageManagerService class.

Getting ready

The source code of the Package Manager service is located at ANDROID_SRC/frameworks/base/services/java/com/android/server/. Open up the file named PackageManagerService.java in your favorite code editor.

How to do it...

  1. Built-in permission UID mappings are stored on the filesystem at /system/etc/permissions/platform.xml. This is loaded into the Package Manager service into the following variable:

    final SparseArray<HashSet<String>> mSystemPermissions =
                new SparseArray<HashSet<String>>();
  2. The service maintains user application permissions in the filesystem at /data/system/packages.xml. Fire up the emulator and drop into an adb shell. We will inspect the contents of the packages.xml file. Obtain a copy with the following command:

    adb pull /data/system/packages.xml
    
  3. Open up the file in a text editor and search for the term "packt":

    <package name="com.packtclient" codePath="/system/app/PacktLibraryClient.apk" nativeLibraryPath="/data/data/com.packtclient/lib" flags="1" ft="1389a1a82a8" it="13879fbb780" ut="1389a1b8ae2" version="10" userId="10010">
    <sigs count="1">
    <cert index="1" />
    </sigs>
    </package>

    Here we notice that PacktLibraryClient has been assigned a userId of 10010.

How it works...

The Package Manager service stores and manages all configuration data in XML files with proper Linux permissions on these files. Internally, the service makes use of XML pull parsers to read and process these files. This service is interrogated by other services, most notably the Activity Manager service, as to whether a package possesses a certain permission or not.

There's more...

Whenever you make changes to this service, please rethink your design carefully and only if there is no other way to accomplish the task, run the unit tests.

To run these tests, run the following command in a terminal (as always, I assume you have set up a build environment by including build/envsetup.sh):

mmm frameworks/base/tests/AndroidTests

Then start the emulator and execute this in a terminal over ADB:

adb install -r -f out/target/product/passion/data/app/AndroidTests.apk

Finally drop into an adb shell and execute:

adb shell am instrument -w -e class com.android.unit_tests.PackageManagerTests com.android.unit_tests/android.test.InstrumentationTestRunner

The unit tests are part of the Compatibility Test Suite (CTS). The CTS is part of the Android compatibility program. The aim is to achieve standardization among Android vendor implementations. The interested reader is referred to an overview of the compatibility program at http://source.android.com/compatibility/overview.html. Details on the CTS itself may be found at http://source.android.com/compatibility/cts-intro.html.