yashsingh1/xdaforums huggingface dataset:
[Android Software/Hacking General [Developers Only]] llrraa2010:
startService--example

package shy.luo.ashmem;

......

public class Client extends Activity implements OnClickListener {

......

IMemoryService memoryService = null;

......

[MENTION=439709]override[/MENTION]

public void onCreate(Bundle savedInstanceState) {

......

IMemoryService ms = getMemoryService();

if(ms == null) {

startService(new Intent("shy.luo.ashmem.server"));

} else {

Log.i(LOG_TAG, "Memory Service has started.");

}

......

Log.i(LOG_TAG, "Client Activity Created.");

}

......

}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="shy.luo.ashmem"

android:sharedUserId="android.uid.system"

android:versionCode="1"

android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

......

<service

android:enabled="true"

android:name=".Server"

android:process=".Server" >

<intent-filter>

<action android:name="shy.luo.ashmem.server"/>

<category android:name="android.intent.category.DEFAULT"/>

</intent-filter>

</service>

</application>

</manifest>

package shy.luo.ashmem;

......

public class Server extends Service {

......

[MENTION=439709]override[/MENTION]

public IBinder onBind(Intent intent) {

return null;

}

[MENTION=439709]override[/MENTION]

public void onCreate() {

......

}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
startService--startService of Activity

frameworks/base/services/java/com/android/server/SystemServer.java

class ServerThread extends Thread {

......

[MENTION=439709]override[/MENTION]

public void run() {

......

// Critical services...

try {

......

context = ActivityManagerService.main(factoryTest);

......

ActivityManagerService.setSystemProcess();

......

} catch (RuntimeException e) {

Slog.e("System", "Failure starting core service", e);

}

......

}

......

}

frameworks/base/services/java/com/android/server/am/ActivityManagerService.java

public final class ActivityManagerService extends ActivityManagerNative

implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {

......

static ActivityManagerService mSelf;

......

public static void setSystemProcess() {

try {

ActivityManagerService m = mSelf;

ServiceManager.addService("activity", m);

......

} catch (PackageManager.NameNotFoundException e) {

......

}

}

......

public static final Context main(int factoryTest) {

......

ActivityManagerService m = thr.mService;

mSelf = m;

......

}

}

frameworks/base/core/java/android/app/ActivityManagerNative.java

class ActivityManagerProxy implements IActivityManager

{

......

public ComponentName startService(IApplicationThread caller, Intent service,

String resolvedType) throws RemoteException

{

Parcel data = Parcel.obtain();

Parcel reply = Parcel.obtain();

data.writeInterfaceToken(IActivityManager.descriptor);

data.writeStrongBinder(caller != null ? caller.asBinder() : null);

service.writeToParcel(data, 0);

data.writeString(resolvedType);

mRemote.transact(START_SERVICE_TRANSACTION, data, reply, 0);

reply.readException();

ComponentName res = ComponentName.readFromParcel(reply);

data.recycle();

reply.recycle();

return res;

}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
startService--startService of ActivityManagerService
[Android Software/Hacking General [Developers Only]] llrraa2010:
startService--startService of ActivityManagerService--step 1

Step 1. ActivityManagerService.startService

frameworks/base/services/java/com/android/server/am/ActivityManagerService.java

public final class ActivityManagerService extends ActivityManagerNative

implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {

......

public ComponentName startService(IApplicationThread caller, Intent service,

String resolvedType) {

// Refuse possible leaked file descriptors

if (service != null && service.hasFileDescriptors() == true) {

throw new IllegalArgumentException("File descriptors passed in Intent");

}

synchronized(this) {

final int callingPid = Binder.getCallingPid();

final int callingUid = Binder.getCallingUid();

final long origId = Binder.clearCallingIdentity();

ComponentName res = startServiceLocked(caller, service,

resolvedType, callingPid, callingUid);

Binder.restoreCallingIdentity(origId);

return res;

}

}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
startService--startService of ActivityManagerService--step 2

Step 2. ActivityManagerService.startServiceLocked

frameworks/base/services/java/com/android/server/am/ActivityManagerService.java

public final class ActivityManagerService extends ActivityManagerNative

implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {

......

ComponentName startServiceLocked(IApplicationThread caller,

Intent service, String resolvedType,

int callingPid, int callingUid) {

synchronized(this) {

......

ServiceLookupResult res =

retrieveServiceLocked(service, resolvedType,

callingPid, callingUid);

......

ServiceRecord r = res.record;

......

if (!bringUpServiceLocked(r, service.getFlags(), false)) {

return new ComponentName("!", "Service process is bad");

}

return r.name;

}

}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
startService--startService of ActivityManagerService--step 3

Step 3. ActivityManagerService.bringUpServiceLocked

frameworks/base/services/java/com/android/server/am/ActivityManagerService.java

public final class ActivityManagerService extends ActivityManagerNative

implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {

......

private final boolean bringUpServiceLocked(ServiceRecord r,

int intentFlags, boolean whileRestarting) {

......

final String appName = r.processName;

......

// Not running -- get it started, and enqueue this service record

// to be executed when the app comes up.

if (startProcessLocked(appName, r.appInfo, true, intentFlags,

"service", r.name, false) == null) {

......

return false;

}

if (!mPendingServices.contains(r)) {

mPendingServices.add(r);

}

return true;

}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
startService--startService of ActivityManagerService--step 4

Step 4. ActivityManagerService.startProcessLocked

frameworks/base/services/java/com/android/server/am/ActivityManagerService.java

public final class ActivityManagerService extends ActivityManagerNative

implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {

......

private final void startProcessLocked(ProcessRecord app,

String hostingType, String hostingNameStr) {

......

try {

......

int pid = Process.start("android.app.ActivityThread",

mSimpleProcessManagement ? app.processName : null, uid, uid,

gids, debugFlags, null);

......

if (pid == 0 || pid == MY_PID) {

......

} else if (pid > 0) {

app.pid = pid;

app.removed = false;

synchronized (mPidsSelfLocked) {

this.mPidsSelfLocked.put(pid, app);

......

}

} else {

......

}

} catch (RuntimeException e) {

......

}

}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
startService--startService of ActivityManagerService--step 5

Step 5. Process.start

frameworks/base/core/java/android/os/Process.java

Step 6. ActivityThread.main

frameworks/base/core/java/android/app/ActivityThread.java

public final class ActivityThread {

......

public static final void main(String[] args) {

......

Looper.prepareMainLooper();

......

ActivityThread thread = new ActivityThread();

thread.attach(false);

......

Looper.loop();

......

thread.detach();

......

}

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
startService--startService of ActivityManagerService--step 7

Step 7. ActivityThread.attach

frameworks/base/core/java/android/app/ActivityThread.java

public final class ActivityThread {

......

private final void attach(boolean system) {

......

if (!system) {

......

IActivityManager mgr = ActivityManagerNative.getDefault();

try {

mgr.attachApplication(mAppThread);

} catch (RemoteException ex) {

}

} else {

......

}

......

}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
startService--startService of ActivityManagerService--step 8

Step 8. ActivityManagerProxy.attachApplication

frameworks/base/core/java/android/app/ActivityManagerNative.java

class ActivityManagerProxy implements IActivityManager

{

......

public void attachApplication(IApplicationThread app) throws RemoteException

{

Parcel data = Parcel.obtain();

Parcel reply = Parcel.obtain();

data.writeInterfaceToken(IActivityManager.descriptor);

data.writeStrongBinder(app.asBinder());

mRemote.transact(ATTACH_APPLICATION_TRANSACTION, data, reply, 0);

reply.readException();

data.recycle();

reply.recycle();

}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
startService--startService of ActivityManagerService--step 9

Step 9. ActivityManagerService.attachApplication

frameworks/base/services/java/com/android/server/am/ActivityManagerService.java

public final class ActivityManagerService extends ActivityManagerNative

implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {

......

public final void attachApplication(IApplicationThread thread)

{

synchronized (this) {

int callingPid = Binder.getCallingPid();

final long origId = Binder.clearCallingIdentity();

attachApplicationLocked(thread, callingPid);

Binder.restoreCallingIdentity(origId);

}

}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
startService--startService of ActivityManagerService--step 10

Step 10. ActivityManagerService.attachApplicationLocked

frameworks/base/services/java/com/android/server/am/ActivityManagerService.java

public final class ActivityManagerService extends ActivityManagerNative

implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {

......

private final boolean attachApplicationLocked(IApplicationThread thread,

int pid) {

// Find the application record that is being attached... either via

// the pid if we are running in multiple processes, or just pull the

// next app record if we are emulating process with anonymous threads.

ProcessRecord app;

if (pid != MY_PID && pid >= 0) {

synchronized (mPidsSelfLocked) {

app = mPidsSelfLocked.get(pid);

}

} else if (mStartingProcesses.size() > 0) {

app = mStartingProcesses.remove(0);

app.setPid(pid);

} else {

app = null;

}

......

String processName = app.processName;

......

app.thread = thread;

......

boolean badApp = false;

......

// Find any services that should be running in this process...

if (!badApp && mPendingServices.size() > 0) {

ServiceRecord sr = null;

try {

for (int i=0; i<mPendingServices.size(); i++) {

sr = mPendingServices.get(i);

if (app.info.uid != sr.appInfo.uid

|| !processName.equals(sr.processName)) {

continue;

}

mPendingServices.remove(i);

i--;

realStartServiceLocked(sr, app);

didSomething = true;

}

} catch (Exception e) {

......

}

}

......

return true;

}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
startService--startService of ActivityManagerService--step 11

Step 11. ActivityManagerService.realStartServiceLocked

frameworks/base/services/java/com/android/server/am/ActivityManagerService.java

class ActivityManagerProxy implements IActivityManager

{

......

private final void realStartServiceLocked(ServiceRecord r,

ProcessRecord app) throws RemoteException {

......

r.app = app;

......

try {

......

app.thread.scheduleCreateService(r, r.serviceInfo);

......

} finally {

......

}

......

}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
startService--startService of ActivityManagerService--step 12

Step 12. ApplicationThreadProxy.scheduleCreateService

frameworks/base/core/java/android/app/ApplicationThreadNative.java

class ApplicationThreadProxy implements IApplicationThread {

......

public final void scheduleCreateService(IBinder token, ServiceInfo info)

throws RemoteException {

Parcel data = Parcel.obtain();

data.writeInterfaceToken(IApplicationThread.descriptor);

data.writeStrongBinder(token);

info.writeToParcel(data, 0);

mRemote.transact(SCHEDULE_CREATE_SERVICE_TRANSACTION, data, null,

IBinder.FLAG_ONEWAY);

data.recycle();

}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
startService--startService of ActivityManagerService--step 13

Step 13. ApplicationThread.scheduleCreateServic

frameworks/base/core/java/android/app/ActivityThread.java

public final class ActivityThread {

......

private final class ApplicationThread extends ApplicationThreadNative {

......

public final void scheduleCreateService(IBinder token,

ServiceInfo info) {

CreateServiceData s = new CreateServiceData();

s.token = token;

s.info = info;

queueOrSendMessage(H.CREATE_SERVICE, s);

}

......

}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
startService--startService of ActivityManagerService--step 14

Step 14. ActivityThread.queueOrSendMessage

frameworks/base/core/java/android/app/ActivityThread.java

public final class ActivityThread {

......

private final void queueOrSendMessage(int what, Object obj) {

queueOrSendMessage(what, obj, 0, 0);

}

private final void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {

synchronized (this) {

......

Message msg = Message.obtain();

msg.what = what;

msg.obj = obj;

msg.arg1 = arg1;

msg.arg2 = arg2;

mH.sendMessage(msg);

}

}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
startService--startService of ActivityManagerService--step 15

Step 15. H.sendMessage

frameworks/base/core/java/android/os/Handler.java

Step 16. H.handleMessage

frameworks/base/core/java/android/app/ActivityThread.java

public final class ActivityThread {

......

private final class H extends Handler {

......

public void handleMessage(Message msg) {

......

switch (msg.what) {

......

case CREATE_SERVICE:

handleCreateService((CreateServiceData)msg.obj);

break;

......

}

......

}

......

}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
startService--startService of ActivityManagerService--step 17

Step 17. ActivityThread.handleCreateService

frameworks/base/core/java/android/app/ActivityThread.java

public final class ActivityThread {

......

private final void handleCreateService(CreateServiceData data) {

// If we are getting ready to gc after going to the background, well

// we are back active so skip it.

unscheduleGcIdler();

LoadedApk packageInfo = getPackageInfoNoCheck(

data.info.applicationInfo);

Service service = null;

try {

java.lang.ClassLoader cl = packageInfo.getClassLoader();

service = (Service) cl.loadClass(data.info.name).newInstance();

} catch (Exception e) {

if (!mInstrumentation.onException(service, e)) {

throw new RuntimeException(

"Unable to instantiate service " + data.info.name

+ ": " + e.toString(), e);

}

}

try {

if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);

ContextImpl context = new ContextImpl();

context.init(packageInfo, null, this);

Application app = packageInfo.makeApplication(false, mInstrumentation);

context.setOuterContext(service);

service.attach(context, this, data.info.name, data.token, app,

ActivityManagerNative.getDefault());

service.onCreate();

mServices.put(data.token, service);

try {

ActivityManagerNative.getDefault().serviceDoneExecuting(

data.token, 0, 0, 0);

} catch (RemoteException e) {

// nothing to do.

}

} catch (Exception e) {

if (!mInstrumentation.onException(service, e)) {

throw new RuntimeException(

"Unable to create service " + data.info.name

+ ": " + e.toString(), e);

}

}

}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
startService--startService of ActivityManagerService--step 18

Step 18. ClassLoader.loadClass

java.lang.ClassLoader cl = packageInfo.getClassLoader();

service = (Service) cl.loadClass(data.info.name).newInstance();

Step 19. Obtain Service

ActivityThread.handleCreateService

Step 20. Service.onCreate

public class Server extends Service {

......

[MENTION=439709]override[/MENTION]

public void onCreate() {

......

}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
start of Activity--Activity example--MainActivity.java

src/shy/luo/activity/MainActivity.java

package shy.luo.activity;

import shy.luo.activity.R;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

private final static String LOG_TAG = "shy.luo.activity.MainActivity";

private Button startButton = null;

[MENTION=439709]override[/MENTION]

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

startButton = (Button)findViewById(R.id.button_start);

startButton.setOnClickListener(this);

Log.i(LOG_TAG, "Main Activity Created.");

}

[MENTION=439709]override[/MENTION]

public void onClick(View v) {

if(v.equals(startButton)) {

Intent intent = new Intent("shy.luo.activity.subactivity");

startActivity(intent);

}

}

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
start of Activity--Activity example--SubActivity.java

package shy.luo.activity;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class SubActivity extends Activity implements OnClickListener {

private final static String LOG_TAG = "shy.luo.activity.SubActivity";

private Button finishButton = null;

[MENTION=439709]override[/MENTION]

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.sub);

finishButton = (Button)findViewById(R.id.button_finish);

finishButton.setOnClickListener(this);

Log.i(LOG_TAG, "Sub Activity Created.");

}

[MENTION=439709]override[/MENTION]

public void onClick(View v) {

if(v.equals(finishButton)) {

finish();

}

}

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
start of Activity--Activity example--AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="shy.luo.activity"

android:versionCode="1"

android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".MainActivity"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<activity android:name=".SubActivity"

android:label="@string/sub_activity">

<intent-filter>

<action android:name="shy.luo.activity.subactivity"/>

<category android:name="android.intent.category.DEFAULT"/>

</intent-filter>

</activity>

</application>

</manifest>
[Android Software/Hacking General [Developers Only]] llrraa2010:
start of Activity--Activity example--strings.xml

res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="app_name">Activity</string>

<string name="sub_activity">Sub Activity</string>

<string name="start">Start sub-activity</string>

<string name="finish">Finish activity</string>

</resources>
[Android Software/Hacking General [Developers Only]] llrraa2010:
start of Activity--Activity example--Android.mk

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES := $(call all-subdir-java-files)

LOCAL_PACKAGE_NAME := Activity

include $(BUILD_PACKAGE)
[Android Software/Hacking General [Developers Only]] llrraa2010:
start of Activity--flow chart
[Android Software/Hacking General [Developers Only]] llrraa2010:
start of Activity--start of MainActivity

Please look into the attached for the clearer version.
[Android Software/Hacking General [Developers Only]] llrraa2010:
start of Activity--start of MainActivity--Step 1

Step 1. Launcher.startActivitySafely

packages/apps/Launcher2/src/com/android/launcher2/Launcher.java

/**
* Default launcher application.
*/
public final class Launcher extends Activity
implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {

......

/**
* Launches the intent referred by the clicked shortcut.
*
* [MENTION=955119]param[/MENTION] v The view representing the clicked shortcut.
*/
public void onClick(View v) {
Object tag = v.getTag();
if (tag instanceof ShortcutInfo) {
// Open shortcut
final Intent intent = ((ShortcutInfo) tag).intent;
int[] pos = new int[2];
v.getLocationOnScreen(pos);
intent.setSourceBounds(new Rect(pos[0], pos[1],
pos[0] + v.getWidth(), pos[1] + v.getHeight()));
startActivitySafely(intent, tag);
} else if (tag instanceof FolderInfo) {
......
} else if (v == mHandleView) {
......
}
}

void startActivitySafely(Intent intent, Object tag) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
......
} catch (SecurityException e) {
......
}
}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
start of Activity--start of MainActivity--Step 2

Step 2. Activity.startActivity

frameworks/base/core/java/android/app/Activity.java
public class Activity extends ContextThemeWrapper
implements LayoutInflater.Factory,
Window.Callback, KeyEvent.Callback,
OnCreateContextMenuListener, ComponentCallbacks {

......

[MENTION=439709]override[/MENTION]
public void startActivity(Intent intent) {
startActivityForResult(intent, -1);
}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
start of Activity--start of MainActivity--Step 3

Step 3. Activity.startActivityForResult

frameworks/base/core/java/android/app/Activity.java

public class Activity extends ContextThemeWrapper
implements LayoutInflater.Factory,
Window.Callback, KeyEvent.Callback,
OnCreateContextMenuListener, ComponentCallbacks {

......

public void startActivityForResult(Intent intent, int requestCode) {
if (mParent == null) {
Instrumentation.ActivityResult ar =
mInstrumentation.execStartActivity(
this, mMainThread.getApplicationThread(), mToken, this,
intent, requestCode);
......
} else {
......
}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
start of Activity--start of MainActivity--Step 4

Step 4. Instrumentation.execStartActivity

frameworks/base/core/java/android/app/Instrumentation.java

public class Instrumentation {

......

public ActivityResult execStartActivity(
Context who, IBinder contextThread, IBinder token, Activity target,
Intent intent, int requestCode) {
IApplicationThread whoThread = (IApplicationThread) contextThread;
if (mActivityMonitors != null) {
......
}
try {
int result = ActivityManagerNative.getDefault()
.startActivity(whoThread, intent,
intent.resolveTypeIfNeeded(who.getContentResolver()),
null, 0, token, target != null ? target.mEmbeddedID : null,
requestCode, false, false);
......
} catch (RemoteException e) {
}
return null;
}

......

}