Java Code Examples for android.app.Activity#RESULT_CANCELED

The following examples show how to use android.app.Activity#RESULT_CANCELED . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
   	if (resultCode == Activity.RESULT_OK) {
   		Uri uri = data.getData();
   		String url = uri.getPath();
   		Log.i(LOG_TAG, "url:"+url+", edttxtFile:"+edttxtFile);
   		
   		/* 遇到内存不足导致MainActivity重新加载时,需要重新载入AnalyzeActivity */
   		if(null == edttxtFile){
			initAnalyzeView(view);
   		}
   		edttxtFile.setText(url);
   		pgsdlgAnalyzing.dismiss();
   	}
   	
   	if(Activity.RESULT_CANCELED == resultCode){
   		pgsdlgAnalyzing.dismiss();
   	}
   	
   	Log.i(LOG_TAG, "Activity result:"+resultCode);
   }
 
Example 2
Source Project: Travel-Mate   File: ListViewRealTimeActivity.java    License: MIT License 6 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        // Check for the integer request code originally supplied to startResolutionForResult().
        case REQUEST_LOCATION:
            switch (resultCode) {
                case Activity.RESULT_OK:
                    //User agreed to make required location settings changes
                    //startLocationUpdates();
                    TravelmateSnackbars.createSnackBar(findViewById(R.id.list_view_realtime),
                            R.string.location_enabled, Snackbar.LENGTH_LONG).show();
                    mCurlat = Double.toString(mTracker.getLatitude());
                    mCurlon = Double.toString(mTracker.getLongitude());
                    break;

                case Activity.RESULT_CANCELED:
                    //User chose not to make required location settings changes
                    TravelmateSnackbars.createSnackBar(findViewById(R.id.list_view_realtime),
                            R.string.location_not_enabled, Snackbar.LENGTH_LONG).show();
                    break;
            }
            break;
    }
}
 
Example 3
@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_OAUTH) {
        mAuthInProgress = false;
        if (resultCode == Activity.RESULT_OK) {
            // Make sure the app is not already connected or attempting to connect
            if (!mApiClient.isConnecting() && !mApiClient.isConnected()) {
                mApiClient.connect();
            }
        } else if (resultCode == Activity.RESULT_CANCELED) {
            Log.e(TAG, "Authorization - Cancel");
            WritableMap map = Arguments.createMap();
            map.putString("message", "" + "Authorization cancelled");
            sendEvent(mReactContext, "GoogleFitAuthorizeFailure", map);
        }
    }
}
 
Example 4
Source Project: FixMath   File: GameHelperUtils.java    License: Apache License 2.0 6 votes vote down vote up
static String activityResponseCodeToString(int respCode) {
    switch (respCode) {
        case Activity.RESULT_OK:
            return "RESULT_OK";
        case Activity.RESULT_CANCELED:
            return "RESULT_CANCELED";
        case GamesActivityResultCodes.RESULT_APP_MISCONFIGURED:
            return "RESULT_APP_MISCONFIGURED";
        case GamesActivityResultCodes.RESULT_LEFT_ROOM:
            return "RESULT_LEFT_ROOM";
        case GamesActivityResultCodes.RESULT_LICENSE_FAILED:
            return "RESULT_LICENSE_FAILED";
        case GamesActivityResultCodes.RESULT_RECONNECT_REQUIRED:
            return "RESULT_RECONNECT_REQUIRED";
        case GamesActivityResultCodes.RESULT_SIGN_IN_FAILED:
            return "SIGN_IN_FAILED";
        default:
            return String.valueOf(respCode);
    }
}
 
Example 5
@Override
boolean onActivityResult(int requestCode, int resultCode, Intent data) {
    Result outcome;

    if (data == null) {
        // This happens if the user presses 'Back'.
        outcome = Result.createCancelResult("Operation canceled");
    } else if (NativeProtocol.isServiceDisabledResult20121101(data)) {
        outcome = null;
    } else if (resultCode == Activity.RESULT_CANCELED) {
        outcome = Result.createCancelResult(
                data.getStringExtra(NativeProtocol.STATUS_ERROR_DESCRIPTION));
    } else if (resultCode != Activity.RESULT_OK) {
        outcome = Result.createErrorResult("Unexpected resultCode from authorization.", null);
    } else {
        outcome = handleResultOk(data);
    }

    if (outcome != null) {
        completeAndValidate(outcome);
    } else {
        tryNextHandler();
    }

    return true;
}
 
