-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lucky
committed
Jul 9, 2022
1 parent
3182199
commit e68d9ca
Showing
13 changed files
with
142 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,139 @@ | ||
package me.lucky.sentry | ||
|
||
import android.content.pm.PackageManager | ||
import android.content.SharedPreferences | ||
import android.os.Build | ||
import android.os.Bundle | ||
import android.view.View | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.biometric.BiometricManager | ||
import androidx.biometric.BiometricPrompt | ||
import androidx.core.content.ContextCompat | ||
import com.google.android.material.snackbar.Snackbar | ||
|
||
import me.lucky.sentry.databinding.ActivityMainBinding | ||
|
||
class MainActivity : AppCompatActivity() { | ||
private lateinit var binding: ActivityMainBinding | ||
private lateinit var prefs: PreferencesProxy | ||
private lateinit var prefs: Preferences | ||
private lateinit var prefsdb: Preferences | ||
private lateinit var admin: DeviceAdminManager | ||
|
||
private val registerForDeviceAdmin = | ||
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { | ||
if (it.resultCode != RESULT_OK) binding.toggle.isChecked = false else setOn() | ||
if (it.resultCode != RESULT_OK) setOff() else setOn() | ||
} | ||
|
||
private val prefsListener = SharedPreferences.OnSharedPreferenceChangeListener { _, key -> | ||
prefs.copyTo(prefsdb, key) | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
binding = ActivityMainBinding.inflate(layoutInflater) | ||
setContentView(binding.root) | ||
init() | ||
if (initBiometric()) return | ||
setup() | ||
} | ||
|
||
override fun onStart() { | ||
super.onStart() | ||
prefs.registerListener(prefsListener) | ||
update() | ||
} | ||
|
||
override fun onStop() { | ||
super.onStop() | ||
prefs.unregisterListener(prefsListener) | ||
} | ||
|
||
private fun initBiometric(): Boolean { | ||
val authenticators = BiometricManager.Authenticators.BIOMETRIC_STRONG or | ||
BiometricManager.Authenticators.DEVICE_CREDENTIAL | ||
when (BiometricManager | ||
.from(this) | ||
.canAuthenticate(authenticators)) | ||
{ | ||
BiometricManager.BIOMETRIC_SUCCESS -> {} | ||
else -> return false | ||
} | ||
val executor = ContextCompat.getMainExecutor(this) | ||
val prompt = BiometricPrompt( | ||
this, | ||
executor, | ||
object : BiometricPrompt.AuthenticationCallback() | ||
{ | ||
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) { | ||
super.onAuthenticationError(errorCode, errString) | ||
finishAndRemoveTask() | ||
} | ||
|
||
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) { | ||
super.onAuthenticationSucceeded(result) | ||
setup() | ||
} | ||
}) | ||
prompt.authenticate(BiometricPrompt.PromptInfo.Builder() | ||
.setTitle(getString(R.string.biometric_title)) | ||
.setConfirmationRequired(false) | ||
.setAllowedAuthenticators(authenticators) | ||
.build()) | ||
return true | ||
} | ||
|
||
private fun init() { | ||
prefs = PreferencesProxy(this) | ||
prefs.clone() | ||
prefs = Preferences(this) | ||
prefsdb = Preferences(this, encrypted = false) | ||
prefs.copyTo(prefsdb) | ||
admin = DeviceAdminManager(this) | ||
if (prefs.isEnabled && prefs.maxFailedPasswordAttempts > 0) | ||
try { admin.setMaximumFailedPasswordsForWipe(0) } catch (exc: SecurityException) {} | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && | ||
!packageManager.hasSystemFeature(PackageManager.FEATURE_SECURE_LOCK_SCREEN)) | ||
hideSecureLockScreenRequired() | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || !admin.canUsbDataSignalingBeDisabled()) | ||
hideUsbDataSignaling() | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || | ||
!admin.canUsbDataSignalingBeDisabled() || | ||
!admin.isDeviceOwner()) | ||
disableUsbDataSignaling() | ||
binding.apply { | ||
maxFailedPasswordAttempts.value = prefs.maxFailedPasswordAttempts.toFloat() | ||
usbDataSignaling.isChecked = isUsbDataSignalingEnabled() | ||
toggle.isChecked = prefs.isEnabled | ||
} | ||
} | ||
|
||
private fun setup() { | ||
binding.apply { | ||
maxFailedPasswordAttempts.addOnChangeListener { _, value, _ -> | ||
prefs.maxFailedPasswordAttempts = value.toInt() | ||
} | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) | ||
usbDataSignaling.setOnCheckedChangeListener { _, isChecked -> | ||
try { | ||
admin.setUsbDataSignalingEnabled(isChecked) | ||
} catch (exc: Exception) { | ||
Snackbar.make( | ||
usbDataSignaling, | ||
R.string.usb_data_signaling_change_failed_popup, | ||
Snackbar.LENGTH_SHORT, | ||
).show() | ||
usbDataSignaling.isChecked = !isChecked | ||
} | ||
private fun setup() = binding.apply { | ||
maxFailedPasswordAttempts.addOnChangeListener { _, value, _ -> | ||
prefs.maxFailedPasswordAttempts = value.toInt() | ||
} | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) | ||
usbDataSignaling.setOnCheckedChangeListener { _, isChecked -> | ||
try { admin.setUsbDataSignalingEnabled(isChecked) } catch (exc: Exception) { | ||
Snackbar.make( | ||
usbDataSignaling, | ||
R.string.usb_data_signaling_change_failed_popup, | ||
Snackbar.LENGTH_SHORT, | ||
).show() | ||
usbDataSignaling.isChecked = !isChecked | ||
} | ||
toggle.setOnCheckedChangeListener { _, isChecked -> | ||
if (isChecked) requestAdmin() else setOff() | ||
} | ||
toggle.setOnCheckedChangeListener { _, isChecked -> | ||
if (isChecked) requestAdmin() else setOff() | ||
} | ||
} | ||
|
||
private fun hideSecureLockScreenRequired() { | ||
binding.apply { | ||
maxFailedPasswordAttempts.visibility = View.GONE | ||
maxFailedPasswordAttemptsDescription.visibility = View.GONE | ||
space.visibility = View.GONE | ||
} | ||
} | ||
|
||
private fun hideUsbDataSignaling() { | ||
binding.apply { | ||
usbDataSignaling.visibility = View.GONE | ||
usbDataSignalingDescription.visibility = View.GONE | ||
} | ||
} | ||
private fun disableUsbDataSignaling() { binding.usbDataSignaling.isEnabled = false } | ||
|
||
private fun setOn() { | ||
prefs.isEnabled = true | ||
binding.toggle.isChecked = true | ||
} | ||
|
||
private fun setOff() { | ||
prefs.isEnabled = false | ||
try { admin.remove() } catch (exc: SecurityException) {} | ||
binding.toggle.isChecked = false | ||
} | ||
|
||
private fun update() { | ||
binding.usbDataSignaling.isChecked = isUsbDataSignalingEnabled() | ||
if (prefs.isEnabled && !admin.isActive()) | ||
Snackbar.make( | ||
binding.toggle, | ||
R.string.service_unavailable_popup, | ||
Snackbar.LENGTH_SHORT, | ||
).show() | ||
} | ||
private fun update() { binding.usbDataSignaling.isChecked = isUsbDataSignalingEnabled() } | ||
|
||
private fun isUsbDataSignalingEnabled() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) | ||
admin.isUsbDataSignalingEnabled() else true | ||
|
||
private fun requestAdmin() = registerForDeviceAdmin.launch(admin.makeRequestIntent()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.