refactor: migrate to androidx.core APIs for Bundle/Intent API changes

This commit is contained in:
Harsh Shandilya 2023-03-27 17:45:35 +05:30
parent 6931f2d7f5
commit 8b97a4a3f1
No known key found for this signature in database
7 changed files with 43 additions and 60 deletions

View file

@ -17,6 +17,7 @@ import android.text.format.DateUtils
import android.view.View import android.view.View
import android.view.autofill.AutofillManager import android.view.autofill.AutofillManager
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.IntentCompat
import app.passwordstore.R import app.passwordstore.R
import app.passwordstore.databinding.ActivityOreoAutofillPublisherChangedBinding import app.passwordstore.databinding.ActivityOreoAutofillPublisherChangedBinding
import app.passwordstore.util.autofill.AutofillMatcher 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.asLog
import app.passwordstore.util.extensions.getApplicationInfoCompat import app.passwordstore.util.extensions.getApplicationInfoCompat
import app.passwordstore.util.extensions.getPackageInfoCompat import app.passwordstore.util.extensions.getPackageInfoCompat
import app.passwordstore.util.extensions.getParcelableExtraCompat
import app.passwordstore.util.extensions.viewBinding import app.passwordstore.util.extensions.viewBinding
import com.github.androidpasswordstore.autofillparser.FormOrigin import com.github.androidpasswordstore.autofillparser.FormOrigin
import com.github.androidpasswordstore.autofillparser.computeCertificatesHash import com.github.androidpasswordstore.autofillparser.computeCertificatesHash
@ -97,7 +97,11 @@ class AutofillPublisherChangedActivity : AppCompatActivity() {
FormOrigin.App(appPackage) FormOrigin.App(appPackage)
) )
val fillResponse = val fillResponse =
intent.getParcelableExtraCompat<FillResponse>(EXTRA_FILL_RESPONSE_AFTER_RESET) IntentCompat.getParcelableExtra(
intent,
EXTRA_FILL_RESPONSE_AFTER_RESET,
FillResponse::class.java
)
setResult( setResult(
RESULT_OK, RESULT_OK,
Intent().apply { putExtra(AutofillManager.EXTRA_AUTHENTICATION_RESULT, fillResponse) } Intent().apply { putExtra(AutofillManager.EXTRA_AUTHENTICATION_RESULT, fillResponse) }

View file

@ -8,9 +8,9 @@ package app.passwordstore.ui.settings
import android.os.Bundle import android.os.Bundle
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.BundleCompat
import app.passwordstore.R import app.passwordstore.R
import app.passwordstore.databinding.ActivityPreferenceRecyclerviewBinding import app.passwordstore.databinding.ActivityPreferenceRecyclerviewBinding
import app.passwordstore.util.extensions.getParcelableCompat
import app.passwordstore.util.extensions.viewBinding import app.passwordstore.util.extensions.viewBinding
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import de.Maxr1998.modernpreferences.Preference import de.Maxr1998.modernpreferences.Preference
@ -84,9 +84,14 @@ class SettingsActivity : AppCompatActivity() {
getString(subScreen.titleRes) getString(subScreen.titleRes)
} }
} }
savedInstanceState if (savedInstanceState != null) {
?.getParcelableCompat<PreferencesAdapter.SavedState>("adapter") BundleCompat.getParcelable(
?.let(adapter::loadSavedState) savedInstanceState,
"adapter",
PreferencesAdapter.SavedState::class.java
)
?.let(adapter::loadSavedState)
}
binding.preferenceRecyclerView.adapter = adapter binding.preferenceRecyclerView.adapter = adapter
} }

View file

