diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2023-03-27 17:45:35 +0530 |
---|---|---|
committer | Harsh Shandilya <me@msfjarvis.dev> | 2023-03-27 17:53:10 +0530 |
commit | 8b97a4a3f1dc799d327dbb7ac631d266a7a986a4 (patch) | |
tree | 63a37d740baef9757c0b90f0679fdcf1e7ce200a | |
parent | 6931f2d7f51fd5d4ed48a6548744c656bf333b6b (diff) |
refactor: migrate to androidx.core APIs for `Bundle`/`Intent` API changes
7 files changed, 43 insertions, 60 deletions
diff --git a/app/src/main/java/app/passwordstore/ui/autofill/AutofillPublisherChangedActivity.kt b/app/src/main/java/app/passwordstore/ui/autofill/AutofillPublisherChangedActivity.kt index 6d348a23..25075746 100644 --- a/app/src/main/java/app/passwordstore/ui/autofill/AutofillPublisherChangedActivity.kt +++ b/app/src/main/java/app/passwordstore/ui/autofill/AutofillPublisherChangedActivity.kt @@ -17,6 +17,7 @@ import android.text.format.DateUtils import android.view.View import android.view.autofill.AutofillManager import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.IntentCompat import app.passwordstore.R import app.passwordstore.databinding.ActivityOreoAutofillPublisherChangedBinding import app.passwordstore.util.autofill.AutofillMatcher @@ -24,7 +25,6 @@ import app.passwordstore.util.autofill.AutofillPublisherChangedException import app.passwordstore.util.extensions.asLog import app.passwordstore.util.extensions.getApplicationInfoCompat import app.passwordstore.util.extensions.getPackageInfoCompat -import app.passwordstore.util.extensions.getParcelableExtraCompat import app.passwordstore.util.extensions.viewBinding import com.github.androidpasswordstore.autofillparser.FormOrigin import com.github.androidpasswordstore.autofillparser.computeCertificatesHash @@ -97,7 +97,11 @@ class AutofillPublisherChangedActivity : AppCompatActivity() { FormOrigin.App(appPackage) ) val fillResponse = - intent.getParcelableExtraCompat<FillResponse>(EXTRA_FILL_RESPONSE_AFTER_RESET) + IntentCompat.getParcelableExtra( + intent, + EXTRA_FILL_RESPONSE_AFTER_RESET, + FillResponse::class.java + ) setResult( RESULT_OK, Intent().apply { putExtra(AutofillManager.EXTRA_AUTHENTICATION_RESULT, fillResponse) } diff --git a/app/src/main/java/app/passwordstore/ui/settings/SettingsActivity.kt b/app/src/main/java/app/passwordstore/ui/settings/SettingsActivity.kt index e4a9de8c..30e2b1b0 100644 --- a/app/src/main/java/app/passwordstore/ui/settings/SettingsActivity.kt +++ b/app/src/main/java/app/passwordstore/ui/settings/SettingsActivity.kt @@ -8,9 +8,9 @@ package app.passwordstore.ui.settings import android.os.Bundle import android.view.MenuItem import androidx.appcompat.app.AppCompatActivity +import androidx.core.os.BundleCompat import app.passwordstore.R import app.passwordstore.databinding.ActivityPreferenceRecyclerviewBinding -import app.passwordstore.util.extensions.getParcelableCompat import app.passwordstore.util.extensions.viewBinding import com.google.android.material.dialog.MaterialAlertDialogBuilder import de.Maxr1998.modernpreferences.Preference @@ -84,9 +84,14 @@ class SettingsActivity : AppCompatActivity() { getString(subScreen.titleRes) } } - savedInstanceState - ?.getParcelableCompat<PreferencesAdapter.SavedState>("adapter") - ?.let(adapter::loadSavedState) + if (savedInstanceState != null) { + BundleCompat.getParcelable( + savedInstanceState, + "adapter", + PreferencesAdapter.SavedState::class.java + ) + ?.let(adapter::loadSavedState) + } binding.preferenceRecyclerView.adapter = adapter } diff --git a/app/src/main/java/app/passwordstore/util/extensions/AndroidExtensions.kt b/app/src/main/java/app/passwordstore/util/extensions/AndroidExtensions.kt index 2c9dbc9f..d3e840c7 100644 --- a/app/src/main/java/app/passwordstore/util/extensions/AndroidExtensions.kt +++ b/app/src/main/java/app/passwordstore/util/extensions/AndroidExtensions.kt @@ -16,8 +16,6 @@ import android.content.pm.PackageManager import android.content.pm.PackageManager.ApplicationInfoFlags import android.content.pm.PackageManager.PackageInfoFlags import android.os.Build -import android.os.Bundle -import android.os.Parcelable import android.util.Base64 import android.util.TypedValue import android.view.View @@ -140,32 +138,6 @@ fun String.base64(): String { return Base64.encodeToString(encodeToByteArray(), Base64.NO_WRAP) } -inline fun <reified T> Bundle.getParcelableCompat(key: String): T? { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - getParcelable(key, T::class.java) - } else { - @Suppress("DEPRECATION") getParcelable(key) - } -} - -inline fun <reified T : Parcelable> Bundle.getParcelableArrayListCompat( - key: String -): ArrayList<T>? { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - getParcelableArrayList(key, T::class.java) - } else { - @Suppress("DEPRECATION") getParcelableArrayList(key) - } -} - -inline fun <reified T : Parcelable> Intent.getParcelableExtraCompat(key: String): T? { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - getParcelableExtra(key, T::class.java) - } else { - @Suppress("DEPRECATION") getParcelableExtra(key) - } -} - fun PackageManager.getPackageInfoCompat(packageName: String, flags: Int): PackageInfo { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { getPackageInfo(packageName, PackageInfoFlags.of(flags.toLong())) diff --git a/app/src/main/java/app/passwordstore/util/services/PasswordExportService.kt b/app/src/main/java/app/passwordstore/util/services/PasswordExportService.kt index 5bbeba50..ec1e654e 100644 --- a/app/src/main/java/app/passwordstore/util/services/PasswordExportService.kt +++ b/app/src/main/java/app/passwordstore/util/services/PasswordExportService.kt @@ -13,11 +13,11 @@ import android.net.Uri import android.os.Build import android.os.IBinder import androidx.core.app.NotificationCompat +import androidx.core.content.IntentCompat import androidx.core.content.getSystemService import androidx.documentfile.provider.DocumentFile import app.passwordstore.R import app.passwordstore.data.repo.PasswordRepository -import app.passwordstore.util.extensions.getParcelableExtraCompat import java.time.LocalDateTime import java.time.format.DateTimeFormatter import java.util.Calendar @@ -30,7 +30,7 @@ class PasswordExportService : Service() { if (intent != null) { when (intent.action) { ACTION_EXPORT_PASSWORD -> { - val uri = intent.getParcelableExtraCompat<Uri>("uri") + val uri = IntentCompat.getParcelableExtra(intent, "uri", Uri::class.java) if (uri != null) { val targetDirectory = DocumentFile.fromTreeUri(applicationContext, uri) diff --git a/autofill-parser/build.gradle.kts b/autofill-parser/build.gradle.kts index 844b7f3b..6f4d5909 100644 --- a/autofill-parser/build.gradle.kts +++ b/autofill-parser/build.gradle.kts @@ -18,6 +18,7 @@ android { dependencies { implementation(libs.androidx.annotation) implementation(libs.androidx.autofill) + implementation(libs.androidx.core.ktx) implementation(libs.kotlin.coroutines.android) implementation(libs.kotlin.coroutines.core) implementation(libs.thirdparty.logcat) diff --git a/autofill-parser/src/main/java/com/github/androidpasswordstore/autofillparser/AutofillScenario.kt b/autofill-parser/src/main/java/com/github/androidpasswordstore/autofillparser/AutofillScenario.kt index 4f14907c..08eab3ff 100644 --- a/autofill-parser/src/main/java/com/github/androidpasswordstore/autofillparser/AutofillScenario.kt +++ b/autofill-parser/src/main/java/com/github/androidpasswordstore/autofillparser/AutofillScenario.kt @@ -7,12 +7,12 @@ package com.github.androidpasswordstore.autofillparser import android.app.assist.AssistStructure import android.os.Build import android.os.Bundle -import android.os.Parcelable import android.service.autofill.Dataset import android.service.autofill.Field import android.view.autofill.AutofillId import android.view.autofill.AutofillValue import androidx.annotation.RequiresApi +import androidx.core.os.BundleCompat import logcat.LogPriority.ERROR import logcat.asLog import logcat.logcat @@ -57,18 +57,36 @@ public sealed class AutofillScenario<out T : Any> { return try { Builder<AutofillId>() .apply { - username = clientState.getParcelableCompat(BUNDLE_KEY_USERNAME_ID) + username = + BundleCompat.getParcelable( + clientState, + BUNDLE_KEY_USERNAME_ID, + AutofillId::class.java + ) fillUsername = clientState.getBoolean(BUNDLE_KEY_FILL_USERNAME) - otp = clientState.getParcelableCompat(BUNDLE_KEY_OTP_ID) + otp = BundleCompat.getParcelable(clientState, BUNDLE_KEY_OTP_ID, AutofillId::class.java) currentPassword.addAll( - clientState.getParcelableArrayListCompat(BUNDLE_KEY_CURRENT_PASSWORD_IDS) + BundleCompat.getParcelableArrayList( + clientState, + BUNDLE_KEY_CURRENT_PASSWORD_IDS, + AutofillId::class.java + ) ?: emptyList() ) newPassword.addAll( - clientState.getParcelableArrayListCompat(BUNDLE_KEY_NEW_PASSWORD_IDS) ?: emptyList() + BundleCompat.getParcelableArrayList( + clientState, + BUNDLE_KEY_NEW_PASSWORD_IDS, + AutofillId::class.java + ) + ?: emptyList() ) genericPassword.addAll( - clientState.getParcelableArrayListCompat(BUNDLE_KEY_GENERIC_PASSWORD_IDS) + BundleCompat.getParcelableArrayList( + clientState, + BUNDLE_KEY_GENERIC_PASSWORD_IDS, + AutofillId::class.java + ) ?: emptyList() ) } @@ -78,24 +96,6 @@ public sealed class AutofillScenario<out T : Any> { null } } - - private inline fun <reified T> Bundle.getParcelableCompat(key: String): T? { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - getParcelable(key, T::class.java) - } else { - @Suppress("DEPRECATION") getParcelable(key) - } - } - - private inline fun <reified T : Parcelable> Bundle.getParcelableArrayListCompat( - key: String - ): ArrayList<T>? { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - getParcelableArrayList(key, T::class.java) - } else { - @Suppress("DEPRECATION") getParcelableArrayList(key) - } - } } internal class Builder<T : Any> { diff --git a/detekt-baselines/app.xml b/detekt-baselines/app.xml index 594291d4..bf021902 100644 --- a/detekt-baselines/app.xml +++ b/detekt-baselines/app.xml @@ -25,6 +25,7 @@ <ID>LongMethod:PasswordCreationActivity.kt$PasswordCreationActivity$private fun encrypt()</ID> <ID>LongMethod:PasswordFragment.kt$PasswordFragment$private fun initializePasswordList()</ID> <ID>LongMethod:RepositorySettings.kt$RepositorySettings$override fun provideSettings(builder: PreferenceScreen.Builder)</ID> + <ID>LongMethod:SettingsActivity.kt$SettingsActivity$override fun onCreate(savedInstanceState: Bundle?)</ID> <ID>LoopWithTooManyJumpStatements:AutofillMatcher.kt$AutofillMatcher.Companion$for ((key, value) in prefs.all) { if (!key.startsWith(PREFERENCE_PREFIX_MATCHES)) continue // We know that preferences starting with `PREFERENCE_PREFIX_MATCHES` were // created with `putStringSet`. @Suppress("UNCHECKED_CAST") val oldMatches = value as? Set<String> if (oldMatches == null) { logcat(WARN) { "Failed to read matches for $key" } continue } // Delete all matches for file locations that are going to be overwritten, then // transfer matches over to the files at their new locations. val newMatches = oldMatches .asSequence() .minus(deletePathList) .minus(oldNewPathMap.values) .map { match -> val newPath = oldNewPathMap[match] ?: return@map match logcat { "Updating match for $key: $match --> $newPath" } newPath } .toSet() if (newMatches != oldMatches) prefs.edit { putStringSet(key, newMatches) } }</ID> <ID>LoopWithTooManyJumpStatements:ErrorMessages.kt$ErrorMessages$while (cause.cause != null) { if (cause is GitException) break val nextCause = cause.cause!! if (nextCause is RemoteException) break cause = nextCause }</ID> <ID>MagicNumber:ClipboardService.kt$ClipboardService$1000</ID> |