├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── safecheckproject │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── safecheckproject │ │ │ └── MainActivity.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── example │ └── safecheckproject │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── lzy │ │ └── safecheck │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── lzy │ │ │ └── safecheck │ │ │ ├── ISafeCheck.java │ │ │ ├── SafeCheck.java │ │ │ ├── SafeCheckService.java │ │ │ ├── TaskEvent.java │ │ │ ├── TaskQueue.java │ │ │ ├── listener │ │ │ ├── OnTaskEventListener.java │ │ │ └── OnTaskListener.java │ │ │ ├── task │ │ │ ├── AbstractCheckTask.java │ │ │ ├── DebugCheckTask.java │ │ │ ├── ICheckTask.java │ │ │ ├── NetProxyCheckTask.java │ │ │ ├── PageHackCheckTask.java │ │ │ ├── RootCheckTask.java │ │ │ ├── SignCheckTask.java │ │ │ └── SimulatorCheckTask.java │ │ │ └── utils │ │ │ ├── DebugCheckUtil.java │ │ │ ├── SafeCheckActivityLifecycle.java │ │ │ ├── SignCheckUtil.java │ │ │ └── Utils.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── lzy │ └── safecheck │ └── ExampleUnitTest.kt ├── settings.gradle └── test.jks /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /.idea/ 4 | /local.properties 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | .cxx 10 | 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ## Android SafeCheck 3 | 4 | [![](https://jitpack.io/v/elivenlzy/androidsafecheck.svg)](https://jitpack.io/#elivenlzy/androidsafecheck) 5 | 6 | 这是一个Android的安全环境检测库,项目中接入该库可以提升APP的安全等级,可用于过等保时的安全扫描。 7 | 8 | > 功能列表: 9 | 10 | 1. 防调试检测 11 | 2. 网络代理检测 12 | 3. Root检测 13 | 4. 模拟器检测 14 | 5. 正版签名检测 15 | 6. 界面劫持检测 16 | 17 | ### 依赖 18 | **** 19 | 20 | **1,在你的项目根目录的build.gradle添加仓库:** 21 | 22 | ``` 23 | allprojects { 24 | repositories { 25 | google() 26 | jcenter() 27 | maven { url 'https://jitpack.io' } 28 | } 29 | } 30 | ``` 31 | 32 | **2,在mudule的build.gradle中添加依赖:** 33 | 34 | ``` 35 | dependencies { 36 | implementation 'com.github.elivenlzy:androidsafecheck:0.0.2' 37 | } 38 | ``` 39 | 40 | ### 使用 41 | **** 42 | 43 | **一,一键快速集成,在APP启动页加上如下代码即可获得本库提供的默认的全部功能:** 44 | 45 | ``` 46 | 47 | TaskQueue taskQueue = new TaskQueue(); 48 | taskQueue.addTask(new DebugCheckTask(this)); 49 | taskQueue.addTask(new PageHackCheckTask(this)); 50 | taskQueue.addTask(new SignCheckTask(this, RIGHT_CER)); 51 | taskQueue.addTask(new RootCheckTask(this)); 52 | taskQueue.addTask(new SimulatorCheckTask(this)); 53 | taskQueue.addTask(new NetProxyCheckTask(this, false)); 54 | SafeCheckService.startCheck(taskQueue, new OnTaskListener() { 55 | @Override 56 | public void onStart() { 57 | Log.d(TAG, "安全检查开始"); 58 | } 59 | 60 | @Override 61 | public void onTaskEvent(ISafeCheck iSafeCheck, TaskEvent taskEvent) { 62 | Log.d(TAG, "${taskEvent?.tag} isCheckPass is ${taskEvent?.isCheckPass}"); 63 | } 64 | 65 | @Override 66 | public void onComplete() { 67 | Log.d(TAG, "安全检查完成"); 68 | Toast.makeText(MainActivity.this, "安全检查完成", Toast.LENGTH_SHORT).show(); 69 | } 70 | }); 71 | 72 | ``` 73 | 74 | **二,功能定制,基于上面的快速集成,最快只需两步:** 75 | 76 | 1. 新建一个task类,继承 AbstractCheckTask ,重写check方法,实现检测逻辑。 77 | 2. 将此task类添加到TaskQueue队列中即可 78 | 79 | **检测不通过的话,默认会弹出一个检测不通过的警告弹窗,如果需要自定义处理检测不通过的操作,有两种办法:** 80 | 81 | 1. 重写 AbstractCheckTask 中的 interruptCheck 方法 82 | 2. 在启动检查时传入的 OnTaskListener 的 OnTaskEvent 方法中,根据 83 | taskEvent.result 和 taskEvent.tag 84 | 来判断处理不通过的task,然后根据处理结果来是否调用 iSafeCheck.check 85 | 方法继续执行后面的检查task 86 | 87 | ### 功能介绍 88 | --------- 89 | 90 | **1. 防调试检测** 91 | 92 | 在应用启动时会启动一个定时任务,每1秒检查一次,持续检查5分钟;定时任务启动成功即认为该任务通过。 93 | 94 | 判定为被调试的几种触发条件: 95 | * 非调试模式下debug标识为true 96 | * 非调试模式下被外部调试器连接 97 | * 本地的23946默认调试端口被占用 98 | * 本地存在TracerPid的调试文件 99 | 100 | 触发以上任何一种条件应用进程都会被强制杀掉 101 | 102 | **2. 网络代理检测** 103 | 104 | 在应用启动时会检查一次当前网络是否被代理,如果被网络代理则检查失败,会弹窗阻塞后续任务执行。 105 | 106 | 开发者也可以在每次应用访问网络前都调用一次 Utils.isWifiProxy 方法执行一次检查。 107 | 108 | **3. Root检测** 109 | 110 | 在应用启动时会检查一次当前设备是否被Root过,如果被Root,会弹窗阻塞后续任务执行。 111 | 112 | **4. 模拟器检测** 113 | 114 | 在应用启动时会检查一次当前设备是否在模拟器上运行,如果是,会弹窗阻塞后续任务执行。 115 | 116 | **5. 正版签名检测** 117 | 118 | 在应用启动时会检查一次当前应用的签名是否正版签名一致,如果不一致,会弹窗阻塞后续任务执行。 119 | 120 | 需要传入正版签名的sha1,可以使用命令:keytool -v -list -keystore 121 | <签名文件路径> 来获取 122 | 123 | **6. 界面劫持检测** 124 | 125 | 在应用启动时会注册一个应用前后台状态的监听,如果应用最上层的页面没有在前台显示,会弹窗一个退到后台的提示;监听注册成功成功即认为该任务通过。 126 | 127 | 128 | 129 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 29 7 | buildToolsVersion "30.0.1" 8 | 9 | defaultConfig { 10 | applicationId "com.example.safecheckproject" 11 | minSdkVersion 16 12 | targetSdkVersion 29 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | } 18 | 19 | signingConfigs { 20 | release { 21 | v1SigningEnabled true 22 | v2SigningEnabled true 23 | storeFile file("../test.jks") 24 | storePassword "test123" 25 | keyAlias "test" 26 | keyPassword "test123" 27 | } 28 | } 29 | 30 | buildTypes { 31 | debug { 32 | minifyEnabled false 33 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 34 | signingConfig signingConfigs.release 35 | } 36 | release { 37 | minifyEnabled false 38 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 39 | signingConfig signingConfigs.release 40 | } 41 | } 42 | } 43 | 44 | dependencies { 45 | implementation fileTree(dir: "libs", include: ["*.jar"]) 46 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 47 | implementation 'androidx.core:core-ktx:1.1.0' 48 | implementation 'androidx.appcompat:appcompat:1.1.0' 49 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 50 | // implementation project(path: ':library') 51 | implementation 'com.github.elivenlzy:androidsafecheck:v0.0.1' 52 | testImplementation 'junit:junit:4.12' 53 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 54 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 55 | 56 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/androidTest/java/com/example/safecheckproject/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.example.safecheckproject 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.example.safecheckproject", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/safecheckproject/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.example.safecheckproject 2 | 3 | import android.os.Bundle 4 | import android.util.Log 5 | import android.widget.Toast 6 | import androidx.appcompat.app.AppCompatActivity 7 | import com.lzy.safecheck.ISafeCheck 8 | import com.lzy.safecheck.SafeCheckService 9 | import com.lzy.safecheck.TaskEvent 10 | import com.lzy.safecheck.TaskQueue 11 | import com.lzy.safecheck.listener.OnTaskListener 12 | import com.lzy.safecheck.task.* 13 | 14 | /** 15 | * 16 | * Create by liuzhiyou on 2020/8/6 5:24 PM 17 | */ 18 | class MainActivity : AppCompatActivity() { 19 | 20 | override fun onCreate(savedInstanceState: Bundle?) { 21 | super.onCreate(savedInstanceState) 22 | setContentView(R.layout.activity_main) 23 | SafeCheckService.setLogEnabled(true) 24 | 25 | val taskQueue = TaskQueue() 26 | taskQueue.addTask(DebugCheckTask(this)) 27 | taskQueue.addTask(PageHackCheckTask(this)) 28 | taskQueue.addTask(SignCheckTask(this, RIGHT_CER)) 29 | taskQueue.addTask(RootCheckTask(this)) 30 | taskQueue.addTask(SimulatorCheckTask(this)) 31 | taskQueue.addTask(NetProxyCheckTask(this, false)) 32 | SafeCheckService.startCheck(taskQueue, object : OnTaskListener { 33 | override fun onStart() { 34 | Log.d(TAG, "安全检查开始") 35 | } 36 | 37 | override fun onTaskEvent(iSafeCheck: ISafeCheck?, taskEvent: TaskEvent?) { 38 | Log.d(TAG, "${taskEvent?.tag} isCheckPass is ${taskEvent?.isCheckPass}") 39 | } 40 | 41 | override fun onComplete() { 42 | Log.d(TAG, "安全检查完成") 43 | Toast.makeText(this@MainActivity, "安全检查完成", Toast.LENGTH_SHORT).show() 44 | } 45 | 46 | }) 47 | } 48 | 49 | companion object { 50 | const val TAG = "lzy" 51 | const val RIGHT_CER = "B4:22:FA:A3:21:D9:7B:CB:BF:56:CB:63:65:07:3F:4C:85:5F:AA:D0" 52 | } 53 | 54 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ElivenLZY/AndroidSafeCheck/a925b96e27fe797ee69d9ddce8ca2cdaea2c6f5b/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ElivenLZY/AndroidSafeCheck/a925b96e27fe797ee69d9ddce8ca2cdaea2c6f5b/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ElivenLZY/AndroidSafeCheck/a925b96e27fe797ee69d9ddce8ca2cdaea2c6f5b/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ElivenLZY/AndroidSafeCheck/a925b96e27fe797ee69d9ddce8ca2cdaea2c6f5b/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ElivenLZY/AndroidSafeCheck/a925b96e27fe797ee69d9ddce8ca2cdaea2c6f5b/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ElivenLZY/AndroidSafeCheck/a925b96e27fe797ee69d9ddce8ca2cdaea2c6f5b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ElivenLZY/AndroidSafeCheck/a925b96e27fe797ee69d9ddce8ca2cdaea2c6f5b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ElivenLZY/AndroidSafeCheck/a925b96e27fe797ee69d9ddce8ca2cdaea2c6f5b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ElivenLZY/AndroidSafeCheck/a925b96e27fe797ee69d9ddce8ca2cdaea2c6f5b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ElivenLZY/AndroidSafeCheck/a925b96e27fe797ee69d9ddce8ca2cdaea2c6f5b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #6200EE 4 | #3700B3 5 | #03DAC5 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | SafeCheckProject 3 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/test/java/com/example/safecheckproject/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.example.safecheckproject 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | buildscript { 3 | ext.kotlin_version = "1.3.72" 4 | repositories { 5 | google() 6 | jcenter() 7 | } 8 | dependencies { 9 | classpath "com.android.tools.build:gradle:4.0.1" 10 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 11 | 12 | // NOTE: Do not place your application dependencies here; they belong 13 | // in the individual module build.gradle files 14 | } 15 | } 16 | 17 | allprojects { 18 | repositories { 19 | google() 20 | jcenter() 21 | maven { url 'https://jitpack.io' } 22 | } 23 | } 24 | 25 | task clean(type: Delete) { 26 | delete rootProject.buildDir 27 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx2048m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app"s APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Automatically convert third-party libraries to use AndroidX 19 | android.enableJetifier=true 20 | # Kotlin code style for this project: "official" or "obsolete": 21 | kotlin.code.style=official -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ElivenLZY/AndroidSafeCheck/a925b96e27fe797ee69d9ddce8ca2cdaea2c6f5b/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Aug 04 11:10:48 CST 2020 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-6.1.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /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 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 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 Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 29 7 | buildToolsVersion "30.0.1" 8 | 9 | defaultConfig { 10 | minSdkVersion 16 11 | targetSdkVersion 29 12 | versionCode 1 13 | versionName "1.0" 14 | 15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 16 | consumerProguardFiles "consumer-rules.pro" 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | } 26 | 27 | dependencies { 28 | implementation fileTree(dir: "libs", include: ["*.jar"]) 29 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 30 | implementation 'androidx.core:core-ktx:1.1.0' 31 | implementation 'androidx.appcompat:appcompat:1.1.0' 32 | testImplementation 'junit:junit:4.12' 33 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 34 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 35 | 36 | } -------------------------------------------------------------------------------- /library/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ElivenLZY/AndroidSafeCheck/a925b96e27fe797ee69d9ddce8ca2cdaea2c6f5b/library/consumer-rules.pro -------------------------------------------------------------------------------- /library/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /library/src/androidTest/java/com/lzy/safecheck/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.lzy.library.test", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | / 5 | -------------------------------------------------------------------------------- /library/src/main/java/com/lzy/safecheck/ISafeCheck.java: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck; 2 | 3 | /** 4 | * Create by liuzhiyou on 2020/11/10 16:17 5 | * Email:1130294881@qq.com 6 | */ 7 | public interface ISafeCheck { 8 | void check(); 9 | } 10 | -------------------------------------------------------------------------------- /library/src/main/java/com/lzy/safecheck/SafeCheck.java: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck; 2 | 3 | import com.lzy.safecheck.listener.OnTaskEventListener; 4 | import com.lzy.safecheck.listener.OnTaskListener; 5 | import com.lzy.safecheck.task.ICheckTask; 6 | import com.lzy.safecheck.utils.Utils; 7 | 8 | /** 9 | * 安全检查 10 | * Create by 2020/6/16 from liuzhiyou 11 | **/ 12 | public class SafeCheck implements ISafeCheck { 13 | 14 | private static final String TAG = SafeCheck.class.getSimpleName(); 15 | 16 | private OnTaskListener mOnTaskListener; 17 | 18 | private TaskQueue mTaskQueue; 19 | 20 | private SafeCheck(Builder builder) { 21 | mOnTaskListener = builder.mOnTaskListener; 22 | mTaskQueue = builder.mTaskQueue; 23 | } 24 | 25 | public void startCheck() { 26 | mOnTaskListener.onStart(); 27 | check(); 28 | } 29 | 30 | @Override 31 | public void check() { 32 | final ICheckTask task = pollTask(); 33 | Utils.log("check task is " + task); 34 | if (task == null) { 35 | mOnTaskListener.onComplete(); 36 | return; 37 | } 38 | 39 | task.execute(new OnTaskEventListener() { 40 | @Override 41 | public void onEvent(TaskEvent taskEvent, boolean callTaskEvent) { 42 | if (callTaskEvent) mOnTaskListener.onTaskEvent(SafeCheck.this, taskEvent); 43 | if (taskEvent.isCheckPass()) check(); 44 | } 45 | }); 46 | } 47 | 48 | private ICheckTask pollTask() { 49 | if (mTaskQueue == null) return null; 50 | return mTaskQueue.pollTask(); 51 | } 52 | 53 | public static final class Builder { 54 | private TaskQueue mTaskQueue; 55 | private OnTaskListener mOnTaskListener; 56 | 57 | public Builder() { 58 | } 59 | 60 | public Builder setTaskQueue(TaskQueue taskQueue) { 61 | mTaskQueue = taskQueue; 62 | return this; 63 | } 64 | 65 | public Builder setOnTaskListener(OnTaskListener onTaskListener) { 66 | this.mOnTaskListener = onTaskListener; 67 | return this; 68 | } 69 | 70 | public SafeCheck build() { 71 | return new SafeCheck(this); 72 | } 73 | } 74 | 75 | 76 | } 77 | -------------------------------------------------------------------------------- /library/src/main/java/com/lzy/safecheck/SafeCheckService.java: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck; 2 | 3 | import com.lzy.safecheck.listener.OnTaskListener; 4 | 5 | /** 6 | * 安全检查管理 7 | * Create by 2020/6/17 from liuzhiyou 8 | **/ 9 | public class SafeCheckService { 10 | 11 | private static boolean debugLog = false; 12 | private static String TAG = "SafeCheck"; 13 | 14 | 15 | public static void setLogTag(String tag) { 16 | TAG=tag; 17 | } 18 | 19 | public static String getTAG() { 20 | return TAG; 21 | } 22 | 23 | public static void setLogEnabled(boolean var0) { 24 | debugLog = var0; 25 | } 26 | 27 | public static boolean isDebugLog() { 28 | return debugLog; 29 | } 30 | 31 | /** 32 | * @return true: 继续执行 false:停止执行 33 | **/ 34 | public static void startCheck(TaskQueue taskQueue, OnTaskListener onTaskListener) { 35 | new SafeCheck.Builder() 36 | .setTaskQueue(taskQueue) 37 | .setOnTaskListener(onTaskListener) 38 | .build() 39 | .startCheck(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /library/src/main/java/com/lzy/safecheck/TaskEvent.java: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck; 2 | 3 | /** 4 | * Create by liuzhiyou on 2020/8/6 17:25 5 | * Email:1130294881@qq.com 6 | */ 7 | public class TaskEvent { 8 | private String tag; 9 | private boolean checkPass; //true: 检查通过 false:不通过,需要手动处理 10 | 11 | public TaskEvent(String tag, boolean checkPass) { 12 | this.tag = tag; 13 | this.checkPass = checkPass; 14 | } 15 | 16 | public String getTag() { 17 | return tag; 18 | } 19 | 20 | public boolean isCheckPass() { 21 | return checkPass; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /library/src/main/java/com/lzy/safecheck/TaskQueue.java: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck; 2 | 3 | import com.lzy.safecheck.task.ICheckTask; 4 | 5 | import java.util.ArrayDeque; 6 | import java.util.Queue; 7 | 8 | /** 9 | * Create by liuzhiyou on 2020/11/9 18:06 10 | * Email:1130294881@qq.com 11 | */ 12 | public class TaskQueue { 13 | 14 | private Queue mQueue = new ArrayDeque<>(); 15 | 16 | public void addTask(ICheckTask task) { 17 | if (task == null) return; 18 | mQueue.add(task); 19 | } 20 | 21 | public ICheckTask pollTask() { 22 | return mQueue.poll(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /library/src/main/java/com/lzy/safecheck/listener/OnTaskEventListener.java: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck.listener; 2 | 3 | import com.lzy.safecheck.TaskEvent; 4 | 5 | /** 6 | * Create by liuzhiyou on 2020/11/6 18:23 7 | * Email:1130294881@qq.com 8 | */ 9 | public interface OnTaskEventListener { 10 | /** 11 | * @param taskEvent 当前任务的事件信息 12 | * @param callTaskEvent 是否调用{@link OnTaskListener} 的 onTaskEvent 方法 13 | */ 14 | void onEvent(TaskEvent taskEvent, boolean callTaskEvent); 15 | } 16 | -------------------------------------------------------------------------------- /library/src/main/java/com/lzy/safecheck/listener/OnTaskListener.java: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck.listener; 2 | 3 | import com.lzy.safecheck.ISafeCheck; 4 | import com.lzy.safecheck.TaskEvent; 5 | 6 | /** 7 | * Create by liuzhiyou on 2020/8/6 17:24 8 | * Email:1130294881@qq.com 9 | */ 10 | public interface OnTaskListener { 11 | void onStart(); 12 | 13 | void onTaskEvent(ISafeCheck iSafeCheck, TaskEvent taskEvent); 14 | 15 | void onComplete(); 16 | } 17 | -------------------------------------------------------------------------------- /library/src/main/java/com/lzy/safecheck/task/AbstractCheckTask.java: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck.task; 2 | 3 | import android.content.DialogInterface; 4 | 5 | import com.lzy.safecheck.R; 6 | import com.lzy.safecheck.TaskEvent; 7 | import com.lzy.safecheck.listener.OnTaskEventListener; 8 | import com.lzy.safecheck.listener.OnTaskListener; 9 | import com.lzy.safecheck.utils.Utils; 10 | 11 | import androidx.annotation.StringRes; 12 | import androidx.appcompat.app.AlertDialog; 13 | import androidx.fragment.app.FragmentActivity; 14 | 15 | /** 16 | * Create by liuzhiyou on 2020/11/9 16:35 17 | * Email:1130294881@qq.com 18 | */ 19 | public abstract class AbstractCheckTask implements ICheckTask { 20 | 21 | protected FragmentActivity mActivity; 22 | protected OnTaskEventListener mOnTaskEventListener; 23 | 24 | public AbstractCheckTask(FragmentActivity activity) { 25 | this.mActivity = activity; 26 | } 27 | 28 | @Override 29 | final public void execute(OnTaskEventListener onTaskEventListener) { 30 | this.mOnTaskEventListener = onTaskEventListener; 31 | execute(); 32 | } 33 | 34 | protected void execute() { 35 | boolean checkPass = check(); 36 | callTaskEventListener(checkPass); 37 | if (!checkPass) { 38 | interruptCheck(); 39 | } 40 | } 41 | 42 | /** 43 | * 具体的检查逻辑 44 | */ 45 | protected abstract boolean check(); 46 | 47 | /** 48 | * 检查不通过时处理 49 | */ 50 | protected void interruptCheck() { 51 | new AlertDialog.Builder(mActivity) 52 | .setCancelable(false) 53 | .setTitle(getString(R.string.safe_check_warning)) 54 | .setMessage(String.format(getString(R.string.safe_check_fail_msg), getTag())) 55 | .setNegativeButton(getString(R.string.safe_check_sure), new DialogInterface.OnClickListener() { 56 | @Override 57 | public void onClick(DialogInterface dialog, int which) { 58 | dialog.dismiss(); 59 | callTaskEventListener(true, false); 60 | } 61 | }) 62 | .setNeutralButton(getString(R.string.safe_check_exit), new DialogInterface.OnClickListener() { 63 | @Override 64 | public void onClick(DialogInterface dialog, int which) { 65 | Utils.exitApp(); 66 | } 67 | }) 68 | .create() 69 | .show(); 70 | } 71 | 72 | protected void callTaskEventListener(boolean result) { 73 | if (mOnTaskEventListener != null) { 74 | mOnTaskEventListener.onEvent(getTaskEvent(result), true); 75 | } 76 | } 77 | 78 | /** 79 | * 调用 {@link OnTaskEventListener} 的 onEvent 方法 80 | * 81 | * @param checkPass 是否通过检查 82 | * @param callTaskEvent 是否调用{@link OnTaskListener} 的 onTaskEvent 方法 83 | */ 84 | protected void callTaskEventListener(boolean checkPass, boolean callTaskEvent) { 85 | if (mOnTaskEventListener != null) { 86 | mOnTaskEventListener.onEvent(getTaskEvent(checkPass), callTaskEvent); 87 | } 88 | } 89 | 90 | protected TaskEvent getTaskEvent(boolean checkPass) { 91 | return new TaskEvent(getTag(), checkPass); 92 | } 93 | 94 | @Override 95 | public String getTag() { 96 | return getClass().getSimpleName(); 97 | } 98 | 99 | public String getString(@StringRes int resId) { 100 | return mActivity.getResources().getString(resId); 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /library/src/main/java/com/lzy/safecheck/task/DebugCheckTask.java: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck.task; 2 | 3 | import androidx.fragment.app.FragmentActivity; 4 | 5 | import com.lzy.safecheck.utils.DebugCheckUtil; 6 | 7 | /** 8 | * 调试检查task 9 | * Create by 2020/6/22 from liuzhiyou 10 | **/ 11 | public class DebugCheckTask extends AbstractCheckTask { 12 | 13 | public static final String TAG = DebugCheckTask.class.getSimpleName(); 14 | 15 | public DebugCheckTask(FragmentActivity activity) { 16 | super(activity); 17 | } 18 | 19 | @Override 20 | protected boolean check() { 21 | DebugCheckUtil.getInstance(mActivity.getApplication()).check(true); 22 | return true; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /library/src/main/java/com/lzy/safecheck/task/ICheckTask.java: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck.task; 2 | 3 | import com.lzy.safecheck.listener.OnTaskEventListener; 4 | 5 | public interface ICheckTask { 6 | 7 | /** 8 | * 执行任务 9 | * 10 | **/ 11 | void execute(OnTaskEventListener onTaskEventListener); 12 | 13 | String getTag(); 14 | } 15 | -------------------------------------------------------------------------------- /library/src/main/java/com/lzy/safecheck/task/NetProxyCheckTask.java: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck.task; 2 | 3 | import android.content.DialogInterface; 4 | 5 | import androidx.appcompat.app.AlertDialog; 6 | import androidx.fragment.app.FragmentActivity; 7 | 8 | import com.lzy.safecheck.R; 9 | import com.lzy.safecheck.utils.Utils; 10 | 11 | /** 12 | * 网络代理检查task(防抓包) 13 | * Create by 2020/6/22 from liuzhiyou 14 | **/ 15 | public class NetProxyCheckTask extends AbstractCheckTask { 16 | 17 | public static final String TAG = NetProxyCheckTask.class.getSimpleName(); 18 | 19 | protected boolean mIsOpenNetProxyCheck; 20 | 21 | public NetProxyCheckTask(FragmentActivity activity, boolean isOpenNetProxyCheck) { 22 | super(activity); 23 | this.mIsOpenNetProxyCheck = isOpenNetProxyCheck; 24 | } 25 | 26 | @Override 27 | protected boolean check() { 28 | if (!mIsOpenNetProxyCheck) return true; 29 | return !Utils.isWifiProxy(mActivity); 30 | } 31 | 32 | @Override 33 | protected void interruptCheck() { 34 | showNoticeDig(); 35 | } 36 | 37 | protected void showNoticeDig() { 38 | new AlertDialog.Builder(mActivity) 39 | .setCancelable(false) 40 | .setTitle(getString(R.string.safe_check_warning)) 41 | .setMessage(getString(R.string.safe_check_net_proxy_warning_msg)) 42 | .setNegativeButton(getString(R.string.safe_check_sure), new DialogInterface.OnClickListener() { 43 | @Override 44 | public void onClick(DialogInterface dialog, int which) { 45 | Utils.exitApp(); 46 | } 47 | }) 48 | .create() 49 | .show(); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /library/src/main/java/com/lzy/safecheck/task/PageHackCheckTask.java: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck.task; 2 | 3 | import android.app.Activity; 4 | import android.app.Application; 5 | import android.widget.Toast; 6 | 7 | import com.lzy.safecheck.utils.SafeCheckActivityLifecycle; 8 | import com.lzy.safecheck.utils.Utils; 9 | 10 | import androidx.fragment.app.FragmentActivity; 11 | 12 | /** 13 | * Create by liuzhiyou on 2020/11/12 10:36 14 | * Email:1130294881@qq.com 15 | */ 16 | public class PageHackCheckTask extends AbstractCheckTask { 17 | 18 | public static final String TAG = RootCheckTask.class.getSimpleName(); 19 | 20 | public PageHackCheckTask(FragmentActivity activity) { 21 | super(activity); 22 | } 23 | 24 | @Override 25 | protected boolean check() { 26 | final Application app = mActivity.getApplication(); 27 | app.registerActivityLifecycleCallbacks(new SafeCheckActivityLifecycle() { 28 | @Override 29 | public void onForeground(Activity activity) { 30 | 31 | } 32 | 33 | @Override 34 | public void onBackground(Activity activity) { 35 | Toast.makeText(app.getApplicationContext(), Utils.getAppName(app) + "已退至后台", Toast.LENGTH_LONG).show(); 36 | } 37 | }); 38 | return true; 39 | } 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /library/src/main/java/com/lzy/safecheck/task/RootCheckTask.java: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck.task; 2 | 3 | import android.content.DialogInterface; 4 | 5 | import androidx.appcompat.app.AlertDialog; 6 | import androidx.fragment.app.FragmentActivity; 7 | 8 | import com.lzy.safecheck.R; 9 | import com.lzy.safecheck.utils.Utils; 10 | 11 | /** 12 | * root检查task 13 | * Create by 2020/6/22 from liuzhiyou 14 | **/ 15 | public class RootCheckTask extends AbstractCheckTask { 16 | 17 | public static final String TAG = RootCheckTask.class.getSimpleName(); 18 | 19 | public RootCheckTask(FragmentActivity activity) { 20 | super(activity); 21 | } 22 | 23 | @Override 24 | protected boolean check() { 25 | return !Utils.isDeviceRooted(); 26 | } 27 | 28 | @Override 29 | protected void interruptCheck() { 30 | showNoticeDig(); 31 | } 32 | 33 | public void showNoticeDig() { 34 | new AlertDialog.Builder(mActivity) 35 | .setCancelable(false) 36 | .setTitle(getString(R.string.safe_check_warning)) 37 | .setMessage(getString(R.string.safe_check_root_warning_msg)) 38 | .setNegativeButton(getString(R.string.safe_check_root_warning_sure), new DialogInterface.OnClickListener() { 39 | @Override 40 | public void onClick(DialogInterface dialog, int which) { 41 | dialog.dismiss(); 42 | callTaskEventListener(true,false); 43 | } 44 | }) 45 | .setNeutralButton(getString(R.string.safe_check_exit), new DialogInterface.OnClickListener() { 46 | @Override 47 | public void onClick(DialogInterface dialog, int which) { 48 | Utils.exitApp(); 49 | } 50 | }) 51 | .create() 52 | .show(); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /library/src/main/java/com/lzy/safecheck/task/SignCheckTask.java: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck.task; 2 | 3 | import android.content.DialogInterface; 4 | 5 | import androidx.appcompat.app.AlertDialog; 6 | import androidx.fragment.app.FragmentActivity; 7 | 8 | import com.lzy.safecheck.R; 9 | import com.lzy.safecheck.utils.SignCheckUtil; 10 | import com.lzy.safecheck.utils.Utils; 11 | 12 | /** 13 | * App正版签名校验task 14 | * Create by 2020/6/16 from liuzhiyou 15 | **/ 16 | public class SignCheckTask extends AbstractCheckTask { 17 | 18 | public static final String TAG = SignCheckTask.class.getSimpleName(); 19 | 20 | private String mRightCer; 21 | 22 | /** 23 | * 24 | * @param rightCer 正版签名的sha1 25 | */ 26 | public SignCheckTask(FragmentActivity activity, String rightCer) { 27 | super(activity); 28 | this.mRightCer = rightCer; 29 | } 30 | 31 | @Override 32 | protected boolean check() { 33 | SignCheckUtil signCheck = new SignCheckUtil(mActivity, mRightCer); 34 | Utils.log("sha1: " + signCheck.getCer()); 35 | return signCheck.check(); 36 | } 37 | 38 | @Override 39 | protected void interruptCheck() { 40 | showNoticeDig(); 41 | } 42 | 43 | protected void showNoticeDig() { 44 | new AlertDialog.Builder(mActivity) 45 | .setCancelable(false) 46 | .setTitle(getString(R.string.safe_check_warning)) 47 | .setMessage(getString(R.string.safe_check_sign_warning_msg)) 48 | .setNegativeButton(getString(R.string.safe_check_sure), new DialogInterface.OnClickListener() { 49 | @Override 50 | public void onClick(DialogInterface dialog, int which) { 51 | Utils.exitApp(); 52 | } 53 | }) 54 | .create() 55 | .show(); 56 | } 57 | 58 | 59 | } 60 | -------------------------------------------------------------------------------- /library/src/main/java/com/lzy/safecheck/task/SimulatorCheckTask.java: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck.task; 2 | 3 | import android.content.DialogInterface; 4 | 5 | import com.lzy.safecheck.R; 6 | import com.lzy.safecheck.utils.Utils; 7 | 8 | import androidx.appcompat.app.AlertDialog; 9 | import androidx.fragment.app.FragmentActivity; 10 | 11 | /** 12 | * 模拟器检查task 13 | * Create by 2020/6/22 from liuzhiyou 14 | **/ 15 | public class SimulatorCheckTask extends AbstractCheckTask { 16 | 17 | public static final String TAG = SimulatorCheckTask.class.getSimpleName(); 18 | 19 | public SimulatorCheckTask(FragmentActivity activity) { 20 | super(activity); 21 | } 22 | 23 | @Override 24 | protected boolean check() { 25 | return !Utils.isEmulator(mActivity); 26 | } 27 | 28 | @Override 29 | protected void interruptCheck() { 30 | showNoticeDig(); 31 | } 32 | 33 | protected void showNoticeDig() { 34 | new AlertDialog.Builder(mActivity) 35 | .setCancelable(false) 36 | .setTitle(getString(R.string.safe_check_warning)) 37 | .setMessage(getString(R.string.safe_check_simu_warning_msg)) 38 | .setNegativeButton(getString(R.string.safe_check_root_warning_sure), 39 | new DialogInterface.OnClickListener() { 40 | @Override 41 | public void onClick(DialogInterface dialog, int which) { 42 | dialog.dismiss(); 43 | callTaskEventListener(true, false); 44 | } 45 | }) 46 | .setNeutralButton(getString(R.string.safe_check_exit), new DialogInterface.OnClickListener() { 47 | @Override 48 | public void onClick(DialogInterface dialog, int which) { 49 | Utils.exitApp(); 50 | } 51 | }) 52 | .create() 53 | .show(); 54 | } 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /library/src/main/java/com/lzy/safecheck/utils/DebugCheckUtil.java: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck.utils; 2 | 3 | import android.app.Application; 4 | import android.content.pm.ApplicationInfo; 5 | import android.os.Debug; 6 | import android.os.SystemClock; 7 | import android.text.TextUtils; 8 | import android.util.Log; 9 | 10 | import com.lzy.safecheck.SafeCheckService; 11 | 12 | import java.io.BufferedReader; 13 | import java.io.FileReader; 14 | import java.io.IOException; 15 | import java.net.InetAddress; 16 | import java.net.Socket; 17 | import java.net.UnknownHostException; 18 | 19 | 20 | /** 21 | * 调试检查 22 | * 严格模式下1秒检查一次,检查5分钟 23 | * 普通模式下10秒检查一次,检查1分钟 24 | * Created by liuzhiyou on 2020-08-05. 25 | */ 26 | public class DebugCheckUtil { 27 | private static final String TAG = DebugCheckUtil.class.getSimpleName(); 28 | private static volatile DebugCheckUtil instance; 29 | private Application application; 30 | private static final int MODE_STRICT_TIME = 5 * 60 * 1000; 31 | private static final int MODE_NORMAL_TIME = 60 * 1000; 32 | private static final int MODE_STRICT_INTERVAL_TIME = 1000; 33 | private static final int MODE_NORMAL_INTERVAL_TIME = 10000; 34 | private static final int CHECK_DEFAULT_PORT = 23946; 35 | private volatile boolean isStopCheck; 36 | 37 | private volatile boolean isStrictMode; 38 | private long executionTime; 39 | private Thread checkThread; 40 | 41 | private DebugCheckUtil(Application application) { 42 | this.application = application; 43 | } 44 | 45 | public static DebugCheckUtil getInstance(Application application) { 46 | if (instance == null) { 47 | synchronized (DebugCheckUtil.class) { 48 | if (instance == null) { 49 | instance = new DebugCheckUtil(application); 50 | } 51 | } 52 | } 53 | return instance; 54 | } 55 | 56 | /** 57 | * 是否启动严格模式,严格模式耗资源 58 | * 59 | * @param isStrictMode 60 | */ 61 | public void check(boolean isStrictMode) { 62 | this.isStrictMode = isStrictMode; 63 | this.isStopCheck = false; 64 | this.executionTime = 0; 65 | if (checkThread != null && checkThread.isAlive()) return; 66 | checkThread = new Thread(new CheckRunnable(), TAG); 67 | checkThread.start(); 68 | } 69 | 70 | private boolean loopCheck(boolean isStrictMode) { 71 | if (!Utils.isBuildConfigDebug(application)) {//非调试模式 72 | Utils.log(" release debuggable run exit "); 73 | if (isDebuggable(application)) {//开启了debug 74 | return true; 75 | } 76 | boolean isDebug = Debug.isDebuggerConnected();//被调试器连接了 77 | Utils.log(" debugger connected exit " + isDebug); 78 | if (isDebug) { 79 | return true; 80 | } 81 | } 82 | if (!isStrictMode) return false; 83 | 84 | boolean portUsing = isLocalPortUsing(CHECK_DEFAULT_PORT); 85 | Utils.log(" portUsing using exit " + portUsing); 86 | if (portUsing) { 87 | return true; 88 | } 89 | 90 | boolean underTraced = isUnderTraced(); 91 | Utils.log(" traced exit underTraced : " + underTraced); 92 | if (underTraced) { 93 | return true; 94 | } 95 | return false; 96 | } 97 | 98 | private void killSelf() { 99 | // ActivityUtils.finishAllActivities(); 100 | android.os.Process.killProcess(android.os.Process.myPid()); 101 | } 102 | 103 | private boolean isDebuggable(Application app) { 104 | return 0 != (app.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE); 105 | } 106 | 107 | 108 | /** 109 | * pid调试检测 110 | */ 111 | private boolean isUnderTraced() { 112 | try { 113 | BufferedReader localBufferedReader = new BufferedReader(new FileReader("/proc/" + android.os.Process.myPid() + "/status")); 114 | int tracerPid = 0; 115 | for (; ; ) { 116 | String str = localBufferedReader.readLine(); 117 | if (TextUtils.isEmpty(str) || !str.contains("TracerPid")) continue; 118 | String tracerPidStr = str.substring(str.indexOf(":") + 1).trim(); 119 | if (TextUtils.isEmpty(tracerPidStr)) break; 120 | tracerPid = Integer.valueOf(tracerPidStr); 121 | break; 122 | } 123 | Utils.log(" tracerPid " + tracerPid); 124 | localBufferedReader.close(); 125 | return tracerPid > 1000; 126 | } catch (Exception e) { 127 | e.printStackTrace(); 128 | return false; 129 | } 130 | } 131 | 132 | /** 133 | * 本地调试端口检测 134 | */ 135 | public boolean isLocalPortUsing(int port) { 136 | boolean flag = true; 137 | try { 138 | flag = isPortUsing("127.0.0.1", port); 139 | } catch (Exception e) { 140 | // e.printStackTrace(); 141 | } 142 | return flag; 143 | } 144 | 145 | public boolean isPortUsing(String host, int port) throws UnknownHostException { 146 | boolean flag = false; 147 | InetAddress theAddress = InetAddress.getByName(host); 148 | try { 149 | Socket socket = new Socket(theAddress, port); 150 | flag = true; 151 | } catch (IOException e) { 152 | // e.printStackTrace(); 153 | } 154 | return flag; 155 | } 156 | 157 | 158 | private final class CheckRunnable implements Runnable { 159 | 160 | @Override 161 | public void run() { 162 | while (!isStopCheck && executionTime <= (isStrictMode ? MODE_STRICT_TIME : MODE_NORMAL_TIME)) { 163 | try { 164 | boolean isTrack = loopCheck(isStrictMode); 165 | Utils.log(" check run is track : " + isTrack + " thread id : " + Thread.currentThread().getId() + " execution time : " + executionTime); 166 | if (isTrack) { 167 | Log.e(SafeCheckService.getTAG(), "应用被调试,强制退出!"); 168 | killSelf(); 169 | break; 170 | } 171 | long sleepTime = isStrictMode ? MODE_STRICT_INTERVAL_TIME : MODE_NORMAL_INTERVAL_TIME; 172 | SystemClock.sleep(sleepTime); 173 | executionTime += sleepTime; 174 | } catch (Exception e) { 175 | e.printStackTrace(); 176 | } 177 | } 178 | } 179 | } 180 | 181 | public void onDestroy() { 182 | isStopCheck = true; 183 | executionTime = 0; 184 | } 185 | } 186 | 187 | -------------------------------------------------------------------------------- /library/src/main/java/com/lzy/safecheck/utils/SafeCheckActivityLifecycle.java: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck.utils; 2 | 3 | import android.app.Activity; 4 | import android.app.Application; 5 | import android.os.Bundle; 6 | 7 | import androidx.annotation.NonNull; 8 | import androidx.annotation.Nullable; 9 | 10 | /** 11 | * Create by liuzhiyou on 2020/11/12 10:28 12 | * Email:1130294881@qq.com 13 | */ 14 | public abstract class SafeCheckActivityLifecycle implements Application.ActivityLifecycleCallbacks { 15 | 16 | private int mForegroundCount = 0; 17 | private int mConfigCount = 0; 18 | private boolean mIsBackground = false; 19 | 20 | @Override 21 | public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) { 22 | 23 | } 24 | 25 | @Override 26 | public void onActivityStarted(@NonNull Activity activity) { 27 | if (mConfigCount < 0) { 28 | ++mConfigCount; 29 | } else { 30 | ++mForegroundCount; 31 | } 32 | } 33 | 34 | @Override 35 | public void onActivityResumed(@NonNull Activity activity) { 36 | if (mIsBackground) { 37 | mIsBackground = false; 38 | postStatus(activity, true); 39 | } 40 | } 41 | 42 | @Override 43 | public void onActivityPaused(@NonNull Activity activity) { 44 | 45 | } 46 | 47 | @Override 48 | public void onActivityStopped(@NonNull Activity activity) { 49 | if (activity.isChangingConfigurations()) { 50 | --mConfigCount; 51 | } else { 52 | --mForegroundCount; 53 | if (mForegroundCount <= 0) { 54 | mIsBackground = true; 55 | postStatus(activity, false); 56 | } 57 | } 58 | } 59 | 60 | @Override 61 | public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) { 62 | 63 | } 64 | 65 | @Override 66 | public void onActivityDestroyed(@NonNull Activity activity) { 67 | 68 | } 69 | 70 | private void postStatus(final Activity activity, final boolean isForeground) { 71 | if (isForeground) { 72 | onForeground(activity); 73 | } else { 74 | onBackground(activity); 75 | } 76 | } 77 | 78 | 79 | public abstract void onForeground(Activity activity); 80 | 81 | public abstract void onBackground(Activity activity); 82 | 83 | } 84 | -------------------------------------------------------------------------------- /library/src/main/java/com/lzy/safecheck/utils/SignCheckUtil.java: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck.utils; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.content.Context; 5 | import android.content.pm.PackageInfo; 6 | import android.content.pm.PackageManager; 7 | import android.content.pm.Signature; 8 | import android.text.TextUtils; 9 | 10 | import java.security.MessageDigest; 11 | import java.security.NoSuchAlgorithmException; 12 | 13 | public class SignCheckUtil { 14 | private Context context; 15 | private String cer = null; 16 | private String realCer = null; 17 | private static final String TAG = "SignCheck"; 18 | 19 | public SignCheckUtil(Context context) { 20 | this.context = context; 21 | this.cer = getCertificateSHA1Fingerprint(); 22 | } 23 | 24 | public SignCheckUtil(Context context, String realCer) { 25 | this.context = context; 26 | this.realCer = realCer; 27 | this.cer = getCertificateSHA1Fingerprint(); 28 | } 29 | 30 | 31 | public String getRealCer() { 32 | return realCer; 33 | } 34 | 35 | /** 36 | * 设置正确的签名 37 | * 38 | * @param realCer 39 | */ 40 | public void setRealCer(String realCer) { 41 | this.realCer = realCer; 42 | } 43 | 44 | //当前应用的签名sha1 45 | public String getCer() { 46 | return cer; 47 | } 48 | 49 | /** 50 | * 获取应用的签名 51 | * 52 | * @return 53 | */ 54 | public String getCertificateSHA1Fingerprint() { 55 | return getAppSignatureSHA1(); 56 | } 57 | 58 | /** 59 | * 检测签名是否正确 60 | * 61 | * @return true 签名正常 false 签名不正常 62 | */ 63 | public boolean check() { 64 | if (!TextUtils.isEmpty(realCer)) { 65 | realCer = realCer.trim(); 66 | cer = cer.trim(); 67 | if (this.realCer.equals(this.cer)) { 68 | return true; 69 | } 70 | } else { 71 | Utils.log("未给定正版的签名 SHA-1 值"); 72 | } 73 | return false; 74 | } 75 | 76 | public String getAppSignatureSHA1() { 77 | return getAppSignatureSHA1(context.getPackageName()); 78 | } 79 | 80 | /** 81 | * Return the application's signature for SHA1 value. 82 | * 83 | * @param packageName The name of the package. 84 | * @return the application's signature for SHA1 value 85 | */ 86 | public String getAppSignatureSHA1(final String packageName) { 87 | return getAppSignatureHash(packageName, "SHA1"); 88 | } 89 | 90 | private String getAppSignatureHash(final String packageName, final String algorithm) { 91 | if (TextUtils.isEmpty(packageName)) return ""; 92 | Signature[] signature = getAppSignature(packageName); 93 | if (signature == null || signature.length <= 0) return ""; 94 | return bytes2HexString(hashTemplate(signature[0].toByteArray(), algorithm)) 95 | .replaceAll("(?<=[0-9A-F]{2})[0-9A-F]{2}", ":$0"); 96 | } 97 | 98 | public Signature[] getAppSignature(final String packageName) { 99 | if (TextUtils.isEmpty(packageName)) return null; 100 | try { 101 | PackageManager pm =context .getPackageManager(); 102 | @SuppressLint("PackageManagerGetSignatures") 103 | PackageInfo pi = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES); 104 | return pi == null ? null : pi.signatures; 105 | } catch (PackageManager.NameNotFoundException e) { 106 | e.printStackTrace(); 107 | return null; 108 | } 109 | } 110 | 111 | private static final char[] HEX_DIGITS = 112 | {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 113 | 114 | private static String bytes2HexString(final byte[] bytes) { 115 | if (bytes == null) return ""; 116 | int len = bytes.length; 117 | if (len <= 0) return ""; 118 | char[] ret = new char[len << 1]; 119 | for (int i = 0, j = 0; i < len; i++) { 120 | ret[j++] = HEX_DIGITS[bytes[i] >> 4 & 0x0f]; 121 | ret[j++] = HEX_DIGITS[bytes[i] & 0x0f]; 122 | } 123 | return new String(ret); 124 | } 125 | 126 | private static byte[] hashTemplate(final byte[] data, final String algorithm) { 127 | if (data == null || data.length <= 0) return null; 128 | try { 129 | MessageDigest md = MessageDigest.getInstance(algorithm); 130 | md.update(data); 131 | return md.digest(); 132 | } catch (NoSuchAlgorithmException e) { 133 | e.printStackTrace(); 134 | return null; 135 | } 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /library/src/main/java/com/lzy/safecheck/utils/Utils.java: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck.utils; 2 | 3 | import android.app.Activity; 4 | import android.app.Application; 5 | import android.content.Context; 6 | import android.content.Intent; 7 | import android.content.pm.PackageInfo; 8 | import android.content.pm.PackageManager; 9 | import android.net.Uri; 10 | import android.os.Build; 11 | import android.telephony.TelephonyManager; 12 | import android.text.TextUtils; 13 | import android.util.Log; 14 | 15 | import com.lzy.safecheck.SafeCheckService; 16 | 17 | import java.io.File; 18 | import java.lang.reflect.Field; 19 | 20 | /** 21 | * Create by liuzhiyou on 2020/8/5 5:23 PM 22 | */ 23 | public class Utils { 24 | 25 | public static void log(String msg) { 26 | if (SafeCheckService.isDebugLog()) Log.d(SafeCheckService.getTAG(), msg); 27 | } 28 | 29 | public static void exitApp() { 30 | System.exit(0); 31 | } 32 | 33 | /** 34 | * 判断是否是debug模式 35 | */ 36 | public static boolean isBuildConfigDebug(Context context) { 37 | try { 38 | Class clazz = Class.forName(context.getPackageName() + ".BuildConfig"); 39 | Field field = clazz.getField("DEBUG"); 40 | field.setAccessible(true); 41 | return field.getBoolean(null); 42 | } catch (ClassNotFoundException e) { 43 | e.printStackTrace(); 44 | } catch (NoSuchFieldException e) { 45 | e.printStackTrace(); 46 | } catch (IllegalAccessException e) { 47 | e.printStackTrace(); 48 | } 49 | return false; 50 | } 51 | 52 | public static boolean isWifiProxy(Context context) { 53 | final boolean IS_ICS_OR_LATER = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH; 54 | String proxyAddress; 55 | int proxyPort; 56 | if (IS_ICS_OR_LATER) { 57 | proxyAddress = System.getProperty("http.proxyHost"); 58 | String portStr = System.getProperty("http.proxyPort"); 59 | proxyPort = Integer.parseInt((portStr != null ? portStr : "-1")); 60 | } else { 61 | proxyAddress = android.net.Proxy.getHost(context); 62 | proxyPort = android.net.Proxy.getPort(context); 63 | } 64 | return (!TextUtils.isEmpty(proxyAddress)) && (proxyPort != -1); 65 | 66 | } 67 | 68 | public static boolean isDeviceRooted() { 69 | String su = "su"; 70 | String[] locations = {"/system/bin/", "/system/xbin/", "/sbin/", "/system/sd/xbin/", 71 | "/system/bin/failsafe/", "/data/local/xbin/", "/data/local/bin/", "/data/local/", 72 | "/system/sbin/", "/usr/bin/", "/vendor/bin/"}; 73 | for (String location : locations) { 74 | if (new File(location + su).exists()) { 75 | return true; 76 | } 77 | } 78 | return false; 79 | } 80 | 81 | public static boolean isEmulator(Activity activity) { 82 | boolean checkProperty = Build.FINGERPRINT.startsWith("generic") 83 | || Build.FINGERPRINT.toLowerCase().contains("vbox") 84 | || Build.FINGERPRINT.toLowerCase().contains("test-keys") 85 | || Build.MODEL.contains("google_sdk") 86 | || Build.MODEL.contains("Emulator") 87 | || Build.MODEL.contains("Android SDK built for x86") 88 | || Build.MANUFACTURER.contains("Genymotion") 89 | || (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic")) 90 | || "google_sdk".equals(Build.PRODUCT); 91 | if (checkProperty) return true; 92 | 93 | String operatorName = ""; 94 | TelephonyManager tm = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE); 95 | if (tm != null) { 96 | String name = tm.getNetworkOperatorName(); 97 | if (name != null) { 98 | operatorName = name; 99 | } 100 | } 101 | boolean checkOperatorName = operatorName.toLowerCase().equals("android"); 102 | if (checkOperatorName) return true; 103 | 104 | String url = "tel:" + "123456"; 105 | Intent intent = new Intent(); 106 | intent.setData(Uri.parse(url)); 107 | intent.setAction(Intent.ACTION_DIAL); 108 | boolean checkDial = intent.resolveActivity(activity.getPackageManager()) == null; 109 | if (checkDial) return true; 110 | 111 | return false; 112 | } 113 | 114 | 115 | public static String getAppName(Application app) { 116 | try { 117 | PackageManager pm = app.getPackageManager(); 118 | PackageInfo pi = pm.getPackageInfo(app.getPackageName(), 0); 119 | return pi == null ? null : pi.applicationInfo.loadLabel(pm).toString(); 120 | } catch (PackageManager.NameNotFoundException e) { 121 | e.printStackTrace(); 122 | return ""; 123 | } 124 | } 125 | 126 | } 127 | -------------------------------------------------------------------------------- /library/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 警告 3 | 确定 4 | 退出应用 5 | 请前往官方渠道下载正版应用 6 | 为了您的信息安全,请不要在root的设备上使用该应用 7 | 我已了解该风险 8 | 您正在模拟器上使用该应用,请注意隐私安全 9 | 为了您的信息安全,请不要使用代理网络访问数据 10 | %s检查不通过 11 | 12 | -------------------------------------------------------------------------------- /library/src/test/java/com/lzy/safecheck/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.lzy.safecheck 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':library' 2 | include ':app' 3 | rootProject.name = "SafeCheckProject" -------------------------------------------------------------------------------- /test.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ElivenLZY/AndroidSafeCheck/a925b96e27fe797ee69d9ddce8ca2cdaea2c6f5b/test.jks --------------------------------------------------------------------------------