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:net.peterkuterna.android.apps.devoxxsched.service.SyncService.java

@Override
protected void onHandleIntent(Intent intent) {
    final ResultReceiver receiver = intent.getParcelableExtra(EXTRA_STATUS_RECEIVER);
    if (receiver != null)
        receiver.send(STATUS_RUNNING, Bundle.EMPTY);

    final Context context = this;
    final SharedPreferences syncServicePrefs = getSharedPreferences(SyncPrefs.DEVOXXSCHED_SYNC,
            Context.MODE_PRIVATE);
    final int localVersion = syncServicePrefs.getInt(SyncPrefs.LOCAL_VERSION, VERSION_NONE);
    final long lastRemoteSync = syncServicePrefs.getLong(SyncPrefs.LAST_REMOTE_SYNC, 0);

    try {/*from w  w  w. j a v  a2 s  .co  m*/
        // Bulk of sync work, performed by executing several fetches from
        // local and online sources.

        final long startLocal = System.currentTimeMillis();
        final boolean localParse = localVersion < VERSION_LOCAL;
        Log.d(TAG, "found localVersion=" + localVersion + " and VERSION_LOCAL=" + VERSION_LOCAL);
        if (localParse) {
            // Parse values from local cache first
            mLocalExecutor.execute(R.xml.search_suggest, new LocalSearchSuggestHandler());
            mLocalExecutor.execute(context, "cache-rooms.json", new RemoteRoomsHandler());
            mLocalExecutor.execute(context, "cache-presentationtypes.json", new RemoteSessionTypesHandler());
            mLocalExecutor.execute(context, "cache-speakers.json", new RemoteSpeakersHandler());
            mLocalExecutor.execute(context, "cache-presentations.json", new RemoteSessionsHandler());
            mLocalExecutor.execute(context, "cache-schedule.json", new RemoteScheduleHandler());

            // Save local parsed version
            syncServicePrefs.edit().putInt(SyncPrefs.LOCAL_VERSION, VERSION_LOCAL).commit();
        }
        Log.d(TAG, "local sync took " + (System.currentTimeMillis() - startLocal) + "ms");

        final long startRemote = System.currentTimeMillis();
        boolean performRemoteSync = performRemoteSync(mResolver, mHttpClient, intent, context);
        if (performRemoteSync) {
            // Parse values from REST interface
            ArrayList<RequestHash> result = mRemoteExecutor.executeGet(new String[] { Constants.ROOMS_URL, },
                    new RemoteRoomsHandler());
            for (RequestHash requestHash : result) {
                SyncUtils.updateLocalMd5(mResolver, requestHash.getUrl(), requestHash.getMd5());
            }
            result = mRemoteExecutor.executeGet(new String[] { Constants.LABS_PRESENTATION_TYPES_URL, },
                    new RemoteSessionTypesHandler());
            for (RequestHash requestHash : result) {
                SyncUtils.updateLocalMd5(mResolver, requestHash.getUrl(), requestHash.getMd5());
            }
            result = mRemoteExecutor.executeGet(
                    new String[] { Constants.SPEAKERS_URL, Constants.LABS_SPEAKERS_URL, },
                    new RemoteSpeakersHandler());
            for (RequestHash requestHash : result) {
                SyncUtils.updateLocalMd5(mResolver, requestHash.getUrl(), requestHash.getMd5());
            }
            result = mRemoteExecutor.executeGet(
                    new String[] { Constants.PRESENTATIONS_URL, Constants.LABS_PRESENTATIONS_URL, },
                    new RemoteSessionsHandler());
            for (RequestHash requestHash : result) {
                SyncUtils.updateLocalMd5(mResolver, requestHash.getUrl(), requestHash.getMd5());
            }
            result = mRemoteExecutor.executeGet(
                    new String[] { Constants.SCHEDULE_URL, Constants.LABS_SCHEDULE_URL, },
                    new RemoteScheduleHandler());
            for (RequestHash requestHash : result) {
                SyncUtils.updateLocalMd5(mResolver, requestHash.getUrl(), requestHash.getMd5());
            }

            // Save last remote sync time
            syncServicePrefs.edit().putLong(SyncPrefs.LAST_REMOTE_SYNC, startRemote).commit();
            // Save remote parsed version
            syncServicePrefs.edit().putInt(SyncPrefs.LOCAL_VERSION, VERSION_REMOTE).commit();
        }
        Log.d(TAG, "remote sync took " + (System.currentTimeMillis() - startRemote) + "ms");

        if (!localParse && performRemoteSync) {
            NotificationUtils.cancelNotifications(context);
            NotificationUtils.notifyNewSessions(context, getContentResolver());
            NotificationUtils.notifyChangedStarredSessions(context, getContentResolver());
        }
    } catch (Exception e) {
        Log.e(TAG, "Problem while syncing", e);

        if (receiver != null) {
            // Pass back error to surface listener
            final Bundle bundle = new Bundle();
            bundle.putString(Intent.EXTRA_TEXT, e.toString());
            receiver.send(STATUS_ERROR, bundle);
        }
    }

    // Announce success to any surface listener
    Log.d(TAG, "sync finished");
    if (receiver != null)
        receiver.send(STATUS_FINISHED, Bundle.EMPTY);
}

