NetGuard/app/src/main/java/eu/faircode/netguard/IAB.java

184 lines
6.4 KiB
Java
Raw Normal View History

2015-11-14 20:25:05 +00:00
package eu.faircode.netguard;
/*
This file is part of NetGuard.
NetGuard is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NetGuard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2015 by Marcel Bokhorst (M66B)
*/
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.android.vending.billing.IInAppBillingService;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class IAB implements ServiceConnection {
private static final String TAG = "Netguard.IAB";
private Context context;
private boolean available = false;
private IInAppBillingService service = null;
2015-11-18 16:56:05 +00:00
private static final int IAB_VERSION = 3;
2015-11-14 20:25:05 +00:00
// adb shell pm clear com.android.vending
// adb shell am start -n eu.faircode.netguard/eu.faircode.netguard.ActivityMain
private static final String SKU_DONATE = "donation";
// private static final String SKU_DONATE = "android.test.purchased";
public static final String ACTION_PURCHASED = "eu.faircode.netguard.IAB";
public IAB(Context context) {
this.context = context;
}
public void bind() {
Log.i(TAG, "Bind");
Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
context.bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);
}
public IntentSender getIntentSender() throws RemoteException {
return (service != null && available ? IABgetIntent(SKU_DONATE) : null);
}
public void unbind() {
if (service != null) {
Log.i(TAG, "Unbind");
context.unbindService(this);
service = null;
}
}
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
Log.i(TAG, "Connected");
try {
service = IInAppBillingService.Stub.asInterface(binder);
if (IABisPurchased(SKU_DONATE)) {
Intent intent = new Intent(ACTION_PURCHASED);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
} else
available = (service != null && IABisAvailable(SKU_DONATE));
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "Disconnected");
service = null;
available = false;
}
private boolean IABisAvailable(String sku) throws RemoteException, JSONException {
// Get available SKUs
ArrayList<String> skuList = new ArrayList<>();
skuList.add(sku);
Bundle query = new Bundle();
query.putStringArrayList("ITEM_ID_LIST", skuList);
2015-11-18 16:56:05 +00:00
Bundle bundle = service.getSkuDetails(IAB_VERSION, context.getPackageName(), "inapp", query);
2015-11-14 20:25:05 +00:00
Log.i(TAG, "getSkuDetails");
Util.logBundle(TAG, bundle);
int response = (bundle == null ? -1 : bundle.getInt("RESPONSE_CODE", -1));
Log.i(TAG, "Response=" + getIABResult(response));
if (response != 0)
return false;
// Check available SKUs
boolean found = false;
ArrayList<String> details = bundle.getStringArrayList("DETAILS_LIST");
if (details != null)
for (String item : details) {
JSONObject object = new JSONObject(item);
if (sku.equals(object.getString("productId"))) {
found = true;
break;
}
}
Log.i(TAG, sku + "=" + found);
return found;
}
private boolean IABisPurchased(String sku) throws RemoteException {
// Get purchases
2015-11-18 16:56:05 +00:00
Bundle bundle = service.getPurchases(IAB_VERSION, context.getPackageName(), "inapp", null);
2015-11-14 20:25:05 +00:00
Log.i(TAG, "getPurchases");
Util.logBundle(TAG, bundle);
int response = (bundle == null ? -1 : bundle.getInt("RESPONSE_CODE", -1));
Log.i(TAG, "Response=" + getIABResult(response));
if (response != 0)
return false;
// Check purchases
ArrayList<String> skus = bundle.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
return (skus != null && skus.contains(sku));
}
private IntentSender IABgetIntent(String sku) throws RemoteException {
2015-11-18 16:56:05 +00:00
Bundle bundle = service.getBuyIntent(IAB_VERSION, context.getPackageName(), sku, "inapp", "");
2015-11-14 20:25:05 +00:00
Log.i(TAG, "getBuyIntent");
Util.logBundle(TAG, bundle);
int response = (bundle == null ? -1 : bundle.getInt("RESPONSE_CODE", -1));
Log.i(TAG, "Response=" + getIABResult(response));
if (response != 0 || !bundle.containsKey("BUY_INTENT"))
return null;
PendingIntent pi = bundle.getParcelable("BUY_INTENT");
return (pi == null ? null : pi.getIntentSender());
}
public static String getIABResult(int responseCode) {
switch (responseCode) {
case 0:
return "OK";
case 1:
return "USER_CANCELED";
case 2:
return "SERVICE_UNAVAILABLE";
case 3:
return "BILLING_UNAVAILABLE";
case 4:
return "ITEM_UNAVAILABLE";
case 5:
return "DEVELOPER_ERROR";
case 6:
return "ERROR";
case 7:
return "ITEM_ALREADY_OWNED";
case 8:
return "ITEM_NOT_OWNED";
default:
return Integer.toString(responseCode);
}
}
}