Example 6
Source Project: Travel-Mate   File: MapViewRealTimeActivity.java    License: MIT License 6 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        // Check for the integer request code originally supplied to startResolutionForResult().
        case REQUEST_LOCATION:
            switch (resultCode) {
                case Activity.RESULT_OK:
                    //User agreed to make required location settings changes
                    //startLocationUpdates();
                    TravelmateSnackbars.createSnackBar(findViewById(R.id.map_real_time),
                            R.string.location_enabled, Snackbar.LENGTH_LONG).show();
                    mCurlat = Double.toString(tracker.getLatitude());
                    mCurlon = Double.toString(tracker.getLongitude());
                    getMarkers("eat-drink", R.drawable.ic_local_pizza_black);

                    break;
                case Activity.RESULT_CANCELED:
                    //User chose not to make required location settings changes
                    TravelmateSnackbars.createSnackBar(findViewById(R.id.map_real_time),
                            R.string.location_not_enabled, Snackbar.LENGTH_LONG).show();
                    break;
            }
            break;
    }
}
 
Example 7
static String activityResponseCodeToString(int respCode) {
    switch (respCode) {
        case Activity.RESULT_OK:
            return "RESULT_OK";
        case Activity.RESULT_CANCELED:
            return "RESULT_CANCELED";
        case GamesActivityResultCodes.RESULT_APP_MISCONFIGURED:
            return "RESULT_APP_MISCONFIGURED";
        case GamesActivityResultCodes.RESULT_LEFT_ROOM:
            return "RESULT_LEFT_ROOM";
        case GamesActivityResultCodes.RESULT_LICENSE_FAILED:
            return "RESULT_LICENSE_FAILED";
        case GamesActivityResultCodes.RESULT_RECONNECT_REQUIRED:
            return "RESULT_RECONNECT_REQUIRED";
        case GamesActivityResultCodes.RESULT_SIGN_IN_FAILED:
            return "SIGN_IN_FAILED";
        default:
            return String.valueOf(respCode);
    }
}
 
Example 8
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CHECK_SETTINGS:
            switch (resultCode) {
                case Activity.RESULT_OK:
                    startLocationUpdates();
                    break;
                case Activity.RESULT_CANCELED:
                    break;
            }
            break;
    }
}
 
Example 9
Source Project: KlyphMessenger   File: AuthorizationClient.java    License: MIT License 5 votes vote down vote up
@Override
boolean onActivityResult(int requestCode, int resultCode, Intent data) {
    Result outcome;

    logEvent(AnalyticsEvents.EVENT_NATIVE_LOGIN_DIALOG_COMPLETE,
            AnalyticsEvents.PARAMETER_NATIVE_LOGIN_DIALOG_COMPLETE_TIME, callId);

    if (data == null) {
        // This happens if the user presses 'Back'.
        outcome = Result.createCancelResult(pendingRequest, "Operation canceled");
    } else if (NativeProtocol.isServiceDisabledResult20121101(data)) {
        outcome = null;
    } else if (resultCode == Activity.RESULT_CANCELED) {
        outcome = createCancelOrErrorResult(pendingRequest, data);
    } else if (resultCode != Activity.RESULT_OK) {
        outcome = Result.createErrorResult(pendingRequest, "Unexpected resultCode from authorization.", null);
    } else {
        outcome = handleResultOk(data);
    }

    if (outcome != null) {
        completeAndValidate(outcome);
    } else {
        tryNextHandler();
    }

    return true;
}
 