From source file:org.immopoly.android.api.IS24ApiService.java

@Override
protected void onHandleIntent(Intent intent) {
    final ResultReceiver receiver = intent.getParcelableExtra(API_RECEIVER);
    double lat = intent.getDoubleExtra(LAT, 0.0);
    double lon = intent.getDoubleExtra(LNG, 0.0);
    Bundle b = new Bundle();
    Log.d(Const.LOG_TAG, "IS24 search started");
    try {// w w w. jav  a2  s.  c o  m
        receiver.send(STATUS_RUNNING, Bundle.EMPTY);
        final float[] radii = Const.SEARCH_RADII;
        // search for flats in radii[i]. finish if enough flats found (1 is enough in the last run)
        for (int i = 0; i < radii.length; i++) {
            int min = i == radii.length - 1 ? 1 : Const.SEARCH_MIN_RESULTS;
            Flats flats = loadFlats(lat, lon, radii[i], min, Const.SEARCH_MAX_RESULTS);
            if (flats != null) {
                Log.d(Const.LOG_TAG, "IS24 search finished. #flats: " + flats.size());
                b.putParcelableArrayList("flats", flats);
                receiver.send(STATUS_FINISHED, b);
                this.stopSelf();
                return;
            }
        }
        // no flats found
        Log.d(Const.LOG_TAG, "IS24 search finished with no flats: ");
        b.putString(Intent.EXTRA_TEXT, NO_FLATS);
    } catch (Exception e) {
        Log.e(Const.LOG_TAG, "IS24 search caught Exception: ", e);
    }
    receiver.send(STATUS_ERROR, b);
    this.stopSelf();
}

From source file:net.niyonkuru.koodroid.service.SessionService.java

/**
 * Announce success to the available receiver
 *//*from   ww  w.  j a v  a2s  . c  o m*/
private void announce(ResultReceiver receiver, int status) {
    if (receiver != null) {
        receiver.send(status, Bundle.EMPTY);
    }
}

From source file:org.ietf.ietfsched.service.SyncService.java

