Skip to main content

Posts

Prologue

This is just a write up of some of the things I figured out during my time spent on figuring out Android.  P.S I may be wrong. Things change all the time in android Most of this is based on M This is mostly about the Java part of the framework Contents: The Startups: Init The Startups: App_Process The Startups: AndroidRuntime The Startups: ZygoteInit The Startups: SystemServer ActivityManager: Basics ActivityManager: How an Activity Starts ActivityManager:Configuration Management
Recent posts

ActivityManager: Configuration Management

Configuration describes all device configuration information that can impact the resources the application retrieves.  This includes both user-specified configuration options (locale and scaling) as well as device configurations (such as input modes, screen size and screen orientation). Configuration is defined in: frameworks/base/core/java/android/content/res/Configuration.java You can get the value for an activity by - Configuration config = getResources().getConfiguration(); These include values like: MCC MNC Locale Orientation colormode densitydpi fontscale keyboard hardkeyboardHidden keyboard keyboardHidden navigation navigatioHidden screenHeightDp screenLayout screenwidthDp touchscreen uimode android:configChanges:  flag that lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute...

ActivityManager: How an Activity Starts

How activity starts User calls startActivity defined in Context class startActivity - ContextImpl.java execstartActivity - Instrumentation.java startActivity - ActivityMnagerNative.java START_ACTIVITY_TRANSACTION Start an activity in this task.  Brings the task to the foreground.  If this task is not currently active (that is, its id < 0), then a new activity for the given Intent will be launched as the root of the task and the task brought to the foreground.  Otherwise, if this task is currently active and the Intent does not specify an activity to launch in a new task, then a new activity for the given Intent will be launched on top of the task and the task brought to the foreground.  If this task is currently active and the Intent specifies FLAG_ACTIVITY_NEW_TASK or would otherwise be launched in to a new task, then the activity not launched but this task be brought to the foreground and a new intent delivered to the top activity if appropri...

ActivityManagerService: Basics

ActivityManagerService: Is a rockstar of a systemservice.  It has multiple responsibilities: PROCESS MANAGEMENT ACTIVITY MANAGEMENT PERMISSIONS TASK MANAGEMENT CONTENT PROVIDERS GLOBAL MANAGEMENT SERVICES BACKUP AND RESTORE BROADCASTS INSTRUMENTATION CONFIGURATION LIFETIME MANAGEMENT Source Code: ActivityManager: ActivityManagerNative ActivityManagerService Other files in /frameworks/base/services/core/java/com/android/server/am/ Creation: Starts from SystemServer.         LifeCycle class which extends SystemService is used to star and initialize          ActivityManagerService..We do a Lifecycle.startService to start ActivityManagerService. AMS Tasks in systemserver (initialization): mActivityManagerService = mSystemServiceManager.startService(); mActivityManagerService.setSystemServiceManager(mSystemServiceManager); mActivityManagerService.setInstaller(installer); mActivityManagerSer...

The Startups: SystemServer

SystemServer class kind of initializes all core system services. Before going into the codeflow its good to know about the SystemService and SystemServiceManager classes SystemSevice : This is the base class for services running in the system process. We can Override and implement the lifecycle event callback methods as needed. The lifecycle of a SystemService: /frameworks/base/services/core/java/com/android/server/SystemService.java       The constructor is called and provided with the system Context to initialize the system service.       onStart() is called to get the service running.   The service should publish its binder interface at this point using publishBinderService() .      It may also publish additional local interfaces that other services within the system server may use to access privileged internal functions.      onBootPhase(int phase) is called as many times as there are boot pha...

The Startups: ZygoteInit

ZygoteInit is the Java side startup class for Zygote Process. /frameworks/base/core/java/com/android/internal/os/ZygoteInit.java This pre-initializes some classes and waits for commands on the zygote socket Based on these commands, forks off child processes that inherit the initial state of the VM. When main process is called with start-system-server as arg: Steps: 1, registerZygoteSocket(socketName);     Registers a server socket for zygote command connections 2. preload();     Preload      -Classes          List maintained at /system/etc/preloaded-classes Load and explicitly initialize the given class Class.forName(line, true, null); //This would load and initialize the class       -Resources           Load in commonly used resources, so they can be shared across processes.   Resouces.startPreloading()   TypedArray ar = mResources.obtainTypedArray( ...

The StartUps: Android Runtime

The Zygote Process starts the Android Runtime with the following command. The code for this is at frameworks/base/core/jni/AndroidRuntime.cpp runtime.start("com.android.internal.os.ZygoteInit", args, zygote); This involves starting the virtual machine and calling the "static void main(String[] args)" method of ZygoteInit.  Event Log for Boot Progrsess Start is printed here. Steps : Check is /system/ directory is available Initialize Jni_invocation jni_invocation.Init(NULL);     - Jni_invocation: adds a layer of indirection for applications using the JNI invocation API to allow the JNI implementation to be  selected dynamically. Apps can specify a specific implementation to be used by calling InitJniInvocation.      If this is not done, the  library will chosen based on the value of Android system property "persist.sys.dalvik.vm.lib"(say libart.so, libdvm.so or libartd.so)  on the device, and otherwise fall back to a hard-code...

The Startups: App_Process

As discussed earlier the app_process(Zygote) is started by the init.rc with the following command  -- /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server The syntax to be used is   -- app_process [java-options] cmd-dir start-class-name [options] The implementation of this is present in / frameworks / base / cmds / app_process / app_main.cpp  This process starts the interpreted runtime, then starts up the application(system-server in this case)  arg -Xzygote : Starts in zygote mode  arg start-system-server: starts system-server process  When starting system server this process:  Creates Dalvik Cache Folder maybeCreateDalvikCache()  Calls Android Runtime.start, with args "start-system-server" and "abi-list"     runtime.start("com.android.internal.os.ZygoteInit", args, zygote);  When not starting in Zygote Mode, it just passes the arguements to the application process     runtime.s...