Example 10
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (requestCode == REQ_CODE_VERSION_UPDATE) {
        if (resultCode == Activity.RESULT_CANCELED) {
            // If the update is cancelled by the user,
            // you can request to start the update again.
            inAppUpdateManager.checkForAppUpdate();

            Log.d(TAG, "Update flow failed! Result code: " + resultCode);
        }
    }

    super.onActivityResult(requestCode, resultCode, data);
}
 
Example 11
/**
 * Handle a resolved activity from the Google Pay payment sheet.
 *
 * @param requestCode Request code originally supplied to AutoResolveHelper in requestPayment().
 * @param resultCode  Result code returned by the Google Pay API.
 * @param data        Intent from the Google Pay API containing payment or error data.
 * @see <a href="https://developer.android.com/training/basics/intents/result">Getting a result
 * from an Activity</a>
 */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  
  switch (requestCode) {

    case LOAD_PAYMENT_DATA_REQUEST_CODE:
      switch (resultCode) {

        case Activity.RESULT_OK:
          PaymentData paymentData = PaymentData.getFromIntent(data);
          handlePaymentSuccess(paymentData);
          break;

        case Activity.RESULT_CANCELED:
          // The user simply cancelled without selecting a payment method.
          break;

        case AutoResolveHelper.RESULT_ERROR:
          // Get more details on the error with – AutoResolveHelper.getStatusFromIntent(data);
          break;
      }

      // Close the activity
      finishAndRemoveTask();
  }
}
 
Example 12
@Override
public void fetchFilePathFromIntent(int requestCode, int resultCode, Intent data) {

    LogUtils.i(TAG, "request:" + requestCode + "  result:" + resultCode + "  data:" + data);
    if (REQUEST_CODE != requestCode)
        return;

    if (resultCode == Activity.RESULT_CANCELED) {
        cancel();
        return;
    }

    if (resultCode == Activity.RESULT_OK) {

        if (isL)
            handleAboveL(cameraState ? new Uri[]{data.getParcelableExtra(ActionActivity.KEY_URI)} : processData(data));
        else if (jsChannel)
            convertFileAndCallBack(cameraState ? new Uri[]{data.getParcelableExtra(ActionActivity.KEY_URI)} : processData(data));
        else {
            if (cameraState && mUriValueCallback != null)
                mUriValueCallback.onReceiveValue((Uri) data.getParcelableExtra(ActionActivity.KEY_URI));
            else
                handleBelowLData(data);
        }

    }


}
 
Example 13
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == CALLOUT_ZPL) {
        Intent response = new Intent();
        if(resultCode != Activity.RESULT_CANCELED) {
            response.putExtra(IntentCallout.INTENT_RESULT_VALUE, "");
        }
        this.setResult(resultCode, response);
        this.finish();
    }
}
 
Example 14
Source Project: Onosendai   File: TwitterOauthWizard.java    License: Apache License 2.0 5 votes vote down vote up
public void onActivityResult (final int requestCode, final int resultCode, final Intent intent) {
	LOG.d("onActivityResult(%d, %d, %s)", requestCode, resultCode, intent);
	final TwitterOauthComplete completeCallback = this.completeCallbacks.get(Integer.valueOf(requestCode));
	if (completeCallback != null) {
		if (resultCode == Activity.RESULT_OK) {
			final String oauthVerifier = intent.getExtras().getString(TwitterOauth.IEXTRA_OAUTH_VERIFIER);
			new TwitterOauthPostTask(this, requestCode, oauthVerifier, completeCallback).execute();
		}
		else if (resultCode == Activity.RESULT_CANCELED) {
			DialogHelper.alert(getContext(), "Twitter auth canceled."); //ES
		}
	}
}
 
Example 15
Source Project: reader   File: CameraLauncher.java    License: MIT License 4 votes vote down vote up
/**
 * Called when the camera view exits.
 *
 * @param requestCode       The request code originally supplied to startActivityForResult(),
 *                          allowing you to identify who this result came from.
 * @param resultCode        The integer result code returned by the child activity through its setResult().
 * @param intent            An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
 */
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    // Get src and dest types from request code
    int srcType = (requestCode / 16) - 1;
    int destType = (requestCode % 16) - 1;
    // if camera crop
