├── app ├── .gitignore ├── gradle.properties ├── src │ └── main │ │ ├── AndroidManifest.xml │ │ ├── res │ │ └── values │ │ │ └── strings.xml │ │ └── java │ │ └── com │ │ └── cuneytayyildiz │ │ └── adblockerdetector │ │ ├── Utils.java │ │ ├── Constants.java │ │ └── AdBlockerDetector.java ├── build.gradle └── proguard-rules.pro ├── settings.gradle ├── .idea ├── copyright │ ├── profiles_settings.xml │ └── MIT.xml ├── dictionaries │ └── cuney.xml ├── encodings.xml ├── modules.xml ├── runConfigurations.xml ├── gradle.xml ├── compiler.xml └── misc.xml ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── LICENSE ├── gradle.properties ├── gradlew.bat ├── README.md └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /app/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_NAME=AdBlockerDetector 2 | POM_ARTIFACT_ID=adblockerdetector 3 | POM_PACKAGING=aar 4 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/dictionaries/cuney.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Swisyn/AdBlockerDetector/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip 7 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 24 5 | buildToolsVersion "24.0.1" 6 | defaultConfig { 7 | minSdkVersion 10 8 | targetSdkVersion 24 9 | versionCode 1 10 | versionName "1.0" 11 | } 12 | buildTypes { 13 | release { 14 | minifyEnabled false 15 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 16 | } 17 | } 18 | } 19 | 20 | dependencies { 21 | compile fileTree(dir: 'libs', include: ['*.jar']) 22 | } 23 | apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle' -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Programs\android-sdk\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Ad blocker detection 4 | ad blocker has been detected on your device.
6 | If you don\'t understand why you get this message, please feel free to contact us.
7 |
8 | Development of this application is supported by advertising, so please uninstall your ad blocker in order to use it.
9 |
10 | ]]>
11 | %s button to use this application without ads. 13 | ]]> 14 |
-------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Cüneyt Ayyıldız 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.idea/copyright/MIT.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | VERSION_NAME=1.0.0 2 | VERSION_CODE=1 3 | GROUP=com.cuneytayyildiz 4 | 5 | POM_DESCRIPTION=Many ad blockers exist on Android, this is a real problem for developers that rely on ad incomes. This project proposes an open source library that can detect most of ad blockers. Then developers can display a dialog to inform user that an ad blocker has been detected and to propose, for example, to buy an ad-free version of the application or to quit. 6 | POM_URL=https://github.com/Swisyn/AdBlockerDetector 7 | POM_SCM_URL=https://github.com/Swisyn/AdBlockerDetector 8 | POM_SCM_CONNECTION=scm:git@github.com:Swisyn/AdBlockerDetector.git 9 | POM_SCM_DEV_CONNECTION=scm:git@github.com:Swisyn/AdBlockerDetector.git 10 | POM_LICENCE_NAME=The MIT License (MIT) 11 | POM_LICENCE_URL=https://github.com/Swisyn/AdBlockerDetector/blob/master/LICENSE 12 | POM_LICENCE_DIST=repo 13 | POM_DEVELOPER_ID=swisyn 14 | POM_DEVELOPER_NAME=Cuneyt AYYILDIZ 15 | 16 | NEXUS_USERNAME=swisyn 17 | NEXUS_PASSWORD=2819320130s 18 | 19 | SNAPSHOT_REPOSITORY_URL=https://oss.sonatype.org/content/repositories/snapshots 20 | RELEASE_REPOSITORY_URL=https://oss.sonatype.org/service/local/staging/deploy/maven2 21 | 22 | signing.keyId=8B3E18E1 23 | signing.password=2819320130 24 | signing.secretKeyRingFile=C:\\Users\\cuney\\AppData\\Roaming\\gnupg\\secring.gpg 25 | 26 | org.gradle.jvmargs=-Xmx1536m -------------------------------------------------------------------------------- /app/src/main/java/com/cuneytayyildiz/adblockerdetector/Utils.java: -------------------------------------------------------------------------------- 1 | package com.cuneytayyildiz.adblockerdetector; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.net.Uri; 6 | 7 | public class Utils { 8 | 9 | /** 10 | * Quit the current application. 11 | */ 12 | public static void quitApplication() { 13 | android.os.Process.killProcess(android.os.Process.myPid()); 14 | System.exit(0); 15 | } 16 | 17 | /** 18 | * Propose user to send an email with pre-filled fields. 19 | */ 20 | public static void sendEMail(final Context context, final String dialogTitle, final String to, final String subject, final String body) { 21 | final Intent send = new Intent(Intent.ACTION_SENDTO); 22 | final String uriText = 23 | "mailto:" + Uri.encode(to) + 24 | "?subject=" + Uri.encode(subject) + 25 | "&body=" + Uri.encode(body); 26 | send.setData(Uri.parse(uriText)); 27 | context.startActivity(Intent.createChooser(send, dialogTitle)); 28 | } 29 | 30 | /** 31 | * Propose user to uninstall the given application 32 | * 33 | * @param context 34 | * @param packageName package of the application to uninstall 35 | */ 36 | public static void uinstallApplication(final Context context, final String packageName) { 37 | final Intent intent = new Intent(Intent.ACTION_DELETE, Uri.fromParts("package", packageName, null)); 38 | context.startActivity(intent); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /app/src/main/java/com/cuneytayyildiz/adblockerdetector/Constants.java: -------------------------------------------------------------------------------- 1 | package com.cuneytayyildiz.adblockerdetector; 2 | 3 | import android.annotation.SuppressLint; 4 | 5 | /** 6 | * Created by cuneyt on 015, 15, 08, 2016. 7 | */ 8 | 9 | public class Constants { 10 | // Asynchronous callback 11 | public interface AdBlockerCallback { 12 | void onResult(boolean adBlockerFound, Info info); 13 | } 14 | 15 | // Detection method 16 | public enum Method { 17 | NONE, BY_HOSTS_FILE, BY_APP_NAME, BY_HOST_RESOLUTION, BY_LOCAL_PROXY 18 | } 19 | 20 | // Name of known ad blockers 21 | 22 | public static final String[] BLOCKERS_APP_NAMES = 23 | { 24 | "de.ub0r.android.adBlock", 25 | "org.adblockplus.android", 26 | "com.bigtincan.android.adfree", 27 | "org.adaway", 28 | "org.czzsunset.adblock", 29 | "com.pasvante.adblocker", 30 | "com.perlapps.MyInternetSecurity", 31 | "net.xdevelop.adblocker_t", 32 | "net.xdevelop.adblocker", 33 | "com.jrummy.apps.ad.blocker", 34 | "com.atejapps.advanishlite", 35 | "com.atejapps.advanish", 36 | "pl.adblocker.free", 37 | "de.resolution.blockit" 38 | }; 39 | 40 | 41 | // Name of known blocked hosts 42 | public static final String[] BLOCKED_HOSTS = 43 | { 44 | "a.admob.com", 45 | "mm.admob.com", 46 | "p.admob.com", 47 | "r.admob.com", 48 | "mmv.admob.com", 49 | "aax-fe-sin.amazon-adsystem.com", 50 | "rcm-na.amazon-adsystem.com", 51 | "aax-us-east.amazon-adsystem.com", 52 | "ir-na.amazon-adsystem.com", 53 | "aax-eu.amazon-adsystem.com" 54 | }; 55 | 56 | // "hosts" file possible paths 57 | @SuppressLint("SdCardPath") 58 | public static final String[] HOSTS_FILES = {"/etc/hosts", "/system/etc/hosts", "/data/data/hosts"}; 59 | 60 | // Pattern to search in hosts file 61 | 62 | public static final String[] HOSTS_FILE_PATTERNS = {"admob", "amazon-adsystem"}; 63 | 64 | // Give information on how ad blocker was detected 65 | public static class Info { 66 | public Method method; 67 | public String details1; 68 | public String details2; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | 22 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | Android 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 65 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

