Skip to content

Commit cec690d

Browse files
author
Robot Media
committed
Closes robotmedia#22.
1 parent 3a5e5f9 commit cec690d

File tree

6 files changed

+31
-20
lines changed

6 files changed

+31
-20
lines changed

AndroidBillingLibrary/src/net/robotmedia/billing/AbstractBillingActivity.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
public abstract class AbstractBillingActivity extends Activity implements IBillingObserver, BillingController.IConfiguration {
2626

27-
private static final String PREFERENCE_SUBSEQUENT_SESSION = "net.robotmedia.billing.AbstractBillingActivity.subsequentSession";;
27+
private static final String KEY_TRANSACTIONS_RESTORED = "net.robotmedia.billing.AbstractBillingActivity.transactionsRestored";
2828

2929
/**
3030
* Returns the billing status. If it's currently unknown, requests to check
@@ -39,15 +39,11 @@ public BillingStatus checkBillingSupported() {
3939
return BillingController.checkBillingSupported(this);
4040
}
4141

42-
private boolean isSubsequentSession() {
42+
private boolean isTransactionsRestored() {
4343
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
44-
final boolean subsequentSession = preferences.getBoolean(PREFERENCE_SUBSEQUENT_SESSION, false);
45-
final Editor editor = preferences.edit();
46-
editor.putBoolean(PREFERENCE_SUBSEQUENT_SESSION, true);
47-
editor.commit();
48-
return subsequentSession;
44+
return preferences.getBoolean(KEY_TRANSACTIONS_RESTORED, false);
4945
}
50-
46+
5147
@Override
5248
protected void onCreate(android.os.Bundle savedInstanceState) {
5349
super.onCreate(savedInstanceState);
@@ -56,7 +52,7 @@ protected void onCreate(android.os.Bundle savedInstanceState) {
5652
BillingController.setConfiguration(this); // This activity will provide
5753
// the public key and salt
5854
this.checkBillingSupported();
59-
if (!isSubsequentSession()) {
55+
if (!isTransactionsRestored()) {
6056
BillingController.restoreTransactions(this);
6157
}
6258
};
@@ -84,6 +80,14 @@ public void onPurchaseIntent(String itemId, PendingIntent purchaseIntent) {
8480
BillingController.startPurchaseIntent(this, purchaseIntent, null);
8581
}
8682

83+
@Override
84+
public void onTransactionsRestored() {
85+
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
86+
final Editor editor = preferences.edit();
87+
editor.putBoolean(KEY_TRANSACTIONS_RESTORED, true);
88+
editor.commit();
89+
}
90+
8791
/**
8892
* Requests the purchase of the specified item. The transaction will not be
8993
* confirmed automatically; such confirmation could be handled in
@@ -98,7 +102,7 @@ public void onPurchaseIntent(String itemId, PendingIntent purchaseIntent) {
98102
public void requestPurchase(String itemId) {
99103
BillingController.requestPurchase(this, itemId);
100104
}
101-
105+
102106
/**
103107
* Requests to restore all transactions.
104108
*/

AndroidBillingLibrary/src/net/robotmedia/billing/BillingController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public static int countPurchases(Context context, String itemId) {
172172
* @return number of net purchases for the specified item.
173173
*/
174174
public static int countPurchasesNet(Context context, String itemId) {
175-
final List<Transaction> transactions = BillingController.getTransactions(context);
175+
final List<Transaction> transactions = BillingController.getTransactions(context, itemId);
176176
int count = 0;
177177
for (Transaction t : transactions) {
178178
switch (t.purchaseState) {

AndroidBillingLibraryTest/src/net/robotmedia/billing/BillingControllerTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public void testIsPurchasedNet() throws Exception {
6161
assertTrue(BillingController.isPurchasedNet(getContext(), TransactionTest.TRANSACTION_1.productId));
6262
BillingController.storeTransaction(getContext(), TransactionTest.TRANSACTION_1_REFUNDED);
6363
assertFalse(BillingController.isPurchasedNet(getContext(), TransactionTest.TRANSACTION_1.productId));
64+
BillingController.storeTransaction(getContext(), TransactionTest.TRANSACTION_2);
65+
assertFalse(BillingController.isPurchasedNet(getContext(), TransactionTest.TRANSACTION_1.productId));
6466
}
6567

6668
@MediumTest
@@ -70,6 +72,8 @@ public void testCountPurchases() throws Exception {
7072
assertEquals(BillingController.countPurchases(getContext(), TransactionTest.TRANSACTION_1.productId), 1);
7173
BillingController.storeTransaction(getContext(), TransactionTest.TRANSACTION_1_REFUNDED);
7274
assertEquals(BillingController.countPurchases(getContext(), TransactionTest.TRANSACTION_1.productId), 1);
75+
BillingController.storeTransaction(getContext(), TransactionTest.TRANSACTION_2);
76+
assertEquals(BillingController.countPurchases(getContext(), TransactionTest.TRANSACTION_1.productId), 1);
7377
}
7478

7579
@MediumTest
@@ -79,6 +83,8 @@ public void testCountPurchasesNet() throws Exception {
7983
assertEquals(BillingController.countPurchasesNet(getContext(), TransactionTest.TRANSACTION_1.productId), 1);
8084
BillingController.storeTransaction(getContext(), TransactionTest.TRANSACTION_1_REFUNDED);
8185
assertEquals(BillingController.countPurchasesNet(getContext(), TransactionTest.TRANSACTION_1.productId), 0);
86+
BillingController.storeTransaction(getContext(), TransactionTest.TRANSACTION_2);
87+
assertEquals(BillingController.countPurchasesNet(getContext(), TransactionTest.TRANSACTION_1.productId), 0);
8288
}
8389

8490
@MediumTest
@@ -88,7 +94,7 @@ public void testGetTransactions() throws Exception {
8894
BillingController.storeTransaction(getContext(), TransactionTest.TRANSACTION_1);
8995
final List<Transaction> transactions1 = BillingController.getTransactions(getContext());
9096
assertEquals(transactions1.size(), 1);
91-
BillingController.storeTransaction(getContext(), TransactionTest.TRANSACTION_2);
97+
BillingController.storeTransaction(getContext(), TransactionTest.TRANSACTION_2_REFUNDED);
9298
final List<Transaction> transactions2 = BillingController.getTransactions(getContext());
9399
assertEquals(transactions2.size(), 2);
94100
}
@@ -100,7 +106,7 @@ public void testGetTransactionsString() throws Exception {
100106
BillingController.storeTransaction(getContext(), TransactionTest.TRANSACTION_1);
101107
final List<Transaction> transactions1 = BillingController.getTransactions(getContext(), TransactionTest.TRANSACTION_1.productId);
102108
assertEquals(transactions1.size(), 1);
103-
BillingController.storeTransaction(getContext(), TransactionTest.TRANSACTION_2);
109+
BillingController.storeTransaction(getContext(), TransactionTest.TRANSACTION_2_REFUNDED);
104110
final List<Transaction> transactions2 = BillingController.getTransactions(getContext(), TransactionTest.TRANSACTION_1.productId);
105111
assertEquals(transactions2.size(), 1);
106112
}

AndroidBillingLibraryTest/src/net/robotmedia/billing/model/BillingDBTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void testQueryTransactions() throws Exception {
8484
TransactionTest.assertEquals(TransactionTest.TRANSACTION_1, stored);
8585
cursor2.close();
8686

87-
mData.insert(TransactionTest.TRANSACTION_2);
87+
mData.insert(TransactionTest.TRANSACTION_2_REFUNDED);
8888
final Cursor cursor3 = mData.queryTransactions();
8989
assertEquals(cursor3.getCount(), 2);
9090
cursor3.close();
@@ -106,7 +106,7 @@ public void testQueryTransactionsString() throws Exception {
106106
TransactionTest.assertEquals(TransactionTest.TRANSACTION_1, stored);
107107
cursor2.close();
108108

109-
mData.insert(TransactionTest.TRANSACTION_2);
109+
mData.insert(TransactionTest.TRANSACTION_2_REFUNDED);
110110
final Cursor cursor3 = mData.queryTransactions(TransactionTest.TRANSACTION_1.productId);
111111
assertEquals(cursor3.getCount(), 1);
112112
cursor3.close();
@@ -119,7 +119,7 @@ public void testQueryTransactionsStringPurchaseState() throws Exception {
119119
cursor1.close();
120120

121121
mData.insert(TransactionTest.TRANSACTION_1);
122-
mData.insert(TransactionTest.TRANSACTION_2);
122+
mData.insert(TransactionTest.TRANSACTION_2_REFUNDED);
123123
final Cursor cursor2 = mData.queryTransactions(TransactionTest.TRANSACTION_1.productId, TransactionTest.TRANSACTION_1.purchaseState);
124124
assertEquals(cursor2.getCount(), 1);
125125
cursor2.moveToNext();

AndroidBillingLibraryTest/src/net/robotmedia/billing/model/TransactionManagerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void testGetTransactions() throws Exception {
6464
TransactionManager.addTransaction(getContext(), TransactionTest.TRANSACTION_1);
6565
final List<Transaction> transactions2 = TransactionManager.getTransactions(getContext());
6666
assertEquals(transactions2.size(), 1);
67-
TransactionManager.addTransaction(getContext(), TransactionTest.TRANSACTION_2);
67+
TransactionManager.addTransaction(getContext(), TransactionTest.TRANSACTION_2_REFUNDED);
6868
final List<Transaction> transactions3 = TransactionManager.getTransactions(getContext());
6969
assertEquals(transactions3.size(), 2);
7070
}
@@ -76,7 +76,7 @@ public void testGetTransactionsString() throws Exception {
7676
TransactionManager.addTransaction(getContext(), TransactionTest.TRANSACTION_1);
7777
final List<Transaction> transactions2 = TransactionManager.getTransactions(getContext(), TransactionTest.TRANSACTION_1.productId);
7878
assertEquals(transactions2.size(), 1);
79-
TransactionManager.addTransaction(getContext(), TransactionTest.TRANSACTION_2);
79+
TransactionManager.addTransaction(getContext(), TransactionTest.TRANSACTION_2_REFUNDED);
8080
final List<Transaction> transactions3 = TransactionManager.getTransactions(getContext(), TransactionTest.TRANSACTION_1.productId);
8181
assertEquals(transactions3.size(), 1);
8282
}

AndroidBillingLibraryTest/src/net/robotmedia/billing/model/TransactionTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
public class TransactionTest extends TestCase {
2727

2828
public static final Transaction TRANSACTION_1 = new Transaction("order1", "android.test.purchased", "com.example", Transaction.PurchaseState.PURCHASED, "notificationId", new Date().getTime(), "developerPayload");
29-
public static final Transaction TRANSACTION_2 = new Transaction("order2", "android.test.refunded", "com.example", Transaction.PurchaseState.REFUNDED, "notificationId", new Date().getTime(), "developerPayload");
29+
public static final Transaction TRANSACTION_2 = new Transaction("order2", "product_2", "com.example", Transaction.PurchaseState.PURCHASED, "notificationId", new Date().getTime(), "developerPayload");
30+
public static final Transaction TRANSACTION_2_REFUNDED = new Transaction("order4", "product_2", "com.example", Transaction.PurchaseState.REFUNDED, "notificationId", new Date().getTime(), "developerPayload");
3031
public static final Transaction TRANSACTION_1_REFUNDED = new Transaction("order3", "android.test.purchased", "com.example", Transaction.PurchaseState.REFUNDED, "notificationId", new Date().getTime(), "developerPayload");
3132
public static void assertEquals(Transaction a, Transaction b) {
3233
assertTrue(a.equals(b));
@@ -74,7 +75,7 @@ public void testPurchaseStateOrdinal() throws Exception {
7475
public void testEquals() throws Exception {
7576
assertTrue(TRANSACTION_1.equals(TRANSACTION_1));
7677
assertTrue(TRANSACTION_1.equals(TRANSACTION_1.clone()));
77-
assertFalse(TRANSACTION_1.equals(TRANSACTION_2));
78+
assertFalse(TRANSACTION_1.equals(TRANSACTION_2_REFUNDED));
7879
}
7980

8081
@SmallTest

0 commit comments

Comments
 (0)