From 7f5c712e88b47261591408ac1009dba78ca47cfe Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 16 Sep 2022 18:02:40 +0200 Subject: [PATCH] No need to use `@SuppressLint("NewApi")` when `@ChecksSdkIntAtLeast` is used - more cleanup --- .../api/securestorage/SecretStoringUtils.kt | 7 ++----- .../im/vector/app/core/extensions/Context.kt | 4 +--- .../VectorActivityLifecycleCallbacks.kt | 2 -- .../features/login/LoginCaptchaFragment.kt | 1 - .../notifications/NotificationUtils.kt | 3 --- .../onboarding/ftueauth/CaptchaWebview.kt | 1 - .../lockscreen/biometrics/BiometricHelper.kt | 2 -- .../pin/lockscreen/crypto/KeyStoreCrypto.kt | 5 +---- .../crypto/LockScreenKeysMigrator.kt | 1 - .../migrations/MissingSystemKeyMigrator.kt | 5 ++--- .../pin/lockscreen/ui/LockScreenViewModel.kt | 20 +++++++++---------- .../VectorSettingsSecurityPrivacyFragment.kt | 2 -- .../features/widgets/webview/WidgetWebView.kt | 2 -- 13 files changed, 15 insertions(+), 40 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/securestorage/SecretStoringUtils.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/securestorage/SecretStoringUtils.kt index e701e0f3ba..234a8eee98 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/securestorage/SecretStoringUtils.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/securestorage/SecretStoringUtils.kt @@ -131,11 +131,10 @@ class SecretStoringUtils @Inject constructor( * * The secret is encrypted using the following method: AES/GCM/NoPadding */ - @SuppressLint("NewApi") @Throws(Exception::class) fun securelyStoreBytes(secret: ByteArray, keyAlias: String): ByteArray { return when { - buildVersionSdkIntProvider.get() >= Build.VERSION_CODES.M -> encryptBytesM(secret, keyAlias) + buildVersionSdkIntProvider.isAtLeast(Build.VERSION_CODES.M) -> encryptBytesM(secret, keyAlias) else -> encryptBytes(secret, keyAlias) } } @@ -156,10 +155,9 @@ class SecretStoringUtils @Inject constructor( } } - @SuppressLint("NewApi") fun securelyStoreObject(any: Any, keyAlias: String, output: OutputStream) { when { - buildVersionSdkIntProvider.get() >= Build.VERSION_CODES.M -> saveSecureObjectM(keyAlias, output, any) + buildVersionSdkIntProvider.isAtLeast(Build.VERSION_CODES.M) -> saveSecureObjectM(keyAlias, output, any) else -> saveSecureObject(keyAlias, output, any) } } @@ -189,7 +187,6 @@ class SecretStoringUtils @Inject constructor( return cipher } - @SuppressLint("NewApi") @RequiresApi(Build.VERSION_CODES.M) private fun getOrGenerateSymmetricKeyForAliasM(alias: String): SecretKey { val secretKeyEntry = (keyStore.getEntry(alias, null) as? KeyStore.SecretKeyEntry) diff --git a/vector/src/main/java/im/vector/app/core/extensions/Context.kt b/vector/src/main/java/im/vector/app/core/extensions/Context.kt index 1ed5aa8ba1..2e3e8c9306 100644 --- a/vector/src/main/java/im/vector/app/core/extensions/Context.kt +++ b/vector/src/main/java/im/vector/app/core/extensions/Context.kt @@ -16,7 +16,6 @@ package im.vector.app.core.extensions -import android.annotation.SuppressLint import android.content.Context import android.graphics.drawable.Drawable import android.net.ConnectivityManager @@ -91,10 +90,9 @@ fun Context.safeOpenOutputStream(uri: Uri): OutputStream? { * * @return true if no active connection is found */ -@SuppressLint("NewApi") // false positive fun Context.inferNoConnectivity(sdkIntProvider: BuildVersionSdkIntProvider): Boolean { val connectivityManager = getSystemService()!! - return if (sdkIntProvider.get() > Build.VERSION_CODES.M) { + return if (sdkIntProvider.isAtLeast(Build.VERSION_CODES.M)) { val networkCapabilities = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork) when { networkCapabilities?.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) == true -> false diff --git a/vector/src/main/java/im/vector/app/features/lifecycle/VectorActivityLifecycleCallbacks.kt b/vector/src/main/java/im/vector/app/features/lifecycle/VectorActivityLifecycleCallbacks.kt index 75f02c36d7..c884843f5c 100644 --- a/vector/src/main/java/im/vector/app/features/lifecycle/VectorActivityLifecycleCallbacks.kt +++ b/vector/src/main/java/im/vector/app/features/lifecycle/VectorActivityLifecycleCallbacks.kt @@ -16,7 +16,6 @@ package im.vector.app.features.lifecycle -import android.annotation.SuppressLint import android.app.Activity import android.app.ActivityManager import android.app.Application @@ -91,7 +90,6 @@ class VectorActivityLifecycleCallbacks constructor(private val popupAlertManager * * @return true if an app task is corrupted by a potentially malicious activity */ - @SuppressLint("NewApi") private suspend fun isTaskCorrupted(activity: Activity): Boolean = withContext(Dispatchers.Default) { val context = activity.applicationContext val packageManager: PackageManager = context.packageManager diff --git a/vector/src/main/java/im/vector/app/features/login/LoginCaptchaFragment.kt b/vector/src/main/java/im/vector/app/features/login/LoginCaptchaFragment.kt index 25403b06f3..b082e37933 100644 --- a/vector/src/main/java/im/vector/app/features/login/LoginCaptchaFragment.kt +++ b/vector/src/main/java/im/vector/app/features/login/LoginCaptchaFragment.kt @@ -144,7 +144,6 @@ class LoginCaptchaFragment : // runOnUiThread(Runnable { finish() }) } - @SuppressLint("NewApi") override fun onReceivedHttpError(view: WebView, request: WebResourceRequest, errorResponse: WebResourceResponse) { super.onReceivedHttpError(view, request, errorResponse) diff --git a/vector/src/main/java/im/vector/app/features/notifications/NotificationUtils.kt b/vector/src/main/java/im/vector/app/features/notifications/NotificationUtils.kt index cef94b5db2..7e91a89603 100755 --- a/vector/src/main/java/im/vector/app/features/notifications/NotificationUtils.kt +++ b/vector/src/main/java/im/vector/app/features/notifications/NotificationUtils.kt @@ -218,7 +218,6 @@ class NotificationUtils @Inject constructor( * @param withProgress true to show indeterminate progress on the notification * @return the polling thread listener notification */ - @SuppressLint("NewApi") fun buildForegroundServiceNotification(@StringRes subTitleResId: Int, withProgress: Boolean = true): Notification { // build the pending intent go to the home screen if this is clicked. val i = HomeActivity.newIntent(context, firstStartMainActivity = false) @@ -287,7 +286,6 @@ class NotificationUtils @Inject constructor( * @param fromBg true if the app is in background when posting the notification * @return the call notification. */ - @SuppressLint("NewApi") fun buildIncomingCallNotification( call: WebRtcCall, title: String, @@ -420,7 +418,6 @@ class NotificationUtils @Inject constructor( * @param title title of the notification * @return the call notification. */ - @SuppressLint("NewApi") fun buildPendingCallNotification( call: WebRtcCall, title: String diff --git a/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/CaptchaWebview.kt b/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/CaptchaWebview.kt index 23c6c13b5e..11f257c4e8 100644 --- a/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/CaptchaWebview.kt +++ b/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/CaptchaWebview.kt @@ -92,7 +92,6 @@ class CaptchaWebview @Inject constructor( Timber.e("## onError() : $errorMessage") } - @SuppressLint("NewApi") override fun onReceivedHttpError(view: WebView, request: WebResourceRequest, errorResponse: WebResourceResponse) { super.onReceivedHttpError(view, request, errorResponse) when { diff --git a/vector/src/main/java/im/vector/app/features/pin/lockscreen/biometrics/BiometricHelper.kt b/vector/src/main/java/im/vector/app/features/pin/lockscreen/biometrics/BiometricHelper.kt index 9bcf6e4264..026ee159ed 100644 --- a/vector/src/main/java/im/vector/app/features/pin/lockscreen/biometrics/BiometricHelper.kt +++ b/vector/src/main/java/im/vector/app/features/pin/lockscreen/biometrics/BiometricHelper.kt @@ -16,7 +16,6 @@ package im.vector.app.features.pin.lockscreen.biometrics -import android.annotation.SuppressLint import android.content.Context import android.os.Build import androidx.annotation.MainThread @@ -156,7 +155,6 @@ class BiometricHelper @AssistedInject constructor( return authenticate(activity) } - @SuppressLint("NewApi") @OptIn(ExperimentalCoroutinesApi::class) private fun authenticateInternal( activity: FragmentActivity, diff --git a/vector/src/main/java/im/vector/app/features/pin/lockscreen/crypto/KeyStoreCrypto.kt b/vector/src/main/java/im/vector/app/features/pin/lockscreen/crypto/KeyStoreCrypto.kt index a42ce3a9b7..fd676f1662 100644 --- a/vector/src/main/java/im/vector/app/features/pin/lockscreen/crypto/KeyStoreCrypto.kt +++ b/vector/src/main/java/im/vector/app/features/pin/lockscreen/crypto/KeyStoreCrypto.kt @@ -16,7 +16,6 @@ package im.vector.app.features.pin.lockscreen.crypto -import android.annotation.SuppressLint import android.content.Context import android.os.Build import android.security.keystore.KeyPermanentlyInvalidatedException @@ -55,7 +54,6 @@ class KeyStoreCrypto @AssistedInject constructor( * Ensures a [Key] for the [alias] exists and validates it. * @throws KeyPermanentlyInvalidatedException if key is not valid. */ - @SuppressLint("NewApi") @Throws(KeyPermanentlyInvalidatedException::class) fun ensureKey() = secretStoringUtils.ensureKey(alias).also { // Check validity of Key by initializing an encryption Cipher @@ -109,10 +107,9 @@ class KeyStoreCrypto @AssistedInject constructor( /** * Check if the key associated with the [alias] is valid. */ - @SuppressLint("NewApi") fun hasValidKey(): Boolean { val keyExists = hasKey() - return if (buildVersionSdkIntProvider.get() >= Build.VERSION_CODES.M && keyExists) { + return if (buildVersionSdkIntProvider.isAtLeast(Build.VERSION_CODES.M) && keyExists) { val initializedKey = tryOrNull("Error validating lockscreen system key.") { ensureKey() } initializedKey != null } else { diff --git a/vector/src/main/java/im/vector/app/features/pin/lockscreen/crypto/LockScreenKeysMigrator.kt b/vector/src/main/java/im/vector/app/features/pin/lockscreen/crypto/LockScreenKeysMigrator.kt index 84e98785f4..c2d70a3734 100644 --- a/vector/src/main/java/im/vector/app/features/pin/lockscreen/crypto/LockScreenKeysMigrator.kt +++ b/vector/src/main/java/im/vector/app/features/pin/lockscreen/crypto/LockScreenKeysMigrator.kt @@ -16,7 +16,6 @@ package im.vector.app.features.pin.lockscreen.crypto -import android.annotation.SuppressLint import android.os.Build import im.vector.app.features.pin.lockscreen.crypto.migrations.LegacyPinCodeMigrator import im.vector.app.features.pin.lockscreen.crypto.migrations.MissingSystemKeyMigrator diff --git a/vector/src/main/java/im/vector/app/features/pin/lockscreen/crypto/migrations/MissingSystemKeyMigrator.kt b/vector/src/main/java/im/vector/app/features/pin/lockscreen/crypto/migrations/MissingSystemKeyMigrator.kt index 4c33c14954..7593aa6de3 100644 --- a/vector/src/main/java/im/vector/app/features/pin/lockscreen/crypto/migrations/MissingSystemKeyMigrator.kt +++ b/vector/src/main/java/im/vector/app/features/pin/lockscreen/crypto/migrations/MissingSystemKeyMigrator.kt @@ -16,7 +16,6 @@ package im.vector.app.features.pin.lockscreen.crypto.migrations -import android.annotation.SuppressLint import android.os.Build import im.vector.app.features.pin.lockscreen.crypto.KeyStoreCrypto import im.vector.app.features.pin.lockscreen.di.BiometricKeyAlias @@ -38,9 +37,9 @@ class MissingSystemKeyMigrator @Inject constructor( /** * If user had biometric auth enabled, ensure system key exists, creating one if needed. */ - @SuppressLint("NewApi") fun migrateIfNeeded() { - if (buildVersionSdkIntProvider.get() >= Build.VERSION_CODES.M && vectorPreferences.useBiometricsToUnlock()) { + if (buildVersionSdkIntProvider.isAtLeast(Build.VERSION_CODES.M) && + vectorPreferences.useBiometricsToUnlock()) { val systemKeyStoreCrypto = keystoreCryptoFactory.provide(systemKeyAlias, true) runCatching { systemKeyStoreCrypto.ensureKey() diff --git a/vector/src/main/java/im/vector/app/features/pin/lockscreen/ui/LockScreenViewModel.kt b/vector/src/main/java/im/vector/app/features/pin/lockscreen/ui/LockScreenViewModel.kt index 33ea590f1d..87d3f93f9b 100644 --- a/vector/src/main/java/im/vector/app/features/pin/lockscreen/ui/LockScreenViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/pin/lockscreen/ui/LockScreenViewModel.kt @@ -16,7 +16,6 @@ package im.vector.app.features.pin.lockscreen.ui -import android.annotation.SuppressLint import android.app.KeyguardManager import android.os.Build import android.security.keystore.KeyPermanentlyInvalidatedException @@ -139,12 +138,12 @@ class LockScreenViewModel @AssistedInject constructor( } }.launchIn(viewModelScope) - @SuppressLint("NewApi") private fun showBiometricPrompt(activity: FragmentActivity) = flow { emitAll(biometricHelper.authenticate(activity)) }.catch { error -> when { - versionProvider.get() >= Build.VERSION_CODES.M && error is KeyPermanentlyInvalidatedException -> { + versionProvider.isAtLeast(Build.VERSION_CODES.M) && + error is KeyPermanentlyInvalidatedException -> { onBiometricKeyInvalidated() } else -> { @@ -168,15 +167,14 @@ class LockScreenViewModel @AssistedInject constructor( _viewEvents.post(LockScreenViewEvent.ShowBiometricKeyInvalidatedMessage) } - @SuppressLint("NewApi") private suspend fun updateStateWithBiometricInfo() { // This is a terrible hack, but I found no other way to ensure this would be called only after the device is considered unlocked on Android 12+ waitUntilKeyguardIsUnlocked() setState { val isBiometricKeyInvalidated = biometricHelper.hasSystemKey && !biometricHelper.isSystemKeyValid val canUseBiometricAuth = lockScreenConfiguration.mode == LockScreenMode.VERIFY && - !isSystemAuthTemporarilyDisabledByBiometricPrompt && - biometricHelper.isSystemAuthEnabledAndValid + !isSystemAuthTemporarilyDisabledByBiometricPrompt && + biometricHelper.isSystemAuthEnabledAndValid val showBiometricPromptAutomatically = canUseBiometricAuth && lockScreenConfiguration.autoStartBiometric copy( canUseBiometricAuth = canUseBiometricAuth, @@ -191,12 +189,12 @@ class LockScreenViewModel @AssistedInject constructor( * after an Activity's `onResume` method. If we mix that with the system keys needing the device to be unlocked before they're used, we get crashes. * See issue [#6768](https://github.com/vector-im/element-android/issues/6768). */ - @SuppressLint("NewApi") private suspend fun waitUntilKeyguardIsUnlocked() { - if (versionProvider.get() < Build.VERSION_CODES.S) return - withTimeoutOrNull(5.seconds) { - while (keyguardManager.isDeviceLocked) { - delay(50.milliseconds) + if (versionProvider.isAtLeast(Build.VERSION_CODES.S)) { + withTimeoutOrNull(5.seconds) { + while (keyguardManager.isDeviceLocked) { + delay(50.milliseconds) + } } } } diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsSecurityPrivacyFragment.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsSecurityPrivacyFragment.kt index ecb1779a4a..5cbdf114a5 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsSecurityPrivacyFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsSecurityPrivacyFragment.kt @@ -17,7 +17,6 @@ package im.vector.app.features.settings -import android.annotation.SuppressLint import android.app.Activity import android.content.Intent import android.net.Uri @@ -448,7 +447,6 @@ class VectorSettingsSecurityPrivacyFragment : /** * Manage the e2e keys import. */ - @SuppressLint("NewApi") private fun importKeys() { openFileSelection( requireActivity(), diff --git a/vector/src/main/java/im/vector/app/features/widgets/webview/WidgetWebView.kt b/vector/src/main/java/im/vector/app/features/widgets/webview/WidgetWebView.kt index ac9930866f..254a7f97f5 100644 --- a/vector/src/main/java/im/vector/app/features/widgets/webview/WidgetWebView.kt +++ b/vector/src/main/java/im/vector/app/features/widgets/webview/WidgetWebView.kt @@ -16,7 +16,6 @@ package im.vector.app.features.widgets.webview -import android.annotation.SuppressLint import android.app.Activity import android.view.ViewGroup import android.webkit.CookieManager @@ -29,7 +28,6 @@ import im.vector.app.features.themes.ThemeUtils import im.vector.app.features.webview.VectorWebViewClient import im.vector.app.features.webview.WebEventListener -@SuppressLint("NewApi") fun WebView.setupForWidget(activity: Activity, checkWebViewPermissionsUseCase: CheckWebViewPermissionsUseCase, eventListener: WebEventListener,