AdBlockerDetector

2 |

Android Library

3 | 4 | 5 |

6 | 7 | 8 | 9 | 10 |

11 | 12 | 13 | Many ad blockers exist on Android, this is a real problem for developers that rely on ad incomes. 14 | 15 | This project proposes an open source library that can detect most of ad blockers. 16 | 17 | Then developers can display a dialog to inform user that an ad blocker has been detected and to propose, for example, to buy an ad-free version of the application or to quit. 18 | 19 | # Details 20 | Currently, the following methods are used to detect ad blockers: 21 | * Search for known ad blockers application package names 22 | * Resolve known ad server domains and check if it redirects to a local address (work for both DNS & hosts file modification) 23 | * Check in hosts file for known patterns (work for hosts file modification) 24 | 25 | Detection methods will not give false positive but may not detect some ad blockers. 26 | 27 | This library will be improved regularly to ensure a maximum ad blocker detection rate. 28 | 29 | # Usage 30 | Add dependency and plugin to project level build.gradle. 31 | ``` 32 | dependencies { 33 | compile 'com.cuneytayyildiz:adblockerdetector:1.0.0' 34 | } 35 | ``` 36 | 37 | # Simple example 38 | ``` 39 | public void checkAdBlocker() 40 | { 41 | // Asynchronous detection in a background thread 42 | new AdBlockerDetector(this).detectAdBlockers(new Constants.AdBlockerCallback() 43 | { 44 | @Override 45 | public void onResult(boolean adBlockerFound, Info info) 46 | { 47 | if(adBlockerFound) 48 | { 49 | // Show your information dialog here to user 50 | } 51 | } 52 | }); 53 | } 54 | ``` 55 | The method checkAdBlocker() should be called in an Handler, by a delayed message sent in onCreate(). 56 | 57 | # Licence 58 | ``` 59 | The MIT License (MIT) 60 | 61 | Copyright (c) 2016 Cüneyt Ayyıldız 62 | 63 | Permission is hereby granted, free of charge, to any person obtaining a copy 64 | of this software and associated documentation files (the "Software"), to deal 65 | in the Software without restriction, including without limitation the rights 66 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 67 | copies of the Software, and to permit persons to whom the Software is 68 | furnished to do so, subject to the following conditions: 69 | 70 | The above copyright notice and this permission notice shall be included in all 71 | copies or substantial portions of the Software. 72 | 73 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 74 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 75 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 76 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 77 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 78 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 79 | SOFTWARE. 80 | ` 81 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /app/src/main/java/com/cuneytayyildiz/adblockerdetector/AdBlockerDetector.java: -------------------------------------------------------------------------------- 1 | 2 | package com.cuneytayyildiz.adblockerdetector; 3 | 4 | import android.content.Context; 5 | import android.content.pm.PackageManager; 6 | import android.os.AsyncTask; 7 | 8 | import java.io.BufferedReader; 9 | import java.io.File; 10 | import java.io.FileReader; 11 | import java.io.IOException; 12 | import java.lang.ref.WeakReference; 13 | import java.net.InetAddress; 14 | 15 | import static com.cuneytayyildiz.adblockerdetector.Constants.BLOCKED_HOSTS; 16 | import static com.cuneytayyildiz.adblockerdetector.Constants.Info; 17 | import static com.cuneytayyildiz.adblockerdetector.Constants.Method; 18 | 19 | /** 20 | * Created by cuneyt on 015, 15, 08, 2016. 21 | */ 22 | public class AdBlockerDetector { 23 | 24 | private WeakReference context; 25 | 26 | /** 27 | * @param context can be null, in this case the method using package name is not used. 28 | */ 29 | public AdBlockerDetector(Context context) { 30 | this.context = new WeakReference<>(context); 31 | } 32 | 33 | /** 34 | * Asynchronous ad-blockers detection. 35 | * Callback is called in GUI thread. 36 | * 37 | * @param callback 38 | */ 39 | public void detectAdBlockers(Constants.AdBlockerCallback callback) { 40 | new DetectTask(callback).execute(); 41 | } 42 | 43 | /** 44 | * Synchronous ad-blockers detection 45 | * This is blocking and should be called in a separated thread. 46 | * In Android activities, prefer the asynchronous version. 47 | * 48 | * @param info if not null, it will be filled. 49 | * @return true if an ad-blocker is detected 50 | */ 51 | public boolean detectAdBlockers(Info info) { 52 | if (info != null) { 53 | info.method = Method.NONE; 54 | info.details1 = ""; 55 | info.details2 = ""; 56 | } 57 | return detectAppNames(info) || (detectHostName(info)) || detectInHostFile(info); 58 | } 59 | 60 | /** 61 | * Synchronous ad-blockers detection 62 | * This is blocking and should be called in a separated thread. 63 | * In Android activities, prefer the asynchronous version. 64 | * 65 | * @return true if an AdBlocker is detected 66 | */ 67 | public boolean detectAdBlockers() { 68 | return detectAdBlockers((Info) null); 69 | } 70 | 71 | private boolean detectInHostFile(Info info) { 72 | // search a readable hosts file 73 | File hostsFile = null; 74 | for (final String fileName : Constants.HOSTS_FILES) { 75 | hostsFile = new File(fileName); 76 | if (hostsFile.canRead()) 77 | break; 78 | } 79 | // and read it 80 | if (hostsFile != null && hostsFile.canRead()) { 81 | BufferedReader in = null; 82 | try { 83 | in = new BufferedReader(new FileReader(hostsFile)); 84 | String line; 85 | while ((line = in.readLine()) != null) { 86 | line = line.trim(); 87 | if (line.length() > 0 && line.charAt(0) != '#') { 88 | for (final String pattern : Constants.HOSTS_FILE_PATTERNS) { 89 | if (line.contains(pattern)) { 90 | if (info != null) { 91 | info.method = Method.BY_HOSTS_FILE; 92 | info.details1 = hostsFile.getAbsolutePath(); 93 | info.details2 = line; 94 | } 95 | return true; 96 | } 97 | } 98 | } 99 | } 100 | } catch (Exception e) { 101 | return false; 102 | } finally { 103 | try { 104 | if (in != null) 105 | in.close(); 106 | } catch (IOException e) { 107 | e.printStackTrace(); 108 | } 109 | } 110 | } 111 | return false; 112 | } 113 | 114 | private boolean detectHostName(Info info) { 115 | for (final String host : BLOCKED_HOSTS) { 116 | final String address = isLocalHost(host); 117 | if (address != null) { 118 | if (info != null) { 119 | info.method = Method.BY_HOST_RESOLUTION; 120 | info.details1 = host; 121 | info.details2 = address; 122 | } 123 | return true; 124 | } 125 | } 126 | return false; 127 | } 128 | 129 | private String isLocalHost(String hostName) { 130 | try { 131 | final InetAddress a = InetAddress.getByName(hostName); 132 | if (a != null && (a.isAnyLocalAddress() || a.isLinkLocalAddress() || a.isLoopbackAddress())) 133 | return a.getHostAddress(); 134 | } catch (Exception ex) { 135 | ex.printStackTrace(); 136 | } 137 | return null; 138 | } 139 | 140 | private boolean detectAppNames(Info info) { 141 | if (context != null) { 142 | for (final String app : Constants.BLOCKERS_APP_NAMES) { 143 | if (isAppInstalled(app)) { 144 | if (info != null) { 145 | info.method = Method.BY_APP_NAME; 146 | info.details1 = app; 147 | } 148 | return true; 149 | } 150 | } 151 | } 152 | return false; 153 | } 154 | 155 | private boolean isAppInstalled(String packageName) { 156 | try { 157 | final Context c = context.get(); 158 | return (c != null) && (c.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES) != null); 159 | } catch (Exception e) { 160 | return false; 161 | } 162 | } 163 | 164 | 165 | private class DetectTask extends AsyncTask { 166 | private WeakReference callback; 167 | private Constants.Info info; 168 | 169 | DetectTask(Constants.AdBlockerCallback c) { 170 | callback = new WeakReference<>(c); 171 | } 172 | 173 | @Override 174 | protected Boolean doInBackground(Void... params) { 175 | try { 176 | info = new Constants.Info(); 177 | return detectAdBlockers(info); 178 | } catch (Throwable t) { 179 | return false; 180 | } 181 | } 182 | 183 | @Override 184 | protected void onPostExecute(Boolean r) { 185 | final Constants.AdBlockerCallback c = callback.get(); 186 | if (c != null && r != null) 187 | c.onResult(r, info); 188 | } 189 | } 190 | } 191 | --------------------------------------------------------------------------------