@Override
protected void onHandleIntent(Intent intent) {
    final ResultReceiver receiver = intent.getParcelableExtra(EXTRA_STATUS_RECEIVER);
    if (debbug)//  w  ww . j  a  v a  2s  .com
        Log.d(TAG, "Receiver is = " + receiver);
    if (receiver != null)
        receiver.send(STATUS_RUNNING, Bundle.EMPTY);
    final Context context = this;
    final SharedPreferences prefs = getSharedPreferences(Prefs.IETFSCHED_SYNC, Context.MODE_PRIVATE);
    final int localVersion = prefs.getInt(Prefs.LOCAL_VERSION, VERSION_NONE);
    //      final int lastLength = prefs.getInt(Prefs.LAST_LENGTH, VERSION_NONE);
    final String lastEtag = prefs.getString(Prefs.LAST_ETAG, "");
    //      final long startLocal = System.currentTimeMillis();

    //boolean localParse = localVersion < VERSION_CURRENT;
    boolean localParse = false;
    Log.d(TAG, "found localVersion=" + localVersion + " and VERSION_CURRENT=" + VERSION_CURRENT);
    boolean remoteParse = true;
    //      int remoteLength = -1;
    String remoteEtag = "";

    try {
        String htmlURL = BASE_URL + "agenda.csv";
        if (debbug)
            Log.d(TAG, "HEAD " + htmlURL);
        remoteEtag = mRemoteExecutor.executeHead(htmlURL);
        if (debbug)
            Log.d(TAG, "HEAD " + htmlURL + " " + remoteEtag);
        if (remoteEtag == null) {
            Log.d(TAG, "Error connection, cannot retrieve any information from" + htmlURL);
            remoteParse = false;
        } else {
            remoteParse = !remoteEtag.equals(lastEtag);
        }
    } catch (Exception e) {
        remoteParse = false;
        e.printStackTrace();
    }

    // HACK FOR TESTS PURPOSES. TO REMOVE 
    //      Log.w(TAG, "For tests purposes, only the local parsing is activated");
    //      remoteParse = false;
    //      localParse = true;
    // HACK FIN.   

    if (!remoteParse && !localParse) {
        Log.d(TAG, "Already synchronized");
        if (receiver != null)
            receiver.send(STATUS_FINISHED, Bundle.EMPTY);
        return;
    }

    if (remoteParse) {
        String csvURL = BASE_URL + "agenda.csv";
        try {
            if (debbug)
                Log.d(TAG, csvURL);
            InputStream agenda = mRemoteExecutor.executeGet(csvURL);
            mLocalExecutor.execute(agenda);
            prefs.edit().putString(Prefs.LAST_ETAG, remoteEtag).commit();
            prefs.edit().putInt(Prefs.LOCAL_VERSION, VERSION_CURRENT).commit();
            localParse = false;
            Log.d(TAG, "remote sync finished");
            if (receiver != null)
                receiver.send(STATUS_FINISHED, Bundle.EMPTY);
        } catch (Exception e) {
            Log.e(TAG, "Error HTTP request " + csvURL, e);
            if (!localParse) {
                final Bundle bundle = new Bundle();
                bundle.putString(Intent.EXTRA_TEXT, "Connection error. No updates.");
                if (receiver != null) {
                    receiver.send(STATUS_ERROR, bundle);
                }
            }
        }
    }

    if (localParse) {
        try {
            mLocalExecutor.execute(context, "agenda-83.csv");
            Log.d(TAG, "local sync finished");
            prefs.edit().putInt(Prefs.LOCAL_VERSION, VERSION_CURRENT).commit();
            if (receiver != null)
                receiver.send(STATUS_FINISHED, Bundle.EMPTY);
        } catch (Exception e) {
            e.printStackTrace();
            final Bundle bundle = new Bundle();
            bundle.putString(Intent.EXTRA_TEXT, e.toString());
            if (receiver != null) {
                receiver.send(STATUS_ERROR, bundle);
            }
        }
    }
}

From source file:com.infine.android.devoxx.service.RestService.java