if (requestCode == CROP_CAMERA) {
  if (resultCode == Activity.RESULT_OK) {
    // // Send Uri back to JavaScript for viewing image
    this.callbackContext
        .success(croppedUri.toString());
    croppedUri = null;
    
  }// If cancelled
  else if (resultCode == Activity.RESULT_CANCELED) {
    this.failPicture("Camera cancelled.");
  }

  // If something else
  else {
    this.failPicture("Did not complete!");
  }

}
    // If CAMERA
    if (srcType == CAMERA) {
        // If image available
        if (resultCode == Activity.RESULT_OK) {
            try {
                this.processResultFromCamera(destType, intent);
            } catch (IOException e) {
                e.printStackTrace();
                this.failPicture("Error capturing image.");
            }
        }

        // If cancelled
        else if (resultCode == Activity.RESULT_CANCELED) {
            this.failPicture("Camera cancelled.");
        }

        // If something else
        else {
            this.failPicture("Did not complete!");
        }
    }

    // If retrieving photo from library
    else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) {
        if (resultCode == Activity.RESULT_OK && intent != null) {
            this.processResultFromGallery(destType, intent);
        }
        else if (resultCode == Activity.RESULT_CANCELED) {
            this.failPicture("Selection cancelled.");
        }
        else {
            this.failPicture("Selection did not complete!");
        }
    }
}
 
Example 16
Source Project: react-native-cafe-bazaar   File: IabHelper.java    License: MIT License 4 votes vote down vote up
/**
 * Handles an activity result that's part of the purchase flow in in-app billing. If you
 * are calling {@link #launchPurchaseFlow}, then you must call this method from your
 * Activity's {@link [email protected]} method. This method
 * MUST be called from the UI thread of the Activity.
 *
 * @param requestCode The requestCode as you received it.
 * @param resultCode The resultCode as you received it.
 * @param data The data (Intent) as you received it.
 * @return Returns true if the result was related to a purchase flow and was handled;
 *     false if the result was not related to a purchase, in which case you should
 *     handle it normally.
 */
public boolean handleActivityResult(int requestCode, int resultCode, Intent data) {
    IabResult result;
    if (requestCode != mRequestCode) return false;

    checkNotDisposed();
    checkSetupDone("handleActivityResult");

    // end of async purchase operation that started on launchPurchaseFlow
    flagEndAsync();

    if (data == null) {
        logError("Null data in IAB activity result.");
        result = new IabResult(IABHELPER_BAD_RESPONSE, "Null data in IAB result");
        if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
        return true;
    }

    int responseCode = getResponseCodeFromIntent(data);
    String purchaseData = data.getStringExtra(RESPONSE_INAPP_PURCHASE_DATA);
    String dataSignature = data.getStringExtra(RESPONSE_INAPP_SIGNATURE);

    if (resultCode == Activity.RESULT_OK && responseCode == BILLING_RESPONSE_RESULT_OK) {
        logDebug("Successful resultcode from purchase activity.");
        logDebug("Purchase data: " + purchaseData);
        logDebug("Data signature: " + dataSignature);
        logDebug("Extras: " + data.getExtras());
        logDebug("Expected item type: " + mPurchasingItemType);

        if (purchaseData == null || dataSignature == null) {
            logError("BUG: either purchaseData or dataSignature is null.");
            logDebug("Extras: " + data.getExtras().toString());
            result = new IabResult(IABHELPER_UNKNOWN_ERROR, "IAB returned null purchaseData or dataSignature");
            if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
            return true;
        }

        Purchase purchase = null;
        try {
            purchase = new Purchase(mPurchasingItemType, purchaseData, dataSignature);
            String sku = purchase.getSku();

            // Verify signature
            if (!Security.verifyPurchase(mSignatureBase64, purchaseData, dataSignature)) {
                logError("Purchase signature verification FAILED for sku " + sku);
                result = new IabResult(IABHELPER_VERIFICATION_FAILED, "Signature verification failed for sku " + sku);
                if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, purchase);
                return true;
            }
            logDebug("Purchase signature successfully verified.");
        }
        catch (JSONException e) {
            logError("Failed to parse purchase data.");
            e.printStackTrace();
            result = new IabResult(IABHELPER_BAD_RESPONSE, "Failed to parse purchase data.");
            if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
            return true;
        }

        if (mPurchaseListener != null) {
            mPurchaseListener.onIabPurchaseFinished(new IabResult(BILLING_RESPONSE_RESULT_OK, "Success"), purchase);
        }
    }
    else if (resultCode == Activity.RESULT_OK) {
        // result code was OK, but in-app billing response was not OK.
        logDebug("Result code was OK but in-app billing response was not OK: " + getResponseDesc(responseCode));
        if (mPurchaseListener != null) {
            result = new IabResult(responseCode, "Problem purchashing item.");
            mPurchaseListener.onIabPurchaseFinished(result, null);
        }
    }
    else if (resultCode == Activity.RESULT_CANCELED) {
        logDebug("Purchase canceled - Response: " + getResponseDesc(responseCode));
        result = new IabResult(IABHELPER_USER_CANCELLED, "User canceled.");
        if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
    }
    else {
        logError("Purchase failed. Result code: " + Integer.toString(resultCode)
                + ". Response: " + getResponseDesc(responseCode));
        result = new IabResult(IABHELPER_UNKNOWN_PURCHASE_RESPONSE, "Unknown purchase response.");
        if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
    }
    return true;
}
 
