yashsingh1/xdaforums huggingface dataset:
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 11.*ApplicationThreadProxy.scheduleRegisteredReceiver

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

class ApplicationThreadProxy implements IApplicationThread {
......

public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
int resultCode, String dataStr, Bundle extras, boolean ordered, boolean sticky)
throws RemoteException {
Parcel data = Parcel.obtain();
data.writeInterfaceToken(IApplicationThread.descriptor);
data.writeStrongBinder(receiver.asBinder());
intent.writeToParcel(data, 0);
data.writeInt(resultCode);
data.writeString(dataStr);
data.writeBundle(extras);
data.writeInt(ordered ? 1 : 0);
data.writeInt(sticky ? 1 : 0);
mRemote.transact(SCHEDULE_REGISTERED_RECEIVER_TRANSACTION, data, null,
IBinder.FLAG_ONEWAY);
data.recycle();
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 12.*ApplicaitonThread.scheduleRegisteredReceiver

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

public final class ActivityThread {
......

private final class ApplicationThread extends ApplicationThreadNative {
......

// This function exists to make sure all receiver dispatching is
// correctly ordered, since these are one-way calls and the binder driver
// applies transaction ordering per object for such calls.
public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
int resultCode, String dataStr, Bundle extras, boolean ordered,
boolean sticky) throws RemoteException {
receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
}

......
}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 13.*InnerReceiver.performReceive

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

final class LoadedApk {
......

static final class ReceiverDispatcher {

final static class InnerReceiver extends IIntentReceiver.Stub {
......

public void performReceive(Intent intent, int resultCode,
String data, Bundle extras, boolean ordered, boolean sticky) {

LoadedApk.ReceiverDispatcher rd = mDispatcher.get();
......
if (rd != null) {
rd.performReceive(intent, resultCode, data, extras,
ordered, sticky);
} else {
......
}
}
}

......
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 14.*ReceiverDispatcher.performReceive

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

final class LoadedApk {
......

static final class ReceiverDispatcher {
......

public void performReceive(Intent intent, int resultCode,
String data, Bundle extras, boolean ordered, boolean sticky) {
......

Args args = new Args();
args.mCurIntent = intent;
args.mCurCode = resultCode;
args.mCurData = data;
args.mCurMap = extras;
args.mCurOrdered = ordered;
args.mCurSticky = sticky;
if (!mActivityThread.post(args)) {
......
}
}

......
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 15. Hanlder.post

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

Step 16. Args.run

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

final class LoadedApk {
......

static final class ReceiverDispatcher {
......

final class Args implements Runnable {
......

public void run() {
BroadcastReceiver receiver = mReceiver;

......

Intent intent = mCurIntent;

......

try {
ClassLoader cl = mReceiver.getClass().getClassLoader();
intent.setExtrasClassLoader(cl);
if (mCurMap != null) {
mCurMap.setClassLoader(cl);
}
receiver.setOrderedHint(true);
receiver.setResult(mCurCode, mCurData, mCurMap);
receiver.clearAbortBroadcast();
receiver.setOrderedHint(mCurOrdered);
receiver.setInitialStickyHint(mCurSticky);
receiver.onReceive(mContext, intent);
} catch (Exception e) {
......
}

......
}

......
}

......
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 17. BroadcastReceiver.onReceive

src/shy/luo/broadcast/MainActivity.java

public class MainActivity extends Activity implements OnClickListener {
......

private BroadcastReceiver counterActionReceiver = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent) {
int counter = intent.getIntExtra(CounterService.COUNTER_VALUE, 0);
String text = String.valueOf(counter);
counterText.setText(text);

Log.i(LOG_TAG, "Receive counter event");
}
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
8.2 start of Service in the same process
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 1. ContextWrapper.bindService

frameworks/base/core/java/android/content/ContextWrapper.java

public class ContextWrapper extends Context {
Context mBase;
......

@Override
public boolean bindService(Intent service, ServiceConnection conn,
int flags) {
return mBase.bindService(service, conn, flags);
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 2.*ContextImpl.bindService

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

class ContextImpl extends Context {
......

@Override
public boolean bindService(Intent service, ServiceConnection conn,
int flags) {
IServiceConnection sd;
if (mPackageInfo != null) {
sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(),
mMainThread.getHandler(), flags);
} else {
......
}
try {
int res = ActivityManagerNative.getDefault().bindService(
mMainThread.getApplicationThread(), getActivityToken(),
service, service.resolveTypeIfNeeded(getContentResolver()),
sd, flags);
......
return res != 0;
} catch (RemoteException e) {
return false;
}
}

......

}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 3. ActivityThread.getHandler

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

public final class ActivityThread {
......

final H mH = new H();

......

private final class H extends Handler {
......

public void handleMessage(Message msg) {
......
}

......
}

......

final Handler getHandler() {
return mH;
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 4. LoadedApk.getServiceDispatcher

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

final class LoadedApk {
......

public final IServiceConnection getServiceDispatcher(ServiceConnection c,
Context context, Handler handler, int flags) {
synchronized (mServices) {
LoadedApk.ServiceDispatcher sd = null;
HashMap<ServiceConnection, LoadedApk.ServiceDispatcher> map = mServices.get(context);
if (map != null) {
sd = map.get(c);
}
if (sd == null) {
sd = new ServiceDispatcher(c, context, handler, flags);
if (map == null) {
map = new HashMap<ServiceConnection, LoadedApk.ServiceDispatcher>();
mServices.put(context, map);
}
map.put(c, sd);
} else {
sd.validate(context, handler);
}
return sd.getIServiceConnection();
}
}

......

static final class ServiceDispatcher {
private final ServiceDispatcher.InnerConnection mIServiceConnection;
private final ServiceConnection mConnection;
private final Handler mActivityThread;
......

private static class InnerConnection extends IServiceConnection.Stub {
final WeakReference<LoadedApk.ServiceDispatcher> mDispatcher;
......

InnerConnection(LoadedApk.ServiceDispatcher sd) {
mDispatcher = new WeakReference<LoadedApk.ServiceDispatcher>(sd);
}

......
}

......

ServiceDispatcher(ServiceConnection conn,
Context context, Handler activityThread, int flags) {
mIServiceConnection = new InnerConnection(this);
mConnection = conn;
mActivityThread = activityThread;
......
}

......

IServiceConnection getIServiceConnection() {
return mIServiceConnection;
}

......
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 5. ActivityManagerService.bindService

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

class ActivityManagerProxy implements IActivityManager
{
......

public int bindService(IApplicationThread caller, IBinder token,
Intent service, String resolvedType, IServiceConnection connection,
int flags) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
data.writeInterfaceToken(IActivityManager.descriptor);
data.writeStrongBinder(caller != null ? caller.asBinder() : null);
data.writeStrongBinder(token);
service.writeToParcel(data, 0);
data.writeString(resolvedType);
data.writeStrongBinder(connection.asBinder());
data.writeInt(flags);
mRemote.transact(BIND_SERVICE_TRANSACTION, data, reply, 0);
reply.readException();
int res = reply.readInt();
data.recycle();
reply.recycle();
return res;
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 6. ActivityManagerService.bindService

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

public final class ActivityManagerService extends ActivityManagerNative
implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
......

public int bindService(IApplicationThread caller, IBinder token,
Intent service, String resolvedType,
IServiceConnection connection, int flags) {
......

synchronized(this) {
......
final ProcessRecord callerApp = getRecordForAppLocked(caller);
......

ActivityRecord activity = null;
if (token != null) {
int aindex = mMainStack.indexOfTokenLocked(token);
......
activity = (ActivityRecord)mMainStack.mHistory.get(aindex);
}

......

ServiceLookupResult res =
retrieveServiceLocked(service, resolvedType,
Binder.getCallingPid(), Binder.getCallingUid());

......

ServiceRecord s = res.record;

final long origId = Binder.clearCallingIdentity();

......

AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp);
ConnectionRecord c = new ConnectionRecord(b, activity,
connection, flags, clientLabel, clientIntent);

IBinder binder = connection.asBinder();
ArrayList<ConnectionRecord> clist = s.connections.get(binder);

if (clist == null) {
clist = new ArrayList<ConnectionRecord>();
s.connections.put(binder, clist);
}
clist.add(c);
b.connections.add(c);
if (activity != null) {
if (activity.connections == null) {
activity.connections = new HashSet<ConnectionRecord>();
}
activity.connections.add(c);
}
b.client.connections.add(c);
clist = mServiceConnections.get(binder);
if (clist == null) {
clist = new ArrayList<ConnectionRecord>();
mServiceConnections.put(binder, clist);
}

clist.add(c);

if ((flags&Context.BIND_AUTO_CREATE) != 0) {
......
if (!bringUpServiceLocked(s, service.getFlags(), false)) {
return 0;
}
}

......
}

return 1;
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 7.*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;
ProcessRecord app = getProcessRecordLocked(appName, r.appInfo.uid);

if (app != null && app.thread != null) {
try {
realStartServiceLocked(r, app);
return true;
} catch (RemoteException e) {
......
}
}

// 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) {
......
}

......
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 8.*ActivityManagerService.realStartServiceLocked

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

public final class ActivityManagerService extends ActivityManagerNative
implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
......

private final void realStartServiceLocked(ServiceRecord r,
ProcessRecord app) throws RemoteException {
......
r.app = app;
......

app.services.add(r);
......

try {
......
app.thread.scheduleCreateService(r, r.serviceInfo);
......
} finally {
......
}

requestServiceBindingsLocked(r);

......
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 9. 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:
Step 10.*ApplicationThread.scheduleCreateService

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:
Step 11.*ActivityThread.queueOrSendMessage

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

public final class ActivityThread {
......

// if the thread hasn't started yet, we don't have the handler, so just
// save the messages until we're ready.
private final void queueOrSendMessage(int what, Object obj) {
queueOrSendMessage(what, obj, 0, 0);
}

private final void queueOrSendMessage(int what, Object obj, int arg1) {
queueOrSendMessage(what, obj, arg1, 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:
Step 12. H.sendMessage

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:
Step 13.*ActivityThread.handleCreateService

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

public final class ActivityThread {
......

private final void handleCreateService(CreateServiceData data) {
......

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) {
......
}

try {
......

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);
......
} catch (Exception e) {
......
}
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 14. CounterService.onCreate

src/shy/luo/broadcast/CounterService.java

public class CounterService extends Service implements ICounterService {
......

@Override
public void onCreate() {
super.onCreate();

Log.i(LOG_TAG, "Counter Service Created.");
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 15.*ActivityManagerService.requestServiceBindingsLocked

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

public final class ActivityManagerService extends ActivityManagerNative
implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
......

private final void requestServiceBindingsLocked(ServiceRecord r) {
Iterator<IntentBindRecord> bindings = r.bindings.values().iterator();
while (bindings.hasNext()) {
IntentBindRecord i = bindings.next();
if (!requestServiceBindingLocked(r, i, false)) {
break;
}
}
}

private final boolean requestServiceBindingLocked(ServiceRecord r,
IntentBindRecord i, boolean rebind) {
......
if ((!i.requested || rebind) && i.apps.size() > 0) {
try {
......
r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind);
......
} catch (RemoteException e) {
......
}
}
return true;
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 16.*ApplicationThreadProxy.scheduleBindService

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

class ApplicationThreadProxy implements IApplicationThread {
......

public final void scheduleBindService(IBinder token, Intent intent, boolean rebind)
throws RemoteException {
Parcel data = Parcel.obtain();
data.writeInterfaceToken(IApplicationThread.descriptor);
data.writeStrongBinder(token);
intent.writeToParcel(data, 0);
data.writeInt(rebind ? 1 : 0);
mRemote.transact(SCHEDULE_BIND_SERVICE_TRANSACTION, data, null,
IBinder.FLAG_ONEWAY);
data.recycle();
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 17.*ApplicationThread.scheduleBindService

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

public final class ActivityThread {
......

public final void scheduleBindService(IBinder token, Intent intent,
boolean rebind) {
BindServiceData s = new BindServiceData();
s.token = token;
s.intent = intent;
s.rebind = rebind;

queueOrSendMessage(H.BIND_SERVICE, s);
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 18.*ActivityThread.queueOrSendMessage

Please refer to step 11.

Step 19.*H.sendMessage

Please refer to step 12.

public final class ActivityThread {
......

private final class H extends Handler {
......

public void handleMessage(Message msg) {
......
switch (msg.what) {
......
case BIND_SERVICE:
handleBindService((BindServiceData)msg.obj);
break;
......
}
}
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 20.*ActivityThread.handleBindService

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

public final class ActivityThread {
......

private final void handleBindService(BindServiceData data) {
Service s = mServices.get(data.token);
if (s != null) {
try {
data.intent.setExtrasClassLoader(s.getClassLoader());
try {
if (!data.rebind) {
IBinder binder = s.onBind(data.intent);
ActivityManagerNative.getDefault().publishService(
data.token, data.intent, binder);
} else {
......
}
......
} catch (RemoteException ex) {
}
} catch (Exception e) {
......
}
}
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 21. CounterService.onBind

src/shy/luo/broadcast/CounterService.java

public class CounterService extends Service implements ICounterService {
......

private final IBinder binder = new CounterBinder();

public class CounterBinder extends Binder {
public CounterService getService() {
return CounterService.this;
}
}

@Override
public IBinder onBind(Intent intent) {
return binder;
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 22.*ActivityManagerProxy.publishService

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

class ActivityManagerProxy implements IActivityManager
{
......

public void publishService(IBinder token,
Intent intent, IBinder service) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
data.writeInterfaceToken(IActivityManager.descriptor);
data.writeStrongBinder(token);
intent.writeToParcel(data, 0);
data.writeStrongBinder(service);
mRemote.transact(PUBLISH_SERVICE_TRANSACTION, data, reply, 0);
reply.readException();
data.recycle();
reply.recycle();
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 23.*ActivityManagerService.publishService

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

public final class ActivityManagerService extends ActivityManagerNative
implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
......

public void publishService(IBinder token, Intent intent, IBinder service) {
......
synchronized(this) {
......
ServiceRecord r = (ServiceRecord)token;
......

......
if (r != null) {
Intent.FilterComparison filter
= new Intent.FilterComparison(intent);
IntentBindRecord b = r.bindings.get(filter);
if (b != null && !b.received) {
b.binder = service;
b.requested = true;
b.received = true;
if (r.connections.size() > 0) {
Iterator<ArrayList<ConnectionRecord>> it
= r.connections.values().iterator();
while (it.hasNext()) {
ArrayList<ConnectionRecord> clist = it.next();
for (int i=0; i<clist.size(); i++) {
ConnectionRecord c = clist.get(i);
......
try {
c.conn.connected(r.name, service);
} catch (Exception e) {
......
}
}
}
}
}

......
}
}
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 24. InnerConnection.connected

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

final class LoadedApk {
......

static final class ServiceDispatcher {
......

private static class InnerConnection extends IServiceConnection.Stub {
......

public void connected(ComponentName name, IBinder service) throws RemoteException {
LoadedApk.ServiceDispatcher sd = mDispatcher.get();
if (sd != null) {
sd.connected(name, service);
}
}
......
}

......
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 25.*ServiceDispatcher.connected

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

final class LoadedApk {
......

static final class ServiceDispatcher {
......

public void connected(ComponentName name, IBinder service) {
if (mActivityThread != null) {
mActivityThread.post(new RunConnection(name, service, 0));
} else {
......
}
}

......
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 26. H.post

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

Step 27.*RunConnection.run

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

final class LoadedApk {
......

static final class ServiceDispatcher {
......

private final class RunConnection implements Runnable {
......

public void run() {
if (mCommand == 0) {
doConnected(mName, mService);
} else if (mCommand == 1) {
......
}
}

......
}

......
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 28.*ServiceDispatcher.doConnected

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

final class LoadedApk {
......

static final class ServiceDispatcher {
......

public void doConnected(ComponentName name, IBinder service) {
......

// If there is a new service, it is now connected.
if (service != null) {
mConnection.onServiceConnected(name, service);
}
}

......
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 29. ServiceConnection.onServiceConnected

src/shy/luo/broadcast/MainActivity.java

public class MainActivity extends Activity implements OnClickListener {
......

private ServiceConnection serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
counterService = ((CounterService.CounterBinder)service).getService();

Log.i(LOG_TAG, "Counter Service Connected");
}
......
};

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
Step 30. CounterBinder.getService

src/shy/luo/broadcast/CounterService.java

public class CounterService extends Service implements ICounterService {
......

public class CounterBinder extends Binder {
public CounterService getService() {
return CounterService.this;
}
}

......
}
[Android Software/Hacking General [Developers Only]] llrraa2010:
creation of application process