From ea9fa065a9ed5aca4f57a006de28b1f56a5c3cd4 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 30 Dec 2015 09:00:34 +0100 Subject: [PATCH] Removed donate, refactored IAB --- .../eu/faircode/netguard/ActivityMain.java | 88 +--------- .../main/java/eu/faircode/netguard/IAB.java | 150 ++++++++---------- app/src/main/res/layout/about.xml | 49 +----- app/src/main/res/values-af/strings.xml | 3 - app/src/main/res/values-ar/strings.xml | 3 - app/src/main/res/values-bg/strings.xml | 3 - app/src/main/res/values-ca/strings.xml | 3 - app/src/main/res/values-cs/strings.xml | 3 - app/src/main/res/values-da/strings.xml | 3 - app/src/main/res/values-de/strings.xml | 3 - app/src/main/res/values-el/strings.xml | 3 - app/src/main/res/values-en/strings.xml | 3 - app/src/main/res/values-es/strings.xml | 3 - app/src/main/res/values-et/strings.xml | 3 - app/src/main/res/values-eu/strings.xml | 3 - app/src/main/res/values-fi/strings.xml | 3 - app/src/main/res/values-fr/strings.xml | 4 - app/src/main/res/values-he/strings.xml | 3 - app/src/main/res/values-hr/strings.xml | 3 - app/src/main/res/values-hu/strings.xml | 3 - app/src/main/res/values-it/strings.xml | 3 - app/src/main/res/values-ja/strings.xml | 3 - app/src/main/res/values-ko/strings.xml | 3 - app/src/main/res/values-lt/strings.xml | 3 - app/src/main/res/values-lv/strings.xml | 3 - app/src/main/res/values-nl/strings.xml | 3 - app/src/main/res/values-no/strings.xml | 3 - app/src/main/res/values-pl/strings.xml | 3 - app/src/main/res/values-pt-rBR/strings.xml | 3 - app/src/main/res/values-pt-rPT/strings.xml | 3 - app/src/main/res/values-ro/strings.xml | 3 - app/src/main/res/values-ru/strings.xml | 3 - app/src/main/res/values-sk/strings.xml | 3 - app/src/main/res/values-sl/strings.xml | 3 - app/src/main/res/values-sr/strings.xml | 3 - app/src/main/res/values-sv-rSE/strings.xml | 3 - app/src/main/res/values-tr/strings.xml | 3 - app/src/main/res/values-uk/strings.xml | 3 - app/src/main/res/values-vi/strings.xml | 3 - app/src/main/res/values-zh-rCN/strings.xml | 3 - app/src/main/res/values-zh-rTW/strings.xml | 3 - app/src/main/res/values/strings.xml | 3 - strings.sh | 11 +- 43 files changed, 77 insertions(+), 339 deletions(-) diff --git a/app/src/main/java/eu/faircode/netguard/ActivityMain.java b/app/src/main/java/eu/faircode/netguard/ActivityMain.java index 639aee47..8cf14674 100644 --- a/app/src/main/java/eu/faircode/netguard/ActivityMain.java +++ b/app/src/main/java/eu/faircode/netguard/ActivityMain.java @@ -20,7 +20,6 @@ package eu.faircode.netguard; */ import android.app.AlertDialog; -import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; @@ -69,10 +68,9 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences private AlertDialog dialogAbout = null; private static final int REQUEST_VPN = 1; - private static final int REQUEST_IAB = 2; - private static final int REQUEST_INVITE = 3; - private static final int REQUEST_LOGCAT = 4; - public static final int REQUEST_ROAMING = 5; + private static final int REQUEST_INVITE = 2; + private static final int REQUEST_LOGCAT = 3; + public static final int REQUEST_ROAMING = 4; private static final int MIN_SDK = Build.VERSION_CODES.LOLLIPOP; @@ -301,14 +299,6 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences if (resultCode == RESULT_OK) SinkholeService.start("prepared", this); - } else if (requestCode == REQUEST_IAB) { - // Handle IAB result - Intent intent = new Intent(IAB.ACTION_IAB); - intent.putExtra("RESULT_CODE", resultCode); - if (data != null) - intent.putExtra("RESPONSE_CODE", data.getIntExtra("RESPONSE_CODE", -1)); - LocalBroadcastManager.getInstance(this).sendBroadcast(intent); - } else if (requestCode == REQUEST_INVITE) { // Do nothing @@ -575,9 +565,6 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences View view = inflater.inflate(R.layout.about, null); TextView tvVersion = (TextView) view.findViewById(R.id.tvVersion); Button btnRate = (Button) view.findViewById(R.id.btnRate); - final Button btnDonate = (Button) view.findViewById(R.id.btnDonate); - final TextView tvThanks = (TextView) view.findViewById(R.id.tvThanks); - TextView tvTerms = (TextView) view.findViewById(R.id.tvTerms); TextView tvLicense = (TextView) view.findViewById(R.id.tvLicense); // Show version @@ -585,8 +572,7 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences if (!Util.hasValidFingerprint(this)) tvVersion.setTextColor(Color.GRAY); - // Handle terms/license - tvTerms.setMovementMethod(LinkMovementMethod.getInstance()); + // Handle license tvLicense.setMovementMethod(LinkMovementMethod.getInstance()); // Handle logcat @@ -621,63 +607,6 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences } }); - // In-app billing - final IAB iab = new IAB(this); - - // Handle donate - btnDonate.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - try { - PendingIntent pi = iab.getIntentSender(); - if (pi == null) { - Log.i(TAG, "Donate"); - Intent donate = new Intent(Intent.ACTION_VIEW); - donate.setData(Uri.parse("http://www.netguard.me/")); - startActivity(donate); - } else { - btnDonate.setEnabled(false); - Log.i(TAG, "IAB donate"); - startIntentSenderForResult(pi.getIntentSender(), REQUEST_IAB, new Intent(), 0, 0, 0); - } - } catch (Throwable ex) { - Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); - Util.sendCrashReport(ex, ActivityMain.this); - } - } - }); - - // Handle IAB result - final BroadcastReceiver onIABResult = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - int resultCode = intent.getIntExtra("RESULT_CODE", RESULT_CANCELED); - int responseCode = intent.getIntExtra("RESPONSE_CODE", -1); - final boolean ok = (resultCode == RESULT_OK); - Log.i(TAG, "IAB result ok=" + ok + " response=" + IAB.getResult(responseCode)); - - runOnUiThread(new Runnable() { - @Override - public void run() { - if (running) { - btnDonate.setEnabled(true); - if (ok) { - btnDonate.setVisibility(View.GONE); - tvThanks.setVisibility(View.VISIBLE); - } else { - Intent donate = new Intent(Intent.ACTION_VIEW); - donate.setData(Uri.parse("http://www.netguard.me/")); - if (donate.resolveActivity(getPackageManager()) != null) - startActivity(donate); - } - } - } - }); - } - }; - IntentFilter iff = new IntentFilter(IAB.ACTION_IAB); - LocalBroadcastManager.getInstance(this).registerReceiver(onIABResult, iff); - // Show dialog dialogAbout = new AlertDialog.Builder(this) .setView(view) @@ -685,20 +614,11 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences .setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialogInterface) { - if (running) - LocalBroadcastManager.getInstance(ActivityMain.this).unregisterReceiver(onIABResult); - - iab.unbind(); - dialogAbout = null; } }) .create(); dialogAbout.show(); - - // Connect to billing - if (Util.hasValidFingerprint(this)) - iab.bind(); } diff --git a/app/src/main/java/eu/faircode/netguard/IAB.java b/app/src/main/java/eu/faircode/netguard/IAB.java index 28af3197..23b8ef9e 100644 --- a/app/src/main/java/eu/faircode/netguard/IAB.java +++ b/app/src/main/java/eu/faircode/netguard/IAB.java @@ -28,7 +28,6 @@ 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; @@ -42,18 +41,23 @@ public class IAB implements ServiceConnection { private static final String TAG = "Netguard.IAB"; private Context context; - private boolean available = false; + private Delegate delegate; private IInAppBillingService service = null; private static final int IAB_VERSION = 3; // adb shell pm clear com.android.vending // adb shell am start -n eu.faircode.netguard/eu.faircode.netguard.ActivityMain + private static final String SKU_PRO = "pro"; private static final String SKU_DONATE = "donation"; // private static final String SKU_DONATE = "android.test.purchased"; - public static final String ACTION_IAB = "eu.faircode.netguard.IAB"; - public IAB(Context context) { + public interface Delegate { + void onReady(); + } + + public IAB(Delegate delegate, Context context) { this.context = context; + this.delegate = delegate; } public void bind() { @@ -68,10 +72,6 @@ public class IAB implements ServiceConnection { } } - public PendingIntent getIntentSender() throws RemoteException { - return (service != null && available ? getBuyIntent(SKU_DONATE) : null); - } - public void unbind() { if (service != null) try { @@ -89,14 +89,7 @@ public class IAB implements ServiceConnection { Log.i(TAG, "Connected"); try { service = IInAppBillingService.Stub.asInterface(binder); - - if (isPurchased(SKU_DONATE)) { - Intent intent = new Intent(ACTION_IAB); - intent.putExtra("RESULT_CODE", Activity.RESULT_OK); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); - } else - available = (service != null && isAvailable(SKU_DONATE)); - + delegate.onReady(); } catch (Throwable ex) { Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); Util.sendCrashReport(ex, context); @@ -107,83 +100,64 @@ public class IAB implements ServiceConnection { public void onServiceDisconnected(ComponentName name) { Log.i(TAG, "Disconnected"); service = null; - available = false; } - private boolean isAvailable(String sku) throws RemoteException, JSONException { - try { - // Get available SKUs - ArrayList skuList = new ArrayList<>(); - skuList.add(sku); - Bundle query = new Bundle(); - query.putStringArrayList("ITEM_ID_LIST", skuList); - Bundle bundle = service.getSkuDetails(IAB_VERSION, context.getPackageName(), "inapp", query); - Log.i(TAG, "getSkuDetails"); - Util.logBundle(bundle); - int response = (bundle == null ? -1 : bundle.getInt("RESPONSE_CODE", -1)); - Log.i(TAG, "Response=" + getResult(response)); - if (response != 0) - return false; + public boolean isAvailable(String sku) throws RemoteException, JSONException { + // Get available SKUs + ArrayList skuList = new ArrayList<>(); + skuList.add(sku); + Bundle query = new Bundle(); + query.putStringArrayList("ITEM_ID_LIST", skuList); + Bundle bundle = service.getSkuDetails(IAB_VERSION, context.getPackageName(), "inapp", query); + Log.i(TAG, "getSkuDetails"); + Util.logBundle(bundle); + int response = (bundle == null ? -1 : bundle.getInt("RESPONSE_CODE", -1)); + Log.i(TAG, "Response=" + getResult(response)); + if (response != 0) + throw new IllegalArgumentException(getResult(response)); - // Check available SKUs - boolean found = false; - ArrayList 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; - } + // Check available SKUs + boolean found = false; + ArrayList 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; - } catch (Throwable ex) { - Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); - Util.sendCrashReport(ex, context); - return false; - } - } - - private boolean isPurchased(String sku) throws RemoteException { - try { - // Get purchases - Bundle bundle = service.getPurchases(IAB_VERSION, context.getPackageName(), "inapp", null); - Log.i(TAG, "getPurchases"); - Util.logBundle(bundle); - int response = (bundle == null ? -1 : bundle.getInt("RESPONSE_CODE", -1)); - Log.i(TAG, "Response=" + getResult(response)); - if (response != 0) - return false; - - // Check purchases - ArrayList skus = bundle.getStringArrayList("INAPP_PURCHASE_ITEM_LIST"); - return (skus != null && skus.contains(sku)); - } catch (Throwable ex) { - Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); - Util.sendCrashReport(ex, context); - return false; - } - } - - private PendingIntent getBuyIntent(String sku) throws RemoteException { - try { - Bundle bundle = service.getBuyIntent(IAB_VERSION, context.getPackageName(), sku, "inapp", "netguard"); - Log.i(TAG, "getBuyIntent"); - Util.logBundle(bundle); - int response = (bundle == null ? -1 : bundle.getInt("RESPONSE_CODE", -1)); - Log.i(TAG, "Response=" + getResult(response)); - if (response != 0 || !bundle.containsKey("BUY_INTENT")) { - Util.sendCrashReport(new IllegalStateException(getResult(response)), context); - return null; } - return bundle.getParcelable("BUY_INTENT"); - } catch (Throwable ex) { - Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); - Util.sendCrashReport(ex, context); - return null; - } + Log.i(TAG, sku + "=" + found); + + return found; + } + + public boolean isPurchased(String sku) throws RemoteException { + // Get purchases + Bundle bundle = service.getPurchases(IAB_VERSION, context.getPackageName(), "inapp", null); + Log.i(TAG, "getPurchases"); + Util.logBundle(bundle); + int response = (bundle == null ? -1 : bundle.getInt("RESPONSE_CODE", -1)); + Log.i(TAG, "Response=" + getResult(response)); + if (response != 0) + throw new IllegalArgumentException(getResult(response)); + + // Check purchases + ArrayList skus = bundle.getStringArrayList("INAPP_PURCHASE_ITEM_LIST"); + return (skus != null && skus.contains(sku)); + } + + public PendingIntent getBuyIntent(String sku) throws RemoteException { + Bundle bundle = service.getBuyIntent(IAB_VERSION, context.getPackageName(), sku, "inapp", "netguard"); + Log.i(TAG, "getBuyIntent"); + Util.logBundle(bundle); + int response = (bundle == null ? -1 : bundle.getInt("RESPONSE_CODE", -1)); + Log.i(TAG, "Response=" + getResult(response)); + if (response != 0) + throw new IllegalArgumentException(getResult(response)); + if (!bundle.containsKey("BUY_INTENT")) + throw new IllegalArgumentException("BUY_INTENT missing"); + return bundle.getParcelable("BUY_INTENT"); } public static String getResult(int responseCode) { diff --git a/app/src/main/res/layout/about.xml b/app/src/main/res/layout/about.xml index 9ea23f5a..ba7117ed 100644 --- a/app/src/main/res/layout/about.xml +++ b/app/src/main/res/layout/about.xml @@ -55,56 +55,13 @@ android:layout_height="wrap_content" android:orientation="vertical"> - - -