|
| 1 | +/* Copyright 2011 Robot Media SL (http://www.robotmedia.net) |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package net.robotmedia.billing.helper; |
| 17 | + |
| 18 | +import net.robotmedia.billing.BillingController; |
| 19 | +import net.robotmedia.billing.BillingController.BillingStatus; |
| 20 | +import net.robotmedia.billing.model.Transaction.PurchaseState; |
| 21 | +import net.robotmedia.billing.request.ResponseCode; |
| 22 | +import android.app.Activity; |
| 23 | + |
| 24 | +public abstract class AbstractBillingActivity extends Activity implements BillingController.IConfiguration { |
| 25 | + |
| 26 | + protected AbstractBillingObserver mBillingObserver; |
| 27 | + |
| 28 | + /** |
| 29 | + * Returns the billing status. If it's currently unknown, requests to check |
| 30 | + * if billing is supported and |
| 31 | + * {@link AbstractBillingActivity#onBillingChecked(boolean)} should be |
| 32 | + * called later with the result. |
| 33 | + * |
| 34 | + * @return the current billing status (unknown, supported or unsupported). |
| 35 | + * @see AbstractBillingActivity#onBillingChecked(boolean) |
| 36 | + */ |
| 37 | + public BillingStatus checkBillingSupported() { |
| 38 | + return BillingController.checkBillingSupported(this); |
| 39 | + } |
| 40 | + |
| 41 | + public abstract void onBillingChecked(boolean supported); |
| 42 | + |
| 43 | + @Override |
| 44 | + protected void onCreate(android.os.Bundle savedInstanceState) { |
| 45 | + super.onCreate(savedInstanceState); |
| 46 | + |
| 47 | + mBillingObserver = new AbstractBillingObserver(this) { |
| 48 | + |
| 49 | + @Override |
| 50 | + public void onBillingChecked(boolean supported) { |
| 51 | + AbstractBillingActivity.this.onBillingChecked(supported); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void onPurchaseStateChanged(String itemId, PurchaseState state) { |
| 56 | + AbstractBillingActivity.this.onPurchaseStateChanged(itemId, state); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void onRequestPurchaseResponse(String itemId, ResponseCode response) { |
| 61 | + AbstractBillingActivity.this.onRequestPurchaseResponse(itemId, response); |
| 62 | + } |
| 63 | + }; |
| 64 | + BillingController.registerObserver(mBillingObserver); |
| 65 | + BillingController.setConfiguration(this); // This activity will provide |
| 66 | + // the public key and salt |
| 67 | + this.checkBillingSupported(); |
| 68 | + if (!mBillingObserver.isTransactionsRestored()) { |
| 69 | + BillingController.restoreTransactions(this); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected void onDestroy() { |
| 75 | + super.onDestroy(); |
| 76 | + BillingController.unregisterObserver(mBillingObserver); // Avoid |
| 77 | + // receiving |
| 78 | + // notifications after |
| 79 | + // destroy |
| 80 | + BillingController.setConfiguration(null); |
| 81 | + } |
| 82 | + |
| 83 | + public abstract void onPurchaseStateChanged(String itemId, PurchaseState state);; |
| 84 | + |
| 85 | + public abstract void onRequestPurchaseResponse(String itemId, ResponseCode response); |
| 86 | + |
| 87 | + /** |
| 88 | + * Requests the purchase of the specified item. The transaction will not be |
| 89 | + * confirmed automatically; such confirmation could be handled in |
| 90 | + * {@link AbstractBillingActivity#onPurchaseExecuted(String)}. If automatic |
| 91 | + * confirmation is preferred use |
| 92 | + * {@link BillingController#requestPurchase(android.content.Context, String, boolean)} |
| 93 | + * instead. |
| 94 | + * |
| 95 | + * @param itemId |
| 96 | + * id of the item to be purchased. |
| 97 | + */ |
| 98 | + public void requestPurchase(String itemId) { |
| 99 | + BillingController.requestPurchase(this, itemId); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Requests to restore all transactions. |
| 104 | + */ |
| 105 | + public void restoreTransactions() { |
| 106 | + BillingController.restoreTransactions(this); |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments