Example usage for android.os Binder Binder

List of usage examples for android.os Binder Binder

Introduction

In this page you can find the example usage for android.os Binder Binder.

Prototype

public Binder() 

Source Link

Document

Default constructor initializes the object.

Usage

From source file:Main.java

public static Service createService(Context context, ServiceInfo serviceInfo) throws Exception {
    IBinder token = new Binder();

    Class<?> createServiceDataClass = Class.forName("android.app.ActivityThread$CreateServiceData");
    Constructor<?> constructor = createServiceDataClass.getDeclaredConstructor();
    constructor.setAccessible(true);//from   www . j a  v  a  2 s  .com
    Object createServiceData = constructor.newInstance();

    Field tokenField = createServiceDataClass.getDeclaredField("token");
    tokenField.setAccessible(true);
    tokenField.set(createServiceData, token);

    serviceInfo.applicationInfo.packageName = context.getPackageName();
    Field infoField = createServiceDataClass.getDeclaredField("info");
    infoField.setAccessible(true);
    infoField.set(createServiceData, serviceInfo);

    Class<?> compatibilityClass = Class.forName("android.content.res.CompatibilityInfo");
    Field defaultCompatibilityField = compatibilityClass.getDeclaredField("DEFAULT_COMPATIBILITY_INFO");
    defaultCompatibilityField.setAccessible(true);
    Object defaultCompatibility = defaultCompatibilityField.get(null);
    defaultCompatibilityField.set(createServiceData, defaultCompatibility);

    Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
    Method currentActivityThreadMethod = activityThreadClass.getDeclaredMethod("currentActivityThread");
    Object currentActivityThread = currentActivityThreadMethod.invoke(null);

    Method handleCreateServiceMethod = activityThreadClass.getDeclaredMethod("handleCreateService",
            createServiceDataClass);
    handleCreateServiceMethod.setAccessible(true);
    handleCreateServiceMethod.invoke(currentActivityThread, createServiceData);

    Field mServicesField = activityThreadClass.getDeclaredField("mServices");
    mServicesField.setAccessible(true);
    Map mServices = (Map) mServicesField.get(currentActivityThread);
    Service service = (Service) mServices.get(token);
    mServices.remove(token);

    return service;
}

From source file:net.sf.aria2.Aria2Service.java

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

    link = new Binder();

    reusableThread = new HandlerThread("Aria2 handler thread");
    reusableThread.start();/*ww  w  .  j  av  a 2 s . c om*/

    bgThreadHandler = new Handler(reusableThread.getLooper());
}

From source file:com.google.android.apps.muzei.gallery.GalleryArtSource.java

@Override
public IBinder onBind(final Intent intent) {
    if (intent != null && TextUtils.equals(intent.getAction(), ACTION_BIND_GALLERY)) {
        return new Binder();
    }//from w w  w.j a  v a  2 s.c  o m
    return super.onBind(intent);
}

From source file:edu.umich.oasis.sandbox.SandboxService.java