@ -16,8 +16,6 @@ import android.content.pm.PackageManager
import android.content.pm.PackageManager.ApplicationInfoFlags import android.content.pm.PackageManager.ApplicationInfoFlags
import android.content.pm.PackageManager.PackageInfoFlags import android.content.pm.PackageManager.PackageInfoFlags
import android.os.Build import android.os.Build
import android.os.Bundle
import android.os.Parcelable
import android.util.Base64 import android.util.Base64
import android.util.TypedValue import android.util.TypedValue
import android.view.View import android.view.View
@ -140,32 +138,6 @@ fun String.base64(): String {
return Base64.encodeToString(encodeToByteArray(), Base64.NO_WRAP) 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 { fun PackageManager.getPackageInfoCompat(packageName: String, flags: Int): PackageInfo {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getPackageInfo(packageName, PackageInfoFlags.of(flags.toLong())) getPackageInfo(packageName, PackageInfoFlags.of(flags.toLong()))

View file

@ -13,11 +13,11 @@ import android.net.Uri
import android.os.Build import android.os.Build
import android.os.IBinder import android.os.IBinder
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.content.IntentCompat
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import app.passwordstore.R import app.passwordstore.R
import app.passwordstore.data.repo.PasswordRepository import app.passwordstore.data.repo.PasswordRepository
import app.passwordstore.util.extensions.getParcelableExtraCompat
import java.time.LocalDateTime import java.time.LocalDateTime
import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter
import java.util.Calendar import java.util.Calendar
@ -30,7 +30,7 @@ class PasswordExportService : Service() {
if (intent != null) { if (intent != null) {
when (intent.action) { when (intent.action) {
ACTION_EXPORT_PASSWORD -> { ACTION_EXPORT_PASSWORD -> {
val uri = intent.getParcelableExtraCompat<Uri>("uri") val uri = IntentCompat.getParcelableExtra(intent, "uri", Uri::class.java)
if (uri != null) { if (uri != null) {
val targetDirectory = DocumentFile.fromTreeUri(applicationContext, uri) val targetDirectory = DocumentFile.fromTreeUri(applicationContext, uri)

View file

@ -18,6 +18,7 @@ android {
dependencies { dependencies {
implementation(libs.androidx.annotation) implementation(libs.androidx.annotation)
implementation(libs.androidx.autofill) implementation(libs.androidx.autofill)
implementation(libs.androidx.core.ktx)
implementation(libs.kotlin.coroutines.android) implementation(libs.kotlin.coroutines.android)
implementation(libs.kotlin.coroutines.core) implementation(libs.kotlin.coroutines.core)
implementation(libs.thirdparty.logcat) implementation(libs.thirdparty.logcat)

View file

@ -7,12 +7,12 @@ package com.github.androidpasswordstore.autofillparser
import android.app.assist.AssistStructure import android.app.assist.AssistStructure
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Parcelable
import android.service.autofill.Dataset import android.service.autofill.Dataset
import android.service.autofill.Field import android.service.autofill.Field
import android.view.autofill.AutofillId import android.view.autofill.AutofillId
import android.view.autofill.AutofillValue import android.view.autofill.AutofillValue
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.core.os.BundleCompat
import logcat.LogPriority.ERROR import logcat.LogPriority.ERROR
import logcat.asLog import logcat.asLog
import logcat.logcat import logcat.logcat
@ -57,18 +57,36 @@ public sealed class AutofillScenario<out T : Any> {
return try { return try {
Builder<AutofillId>() Builder<AutofillId>()
.apply { .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) 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( currentPassword.addAll(
clientState.getParcelableArrayListCompat(BUNDLE_KEY_CURRENT_PASSWORD_IDS) BundleCompat.getParcelableArrayList(
clientState,
BUNDLE_KEY_CURRENT_PASSWORD_IDS,
AutofillId::class.java
)
?: emptyList() ?: emptyList()
) )
newPassword.addAll( newPassword.addAll(
clientState.getParcelableArrayListCompat(BUNDLE_KEY_NEW_PASSWORD_IDS) ?: emptyList() BundleCompat.getParcelableArrayList(
clientState,
BUNDLE_KEY_NEW_PASSWORD_IDS,
AutofillId::class.java
)
?: emptyList()
) )
genericPassword.addAll( genericPassword.addAll(
clientState.getParcelableArrayListCompat(BUNDLE_KEY_GENERIC_PASSWORD_IDS) BundleCompat.getParcelableArrayList(
clientState,
BUNDLE_KEY_GENERIC_PASSWORD_IDS,
AutofillId::class.java
)
?: emptyList() ?: emptyList()
) )
} }
@ -78,24 +96,6 @@ public sealed class AutofillScenario<out T : Any> {
null 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> { internal class Builder<T : Any> {

View file

@ -25,6 +25,7 @@
<ID>LongMethod:PasswordCreationActivity.kt$PasswordCreationActivity$private fun encrypt()</ID> <ID>LongMethod:PasswordCreationActivity.kt$PasswordCreationActivity$private fun encrypt()</ID>
<ID>LongMethod:PasswordFragment.kt$PasswordFragment$private fun initializePasswordList()</ID> <ID>LongMethod:PasswordFragment.kt$PasswordFragment$private fun initializePasswordList()</ID>
<ID>LongMethod:RepositorySettings.kt$RepositorySettings$override fun provideSettings(builder: PreferenceScreen.Builder)</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&lt;String&gt; 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 -&gt; val newPath = oldNewPathMap[match] ?: return@map match logcat { "Updating match for $key: $match --&gt; $newPath" } newPath } .toSet() if (newMatches != oldMatches) prefs.edit { putStringSet(key, newMatches) } }</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&lt;String&gt; 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 -&gt; val newPath = oldNewPathMap[match] ?: return@map match logcat { "Updating match for $key: $match --&gt; $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>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> <ID>MagicNumber:ClipboardService.kt$ClipboardService$1000</ID>