Example usage for android.os ResultReceiver send

List of usage examples for android.os ResultReceiver send

Introduction

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

Prototype

public void send(int resultCode, Bundle resultData) 

Source Link

Document

Deliver a result to this receiver.

Usage

From source file:androidx.media.MediaSession2StubImplBase.java

private void connect(Bundle extras, final ResultReceiver cb) {
    final ControllerInfo controllerInfo = createControllerInfo(extras);
    mSession.getCallbackExecutor().execute(new Runnable() {
        @Override/*from  w  ww  .j a v a2 s  .c o m*/
        public void run() {
            if (mSession.isClosed()) {
                return;
            }
            synchronized (mLock) {
                // Keep connecting controllers.
                // This helps sessions to call APIs in the onConnect()
                // (e.g. setCustomLayout()) instead of pending them.
                mConnectingControllers.add(controllerInfo.getId());
            }
            SessionCommandGroup2 allowedCommands = mSession.getCallback().onConnect(mSession.getInstance(),
                    controllerInfo);
            // Don't reject connection for the request from trusted app.
            // Otherwise server will fail to retrieve session's information to dispatch
            // media keys to.
            boolean accept = allowedCommands != null || controllerInfo.isTrusted();
            if (accept) {
                if (DEBUG) {
                    Log.d(TAG, "Accepting connection, controllerInfo=" + controllerInfo + " allowedCommands="
                            + allowedCommands);
                }
                if (allowedCommands == null) {
                    // For trusted apps, send non-null allowed commands to keep
                    // connection.
                    allowedCommands = new SessionCommandGroup2();
                }
                synchronized (mLock) {
                    mConnectingControllers.remove(controllerInfo.getId());
                    mControllers.put(controllerInfo.getId(), controllerInfo);
                    mAllowedCommandGroupMap.put(controllerInfo, allowedCommands);
                }
                // If connection is accepted, notify the current state to the
                // controller. It's needed because we cannot call synchronous calls
                // between session/controller.
                // Note: We're doing this after the onConnectionChanged(), but there's
                //       no guarantee that events here are notified after the
                //       onConnected() because IMediaController2 is oneway (i.e. async
                //       call) and Stub will use thread poll for incoming calls.
                final Bundle resultData = new Bundle();
                resultData.putBundle(ARGUMENT_ALLOWED_COMMANDS, allowedCommands.toBundle());
                resultData.putInt(ARGUMENT_PLAYER_STATE, mSession.getPlayerState());
                resultData.putInt(ARGUMENT_BUFFERING_STATE, mSession.getBufferingState());
                resultData.putParcelable(ARGUMENT_PLAYBACK_STATE_COMPAT, mSession.getPlaybackStateCompat());
                resultData.putInt(ARGUMENT_REPEAT_MODE, mSession.getRepeatMode());
                resultData.putInt(ARGUMENT_SHUFFLE_MODE, mSession.getShuffleMode());
                final List<MediaItem2> playlist = allowedCommands.hasCommand(COMMAND_CODE_PLAYLIST_GET_LIST)
                        ? mSession.getPlaylist()
                        : null;
                if (playlist != null) {
                    resultData.putParcelableArray(ARGUMENT_PLAYLIST,
                            MediaUtils2.toMediaItem2ParcelableArray(playlist));
                }
                final MediaItem2 currentMediaItem = allowedCommands.hasCommand(
                        COMMAND_CODE_PLAYLIST_GET_CURRENT_MEDIA_ITEM) ? mSession.getCurrentMediaItem() : null;
                if (currentMediaItem != null) {
                    resultData.putBundle(ARGUMENT_MEDIA_ITEM, currentMediaItem.toBundle());
                }
                resultData.putBundle(ARGUMENT_PLAYBACK_INFO, mSession.getPlaybackInfo().toBundle());
                final MediaMetadata2 playlistMetadata = mSession.getPlaylistMetadata();
                if (playlistMetadata != null) {
                    resultData.putBundle(ARGUMENT_PLAYLIST_METADATA, playlistMetadata.toBundle());
                }
                // Double check if session is still there, because close() can be
                // called in another thread.
                if (mSession.isClosed()) {
                    return;
                }
                cb.send(CONNECT_RESULT_CONNECTED, resultData);
            } else {
                synchronized (mLock) {
                    mConnectingControllers.remove(controllerInfo.getId());
                }
                if (DEBUG) {
                    Log.d(TAG, "Rejecting connection, controllerInfo=" + controllerInfo);
                }
                cb.send(CONNECT_RESULT_DISCONNECTED, null);
            }
        }
    });
}

From source file:com.morlunk.leeroy.LeeroyUpdateService.java