Example 17
Source Project: KlyphMessenger   File: Session.java    License: MIT License 4 votes vote down vote up
/**
 * Provides an implementation for {@link Activity#onActivityResult
 * onActivityResult} that updates the Session based on information returned
 * during the authorization flow. The Activity that calls open or
 * requestNewPermissions should forward the resulting onActivityResult call here to
 * update the Session state based on the contents of the resultCode and
 * data.
 *
 * @param currentActivity The Activity that is forwarding the onActivityResult call.
 * @param requestCode     The requestCode parameter from the forwarded call. When this
 *                        onActivityResult occurs as part of Facebook authorization
 *                        flow, this value is the activityCode passed to open or
 *                        authorize.
 * @param resultCode      An int containing the resultCode parameter from the forwarded
 *                        call.
 * @param data            The Intent passed as the data parameter from the forwarded
 *                        call.
 * @return A boolean indicating whether the requestCode matched a pending
 *         authorization request for this Session.
 */
public final boolean onActivityResult(Activity currentActivity, int requestCode, int resultCode, Intent data) {
    Validate.notNull(currentActivity, "currentActivity");

    initializeStaticContext(currentActivity);

    synchronized (lock) {
        if (pendingAuthorizationRequest == null || (requestCode != pendingAuthorizationRequest.getRequestCode())) {
            return false;
        }
    }

    Exception exception = null;
    AuthorizationClient.Result.Code code = AuthorizationClient.Result.Code.ERROR;

    if (data != null) {
        AuthorizationClient.Result result = (AuthorizationClient.Result) data.getSerializableExtra(
                LoginActivity.RESULT_KEY);
        if (result != null) {
            // This came from LoginActivity.
            handleAuthorizationResult(resultCode, result);
            return true;
        } else if (authorizationClient != null) {
            // Delegate to the auth client.
            authorizationClient.onActivityResult(requestCode, resultCode, data);
            return true;
        }
    } else if (resultCode == Activity.RESULT_CANCELED) {
        exception = new FacebookOperationCanceledException("User canceled operation.");
        code = AuthorizationClient.Result.Code.CANCEL;
    }

    if (exception == null) {
        exception = new FacebookException("Unexpected call to Session.onActivityResult");
    }

    logAuthorizationComplete(code, null, exception);
    finishAuthOrReauth(null, exception);

    return true;
}
 
