Skip to content

Commit 1832d3a

Browse files
author
Robot Media
committed
Refactoring of BillingRequest and RequestCode
BillingRequest was inside its own package and this forced to make public a few BillingController methods that weren't necessary to expose to library users. By moving BillingRequest into the same package than BillingController those methods can become "protected". IBillingObserver implementations will need to update the import of ResponseCode to "import net.robotmedia.billing.BillingRequest.ResponseCode;"
1 parent d6913d7 commit 1832d3a

21 files changed

+286
-302
lines changed

AndroidBillingLibrary/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
package="net.robotmedia.billing"
44
android:versionCode="1"
55
android:versionName="1.0">
6-
<uses-sdk android:minSdkVersion="4" />
6+
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="13"/>
77
<!-- Add this permission to your manifest -->
88
<uses-permission android:name="com.android.vending.BILLING" />
99
<application>

AndroidBillingLibrary/default.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
# project structure.
99

1010
# Project target.
11-
target=android-12
11+
target=android-13
1212
android.library=true

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727

2828
import net.robotmedia.billing.model.Transaction;
2929
import net.robotmedia.billing.model.TransactionManager;
30-
import net.robotmedia.billing.request.BillingRequest;
31-
import net.robotmedia.billing.request.ResponseCode;
3230
import net.robotmedia.billing.security.DefaultSignatureValidator;
3331
import net.robotmedia.billing.security.ISignatureValidator;
3432
import net.robotmedia.billing.utils.Compatibility;
@@ -288,7 +286,7 @@ static void obfuscate(Context context, Transaction purchase) {
288286
*
289287
* @param supported
290288
*/
291-
public static void onBillingChecked(boolean supported) {
289+
protected static void onBillingChecked(boolean supported) {
292290
status = supported ? BillingStatus.SUPPORTED : BillingStatus.UNSUPPORTED;
293291
for (IBillingObserver o : observers) {
294292
o.onBillingChecked(supported);
@@ -318,7 +316,7 @@ protected static void onNotify(Context context, String notifyId) {
318316
* @param purchaseIntent
319317
* intent to purchase the item.
320318
*/
321-
public static void onPurchaseIntent(String itemId, PendingIntent purchaseIntent) {
319+
protected static void onPurchaseIntent(String itemId, PendingIntent purchaseIntent) {
322320
for (IBillingObserver o : observers) {
323321
o.onPurchaseIntent(itemId, purchaseIntent);
324322
}
@@ -389,7 +387,7 @@ protected static void onPurchaseStateChanged(Context context, String signedData,
389387
}
390388

391389
/**
392-
* Called after a {@link net.robotmedia.billing.request.BillingRequest} is
390+
* Called after a {@link net.robotmedia.billing.BillingRequest} is
393391
* sent.
394392
*
395393
* @param requestId
@@ -408,7 +406,7 @@ protected static void onRequestSent(long requestId, BillingRequest request) {
408406
}
409407

410408
/**
411-
* Called after a {@link net.robotmedia.billing.request.BillingRequest} is
409+
* Called after a {@link net.robotmedia.billing.BillingRequest} is
412410
* sent.
413411
*
414412
* @param context
@@ -419,7 +417,7 @@ protected static void onRequestSent(long requestId, BillingRequest request) {
419417
* @see net.robotmedia.billing.request.ResponseCode
420418
*/
421419
protected static void onResponseCode(Context context, long requestId, int responseCode) {
422-
final ResponseCode response = ResponseCode.valueOf(responseCode);
420+
final BillingRequest.ResponseCode response = BillingRequest.ResponseCode.valueOf(responseCode);
423421
debug("Request " + requestId + " received response " + response);
424422

425423
final BillingRequest request = pendingRequests.get(requestId);
@@ -429,7 +427,7 @@ protected static void onResponseCode(Context context, long requestId, int respon
429427
}
430428
}
431429

432-
public static void onTransactionsRestored() {
430+
protected static void onTransactionsRestored() {
433431
for (IBillingObserver o : observers) {
434432
o.onTransactionsRestored();
435433
}
@@ -625,7 +623,7 @@ private static boolean verifyNonce(JSONObject data) {
625623
}
626624
}
627625

628-
public static void onRequestPurchaseResponse(String itemId, ResponseCode response) {
626+
protected static void onRequestPurchaseResponse(String itemId, BillingRequest.ResponseCode response) {
629627
for (IBillingObserver o : observers) {
630628
o.onRequestPurchaseResponse(itemId, response);
631629
}
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
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;
17+
18+
import android.app.PendingIntent;
19+
import android.os.Bundle;
20+
import android.os.RemoteException;
21+
import android.util.Log;
22+
23+
import com.android.vending.billing.IMarketBillingService;
24+
25+
public abstract class BillingRequest {
26+
27+
public static class CheckBillingSupported extends BillingRequest {
28+
29+
public CheckBillingSupported(String packageName) {
30+
super(packageName);
31+
}
32+
33+
@Override
34+
public String getRequestType() {
35+
return "CHECK_BILLING_SUPPORTED";
36+
}
37+
38+
@Override
39+
protected void processOkResponse(Bundle response) {
40+
final boolean supported = this.isSuccess();
41+
BillingController.onBillingChecked(supported);
42+
}
43+
44+
}
45+
public static class ConfirmNotifications extends BillingRequest {
46+
47+
private String[] notifyIds;
48+
49+
private static final String KEY_NOTIFY_IDS = "NOTIFY_IDS";
50+
51+
public ConfirmNotifications(String packageName, String[] notifyIds) {
52+
super(packageName);
53+
this.notifyIds = notifyIds;
54+
}
55+
56+
@Override
57+
protected void addParams(Bundle request) {
58+
request.putStringArray(KEY_NOTIFY_IDS, notifyIds);
59+
}
60+
61+
@Override
62+
public String getRequestType() {
63+
return "CONFIRM_NOTIFICATIONS";
64+
}
65+
66+
}
67+
public static class GetPurchaseInformation extends BillingRequest {
68+
69+
private String[] notifyIds;
70+
71+
private static final String KEY_NOTIFY_IDS = "NOTIFY_IDS";
72+
73+
public GetPurchaseInformation(String packageName, String[] notifyIds) {
74+
super(packageName);
75+
this.notifyIds = notifyIds;
76+
}
77+
78+
@Override
79+
protected void addParams(Bundle request) {
80+
request.putStringArray(KEY_NOTIFY_IDS, notifyIds);
81+
}
82+
83+
@Override
84+
public String getRequestType() {
85+
return "GET_PURCHASE_INFORMATION";
86+
}
87+
88+
@Override public boolean hasNonce() { return true; }
89+
90+
}
91+
public static class RequestPurchase extends BillingRequest {
92+
93+
private String itemId;
94+
private String developerPayload;
95+
96+
private static final String KEY_ITEM_ID = "ITEM_ID";
97+
private static final String KEY_DEVELOPER_PAYLOAD = "DEVELOPER_PAYLOAD";
98+
private static final String KEY_PURCHASE_INTENT = "PURCHASE_INTENT";
99+
100+
public RequestPurchase(String packageName, String itemId, String developerPayload) {
101+
super(packageName);
102+
this.itemId = itemId;
103+
this.developerPayload = developerPayload;
104+
}
105+
106+
@Override
107+
protected void addParams(Bundle request) {
108+
request.putString(KEY_ITEM_ID, itemId);
109+
if (developerPayload != null) {
110+
request.putString(KEY_DEVELOPER_PAYLOAD, developerPayload);
111+
}
112+
}
113+
114+
@Override
115+
public String getRequestType() {
116+
return "REQUEST_PURCHASE";
117+
}
118+
119+
@Override
120+
public void onResponseCode(ResponseCode response) {
121+
super.onResponseCode(response);
122+
BillingController.onRequestPurchaseResponse(itemId, response);
123+
}
124+
125+
@Override
126+
protected void processOkResponse(Bundle response) {
127+
final PendingIntent purchaseIntent = response.getParcelable(KEY_PURCHASE_INTENT);
128+
BillingController.onPurchaseIntent(itemId, purchaseIntent);
129+
}
130+
131+
132+
}
133+
public static enum ResponseCode {
134+
RESULT_OK, // 0
135+
RESULT_USER_CANCELED, // 1
136+
RESULT_SERVICE_UNAVAILABLE, // 2
137+
RESULT_BILLING_UNAVAILABLE, // 3
138+
RESULT_ITEM_UNAVAILABLE, // 4
139+
RESULT_DEVELOPER_ERROR, // 5
140+
RESULT_ERROR; // 6
141+
142+
public static boolean isResponseOk(int response) {
143+
return ResponseCode.RESULT_OK.ordinal() == response;
144+
}
145+
146+
// Converts from an ordinal value to the ResponseCode
147+
public static ResponseCode valueOf(int index) {
148+
ResponseCode[] values = ResponseCode.values();
149+
if (index < 0 || index >= values.length) {
150+
return RESULT_ERROR;
151+
}
152+
return values[index];
153+
}
154+
}
155+
public static class RestoreTransactions extends BillingRequest {
156+
157+
public RestoreTransactions(String packageName) {
158+
super(packageName);
159+
}
160+
161+
@Override
162+
public String getRequestType() {
163+
return "RESTORE_TRANSACTIONS";
164+
}
165+
166+
@Override public boolean hasNonce() { return true; }
167+
168+
@Override
169+
public void onResponseCode(ResponseCode response) {
170+
super.onResponseCode(response);
171+
if (response == ResponseCode.RESULT_OK) {
172+
BillingController.onTransactionsRestored();
173+
}
174+
}
175+
176+
}
177+
private static final String KEY_BILLING_REQUEST = "BILLING_REQUEST";
178+
179+
private static final String KEY_API_VERSION = "API_VERSION";
180+
private static final String KEY_PACKAGE_NAME = "PACKAGE_NAME";
181+
private static final String KEY_RESPONSE_CODE = "RESPONSE_CODE";
182+
183+
protected static final String KEY_REQUEST_ID = "REQUEST_ID";
184+
185+
private static final String KEY_NONCE = "NONCE";
186+
187+
public static final long IGNORE_REQUEST_ID = -1;
188+
private String packageName;
189+
190+
private boolean success;
191+
private long nonce;
192+
public BillingRequest(String packageName) {
193+
this.packageName = packageName;
194+
}
195+
196+
protected void addParams(Bundle request) {
197+
// Do nothing by default
198+
}
199+
200+
public long getNonce() {
201+
return nonce;
202+
}
203+
204+
public abstract String getRequestType();
205+
206+
public boolean hasNonce() {
207+
return false;
208+
}
209+
210+
public boolean isSuccess() {
211+
return success;
212+
}
213+
214+
protected Bundle makeRequestBundle() {
215+
final Bundle request = new Bundle();
216+
request.putString(KEY_BILLING_REQUEST, getRequestType());
217+
request.putInt(KEY_API_VERSION, 1);
218+
request.putString(KEY_PACKAGE_NAME, packageName);
219+
if (hasNonce()) {
220+
request.putLong(KEY_NONCE, nonce);
221+
}
222+
return request;
223+
}
224+
225+
public void onResponseCode(ResponseCode responde) {
226+
// Do nothing by default
227+
}
228+
229+
protected void processOkResponse(Bundle response) {
230+
// Do nothing by default
231+
}
232+
233+
public long run(IMarketBillingService mService) throws RemoteException {
234+
final Bundle request = makeRequestBundle();
235+
addParams(request);
236+
final Bundle response = mService.sendBillingRequest(request);
237+
if (validateResponse(response)) {
238+
processOkResponse(response);
239+
return response.getLong(KEY_REQUEST_ID, IGNORE_REQUEST_ID);
240+
} else {
241+
return IGNORE_REQUEST_ID;
242+
}
243+
}
244+
245+
public void setNonce(long nonce) {
246+
this.nonce = nonce;
247+
}
248+
249+
protected boolean validateResponse(Bundle response) {
250+
final int responseCode = response.getInt(KEY_RESPONSE_CODE);
251+
success = ResponseCode.isResponseOk(responseCode);
252+
if (!success) {
253+
Log.w(this.getClass().getSimpleName(), "Error with response code " + ResponseCode.valueOf(responseCode));
254+
}
255+
return success;
256+
}
257+
258+
}

0 commit comments

Comments
 (0)