private void handleCheckUpdates(Intent intent, boolean notify, ResultReceiver receiver) {
    List<LeeroyApp> appList = LeeroyApp.getApps(getPackageManager());

    if (appList.size() == 0) {
        return;/*from ww w . j  ava  2 s .  co  m*/
    }

    List<LeeroyAppUpdate> updates = new LinkedList<>();
    List<LeeroyApp> notUpdatedApps = new LinkedList<>();
    List<LeeroyException> exceptions = new LinkedList<>();
    for (LeeroyApp app : appList) {
        try {
            String paramUrl = app.getJenkinsUrl() + "/api/json?tree=lastSuccessfulBuild[number,url]";
            URL url = new URL(paramUrl);
            URLConnection conn = url.openConnection();
            Reader reader = new InputStreamReader(conn.getInputStream());

            JsonReader jsonReader = new JsonReader(reader);
            jsonReader.beginObject();
            jsonReader.nextName();
            jsonReader.beginObject();

            int latestSuccessfulBuild = 0;
            String buildUrl = null;
            while (jsonReader.hasNext()) {
                String name = jsonReader.nextName();
                if ("number".equals(name)) {
                    latestSuccessfulBuild = jsonReader.nextInt();
                } else if ("url".equals(name)) {
                    buildUrl = jsonReader.nextString();
                } else {
                    throw new RuntimeException("Unknown key " + name);
                }
            }
            jsonReader.endObject();
            jsonReader.endObject();
            jsonReader.close();

            if (latestSuccessfulBuild > app.getJenkinsBuild()) {
                LeeroyAppUpdate update = new LeeroyAppUpdate();
                update.app = app;
                update.newBuild = latestSuccessfulBuild;
                update.newBuildUrl = buildUrl;
                updates.add(update);
            } else {
                notUpdatedApps.add(app);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            CharSequence appName = app.getApplicationInfo().loadLabel(getPackageManager());
            exceptions.add(new LeeroyException(app, getString(R.string.invalid_url, appName), e));
        } catch (IOException e) {
            e.printStackTrace();
            exceptions.add(new LeeroyException(app, e));
        }
    }

    if (notify) {
        NotificationManagerCompat nm = NotificationManagerCompat.from(this);
        if (updates.size() > 0) {
            NotificationCompat.Builder ncb = new NotificationCompat.Builder(this);
            ncb.setSmallIcon(R.drawable.ic_stat_update);
            ncb.setTicker(getString(R.string.updates_available));
            ncb.setContentTitle(getString(R.string.updates_available));
            ncb.setContentText(getString(R.string.num_updates, updates.size()));
            ncb.setPriority(NotificationCompat.PRIORITY_LOW);
            ncb.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
            Intent appIntent = new Intent(this, AppListActivity.class);
            appIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            ncb.setContentIntent(
                    PendingIntent.getActivity(this, 0, appIntent, PendingIntent.FLAG_CANCEL_CURRENT));
            ncb.setAutoCancel(true);
            NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
            for (LeeroyAppUpdate update : updates) {
                CharSequence appName = update.app.getApplicationInfo().loadLabel(getPackageManager());
                style.addLine(getString(R.string.notify_app_update, appName, update.app.getJenkinsBuild(),
                        update.newBuild));
            }
            style.setSummaryText(getString(R.string.app_name));
            ncb.setStyle(style);
            ncb.setNumber(updates.size());
            nm.notify(NOTIFICATION_UPDATE, ncb.build());
        }

        if (exceptions.size() > 0) {
            NotificationCompat.Builder ncb = new NotificationCompat.Builder(this);
            ncb.setSmallIcon(R.drawable.ic_stat_error);
            ncb.setTicker(getString(R.string.error_checking_updates));
            ncb.setContentTitle(getString(R.string.error_checking_updates));
            ncb.setContentText(getString(R.string.click_to_retry));
            ncb.setPriority(NotificationCompat.PRIORITY_LOW);
            ncb.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
            ncb.setContentIntent(PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));
            ncb.setAutoCancel(true);
            ncb.setNumber(exceptions.size());
            nm.notify(NOTIFICATION_ERROR, ncb.build());
        }
    }

    if (receiver != null) {
        Bundle results = new Bundle();
        results.putParcelableArrayList(EXTRA_UPDATE_LIST, new ArrayList<>(updates));
        results.putParcelableArrayList(EXTRA_NO_UPDATE_LIST, new ArrayList<>(notUpdatedApps));
        results.putParcelableArrayList(EXTRA_EXCEPTION_LIST, new ArrayList<>(exceptions));
        receiver.send(0, results);
    }
}