Example usage for android.content Intent replaceExtras

List of usage examples for android.content Intent replaceExtras

Introduction

In this page you can find the example usage for android.content Intent replaceExtras.

Prototype

public @NonNull Intent replaceExtras(@Nullable Bundle extras) 

Source Link

Document

Completely replace the extras in the Intent with the given Bundle of extras.

Usage

From source file:edu.cmu.cylab.starslinger.view.HomeActivity.java

private void showSave(Bundle args) {
    Intent intent = new Intent(HomeActivity.this, SaveActivity.class);
    intent.replaceExtras(args);
    startActivityForResult(intent, VIEW_SAVE_ID);
}

From source file:com.ratebeer.android.gui.components.PosterService.java

@Override
protected void onHandleIntent(Intent intent) {

    // Proper intent received?
    if (intent == null || intent.getAction() == null) {
        Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME, "No intent action to perform");
        return;/*w  w  w .j a  va2 s  . co  m*/
    }

    // Proper user settings?
    UserSettings user = applicationSettings.getUserSettings();
    if (user == null) {
        Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                "Canceling " + intent.getAction() + " intent because there are no user settings known.");
        return;
    }

    // Try to set the drinking status
    if (intent.getAction().equals(ACTION_SETDRINKINGSTATUS)) {

        // Get new status text
        String newStatus = intent.getStringExtra(EXTRA_NEWSTATUS);
        int beerId = intent.getIntExtra(EXTRA_BEERID, NO_BEER_EXTRA);
        if (newStatus == null) {
            Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                    "No new drinking status is intent; cancelling");
            return;
        }

        // Synchronously set the new drinking status
        // During the operation a notification will be shown
        Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                "Now setting drinking status to " + newStatus);
        Intent recoverIntent;
        if (beerId == NO_BEER_EXTRA) {
            // If no specific beer was tight to this drinking status, assume it was from the home screen's free text
            // input
            recoverIntent = new Intent(this, Home_.class);
        } else {
            recoverIntent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse(String.format(URI_BEER, Integer.toString(beerId))));
        }
        createNotification(NOTIFY_SETDRINKINGSTATUS, getString(R.string.app_settingdrinking),
                getString(R.string.home_nowdrinking, newStatus), true, recoverIntent, null, beerId);
        CommandResult result = new SetDrinkingStatusCommand(user, newStatus).execute(apiConnection);
        if (result instanceof CommandSuccessResult) {
            notificationManager.cancel(NOTIFY_SETDRINKINGSTATUS);
            // If requested, call back the messenger, i.e. the calling activity
            callbackMessenger(intent, RESULT_SUCCESS);
        } else {
            String e = result instanceof CommandFailureResult
                    ? ((CommandFailureResult) result).getException().toString()
                    : "Unknown error";
            Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                    "Setting drinking status to " + newStatus + " failed: " + e);
            createNotification(NOTIFY_SETDRINKINGSTATUS, getString(R.string.app_settingdrinking),
                    getString(R.string.error_commandfailed), true, recoverIntent, null, beerId);
            // If requested, call back the messenger, i.e. the calling activity
            callbackMessenger(intent, RESULT_FAILURE);
        }

    }

    // Try to add a new rating
    if (intent.getAction().equals(ACTION_POSTRATING)) {

        // Get rating details
        int beerId = intent.getIntExtra(EXTRA_BEERID, NO_BEER_EXTRA);
        String beerName = intent.getStringExtra(EXTRA_BEERNAME);
        int offlineId = intent.getIntExtra(EXTRA_OFFLINEID, NO_OFFLINE_EXTRA);
        int ratingId = intent.getIntExtra(EXTRA_ORIGRATINGID, -1);
        String origDate = intent.getStringExtra(EXTRA_ORIGRATINGDATE);
        int aroma = intent.getIntExtra(EXTRA_AROMA, -1);
        int appearance = intent.getIntExtra(EXTRA_APPEARANCE, -1);
        int taste = intent.getIntExtra(EXTRA_TASTE, -1);
        int palate = intent.getIntExtra(EXTRA_PALATE, -1);
        int overall = intent.getIntExtra(EXTRA_OVERALL, -1);
        String comment = intent.getStringExtra(EXTRA_COMMENT);
        if (beerId == NO_BEER_EXTRA || aroma <= 0 || appearance <= 0 || taste <= 0 || palate <= 0
                || overall <= 0 || beerName == null || comment == null) {
            Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                    "Missing extras in the POSTRATING intent; cancelling.");
            return;
        }

        // Synchronously post the new rating
        // During the operation a notification will be shown
        Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME, "Now posting rating for " + beerName);
        Intent recoverIntent = new Intent(getApplicationContext(), Home_.class);
        recoverIntent.replaceExtras(intent.getExtras());
        recoverIntent.setAction(ACTION_EDITRATING);
        createNotification(NOTIFY_POSTINGRATING, getString(R.string.app_postingrating),
                getString(R.string.app_rated, beerName,
                        PostRatingCommand.calculateTotal(aroma, appearance, taste, palate, overall)),
                true, recoverIntent, null, beerId);
        CommandResult result = new PostRatingCommand(user, beerId, ratingId, origDate, beerName, aroma,
                appearance, taste, palate, overall, comment).execute(apiConnection);
        if (result instanceof CommandSuccessResult) {
            notificationManager.cancel(NOTIFY_POSTINGRATING);
            // If requested, call back the messenger, i.e. the calling activity
            callbackMessenger(intent, RESULT_SUCCESS);
            // Ratings are usually stored locally as offline rating using the ORM persistence layer
            // If so, it can now be removed
            try {
                if (offlineId != NO_OFFLINE_EXTRA) {
                    OfflineRating offlineRating = getHelper().getOfflineRatingDao().queryForId(offlineId);
                    if (offlineRating != null) {
                        Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                                "Deleted the offline rating for this beer as well.");
                        getHelper().getOfflineRatingDao().delete(offlineRating);
                    }
                }
            } catch (SQLException e) {
                Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                        "Offline rating not available: " + e.toString());
            }
        } else {
            String e = result instanceof CommandFailureResult
                    ? ((CommandFailureResult) result).getException().toString()
                    : "Unknown error";
            Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                    "Posting of rating for " + beerName + " failed: " + e);
            createNotification(NOTIFY_POSTINGRATING, getString(R.string.app_postingrating),
                    getString(R.string.error_commandfailed), true, recoverIntent, null, beerId);
            // If requested, call back the messenger, i.e. the calling activity
            callbackMessenger(intent, RESULT_FAILURE);
        }

    }

    // Try to post a tick update
    if (intent.getAction().equals(ACTION_POSTTICK)) {

        // Get tick details
        int beerId = intent.getIntExtra(EXTRA_BEERID, NO_BEER_EXTRA);
        String beerName = intent.getStringExtra(EXTRA_BEERNAME);
        int userId = intent.getIntExtra(EXTRA_USERID, -1);
        int liked = intent.getIntExtra(EXTRA_LIKED, EXTRA_TICK_DELETE);
        if (beerId == NO_BEER_EXTRA || beerName == null || userId <= 0 || liked == 0 || liked > 5
                || liked < -1) {
            Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                    "Missing extras in the POSTRATING intent; cancelling.");
            return;
        }

        // Synchronously post the tick update
        // During the operation a notification will be shown
        Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME, "Now ticking " + beerName);
        Intent recoverIntent = new Intent(Intent.ACTION_VIEW,
                Uri.parse(String.format(URI_BEER, Integer.toString(beerId))));
        // If liked (the actual tick) is set to -1 we delete this tick instead
        boolean del = liked == EXTRA_TICK_DELETE;
        createNotification(NOTIFY_POSTINGTICK,
                getString(del ? R.string.app_removingtick : R.string.app_postingtick),
                getString(R.string.app_forbeer, beerName), true, recoverIntent, null, beerId);
        CommandResult result;
        if (del) {
            result = new DeleteTickCommand(user, beerId, userId, beerName).execute(apiConnection);
        } else {
            result = new PostTickCommand(user, beerId, userId, beerName, liked).execute(apiConnection);
        }
        if (result instanceof CommandSuccessResult) {
            notificationManager.cancel(NOTIFY_POSTINGTICK);
            // If requested, call back the messenger, i.e. the calling activity
            callbackMessenger(intent, RESULT_SUCCESS);
        } else {
            String e = result instanceof CommandFailureResult
                    ? ((CommandFailureResult) result).getException().toString()
                    : "Unknown error";
            Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                    (del ? "Removing of tick for " : "Ticking of ") + beerName + " failed: " + e);
            createNotification(NOTIFY_POSTINGRATING,
                    getString(del ? R.string.app_removingtick : R.string.app_postingtick),
                    getString(R.string.error_commandfailed), true, recoverIntent, null, beerId);
            // If requested, call back the messenger, i.e. the calling activity
            callbackMessenger(intent, RESULT_FAILURE);
        }

    }

    // Try to add beer availability info
    if (intent.getAction().equals(ACTION_ADDAVAILABILITY)) {

        // Get beer and selected places
        int beerId = intent.getIntExtra(EXTRA_BEERID, -1);
        String beerName = intent.getStringExtra(EXTRA_BEERNAME);
        int placeId = intent.getIntExtra(EXTRA_PLACEID, -1);
        if (beerId <= 0 || beerName == null) {
            Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                    "Missing extras in the ADDAVAILABILITY intent; cancelling.");
            return;
        }

        // Synchronously post the availability info
        // During the operation a notification will be shown
        Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                "Now adding availability for " + beerName);
        Intent recoverIntent = new Intent(Intent.ACTION_VIEW,
                Uri.parse(String.format(URI_BEER, Integer.toString(beerId))));
        createNotification(NOTIFY_ADDAVAILABILITY, getString(R.string.app_addingavailability),
                getString(R.string.app_addingforbeer, beerName), true, recoverIntent, null, beerId);
        CommandResult result = new AddAvailabilityCommand(user, beerId, placeId).execute(apiConnection);
        if (result instanceof CommandSuccessResult) {
            notificationManager.cancel(NOTIFY_ADDAVAILABILITY);
        } else {
            String e = result instanceof CommandFailureResult
                    ? ((CommandFailureResult) result).getException().toString()
                    : "Unknown error";
            Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                    "Adding of availability info for " + beerName + " failed: " + e);
            createNotification(NOTIFY_ADDAVAILABILITY, getString(R.string.app_addingavailability),
                    getString(R.string.error_commandfailed), true, recoverIntent, null, beerId);
        }

    }

    // Try to add a beer to the cellar (a want or a have)
    if (intent.getAction().equals(ACTION_ADDTOCELLAR)) {

        // Get beer and notes
        int beerId = intent.getIntExtra(EXTRA_BEERID, -1);
        String beerName = intent.getStringExtra(EXTRA_BEERNAME);
        CellarType cellarType = CellarType.valueOf(intent.getStringExtra(EXTRA_CELLARTYPE));
        String memo = intent.getStringExtra(EXTRA_MEMO);
        String vintage = intent.getStringExtra(EXTRA_VINTAGE);
        String quantity = intent.getStringExtra(EXTRA_QUANTITY);
        if (beerId <= 0 || beerName == null) {
            Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                    "Missing extras in the ADDAVAILABILITY intent; cancelling.");
            return;
        }

        // Synchronously post the new cellar beer
        // During the operation a notification will be shown
        Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                "Now adding " + beerName + " to the cellar");
        Intent recoverIntent = new Intent(Intent.ACTION_VIEW,
                Uri.parse(String.format(URI_BEER, Integer.toString(beerId))));
        createNotification(NOTIFY_ADDTOCELLAR, getString(R.string.app_addingtocellar),
                getString(cellarType == CellarType.Have ? R.string.app_addhave : R.string.app_addwant,
                        beerName),
                true, recoverIntent, null, beerId);
        CommandResult result = new AddToCellarCommand(user, cellarType, beerId, memo, vintage, quantity)
                .execute(apiConnection);
        if (result instanceof CommandSuccessResult) {
            notificationManager.cancel(NOTIFY_ADDTOCELLAR);
        } else {
            String e = result instanceof CommandFailureResult
                    ? ((CommandFailureResult) result).getException().toString()
                    : "Unknown error";
            Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                    "Adding of " + beerName + " to cellar failed: " + e);
            createNotification(NOTIFY_ADDTOCELLAR, getString(R.string.app_addingtocellar),
                    getString(R.string.error_commandfailed), true, recoverIntent, null, beerId);
        }

    }

    // Try to send a mail
    if (intent.getAction().equals(ACTION_SENDMAIL)) {

        // Get mail details
        String sendTo = intent.getStringExtra(EXTRA_SENDTO);
        String subject = intent.getStringExtra(EXTRA_SUBJECT);
        String body = intent.getStringExtra(EXTRA_BODY);
        int replyTo = intent.getIntExtra(EXTRA_REPLYTO, NO_REPLY_EXTRA);
        int recipient = intent.getIntExtra(EXTRA_RECIPIENT, NO_REPLY_EXTRA);

        // Synchronously send the mail or reply
        // During the operation a notification will be shown
        Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                (replyTo == NO_REPLY_EXTRA ? "Now sending mail to "
                        : "Now sending reply to " + replyTo + " to ") + sendTo);
        Intent recoverIntent = new Intent(getApplicationContext(), Home_.class);
        recoverIntent.replaceExtras(intent.getExtras());
        recoverIntent.setAction(ACTION_SENDMAIL);
        createNotification(NOTIFY_SENDMAIL, getString(R.string.mail_sendingmail),
                getString((replyTo == NO_REPLY_EXTRA ? R.string.mail_sendingto : R.string.mail_replyingto),
                        sendTo),
                true, recoverIntent, sendTo, NO_BEER_EXTRA);
        CommandResult result;
        if (replyTo == NO_REPLY_EXTRA)
            result = new SendBeerMailCommand(user, sendTo, subject, body).execute(apiConnection);
        else
            result = new SendBeerReplyCommand(user, replyTo, recipient, body).execute(apiConnection);
        if (result instanceof CommandSuccessResult) {
            notificationManager.cancel(NOTIFY_SENDMAIL);
        } else {
            String e = result instanceof CommandFailureResult
                    ? ((CommandFailureResult) result).getException().toString()
                    : "Unknown error";
            Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                    "Sending of mail to " + sendTo + " failed: " + e);
            createNotification(NOTIFY_SENDMAIL, getString(R.string.mail_sendingmail),
                    getString(R.string.error_commandfailed), true, recoverIntent, sendTo, NO_BEER_EXTRA);
        }

    }

    // Upload photo of a beer
    if (intent.getAction().equals(ACTION_UPLOADBEERPHOTO)) {

        // Get photo URI and beer id and name
        File photo = (File) intent.getSerializableExtra(EXTRA_PHOTO);
        int beerId = intent.getIntExtra(EXTRA_BEERID, NO_BEER_EXTRA);
        String beerName = intent.getStringExtra(EXTRA_BEERNAME);
        if (beerName == null) {
            beerName = "beer with ID " + Integer.toString(beerId);
        }
        if (photo == null || photo.getPath() == null || !(new File(photo.getPath()).exists())) {
            Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                    "No photo URI provided or the photo URI does not point to an existing file; cancelling");
            return;
        }

        // Synchronously upload the photo for the specified beer
        // During the operation a notification will be shown
        Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME, "Uploading photo for " + beerName);
        Intent recoverIntent = new Intent(Intent.ACTION_VIEW,
                Uri.parse(String.format(URI_BEER, Integer.toString(beerId))));
        createNotification(NOTIFY_UPLOADPHOTO, getString(R.string.app_uploadingphoto),
                getString(R.string.app_photofor, beerName), true, recoverIntent, null, beerId);

        // Make sure the photo is no bigger than 50kB
        try {
            decodeFile(photo);
        } catch (IOException e1) {
            Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                    "Resizing of photo + " + photo.toString() + " for " + beerName + " failed: " + e1);
            createNotification(NOTIFY_UPLOADPHOTO, getString(R.string.app_uploadingphoto),
                    getString(R.string.error_commandfailed), true, recoverIntent, null, beerId);
            // If requested, call back the messenger, i.e. the calling activity
            callbackMessenger(intent, RESULT_FAILURE);
            return;
        }

        // Start actual upload of the now-resized file
        CommandResult result = new UploadBeerPhotoCommand(user, beerId, photo).execute(apiConnection);
        if (result instanceof CommandSuccessResult) {
            notificationManager.cancel(NOTIFY_UPLOADPHOTO);
            // If requested, call back the messenger, i.e. the calling activity
            callbackMessenger(intent, RESULT_SUCCESS);
        } else {
            String e = result instanceof CommandFailureResult
                    ? ((CommandFailureResult) result).getException().toString()
                    : "Unknown error";
            Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                    "Uploading photo for " + beerName + " failed: " + e);
            createNotification(NOTIFY_UPLOADPHOTO, getString(R.string.app_uploadingphoto),
                    getString(R.string.error_commandfailed), true, recoverIntent, null, beerId);
            // If requested, call back the messenger, i.e. the calling activity
            callbackMessenger(intent, RESULT_FAILURE);
        }

    }

    // Try to add a UPC code to some selected beer
    if (intent.getAction().equals(ACTION_ADDUPCCODE)) {

        // Get beer and upc code
        int beerId = intent.getIntExtra(EXTRA_BEERID, -1);
        String beerName = intent.getStringExtra(EXTRA_BEERNAME);
        String upcCode = intent.getStringExtra(EXTRA_UPCCODE);
        if (beerId <= 0 || beerName == null || upcCode == null || upcCode.equals("")) {
            Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                    "Missing extras in the ADD_UPCCODE intent; cancelling.");
            return;
        }

        // Synchronously call the add UPC code method
        // During the operation a notification will be shown
        Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                "Adding barcode " + upcCode + " to " + beerName);
        Intent recoverIntent = new Intent(getApplicationContext(), Home_.class);
        recoverIntent.replaceExtras(intent.getExtras());
        recoverIntent.setAction(ACTION_ADDUPCCODE);
        createNotification(NOTIFY_ADDUPCCODE, getString(R.string.app_addingupccode),
                getString(R.string.app_addingcodefor, beerName), true, recoverIntent, null, beerId);
        CommandResult result = new AddUpcCodeCommand(user, beerId, upcCode).execute(apiConnection);
        if (result instanceof CommandSuccessResult) {
            notificationManager.cancel(NOTIFY_ADDUPCCODE);
        } else {
            String e = result instanceof CommandFailureResult
                    ? ((CommandFailureResult) result).getException().toString()
                    : "Unknown error";
            Log.d(com.ratebeer.android.gui.components.helpers.Log.LOG_NAME,
                    "Adding of barcode " + upcCode + " to " + beerName + " failed: " + e);
            createNotification(NOTIFY_ADDUPCCODE, getString(R.string.app_addingupccode),
                    getString(R.string.error_commandfailed), true, recoverIntent, null, beerId);
        }

    }

}