@Override
protected void onHandleIntent(Intent intent) {

    final ResultReceiver receiver = intent.getParcelableExtra(EXTRA_STATUS_RECEIVER);

    // si passage du numero de version
    // final int latestVersion = intent.getIntExtra(EXTRA_LASTEST_VERSION,
    // 1);/*from   w w  w  . ja v  a2s. c o m*/

    if (receiver != null)
        receiver.send(ServiceStatus.STATUS_RUNNING, Bundle.EMPTY);

    // final Context context = this;
    // final SharedPreferences prefs =
    // getSharedPreferences(Prefs.DEVOXX_SCHEDULE_SYNC,
    // Context.MODE_PRIVATE);
    // final int localVersion = prefs.getInt(Prefs.LOCAL_VERSION,
    // VERSION_NONE);

    try {

        // Bulk of sync work, performed by executing several fetches from
        // local and online sources.
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mApplicationContext);
        int scheduleVersion = prefs.getInt(PREFS_SCHEDULE_VERSION, -1);
        int sessionVersion = prefs.getInt(PREFS_SESSION_VERSION, -1);
        int speakerVersion = prefs.getInt(PREFS_SPEAKER_VERSION, -1);

        if (scheduleVersion + sessionVersion + speakerVersion < 0) {
            // on charge les fichiers statiques que la premiere fois
            // ou quand un jeu de donnes schedule ou session est pourri
            // verion = -1
            loadStaticFiles();
        }
        loadStaticRoom();

        // Always hit remote spreadsheet for any updates
        loadRemoteData();

    } catch (Exception e) {
        Log.e(TAG, "Problem while syncing", e);
        if (receiver != null) {
            // Pass back error to surface listener
            final Bundle bundle = new Bundle();
            bundle.putString(Intent.EXTRA_TEXT, e.toString());
            receiver.send(STATUS_ERROR, bundle);
        }
    }

    // Announce success to any surface listener
    if (receiver != null)
        receiver.send(STATUS_FINISHED, Bundle.EMPTY);
}

From source file:fr.openbike.android.service.SyncService.java

@SuppressWarnings("unchecked")
@Override/*  w ww . j a v a  2s.  c  o  m*/
protected void onHandleIntent(Intent intent) {
    NetworkInfo activeNetwork = ((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE))
            .getActiveNetworkInfo();
    final ResultReceiver receiver = intent.getParcelableExtra(EXTRA_STATUS_RECEIVER);
    Bundle bundle = new Bundle();
    int status = STATUS_ERROR;
    if (activeNetwork == null || !activeNetwork.isConnectedOrConnecting()) {
        bundle = new Bundle();
        bundle.putString(Intent.EXTRA_TEXT, "No network connectivity");
        receiver.send(STATUS_ERROR, bundle);
        return;
    }
    String action = intent.getAction();
    try {
        if (ACTION_SYNC.equals(action)) {
            if (receiver != null) {
                receiver.send(STATUS_SYNC_STATIONS, Bundle.EMPTY);
            }
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
            Object result = mRemoteExecutor.executeGet(
                    mPreferences.getString(AbstractPreferencesActivity.UPDATE_SERVER_URL, "") + "/v2/stations",
                    new RemoteStationsSyncHandler(
                            prefs.getLong(AbstractPreferencesActivity.STATIONS_VERSION, 0)),
                    this);
            if (result == null) {
                // Need stations update
                action = ACTION_UPDATE;
            } else {
                String message = (String) result;
                status = STATUS_SYNC_STATIONS_FINISHED;
                if (!"".equals(message)) {
                    bundle.putString(EXTRA_RESULT, message);
                }
                prefs.edit().putLong(AbstractPreferencesActivity.LAST_UPDATE, System.currentTimeMillis())
                        .commit();
            }
        }
        if (ACTION_UPDATE.equals(action)) {
            if (receiver != null) {
                receiver.send(STATUS_UPDATE_STATIONS, Bundle.EMPTY);
            }
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
            Object result = mRemoteExecutor
                    .executeGet(mPreferences.getString(AbstractPreferencesActivity.UPDATE_SERVER_URL, "")
                            + "/v2/stations/list", new RemoteStationsUpdateHandler(), this);
            status = STATUS_UPDATE_STATIONS_FINISHED;
            if (result != null) {
                bundle.putString(EXTRA_RESULT, (String) result);
            }
            prefs.edit().putLong(AbstractPreferencesActivity.LAST_UPDATE, System.currentTimeMillis()).commit();
        }
        if (ACTION_CHOOSE_NETWORK.equals(action)) {
            if (receiver != null) {
                receiver.send(STATUS_SYNC_NETWORKS, Bundle.EMPTY);
                bundle = new Bundle();
                bundle.putParcelableArrayList(EXTRA_RESULT, (ArrayList<Network>) mRemoteExecutor
                        .executeGet(NETWORKS_URL, new RemoteNetworksHandler(), this));
                status = STATUS_SYNC_NETWORKS_FINISHED;
            }
        }
        if (receiver != null) {
            receiver.send(status, bundle);
        }
    } catch (HandlerException e) {
        if (receiver != null) {
            // Pass back error to surface listener
            bundle = new Bundle();
            bundle.putString(Intent.EXTRA_TEXT, e.toString());
            receiver.send(STATUS_ERROR, bundle);
        }
    } catch (Exception e) {
        if (receiver != null) {
            // Pass back error to surface listener
            e.printStackTrace();
            bundle = new Bundle();
            bundle.putString(Intent.EXTRA_TEXT, "Une erreur est survenue. Veuillez ressayer.");
            receiver.send(STATUS_ERROR, bundle);
        }
    }
}

