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