Example 18
Source Project: openwebnet-android   File: IabHelper.java    License: MIT License 4 votes vote down vote up
/**
 * Handles an activity result that's part of the purchase flow in in-app billing. If you
 * are calling {@link #launchPurchaseFlow}, then you must call this method from your
 * Activity's {@link [email protected]} method. This method
 * MUST be called from the UI thread of the Activity.
 *
 * @param requestCode The requestCode as you received it.
 * @param resultCode The resultCode as you received it.
 * @param data The data (Intent) as you received it.
 * @return Returns true if the result was related to a purchase flow and was handled;
 *     false if the result was not related to a purchase, in which case you should
 *     handle it normally.
 */
public boolean handleActivityResult(int requestCode, int resultCode, Intent data) {
    IabResult result;
    if (requestCode != mRequestCode) return false;

    checkNotDisposed();
    checkSetupDone("handleActivityResult");

    // end of async purchase operation that started on launchPurchaseFlow
    flagEndAsync();

    if (data == null) {
        logError("Null data in IAB activity result.");
        result = new IabResult(IABHELPER_BAD_RESPONSE, "Null data in IAB result");
        if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
        return true;
    }

    int responseCode = getResponseCodeFromIntent(data);
    String purchaseData = data.getStringExtra(RESPONSE_INAPP_PURCHASE_DATA);
    String dataSignature = data.getStringExtra(RESPONSE_INAPP_SIGNATURE);

    if (resultCode == Activity.RESULT_OK && responseCode == BILLING_RESPONSE_RESULT_OK) {
        logDebug("Successful resultcode from purchase activity.");
        logDebug("Purchase data: " + purchaseData);
        logDebug("Data signature: " + dataSignature);
        logDebug("Extras: " + data.getExtras());
        logDebug("Expected item type: " + mPurchasingItemType);

        if (purchaseData == null || dataSignature == null) {
            logError("BUG: either purchaseData or dataSignature is null.");
            logDebug("Extras: " + data.getExtras().toString());
            result = new IabResult(IABHELPER_UNKNOWN_ERROR, "IAB returned null purchaseData or dataSignature");
            if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
            return true;
        }

        Purchase purchase = null;
        try {
            purchase = new Purchase(mPurchasingItemType, purchaseData, dataSignature);
            String sku = purchase.getSku();

            // Verify signature
            if (!Security.verifyPurchase(mSignatureBase64, purchaseData, dataSignature)) {
                logError("Purchase signature verification FAILED for sku " + sku);
                result = new IabResult(IABHELPER_VERIFICATION_FAILED, "Signature verification failed for sku " + sku);
                if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, purchase);
                return true;
            }
            logDebug("Purchase signature successfully verified.");
        }
        catch (JSONException e) {
            logError("Failed to parse purchase data.");
            e.printStackTrace();
            result = new IabResult(IABHELPER_BAD_RESPONSE, "Failed to parse purchase data.");
            if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
            return true;
        }

        if (mPurchaseListener != null) {
            mPurchaseListener.onIabPurchaseFinished(new IabResult(BILLING_RESPONSE_RESULT_OK, "Success"), purchase);
        }
    }
    else if (resultCode == Activity.RESULT_OK) {
        // result code was OK, but in-app billing response was not OK.
        logDebug("Result code was OK but in-app billing response was not OK: " + getResponseDesc(responseCode));
        if (mPurchaseListener != null) {
            result = new IabResult(responseCode, "Problem purchashing item.");
            mPurchaseListener.onIabPurchaseFinished(result, null);
        }
    }
    else if (resultCode == Activity.RESULT_CANCELED) {
        logDebug("Purchase canceled - Response: " + getResponseDesc(responseCode));
        result = new IabResult(IABHELPER_USER_CANCELLED, "User canceled.");
        if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
    }
    else {
        logError("Purchase failed. Result code: " + Integer.toString(resultCode)
                + ". Response: " + getResponseDesc(responseCode));
        result = new IabResult(IABHELPER_UNKNOWN_PURCHASE_RESPONSE, "Unknown purchase response.");
        if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
    }
    return true;
}
 
