Example usage for android.os Bundle getBinder

List of usage examples for android.os Bundle getBinder

Introduction

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

Prototype

@Nullable
public IBinder getBinder(@Nullable String key) 

Source Link

Document

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

Usage

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

@Override
public IBinder onBind(Intent i) {
    if (localLOGD) {
        Log.d(TAG, "Bound");
    }/*from w w  w  .  ja v  a2s.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("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;
}

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

@Override
public IBinder onBind(Intent i) {
    if (localLOGD) {
        Log.d(TAG, "Bound");
    }/* w w w  .  j a va2s . 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:org.opensilk.music.library.provider.LibraryProvider.java

private IBinder getBinderCallbackFromBundle(Bundle b) {
    if (Build.VERSION.SDK_INT >= 18) {
        return b.getBinder(Extras.CALLBACK);
    } else {/*w  ww  .  j  av a  2  s. c  o m*/
        try {
            synchronized (this) {
                if (_getIBinder == null) {
                    _getIBinder = Bundle.class.getDeclaredMethod("getIBinder", String.class);
                }
            }
            return (IBinder) _getIBinder.invoke(b, Extras.CALLBACK);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:androidx.media.MediaSession2StubImplBase.java

private ControllerInfo createControllerInfo(Bundle extras) {
    IMediaControllerCallback callback = IMediaControllerCallback.Stub
            .asInterface(extras.getBinder(ARGUMENT_ICONTROLLER_CALLBACK));
    String packageName = extras.getString(ARGUMENT_PACKAGE_NAME);
    int uid = extras.getInt(ARGUMENT_UID);
    int pid = extras.getInt(ARGUMENT_PID);
    // TODO: sanity check for packageName, uid, and pid.

    return new ControllerInfo(mContext, uid, pid, packageName, callback);
}

From source file:androidx.media.MediaSession2StubImplBase.java

@Override
public void onCommand(String command, final Bundle extras, final ResultReceiver cb) {
    switch (command) {
    case CONTROLLER_COMMAND_CONNECT:
        connect(extras, cb);/*from w  w  w .j  a v a  2 s.  c o m*/
        break;
    case CONTROLLER_COMMAND_DISCONNECT:
        disconnect(extras);
        break;
    case CONTROLLER_COMMAND_BY_COMMAND_CODE: {
        final int commandCode = extras.getInt(ARGUMENT_COMMAND_CODE);
        IMediaControllerCallback caller = (IMediaControllerCallback) extras
                .getBinder(ARGUMENT_ICONTROLLER_CALLBACK);
        if (caller == null) {
            return;
        }

        onCommand2(caller.asBinder(), commandCode, new Session2Runnable() {
            @Override
            public void run(ControllerInfo controller) {
                switch (commandCode) {
                case COMMAND_CODE_PLAYBACK_PLAY:
                    mSession.play();
                    break;
                case COMMAND_CODE_PLAYBACK_PAUSE:
                    mSession.pause();
                    break;
                case COMMAND_CODE_PLAYBACK_RESET:
                    mSession.reset();
                    break;
                case COMMAND_CODE_PLAYBACK_PREPARE:
                    mSession.prepare();
                    break;
                case COMMAND_CODE_PLAYBACK_SEEK_TO: {
                    long seekPos = extras.getLong(ARGUMENT_SEEK_POSITION);
                    mSession.seekTo(seekPos);
                    break;
                }
                case COMMAND_CODE_PLAYLIST_SET_REPEAT_MODE: {
                    int repeatMode = extras.getInt(ARGUMENT_REPEAT_MODE);
                    mSession.setRepeatMode(repeatMode);
                    break;
                }
                case COMMAND_CODE_PLAYLIST_SET_SHUFFLE_MODE: {
                    int shuffleMode = extras.getInt(ARGUMENT_SHUFFLE_MODE);
                    mSession.setShuffleMode(shuffleMode);
                    break;
                }
                case COMMAND_CODE_PLAYLIST_SET_LIST: {
                    List<MediaItem2> list = MediaUtils2
                            .fromMediaItem2ParcelableArray(extras.getParcelableArray(ARGUMENT_PLAYLIST));
                    MediaMetadata2 metadata = MediaMetadata2
                            .fromBundle(extras.getBundle(ARGUMENT_PLAYLIST_METADATA));
                    mSession.setPlaylist(list, metadata);
                    break;
                }
                case COMMAND_CODE_PLAYLIST_SET_LIST_METADATA: {
                    MediaMetadata2 metadata = MediaMetadata2
                            .fromBundle(extras.getBundle(ARGUMENT_PLAYLIST_METADATA));
                    mSession.updatePlaylistMetadata(metadata);
                    break;
                }
                case COMMAND_CODE_PLAYLIST_ADD_ITEM: {
                    int index = extras.getInt(ARGUMENT_PLAYLIST_INDEX);
                    MediaItem2 item = MediaItem2.fromBundle(extras.getBundle(ARGUMENT_MEDIA_ITEM));
                    mSession.addPlaylistItem(index, item);
                    break;
                }
                case COMMAND_CODE_PLAYLIST_REMOVE_ITEM: {
                    MediaItem2 item = MediaItem2.fromBundle(extras.getBundle(ARGUMENT_MEDIA_ITEM));
                    mSession.removePlaylistItem(item);
                    break;
                }
                case COMMAND_CODE_PLAYLIST_REPLACE_ITEM: {
                    int index = extras.getInt(ARGUMENT_PLAYLIST_INDEX);
                    MediaItem2 item = MediaItem2.fromBundle(extras.getBundle(ARGUMENT_MEDIA_ITEM));
                    mSession.replacePlaylistItem(index, item);
                    break;
                }
                case COMMAND_CODE_PLAYLIST_SKIP_TO_NEXT_ITEM: {
                    mSession.skipToNextItem();
                    break;
                }
                case COMMAND_CODE_PLAYLIST_SKIP_TO_PREV_ITEM: {
                    mSession.skipToPreviousItem();
                    break;
                }
                case COMMAND_CODE_PLAYLIST_SKIP_TO_PLAYLIST_ITEM: {
                    MediaItem2 item = MediaItem2.fromBundle(extras.getBundle(ARGUMENT_MEDIA_ITEM));
                    mSession.skipToPlaylistItem(item);
                    break;
                }
                case COMMAND_CODE_VOLUME_SET_VOLUME: {
                    int value = extras.getInt(ARGUMENT_VOLUME);
                    int flags = extras.getInt(ARGUMENT_VOLUME_FLAGS);
                    VolumeProviderCompat vp = mSession.getVolumeProvider();
                    if (vp == null) {
                        // TODO: Revisit
                    } else {
                        vp.onSetVolumeTo(value);
                    }
                    break;
                }
                case COMMAND_CODE_VOLUME_ADJUST_VOLUME: {
                    int direction = extras.getInt(ARGUMENT_VOLUME_DIRECTION);
                    int flags = extras.getInt(ARGUMENT_VOLUME_FLAGS);
                    VolumeProviderCompat vp = mSession.getVolumeProvider();
                    if (vp == null) {
                        // TODO: Revisit
                    } else {
                        vp.onAdjustVolume(direction);
                    }
                    break;
                }
                case COMMAND_CODE_SESSION_REWIND: {
                    mSession.getCallback().onRewind(mSession.getInstance(), controller);
                    break;
                }
                case COMMAND_CODE_SESSION_FAST_FORWARD: {
                    mSession.getCallback().onFastForward(mSession.getInstance(), controller);
                    break;
                }
                case COMMAND_CODE_SESSION_PLAY_FROM_MEDIA_ID: {
                    String mediaId = extras.getString(ARGUMENT_MEDIA_ID);
                    Bundle extra = extras.getBundle(ARGUMENT_EXTRAS);
                    mSession.getCallback().onPlayFromMediaId(mSession.getInstance(), controller, mediaId,
                            extra);
                    break;
                }
                case COMMAND_CODE_SESSION_PLAY_FROM_SEARCH: {
                    String query = extras.getString(ARGUMENT_QUERY);
                    Bundle extra = extras.getBundle(ARGUMENT_EXTRAS);
                    mSession.getCallback().onPlayFromSearch(mSession.getInstance(), controller, query, extra);
                    break;
                }
                case COMMAND_CODE_SESSION_PLAY_FROM_URI: {
                    Uri uri = extras.getParcelable(ARGUMENT_URI);
                    Bundle extra = extras.getBundle(ARGUMENT_EXTRAS);
                    mSession.getCallback().onPlayFromUri(mSession.getInstance(), controller, uri, extra);
                    break;
                }
                case COMMAND_CODE_SESSION_PREPARE_FROM_MEDIA_ID: {
                    String mediaId = extras.getString(ARGUMENT_MEDIA_ID);
                    Bundle extra = extras.getBundle(ARGUMENT_EXTRAS);
                    mSession.getCallback().onPrepareFromMediaId(mSession.getInstance(), controller, mediaId,
                            extra);
                    break;
                }
                case COMMAND_CODE_SESSION_PREPARE_FROM_SEARCH: {
                    String query = extras.getString(ARGUMENT_QUERY);
                    Bundle extra = extras.getBundle(ARGUMENT_EXTRAS);
                    mSession.getCallback().onPrepareFromSearch(mSession.getInstance(), controller, query,
                            extra);
                    break;
                }
                case COMMAND_CODE_SESSION_PREPARE_FROM_URI: {
                    Uri uri = extras.getParcelable(ARGUMENT_URI);
                    Bundle extra = extras.getBundle(ARGUMENT_EXTRAS);
                    mSession.getCallback().onPrepareFromUri(mSession.getInstance(), controller, uri, extra);
                    break;
                }
                case COMMAND_CODE_SESSION_SET_RATING: {
                    String mediaId = extras.getString(ARGUMENT_MEDIA_ID);
                    Rating2 rating = Rating2.fromBundle(extras.getBundle(ARGUMENT_RATING));
                    mSession.getCallback().onSetRating(mSession.getInstance(), controller, mediaId, rating);
                    break;
                }
                case COMMAND_CODE_SESSION_SUBSCRIBE_ROUTES_INFO: {
                    mSession.getCallback().onSubscribeRoutesInfo(mSession.getInstance(), controller);
                    break;
                }
                case COMMAND_CODE_SESSION_UNSUBSCRIBE_ROUTES_INFO: {
                    mSession.getCallback().onUnsubscribeRoutesInfo(mSession.getInstance(), controller);
                    break;
                }
                case COMMAND_CODE_SESSION_SELECT_ROUTE: {
                    Bundle route = extras.getBundle(ARGUMENT_ROUTE_BUNDLE);
                    mSession.getCallback().onSelectRoute(mSession.getInstance(), controller, route);
                    break;
                }
                case COMMAND_CODE_PLAYBACK_SET_SPEED: {
                    float speed = extras.getFloat(ARGUMENT_PLAYBACK_SPEED);
                    mSession.setPlaybackSpeed(speed);
                    break;
                }
                }
            }
        });
        break;
    }
    case CONTROLLER_COMMAND_BY_CUSTOM_COMMAND: {
        final SessionCommand2 customCommand = SessionCommand2
                .fromBundle(extras.getBundle(ARGUMENT_CUSTOM_COMMAND));
        IMediaControllerCallback caller = (IMediaControllerCallback) extras
                .getBinder(ARGUMENT_ICONTROLLER_CALLBACK);
        if (caller == null || customCommand == null) {
            return;
        }

        final Bundle args = extras.getBundle(ARGUMENT_ARGUMENTS);
        onCommand2(caller.asBinder(), customCommand, new Session2Runnable() {
            @Override
            public void run(ControllerInfo controller) throws RemoteException {
                mSession.getCallback().onCustomCommand(mSession.getInstance(), controller, customCommand, args,
                        cb);
            }
        });
        break;
    }
    }
}