Skip to content

Commit 4812468

Browse files
author
lucky
committed
fix max failed password attempts not showing on Android before Q
1 parent c9ad201 commit 4812468

File tree

8 files changed

+31
-43
lines changed

8 files changed

+31
-43
lines changed

app/build.gradle

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ android {
1010
applicationId "me.lucky.sentry"
1111
minSdk 23
1212
targetSdk 32
13-
versionCode 2
14-
versionName "1.0.1"
13+
versionCode 3
14+
versionName "1.0.2"
1515

1616
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1717
}
@@ -39,10 +39,10 @@ android {
3939
}
4040

4141
dependencies {
42-
implementation 'androidx.core:core-ktx:1.7.0'
43-
implementation 'androidx.appcompat:appcompat:1.4.1'
44-
implementation 'com.google.android.material:material:1.5.0'
45-
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
42+
implementation 'androidx.core:core-ktx:1.8.0'
43+
implementation 'androidx.appcompat:appcompat:1.4.2'
44+
implementation 'com.google.android.material:material:1.6.1'
45+
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
4646
testImplementation 'junit:junit:4.13.2'
4747
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
4848
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

app/src/main/java/me/lucky/sentry/DeviceAdminReceiver.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ class DeviceAdminReceiver : DeviceAdminReceiver() {
1111
super.onPasswordFailed(context, intent, user)
1212
val prefs = Preferences(context)
1313
val maxFailedPasswordAttempts = prefs.maxFailedPasswordAttempts
14-
if (!prefs.isServiceEnabled || maxFailedPasswordAttempts <= 0) return
14+
if (!prefs.isEnabled || maxFailedPasswordAttempts <= 0) return
1515
val admin = DeviceAdminManager(context)
1616
if (admin.getCurrentFailedPasswordAttempts() >= maxFailedPasswordAttempts)
1717
admin.wipeData()
1818
}
1919

2020
override fun onDisabled(context: Context, intent: Intent) {
2121
super.onDisabled(context, intent)
22-
if (Preferences(context).isServiceEnabled)
22+
if (Preferences(context).isEnabled)
2323
Toast.makeText(context, R.string.service_unavailable_popup, Toast.LENGTH_SHORT).show()
2424
}
2525

app/src/main/java/me/lucky/sentry/MainActivity.kt

+7-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package me.lucky.sentry
33
import android.content.pm.PackageManager
44
import android.os.Build
55
import android.os.Bundle
6-
import android.util.Log
76
import android.view.View
87
import androidx.activity.result.contract.ActivityResultContracts
98
import androidx.appcompat.app.AppCompatActivity
@@ -12,10 +11,6 @@ import com.google.android.material.snackbar.Snackbar
1211
import me.lucky.sentry.databinding.ActivityMainBinding
1312

1413
class MainActivity : AppCompatActivity() {
15-
companion object {
16-
private val TAG = MainActivity::class.simpleName
17-
}
18-
1914
private lateinit var binding: ActivityMainBinding
2015
private lateinit var prefs: Preferences
2116
private lateinit var admin: DeviceAdminManager
@@ -41,14 +36,15 @@ class MainActivity : AppCompatActivity() {
4136
private fun init() {
4237
prefs = Preferences(this)
4338
admin = DeviceAdminManager(this)
44-
if (!packageManager.hasSystemFeature(PackageManager.FEATURE_SECURE_LOCK_SCREEN))
45-
hideSecureLockScreenRequired()
39+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
40+
!packageManager.hasSystemFeature(PackageManager.FEATURE_SECURE_LOCK_SCREEN))
41+
hideSecureLockScreenRequired()
4642
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || !admin.canUsbDataSignalingBeDisabled())
4743
hideUsbDataSignaling()
4844
binding.apply {
4945
maxFailedPasswordAttempts.value = prefs.maxFailedPasswordAttempts.toFloat()
5046
usbDataSignaling.isChecked = isUsbDataSignalingEnabled()
51-
toggle.isChecked = prefs.isServiceEnabled
47+
toggle.isChecked = prefs.isEnabled
5248
}
5349
}
5450

@@ -66,7 +62,6 @@ class MainActivity : AppCompatActivity() {
6662
try {
6763
admin.setUsbDataSignalingEnabled(isChecked)
6864
} catch (exc: Exception) {
69-
Log.e(TAG, "usbDataSignaling", exc)
7065
Snackbar.make(
7166
usbDataSignaling,
7267
R.string.usb_data_signaling_change_failed_popup,
@@ -97,19 +92,19 @@ class MainActivity : AppCompatActivity() {
9792
}
9893

9994
private fun setOn() {
100-
prefs.isServiceEnabled = true
95+
prefs.isEnabled = true
10196
}
10297

10398
private fun setOff() {
104-
prefs.isServiceEnabled = false
99+
prefs.isEnabled = false
105100
admin.remove()
106101
}
107102

108103
private fun update() {
109104
binding.apply {
110105
usbDataSignaling.isChecked = isUsbDataSignalingEnabled()
111106
}
112-
if (prefs.isServiceEnabled && !admin.isActive())
107+
if (prefs.isEnabled && !admin.isActive())
113108
Snackbar.make(
114109
binding.toggle,
115110
R.string.service_unavailable_popup,

app/src/main/java/me/lucky/sentry/NotificationListenerService.kt

+5-14
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,9 @@ import android.content.Intent
77
import android.content.IntentFilter
88
import android.os.Build
99
import android.service.notification.NotificationListenerService
10-
import android.util.Log
1110
import androidx.annotation.RequiresApi
12-
import java.lang.Exception
1311

1412
class NotificationListenerService : NotificationListenerService() {
15-
companion object {
16-
private val TAG = NotificationListenerService::class.simpleName
17-
}
18-
1913
private val lockReceiver = LockReceiver()
2014

2115
override fun onCreate() {
@@ -52,25 +46,22 @@ class NotificationListenerService : NotificationListenerService() {
5246
private class LockReceiver : BroadcastReceiver() {
5347
override fun onReceive(context: Context?, intent: Intent?) {
5448
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S ||
55-
!Preferences(context ?: return).isServiceEnabled) return
49+
!Preferences(context ?: return).isEnabled) return
5650
when (intent?.action) {
5751
Intent.ACTION_USER_PRESENT -> {
5852
if (context.getSystemService(KeyguardManager::class.java)
5953
?.isDeviceSecure != true) return
60-
setUsbDataSignalingEnabled(context, true, "user present")
54+
setUsbDataSignalingEnabled(context, true)
6155
}
62-
Intent.ACTION_SCREEN_OFF ->
63-
setUsbDataSignalingEnabled(context, false, "screen off")
56+
Intent.ACTION_SCREEN_OFF -> setUsbDataSignalingEnabled(context, false)
6457
}
6558
}
6659

6760
@RequiresApi(Build.VERSION_CODES.S)
68-
private fun setUsbDataSignalingEnabled(ctx: Context, enabled: Boolean, msg: String) {
61+
private fun setUsbDataSignalingEnabled(ctx: Context, enabled: Boolean) {
6962
try {
7063
DeviceAdminManager(ctx).setUsbDataSignalingEnabled(enabled)
71-
} catch (exc: Exception) {
72-
Log.e(TAG, msg, exc)
73-
}
64+
} catch (exc: Exception) {}
7465
}
7566
}
7667
}

app/src/main/java/me/lucky/sentry/Preferences.kt

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import androidx.security.crypto.MasterKeys
77

88
class Preferences(ctx: Context) {
99
companion object {
10-
private const val SERVICE_ENABLED = "service_enabled"
10+
private const val ENABLED = "enabled"
1111
private const val MAX_FAILED_PASSWORD_ATTEMPTS = "max_failed_password_attempts"
1212

1313
private const val FILE_NAME = "sec_shared_prefs"
14+
// migration
15+
private const val SERVICE_ENABLED = "service_enabled"
1416
}
1517

1618
private val mk = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
@@ -22,9 +24,9 @@ class Preferences(ctx: Context) {
2224
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM,
2325
)
2426

25-
var isServiceEnabled: Boolean
26-
get() = prefs.getBoolean(SERVICE_ENABLED, false)
27-
set(value) = prefs.edit { putBoolean(SERVICE_ENABLED, value) }
27+
var isEnabled: Boolean
28+
get() = prefs.getBoolean(ENABLED, prefs.getBoolean(SERVICE_ENABLED, false))
29+
set(value) = prefs.edit { putBoolean(ENABLED, value) }
2830

2931
var maxFailedPasswordAttempts: Int
3032
get() = prefs.getInt(MAX_FAILED_PASSWORD_ATTEMPTS, 0)

build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22
plugins {
3-
id 'com.android.application' version '7.1.2' apply false
4-
id 'com.android.library' version '7.1.2' apply false
3+
id 'com.android.application' version '7.2.1' apply false
4+
id 'com.android.library' version '7.2.1' apply false
55
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
66
}
77

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fix max failed password attempts not showing on Android before Q
+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#Sat Feb 19 09:22:29 MSK 2022
1+
#Sun Jun 12 01:10:07 MSK 2022
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME
7-
distributionSha256Sum=f581709a9c35e9cb92e16f585d2c4bc99b2b1a5f85d2badbd3dc6bff59e1e6dd

0 commit comments

Comments
 (0)