Example 19
/**
 * Handles an activity result that's part of the purchase flow in in-app billing. If you
 * are calling {@link #launchPurchaseFlow}, then you must call this method from your
 * Activity's {@link [email protected]} method. This method
 * MUST be called from the UI thread of the Activity.
 *
 * @param requestCode The requestCode as you received it.
 * @param resultCode The resultCode as you received it.
 * @param data The data (Intent) as you received it.
 * @return Returns true if the result was related to a purchase flow and was handled;
 *     false if the result was not related to a purchase, in which case you should
 *     handle it normally.
 */
public boolean handleActivityResult(int requestCode, int resultCode, Intent data) {
    IabResult result;
    if (requestCode != mRequestCode) return false;

    checkNotDisposed();
    checkSetupDone("handleActivityResult");

    // end of async purchase operation that started on launchPurchaseFlow
    flagEndAsync();

    if (data == null) {
        logError("Null data in IAB activity result.");
        result = new IabResult(IABHELPER_BAD_RESPONSE, "Null data in IAB result");
        if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
        return true;
    }

    int responseCode = getResponseCodeFromIntent(data);
    String purchaseData = data.getStringExtra(RESPONSE_INAPP_PURCHASE_DATA);
    String dataSignature = data.getStringExtra(RESPONSE_INAPP_SIGNATURE);

    if (resultCode == Activity.RESULT_OK && responseCode == BILLING_RESPONSE_RESULT_OK) {
        logDebug("Successful resultcode from purchase activity.");
        logDebug("Purchase data: " + purchaseData);
        logDebug("Data signature: " + dataSignature);
        logDebug("Extras: " + data.getExtras());
        logDebug("Expected item type: " + mPurchasingItemType);

        if (purchaseData == null || dataSignature == null) {
            logError("BUG: either purchaseData or dataSignature is null.");
            logDebug("Extras: " + data.getExtras().toString());
            result = new IabResult(IABHELPER_UNKNOWN_ERROR, "IAB returned null purchaseData or dataSignature");
            if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
            return true;
        }

        Purchase purchase = null;
        try {
            purchase = new Purchase(mPurchasingItemType, purchaseData, dataSignature);
            String sku = purchase.getSku();

            // Verify signature
            if (!Security.verifyPurchase(mSignatureBase64, purchaseData, dataSignature)) {
                logError("Purchase signature verification FAILED for sku " + sku);
                result = new IabResult(IABHELPER_VERIFICATION_FAILED, "Signature verification failed for sku " + sku);
                if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, purchase);
                return true;
            }
            logDebug("Purchase signature successfully verified.");
        }
        catch (JSONException e) {
            logError("Failed to parse purchase data.");
            e.printStackTrace();
            result = new IabResult(IABHELPER_BAD_RESPONSE, "Failed to parse purchase data.");
            if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
            return true;
        }

        if (mPurchaseListener != null) {
            mPurchaseListener.onIabPurchaseFinished(new IabResult(BILLING_RESPONSE_RESULT_OK, "Success"), purchase);
        }
    }
    else if (resultCode == Activity.RESULT_OK) {
        // result code was OK, but in-app billing response was not OK.
        logDebug("Result code was OK but in-app billing response was not OK: " + getResponseDesc(responseCode));
        if (mPurchaseListener != null) {
            result = new IabResult(responseCode, "Problem purchashing item.");
            mPurchaseListener.onIabPurchaseFinished(result, null);
        }
    }
    else if (resultCode == Activity.RESULT_CANCELED) {
        logDebug("Purchase canceled - Response: " + getResponseDesc(responseCode));
        result = new IabResult(IABHELPER_USER_CANCELLED, "User canceled.");
        if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
    }
    else {
        logError("Purchase failed. Result code: " + Integer.toString(resultCode)
                + ". Response: " + getResponseDesc(responseCode));
        result = new IabResult(IABHELPER_UNKNOWN_PURCHASE_RESPONSE, "Unknown purchase response.");
        if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
    }
    return true;
}
 
