Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DiskReadViolation violations when StrictMode is enabled #2551

Merged
merged 1 commit into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class ExampleApplication : MultiDexApplication() {
.detectDiskWrites()
.detectAll()
.penaltyLog()
.also {
if (IS_PENALTY_DEATH_ENABLED) {
it.penaltyDeath()
}
}
.build()
)

Expand All @@ -45,4 +50,8 @@ class ExampleApplication : MultiDexApplication() {
false
)
}

private companion object {
private val IS_PENALTY_DEATH_ENABLED = true
}
}
21 changes: 14 additions & 7 deletions stripe/src/main/java/com/stripe/android/FingerprintDataStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@ package com.stripe.android

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.liveData
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import org.json.JSONObject

internal interface FingerprintDataStore {
fun get(): LiveData<FingerprintData>
fun save(fingerprintData: FingerprintData)

class Default(context: Context) : FingerprintDataStore {
private val prefs = context.getSharedPreferences(
PREF_FILE, Context.MODE_PRIVATE
)
class Default(
context: Context,
private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO
) : FingerprintDataStore {
private val prefs by lazy {
context.getSharedPreferences(
PREF_FILE, Context.MODE_PRIVATE
)
}

override fun get(): LiveData<FingerprintData> {
return MutableLiveData(
override fun get() = liveData<FingerprintData>(coroutineDispatcher) {
emit(
runCatching {
FingerprintData.fromJson(
JSONObject(prefs.getString(KEY_DATA, null).orEmpty())
Expand Down
46 changes: 22 additions & 24 deletions stripe/src/main/java/com/stripe/android/PaymentSessionPrefs.kt
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
package com.stripe.android

import android.content.Context
import android.content.SharedPreferences

internal interface PaymentSessionPrefs {
fun getPaymentMethodId(customerId: String?): String?
fun savePaymentMethodId(customerId: String, paymentMethodId: String?)

class Default(context: Context) : PaymentSessionPrefs {
val prefs: SharedPreferences by lazy {
context.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE)
}

override fun getPaymentMethodId(customerId: String?): String? {
return customerId?.let {
prefs.getString(getPaymentMethodKey(it), null)
}
}

override fun savePaymentMethodId(
customerId: String,
paymentMethodId: String?
) {
prefs.edit()
.putString(getPaymentMethodKey(customerId), paymentMethodId)
.apply()
}
}

companion object {
private const val PREF_FILE = "PaymentSessionPrefs"

private fun getPaymentMethodKey(customerId: String?): String {
return "customer[$customerId].payment_method"
}

@JvmSynthetic
internal fun create(context: Context): PaymentSessionPrefs {
val prefs = context.getSharedPreferences(
PREF_FILE, Context.MODE_PRIVATE
)
return object : PaymentSessionPrefs {
override fun getPaymentMethodId(customerId: String?): String? {
return if (customerId != null) {
prefs?.getString(getPaymentMethodKey(customerId), null)
} else {
null
}
}

override fun savePaymentMethodId(
customerId: String,
paymentMethodId: String?
) {
prefs?.edit()?.putString(getPaymentMethodKey(customerId), paymentMethodId)
?.apply()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class PaymentSessionViewModel(
private val savedStateHandle: SavedStateHandle,
paymentSessionData: PaymentSessionData,
private val customerSession: CustomerSession,
private val paymentSessionPrefs: PaymentSessionPrefs = PaymentSessionPrefs.create(application)
private val paymentSessionPrefs: PaymentSessionPrefs = PaymentSessionPrefs.Default(application)
) : AndroidViewModel(application) {

var paymentSessionData: PaymentSessionData = paymentSessionData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import org.robolectric.RobolectricTestRunner
@ExperimentalCoroutinesApi
class FingerprintDataRepositoryTest {
private val context = ApplicationProvider.getApplicationContext<Context>()
private val testScope = TestCoroutineScope(TestCoroutineDispatcher())
private val testDispatcher = TestCoroutineDispatcher()
private val testScope = TestCoroutineScope(testDispatcher)
private val fingerprintRequestExecutor: FingerprintRequestExecutor = mock()

@AfterTest
Expand All @@ -45,7 +46,10 @@ class FingerprintDataRepositoryTest {
fun `get() when FingerprintData is expired should request new remote FingerprintData`() {
val expectedFingerprintData = createFingerprintData()
val repository = FingerprintDataRepository.Default(
store = FingerprintDataStore.Default(context),
store = FingerprintDataStore.Default(
context,
testDispatcher
),
fingerprintRequestFactory = FingerprintRequestFactory(context),
fingerprintRequestExecutor = object : FingerprintRequestExecutor {
override fun execute(
Expand Down