From 3345f04d6fd4b0fb1086562245f93b4b1f7d1ebd Mon Sep 17 00:00:00 2001 From: Javier Santos Date: Sat, 28 May 2016 16:59:30 +0200 Subject: [PATCH] Allow multiple installers and add amazon app store --- .../piracychecker/PiracyChecker.java | 11 ++++++++--- .../piracychecker/UtilsLibrary.java | 11 +++++++++-- .../piracychecker/enums/InstallerID.java | 17 ++++++++++++++++- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/library/src/main/java/com/github/javiersantos/piracychecker/PiracyChecker.java b/library/src/main/java/com/github/javiersantos/piracychecker/PiracyChecker.java index fd52b8e..3191c6b 100644 --- a/library/src/main/java/com/github/javiersantos/piracychecker/PiracyChecker.java +++ b/library/src/main/java/com/github/javiersantos/piracychecker/PiracyChecker.java @@ -12,25 +12,30 @@ import com.google.android.vending.licensing.LicenseCheckerCallback; import com.google.android.vending.licensing.ServerManagedPolicy; +import java.util.ArrayList; +import java.util.List; + public class PiracyChecker { private Context context; private String unlicensedDialogTitle, unlicensedDialogDescription; private boolean enableLVL, enableSigningCertificate, enableInstallerId; private String licenseBase64; private String signature; - private InstallerID installerID; + private List installerIDs; private PiracyCheckerCallback callback; public PiracyChecker(Context context) { this.context = context; this.unlicensedDialogTitle = context.getString(R.string.app_unlicensed); this.unlicensedDialogDescription = context.getString(R.string.app_unlicensed_description); + this.installerIDs = new ArrayList<>(); } public PiracyChecker(Context context, String title, String description) { this.context = context; this.unlicensedDialogTitle = title; this.unlicensedDialogDescription = description; + this.installerIDs = new ArrayList<>(); } public PiracyChecker(Context context, @StringRes int title, @StringRes int description) { @@ -51,7 +56,7 @@ public PiracyChecker enableSigningCertificate(String signature) { public PiracyChecker enableInstallerId(InstallerID installerID) { this.enableInstallerId = true; - this.installerID = installerID; + this.installerIDs.add(installerID); return this; } @@ -123,7 +128,7 @@ private boolean verifyInstallerId() { boolean installerIdValid = false; if (enableInstallerId) { - if (UtilsLibrary.verifyInstallerId(context, installerID)) { + if (UtilsLibrary.verifyInstallerId(context, installerIDs)) { installerIdValid = true; } } else { diff --git a/library/src/main/java/com/github/javiersantos/piracychecker/UtilsLibrary.java b/library/src/main/java/com/github/javiersantos/piracychecker/UtilsLibrary.java index 7d00762..3776460 100644 --- a/library/src/main/java/com/github/javiersantos/piracychecker/UtilsLibrary.java +++ b/library/src/main/java/com/github/javiersantos/piracychecker/UtilsLibrary.java @@ -13,6 +13,8 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.List; class UtilsLibrary { static final byte[] SALT = new byte[] { @@ -56,10 +58,15 @@ static boolean verifySigningCertificate(Context context, String appSignature) { return getCurrentSignature(context).equals(appSignature); } - static boolean verifyInstallerId(Context context, InstallerID installerID) { + static boolean verifyInstallerId(Context context, List installerID) { + List validInstallers = new ArrayList<>(); final String installer = context.getPackageManager().getInstallerPackageName(context.getPackageName()); - return installer != null && installer.startsWith(installerID.toString()); + for (InstallerID id : installerID) { + validInstallers.addAll(id.toIDs()); + } + + return installer != null && validInstallers.contains(installer); } } diff --git a/library/src/main/java/com/github/javiersantos/piracychecker/enums/InstallerID.java b/library/src/main/java/com/github/javiersantos/piracychecker/enums/InstallerID.java index 6a6828d..ba53b90 100644 --- a/library/src/main/java/com/github/javiersantos/piracychecker/enums/InstallerID.java +++ b/library/src/main/java/com/github/javiersantos/piracychecker/enums/InstallerID.java @@ -1,7 +1,13 @@ package com.github.javiersantos.piracychecker.enums; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + public enum InstallerID { - GOOGLE_PLAY("com.android.vending"); + GOOGLE_PLAY("com.android.vending|com.google.android.feedback"), + AMAZON_APP_STORE("com.amazon.venezia"); private final String text; @@ -17,4 +23,13 @@ public String toString() { return text; } + public List toIDs() { + if (text.contains("|")) { + String[] split = text.split("\\|"); + return new ArrayList<>(Arrays.asList(split)); + } else { + return new ArrayList<>(Collections.singletonList(text)); + } + } + }