Example 20
Source Project: Contacts   File: IabHelper.java    License: MIT License 4 votes vote down vote up
/**
 * Handles an activity result that's part of the purchase flow in in-app billing. If you
 * are calling {@link #launchPurchaseFlow}, then you must call this method from your
 * Activity's {@link [email protected]} method. This method
 * MUST be called from the UI thread of the Activity.
 *
 * @param requestCode The requestCode as you received it.
 * @param resultCode The resultCode as you received it.
 * @param data The data (Intent) as you received it.
 * @return Returns true if the result was related to a purchase flow and was handled;
 *     false if the result was not related to a purchase, in which case you should
 *     handle it normally.
 */
public boolean handleActivityResult(int requestCode, int resultCode, Intent data) {
    IabResult result;
    if (requestCode != mRequestCode) return false;

    checkNotDisposed();
    checkSetupDone("handleActivityResult");

    // end of async purchase operation that started on launchPurchaseFlow
    flagEndAsync();

    if (data == null) {
        logError("Null data in IAB activity result.");
        result = new IabResult(IABHELPER_BAD_RESPONSE, "Null data in IAB result");
        if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
        return true;
    }

    int responseCode = getResponseCodeFromIntent(data);
    String purchaseData = data.getStringExtra(RESPONSE_INAPP_PURCHASE_DATA);
    String dataSignature = data.getStringExtra(RESPONSE_INAPP_SIGNATURE);

    if (resultCode == Activity.RESULT_OK && responseCode == BILLING_RESPONSE_RESULT_OK) {
        logDebug("Successful resultcode from purchase activity.");
        logDebug("Purchase data: " + purchaseData);
        logDebug("Data signature: " + dataSignature);
        logDebug("Extras: " + data.getExtras());
        logDebug("Expected item type: " + mPurchasingItemType);

        if (purchaseData == null || dataSignature == null) {
            logError("BUG: either purchaseData or dataSignature is null.");
            logDebug("Extras: " + data.getExtras().toString());
            result = new IabResult(IABHELPER_UNKNOWN_ERROR, "IAB returned null purchaseData or dataSignature");
            if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
            return true;
        }

        Purchase purchase = null;
        try {
            purchase = new Purchase(mPurchasingItemType, purchaseData, dataSignature);
            String sku = purchase.getSku();
           
            // Verify signature
            if (!Security.verifyPurchase(mSignatureBase64, purchaseData, dataSignature)) {
                logError("Purchase signature verification FAILED for sku " + sku);
                result = new IabResult(IABHELPER_VERIFICATION_FAILED, "Signature verification failed for sku " + sku);
                if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, purchase);
                return true;
            }
            logDebug("Purchase signature successfully verified.");
        }
        catch (JSONException e) {
            logError("Failed to parse purchase data.");
            e.printStackTrace();
            result = new IabResult(IABHELPER_BAD_RESPONSE, "Failed to parse purchase data.");
            if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
            return true;
        }

        if (mPurchaseListener != null) {
            mPurchaseListener.onIabPurchaseFinished(new IabResult(BILLING_RESPONSE_RESULT_OK, "Success"), purchase);
        }
    }
    else if (resultCode == Activity.RESULT_OK) {
        // result code was OK, but in-app billing response was not OK.
        logDebug("Result code was OK but in-app billing response was not OK: " + getResponseDesc(responseCode));
        if (mPurchaseListener != null) {
            result = new IabResult(responseCode, "Problem purchashing item.");
            mPurchaseListener.onIabPurchaseFinished(result, null);
        }
    }
    else if (resultCode == Activity.RESULT_CANCELED) {
        logDebug("Purchase canceled - Response: " + getResponseDesc(responseCode));
        result = new IabResult(IABHELPER_USER_CANCELLED, "User canceled.");
        if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
    }
    else {
        logError("Purchase failed. Result code: " + Integer.toString(resultCode)
                + ". Response: " + getResponseDesc(responseCode));
        result = new IabResult(IABHELPER_UNKNOWN_PURCHASE_RESPONSE, "Unknown purchase response.");
        if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
    }
    return true;
}