From b1fcc24c26dde49bda41a69d1b76e6ef10fa99dc Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 31 Dec 2015 16:55:39 +0100 Subject: [PATCH] Challenge/response, refactoring --- .../eu/faircode/netguard/ActivityMain.java | 2 +- .../eu/faircode/netguard/ActivityPro.java | 44 +++++++++++-------- .../main/java/eu/faircode/netguard/IAB.java | 37 ++++++++++------ .../main/java/eu/faircode/netguard/Util.java | 8 ++-- app/src/main/res/values/strings.xml | 2 +- 5 files changed, 53 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/eu/faircode/netguard/ActivityMain.java b/app/src/main/java/eu/faircode/netguard/ActivityMain.java index d1ca34aa..8b367e8c 100644 --- a/app/src/main/java/eu/faircode/netguard/ActivityMain.java +++ b/app/src/main/java/eu/faircode/netguard/ActivityMain.java @@ -254,7 +254,7 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences @Override public void onReady(IAB iab) { try { - iab.isPurchased(ActivityPro.SKU_DONATION); + iab.updatePurchases(); iab.unbind(); } catch (Throwable ex) { Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); diff --git a/app/src/main/java/eu/faircode/netguard/ActivityPro.java b/app/src/main/java/eu/faircode/netguard/ActivityPro.java index db8b5fbf..ad96825c 100644 --- a/app/src/main/java/eu/faircode/netguard/ActivityPro.java +++ b/app/src/main/java/eu/faircode/netguard/ActivityPro.java @@ -65,34 +65,39 @@ public class ActivityPro extends AppCompatActivity { getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); + // Initial state updateState(); + // Challenge TextView tvChallenge = (TextView) findViewById(R.id.tvChallenge); tvChallenge.setText(Build.SERIAL); - EditText etResponse = (EditText) findViewById(R.id.etResponse); - etResponse.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - // Do nothing - } - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - // Do nothing - } + // Response + try { + final String response = Util.md5(Build.SERIAL, "NetGuard"); + EditText etResponse = (EditText) findViewById(R.id.etResponse); + etResponse.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + // Do nothing + } - @Override - public void afterTextChanged(Editable editable) { - try { - if (Util.sha256(Build.SERIAL, "").equals(editable.toString())) { + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + // Do nothing + } + + @Override + public void afterTextChanged(Editable editable) { + if (response.equals(editable.toString().toUpperCase())) { IAB.setBought(SKU_DONATION, ActivityPro.this); updateState(); } - } catch (Throwable ex) { - Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); } - } - }); + }); + } catch (Throwable ex) { + Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); + } try { iab = new IAB(new IAB.Delegate() { @@ -100,7 +105,8 @@ public class ActivityPro extends AppCompatActivity { public void onReady(final IAB iab) { Log.i(TAG, "IAB ready"); try { - iab.isPurchased(SKU_DONATION); + iab.updatePurchases(); + updateState(); final Button btnSelect = (Button) findViewById(R.id.btnSelect); final Button btnNotify = (Button) findViewById(R.id.btnNotify); diff --git a/app/src/main/java/eu/faircode/netguard/IAB.java b/app/src/main/java/eu/faircode/netguard/IAB.java index 7b243118..55953583 100644 --- a/app/src/main/java/eu/faircode/netguard/IAB.java +++ b/app/src/main/java/eu/faircode/netguard/IAB.java @@ -36,6 +36,7 @@ import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; +import java.util.List; public class IAB implements ServiceConnection { private static final String TAG = "Netguard.IAB"; @@ -113,7 +114,27 @@ public class IAB implements ServiceConnection { return found; } + public void updatePurchases() throws RemoteException { + // Get purchases + List skus = getPurchases(); + + SharedPreferences prefs = context.getSharedPreferences("IAB", Context.MODE_PRIVATE); + SharedPreferences.Editor editor = prefs.edit(); + for (String product : prefs.getAll().keySet()) + //if (!ActivityPro.SKU_DONATION.equals(product)) + editor.remove(product); + for (String sku : skus) { + Log.i(TAG, "SKU=" + sku); + editor.putBoolean(sku, true); + } + editor.apply(); + } + public boolean isPurchased(String sku) throws RemoteException { + return getPurchases().contains(sku); + } + + public List getPurchases() throws RemoteException { // Get purchases Bundle bundle = service.getPurchases(IAB_VERSION, context.getPackageName(), "inapp", null); Log.i(TAG, "getPurchases"); @@ -123,20 +144,8 @@ public class IAB implements ServiceConnection { if (response != 0) throw new IllegalArgumentException(getResult(response)); - // Check purchases - ArrayList skus = bundle.getStringArrayList("INAPP_PURCHASE_ITEM_LIST"); - - if (skus != null) { - SharedPreferences prefs = context.getSharedPreferences("IAB", Context.MODE_PRIVATE); - SharedPreferences.Editor editor = prefs.edit(); - for (String product : prefs.getAll().keySet()) - editor.remove(product); - for (String product : skus) - editor.putBoolean(product, true); - editor.apply(); - } - - return (skus != null && skus.contains(sku)); + ArrayList details = bundle.getStringArrayList("INAPP_PURCHASE_ITEM_LIST"); + return (details == null ? new ArrayList() : details); } public PendingIntent getBuyIntent(String sku) throws RemoteException { diff --git a/app/src/main/java/eu/faircode/netguard/Util.java b/app/src/main/java/eu/faircode/netguard/Util.java index 5455de99..8bd44e7f 100644 --- a/app/src/main/java/eu/faircode/netguard/Util.java +++ b/app/src/main/java/eu/faircode/netguard/Util.java @@ -517,11 +517,9 @@ public class Util { return sb.toString(); } - public static String sha256(String text, String salt) throws NoSuchAlgorithmException, UnsupportedEncodingException { - MessageDigest digest = MessageDigest.getInstance("SHA-256"); - byte[] bytes = (text + salt).getBytes("UTF-8"); - digest.update(bytes, 0, bytes.length); - bytes = digest.digest(); + public static String md5(String text, String salt) throws NoSuchAlgorithmException, UnsupportedEncodingException { + // MD5 + byte[] bytes = MessageDigest.getInstance("MD5").digest((text + salt).getBytes("UTF-8")); StringBuilder sb = new StringBuilder(); for (byte b : bytes) sb.append(String.format("%02X", b)); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 03e4755b..1b34efec 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -94,7 +94,7 @@ Since NetGuard has no internet permission, you know your internet traffic is not Block Pro features - You can buy the following convenience features: + The following convenience features are available: Search, filter, sort New application notification Theme