Skip to content

Commit 10ba25c

Browse files
author
Robot Media
committed
Added default billing observer implementation
1 parent 91a702c commit 10ba25c

File tree

7 files changed

+258
-182
lines changed

7 files changed

+258
-182
lines changed

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

Lines changed: 0 additions & 113 deletions
This file was deleted.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 android.app.Activity;
19+
import android.app.PendingIntent;
20+
import android.content.SharedPreferences;
21+
import android.content.SharedPreferences.Editor;
22+
import android.preference.PreferenceManager;
23+
import net.robotmedia.billing.BillingController;
24+
import net.robotmedia.billing.IBillingObserver;
25+
26+
/**
27+
* Abstract subclass of IBillingObserver that provides default implementations
28+
* for {@link IBillingObserver#onPurchaseIntent(String, PendingIntent)} and
29+
* {@link IBillingObserver#onTransactionsRestored()}.
30+
*
31+
*/
32+
public abstract class AbstractBillingObserver implements IBillingObserver {
33+
34+
protected static final String KEY_TRANSACTIONS_RESTORED = "net.robotmedia.billing.transactionsRestored";
35+
36+
protected Activity activity;
37+
38+
public AbstractBillingObserver(Activity activity) {
39+
this.activity = activity;
40+
}
41+
42+
public boolean isTransactionsRestored() {
43+
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
44+
return preferences.getBoolean(KEY_TRANSACTIONS_RESTORED, false);
45+
}
46+
47+
/**
48+
* Called after requesting the purchase of the specified item. The default
49+
* implementation simply starts the pending intent.
50+
*
51+
* @param itemId
52+
* id of the item whose purchase was requested.
53+
* @param purchaseIntent
54+
* a purchase pending intent for the specified item.
55+
*/
56+
@Override
57+
public void onPurchaseIntent(String itemId, PendingIntent purchaseIntent) {
58+
BillingController.startPurchaseIntent(activity, purchaseIntent, null);
59+
}
60+
61+
@Override
62+
public void onTransactionsRestored() {
63+
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
64+
final Editor editor = preferences.edit();
65+
editor.putBoolean(KEY_TRANSACTIONS_RESTORED, true);
66+
editor.commit();
67+
}
68+
69+
}

AndroidBillingLibrary/src/net/robotmedia/billing/security/DefaultSignatureValidator.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
116
package net.robotmedia.billing.security;
217

318
import java.security.InvalidKeyException;

AndroidBillingLibrary/src/net/robotmedia/billing/security/ISignatureValidator.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
116
package net.robotmedia.billing.security;
217

318
public interface ISignatureValidator {

0 commit comments

Comments
 (0)