/** * Defines what to do for the different messages. * * @see android.content.BroadcastReceiver#onReceive(android.content.Context, * android.content.Intent) */ @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BillingConstants.ACTION_PURCHASE_STATE_CHANGED.equals(action)) { String signedData = intent.getStringExtra(BillingConstants.INAPP_SIGNED_DATA); String signature = intent.getStringExtra(BillingConstants.INAPP_SIGNATURE); try { JSONObject json = new JSONObject(signedData); long nonce = (Long) json.get(JSON_FIELD_NONCE); // Check data signature and nonce if (!Nonce.isNonceKnown(nonce) || !BillingSecurity.checkData(signedData, signature)) { return; } Nonce.removeNonce(nonce); // Display a thank you message Toast.makeText(context, R.string.thank_you, Toast.LENGTH_LONG).show(); // Send confirmations JSONArray orders = (JSONArray) json.get(JSON_FIELD_ORDERS); for (int i = 0; i < orders.length(); i++) { try { JSONObject order = orders.getJSONObject(i); BillingService.confirmNotifications( new String[] {(String) order.get(JSON_FIELD_NOTIFICATION_ID)}); } catch (JSONException e) { } } } catch (Exception e) { ExceptionHandler.handle( this, R.string.error_during_payment, context.getApplicationContext()); } } else if (BillingConstants.ACTION_NOTIFY.equals(action)) { String notifyId = intent.getStringExtra(BillingConstants.NOTIFICATION_ID); try { // Ask for the details of the transaction BillingService.getPurchaseInformation(new String[] {notifyId}); } catch (RemoteException e) { ExceptionHandler.handle( this, R.string.error_during_payment, context.getApplicationContext()); } } else if (BillingConstants.ACTION_RESPONSE_CODE.equals(action)) { int responseCodeIndex = intent.getIntExtra( BillingConstants.INAPP_RESPONSE_CODE, ResponseCode.RESULT_ERROR.ordinal()); if (!BillingConstants.ResponseCode.RESULT_OK.equals( BillingConstants.ResponseCode.valueOf(responseCodeIndex))) { ExceptionHandler.handle(this, R.string.error_during_payment, context); } } }
/** Called when the Pay Now button is pressed */ public void onClick(View v) { if (v == mBuyButton) { if (BillingConstants.DEBUG) { Log.d(TAG, "buying: " + mItemName + " sku: " + mSku); } /** Update the object */ parkingLocationObj.setAmountPaid( mDurationTotalTimeMinutes * FLAT_PARKING_SPOT_RATE_PER_MINUTE); parkingLocationObj.setDuration(mDurationTotalTimeMinutes); parkingLocationObj.setRate(FLAT_PARKING_SPOT_RATE_PER_MINUTE); parkingLocationObj.setQuantity(mTimePeriods); parkingLocationObj.setStartTimestampMs(System.currentTimeMillis()); parkingLocationObj.setEndTimestampMs( System.currentTimeMillis() + mDurationTotalTimeMinutes * NUM_MILLIS_IN_A_MINUTE); /** Set latitude and longitude in application preferences */ AppPreferences.getInstance().setLastPaidLocationLatitude(parkingLocationObj.getLatitude()); AppPreferences.getInstance().setLastPaidLocationLongitude(parkingLocationObj.getLongitude()); /** Sanity check */ if (!mBillingService.requestPurchase(mSku, mPayloadContents)) { showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID); } } }
/** * If the database has not been initialized, we send a RESTORE_TRANSACTIONS request to Android * Market to get the list of purchased items for this user. This happens if the application has * just been installed or the user wiped data. We do not want to do this on every startup, rather, * we want to do only when the database needs to be initialized. */ private void restoreDatabase() { SharedPreferences prefs = getPreferences(MODE_PRIVATE); boolean initialized = prefs.getBoolean(DB_INITIALIZED, false); if (!initialized) { mBillingService.restoreTransactions(); Toast.makeText(this, R.string.restoring_transactions, Toast.LENGTH_LONG).show(); } }
@Override protected void onDestroy() { super.onDestroy(); mPurchaseDatabase.close(); mBillingService.unbind(); }
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.parkingpayment); mHandler = new Handler(); mParkingPurchaseObserver = new ParkingPurchaseObserver(mHandler); mBillingService = new BillingService(); mBillingService.setContext(this); mPurchaseDatabase = new PurchaseDatabase(DashboardActivity.myContext); // Get on the spot selected Intent starterIntent = getIntent(); Bundle bundle = starterIntent.getExtras(); TextView textAll = (TextView) findViewById(R.id.parkingAllDetailsTextView); /** Obtain time object * */ String timeObj = bundle.getString("time"); try { paidTime = Integer.parseInt(timeObj); Log.v(TAG, "Calculated time : " + paidTime); } catch (NumberFormatException nfe) { Log.v(TAG, "NumberFormatException: " + nfe.getMessage()); } /** Create parkingLocationObj */ String all = bundle.getString("info"); parkingLocationObj = LocationUtility.convertStringToObject(all); /** Use the object to populate fields */ String locAddress = "Address unavailable"; Geocoder geoCoder = new Geocoder(DashboardActivity.myContext, Locale.getDefault()); GeoPoint gp = new GeoPoint( (int) (parkingLocationObj.getLatitude() * 1E6), (int) (parkingLocationObj.getLongitude() * 1E6)); locAddress = LocationUtility.ConvertPointToLocation(gp, geoCoder); Log.v(TAG, "Setting address to: " + locAddress); parkingLocationObj.setAddress(locAddress); String typeToDisplay = parkingLocationObj.getType() == null ? "Not known" : "" + parkingLocationObj.getType(); textAll.setText( "\n" + "Address: " + parkingLocationObj.getAddress() + "\n" + "Type:" + typeToDisplay + "\n" + "MeterId: " + parkingLocationObj.getMeterID() + "\n" + "Number of Parking Spots at this location: " + parkingLocationObj.getQuantity() + "\n"); setupWidgets(); // Check if billing is supported. ResponseHandler.register(mParkingPurchaseObserver); if (!mBillingService.checkBillingSupported()) { showDialog(DIALOG_CANNOT_CONNECT_ID); } }