From source file:org.catrobat.catroid.web.ConnectionWrapper.java

private void sendUpdateIntent(ResultReceiver receiver, long progress, boolean endOfFileReached, boolean unknown,
        Integer notificationId, String projectName) {
    Bundle progressBundle = new Bundle();
    progressBundle.putLong("currentDownloadProgress", progress);
    progressBundle.putBoolean("endOfFileReached", endOfFileReached);
    progressBundle.putBoolean("unknown", unknown);
    progressBundle.putInt("notificationId", notificationId);
    progressBundle.putString("projectName", projectName);
    receiver.send(Constants.UPDATE_DOWNLOAD_PROGRESS, progressBundle);
}

From source file:org.deviceconnect.android.deviceplugin.host.camera.CameraOverlay.java

/**
 * ??????./*www.  ja  v  a2s  .  com*/
 * @param resultReceiver ????
 */
@TargetApi(23)
private void checkOverlayDrawingCapability(@NonNull final ResultReceiver resultReceiver) {
    if (Settings.canDrawOverlays(mContext)) {
        resultReceiver.send(Activity.RESULT_OK, null);
    } else {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + mContext.getPackageName()));
        IntentHandlerActivity.startActivityForResult(mContext, intent, new ResultReceiver(mHandler) {
            @Override
            protected void onReceiveResult(int resultCode, Bundle resultData) {
                if (Settings.canDrawOverlays(mContext)) {
                    resultReceiver.send(Activity.RESULT_OK, null);
                } else {
                    resultReceiver.send(Activity.RESULT_CANCELED, null);
                }
            }
        });
    }
}

From source file:com.ubuntuone.android.files.service.MetaService.java

private void getUser(final ResultReceiver receiver) {
    api.getUser(new U1UserListener() {
        @Override//  w w  w  .j a  v a  2 s .  c o m
        public void onStart() {
            if (receiver != null)
                receiver.send(Status.RUNNING, Bundle.EMPTY);
        }

        @Override
        public void onSuccess(U1User user) {
            onGetUserSuccess(user);
        }

        @Override
        public void onUbuntuOneFailure(U1Failure failure) {
            MetaService.this.onUbuntuOneFailure(failure, receiver);
        }

        @Override
        public void onFailure(U1Failure failure) {
            MetaService.this.onFailure(failure, receiver);
        }

        @Override
        public void onFinish() {
            if (receiver != null)
                receiver.send(Status.FINISHED, Bundle.EMPTY);
        }
    });
}

From source file:com.ubuntuone.android.files.service.MetaService.java

private void createVolume(final String resourcePath, final ResultReceiver receiver) {
    api.createVolume(resourcePath, new U1VolumeListener() {
        @Override//from  ww  w  .  ja  va  2  s. c  om
        public void onStart() {
            if (receiver != null)
                receiver.send(Status.RUNNING, Bundle.EMPTY);
        }

        @Override
        public void onSuccess(U1Volume volume) {
            onCreateVolumeSuccess(volume);
        }

        @Override
        public void onUbuntuOneFailure(U1Failure failure) {
            MetaService.this.onUbuntuOneFailure(failure, receiver);
        }

        @Override
        public void onFailure(U1Failure failure) {
            MetaService.this.onFailure(failure, receiver);
        }

        @Override
        public void onFinish() {
            if (receiver != null) {
                Bundle data = new Bundle();
                data.putString(EXTRA_RESOURCE_PATH, resourcePath);
                receiver.send(Status.FINISHED, data);
            }
        }
    });
}