@Override
public IBinder onBind(Intent i) {
    if (localLOGD) {
        Log.d(TAG, "Bound");
    }//from   w w w . ja  va 2s.c  o m
    Bundle extras = i.getExtras();
    if (extras == null) {
        throw new IllegalArgumentException("No extras");
    }
    IBinder api = extras.getBinder(EXTRA_TRUSTED_API);
    if (api == null) {
        throw new IllegalArgumentException("Trusted API not found in extras");
    }
    IBinder root = extras.getBinder(EXTRA_ROOT_SERVICE);
    if (root == null) {
        throw new IllegalArgumentException("OASISService not found in extras");
    }
    mTrustedAPI = ITrustedAPI.Stub.asInterface(api);
    mRootService = IOASISService.Stub.asInterface(root);
    mID = extras.getInt(EXTRA_SANDBOX_ID, -1);

    if (localLOGV) {
        ClassLoader cl = getClassLoader();
        Log.v(TAG, "ClassLoader chain:");
        while (cl != null) {
            Log.v(TAG, cl.toString());
            cl = cl.getParent();
        }
        Log.v(TAG, "<end of chain>");
    }

    final String[] packagesToLoad = extras.getStringArray(EXTRA_KNOWN_PACKAGES);
    getBackgroundHandler().post(new Runnable() {
        @Override
        public void run() {
            if (localLOGD) {
                Log.d(TAG, "Preloading resolve code");
            }

            // Run through a fake transaction, to preload the appropriate classes.
            SodaDescriptor preloadDesc = SodaDescriptor.forStatic(SandboxService.this, SandboxService.class,
                    "resolveStub");

            Binder testBinder = new Binder() {
                @Override
                protected boolean onTransact(int code, Parcel data, Parcel reply, int flags)
                        throws RemoteException {
                    return mBinder.onTransact(code, data, reply, flags);
                }
            };
            testBinder.attachInterface(null, "");

            ISandboxService proxy = ISandboxService.Stub.asInterface(testBinder);
            try {
                proxy.resolveSoda(preloadDesc, false, null);
            } catch (Exception e) {
                Log.w(TAG, "Couldn't preload resolve", e);
            }

            if (localLOGD) {
                Log.d(TAG, "Preloading packages");
            }

            // Load up packages the trusted service tells us we might need.
            for (String packageName : ArrayUtils.nullToEmpty(packagesToLoad)) {
                try {
                    if (localLOGD) {
                        Log.d(TAG, "Preloading " + packageName);
                    }
                    getContextForPackage(packageName);
                } catch (PackageManager.NameNotFoundException e) {
                    Log.w(TAG, "Can't preload package", e);
                }
            }

            Log.i(TAG, "Sandbox #" + mID + ": preload complete");
        }
    });
    return mBinder;
}

From source file:edu.umich.flowfence.sandbox.SandboxService.java

@Override
public IBinder onBind(Intent i) {
    if (localLOGD) {
        Log.d(TAG, "Bound");
    }/*  w  w w.j av  a  2  s . com*/
    Bundle extras = i.getExtras();
    if (extras == null) {
        throw new IllegalArgumentException("No extras");
    }
    IBinder api = extras.getBinder(EXTRA_TRUSTED_API);
    if (api == null) {
        throw new IllegalArgumentException("Trusted API not found in extras");
    }
    IBinder root = extras.getBinder(EXTRA_ROOT_SERVICE);
    if (root == null) {
        throw new IllegalArgumentException("FlowfenceService not found in extras");
    }
    mTrustedAPI = ITrustedAPI.Stub.asInterface(api);
    mRootService = IFlowfenceService.Stub.asInterface(root);
    mID = extras.getInt(EXTRA_SANDBOX_ID, -1);

    if (localLOGV) {
        ClassLoader cl = getClassLoader();
        Log.v(TAG, "ClassLoader chain:");
        while (cl != null) {
            Log.v(TAG, cl.toString());
            cl = cl.getParent();
        }
        Log.v(TAG, "<end of chain>");
    }

    final String[] packagesToLoad = extras.getStringArray(EXTRA_KNOWN_PACKAGES);
    getBackgroundHandler().post(new Runnable() {
        @Override
        public void run() {
            if (localLOGD) {
                Log.d(TAG, "Preloading resolve code");
            }

            // Run through a fake transaction, to preload the appropriate classes.
            QMDescriptor preloadDesc = QMDescriptor.forStatic(SandboxService.this, SandboxService.class,
                    "resolveStub");

            Binder testBinder = new Binder() {
                @Override
                protected boolean onTransact(int code, Parcel data, Parcel reply, int flags)
                        throws RemoteException {
                    return mBinder.onTransact(code, data, reply, flags);
                }
            };
            testBinder.attachInterface(null, "");

            ISandboxService proxy = ISandboxService.Stub.asInterface(testBinder);
            try {
                proxy.resolveQM(preloadDesc, false, null);
            } catch (Exception e) {
                Log.w(TAG, "Couldn't preload resolve", e);
            }

            if (localLOGD) {
                Log.d(TAG, "Preloading packages");
            }

            // Load up packages the trusted service tells us we might need.
            for (String packageName : ArrayUtils.nullToEmpty(packagesToLoad)) {
                try {
                    if (localLOGD) {
                        Log.d(TAG, "Preloading " + packageName);
                    }
                    getContextForPackage(packageName);
                } catch (PackageManager.NameNotFoundException e) {
                    Log.w(TAG, "Can't preload package", e);
                }
            }

            Log.i(TAG, "Sandbox #" + mID + ": preload complete");
        }
    });
    return mBinder;
}