From 5353ec18105ee334f32eb6e954053c1370f3d342 Mon Sep 17 00:00:00 2001 From: Ruben Gees Date: Mon, 8 Oct 2018 22:50:17 +0200 Subject: [PATCH] Disable disk writes strict mode check as vibration triggers it --- .../kotlin/me/proxer/app/MainApplication.kt | 1 + .../me/proxer/app/util/data/StorageHelper.kt | 56 ++++++++----------- .../util/extension/StrictModeExtensions.kt | 23 -------- 3 files changed, 23 insertions(+), 57 deletions(-) delete mode 100644 src/main/kotlin/me/proxer/app/util/extension/StrictModeExtensions.kt diff --git a/src/main/kotlin/me/proxer/app/MainApplication.kt b/src/main/kotlin/me/proxer/app/MainApplication.kt index 13f48fe7..f2efc2e8 100755 --- a/src/main/kotlin/me/proxer/app/MainApplication.kt +++ b/src/main/kotlin/me/proxer/app/MainApplication.kt @@ -173,6 +173,7 @@ class MainApplication : Application() { if (BuildConfig.DEBUG) { val threadPolicy = StrictModeCompat.ThreadPolicy.Builder() .detectAll() + .permitDiskWrites() .permitDiskReads() .penaltyDeath() .penaltyLog() diff --git a/src/main/kotlin/me/proxer/app/util/data/StorageHelper.kt b/src/main/kotlin/me/proxer/app/util/data/StorageHelper.kt index 78ff7b37..02946829 100644 --- a/src/main/kotlin/me/proxer/app/util/data/StorageHelper.kt +++ b/src/main/kotlin/me/proxer/app/util/data/StorageHelper.kt @@ -7,7 +7,6 @@ import com.orhanobut.hawk.Converter import com.orhanobut.hawk.DataInfo import com.orhanobut.hawk.Hawk import me.proxer.app.auth.LocalUser -import me.proxer.app.util.extension.permitDisk import org.koin.standalone.KoinComponent import timber.log.Timber import java.util.Date @@ -35,22 +34,18 @@ class StorageHelper(context: Context, jsonParser: HawkMoshiParser) : KoinCompone } init { - permitDisk { - initHawk(context, jsonParser) + initHawk(context, jsonParser) - migrateUser(context, jsonParser) - migratePreferences(context) - } + migrateUser(context, jsonParser) + migratePreferences(context) } var user: LocalUser? get() = Hawk.get(USER) set(value) { - permitDisk { - when (value) { - null -> Hawk.delete(USER) - else -> Hawk.put(USER, value) - } + when (value) { + null -> Hawk.delete(USER) + else -> Hawk.put(USER, value) } } @@ -60,25 +55,25 @@ class StorageHelper(context: Context, jsonParser: HawkMoshiParser) : KoinCompone var isTwoFactorAuthenticationEnabled: Boolean get() = Hawk.get(TWO_FACTOR_AUTHENTICATION, false) set(value) { - permitDisk { Hawk.put(TWO_FACTOR_AUTHENTICATION, value) } + Hawk.put(TWO_FACTOR_AUTHENTICATION, value) } var lastNewsDate: Date get() = Date(Hawk.get(LAST_NEWS_DATE, 0L)) set(value) { - permitDisk { Hawk.put(LAST_NEWS_DATE, value.time) } + Hawk.put(LAST_NEWS_DATE, value.time) } var lastNotificationsDate: Date get() = Date(Hawk.get(LAST_NOTIFICATIONS_DATE, 0L)) set(value) { - permitDisk { Hawk.put(LAST_NOTIFICATIONS_DATE, value.time) } + Hawk.put(LAST_NOTIFICATIONS_DATE, value.time) } var lastChatMessageDate: Date get() = Date(Hawk.get(LAST_CHAT_MESSAGE_DATE, 0L)) set(value) { - permitDisk { Hawk.put(LAST_CHAT_MESSAGE_DATE, value.time) } + Hawk.put(LAST_CHAT_MESSAGE_DATE, value.time) } val chatInterval: Long @@ -87,19 +82,19 @@ class StorageHelper(context: Context, jsonParser: HawkMoshiParser) : KoinCompone var areConferencesSynchronized: Boolean get() = Hawk.get(CONFERENCES_SYNCHRONIZED, false) set(value) { - permitDisk { Hawk.put(CONFERENCES_SYNCHRONIZED, value) } + Hawk.put(CONFERENCES_SYNCHRONIZED, value) } var lastTagUpdateDate: Date get() = Date(Hawk.get(LAST_TAG_UPDATE_DATE, 0L)) set(value) { - permitDisk { Hawk.put(LAST_TAG_UPDATE_DATE, value.time) } + Hawk.put(LAST_TAG_UPDATE_DATE, value.time) } var launches: Int get() = Hawk.get(LAUNCHES, 0) private set(value) { - permitDisk { Hawk.put(LAUNCHES, value) } + Hawk.put(LAUNCHES, value) } var hasRated: Boolean @@ -108,27 +103,23 @@ class StorageHelper(context: Context, jsonParser: HawkMoshiParser) : KoinCompone Hawk.put(RATED, value) } - fun incrementChatInterval() = permitDisk { - Hawk.get(CHAT_INTERVAL, DEFAULT_CHAT_INTERVAL).let { - if (it < MAX_CHAT_INTERVAL) { - Hawk.put(CHAT_INTERVAL, (it * 1.5f).toLong()) - } + fun incrementChatInterval() = Hawk.get(CHAT_INTERVAL, DEFAULT_CHAT_INTERVAL).let { + if (it < MAX_CHAT_INTERVAL) { + Hawk.put(CHAT_INTERVAL, (it * 1.5f).toLong()) } } - fun incrementLaunches() = permitDisk { - Hawk.get(LAUNCHES, 0).let { - Hawk.put(LAUNCHES, it + 1) - } + fun incrementLaunches() = Hawk.get(LAUNCHES, 0).let { + Hawk.put(LAUNCHES, it + 1) } - fun resetChatInterval() = permitDisk { Hawk.put(CHAT_INTERVAL, DEFAULT_CHAT_INTERVAL) } + fun resetChatInterval() = Hawk.put(CHAT_INTERVAL, DEFAULT_CHAT_INTERVAL) - fun putMessageDraft(id: String, draft: String) = permitDisk { Hawk.put("$MESSAGE_DRAFT_PREFIX$id", draft) } + fun putMessageDraft(id: String, draft: String) = Hawk.put("$MESSAGE_DRAFT_PREFIX$id", draft) fun getMessageDraft(id: String): String? = Hawk.get("$MESSAGE_DRAFT_PREFIX$id") - fun deleteMessageDraft(id: String) = permitDisk { Hawk.delete("$MESSAGE_DRAFT_PREFIX$id") } + fun deleteMessageDraft(id: String) = Hawk.delete("$MESSAGE_DRAFT_PREFIX$id") private fun initHawk(context: Context, jsonParser: HawkMoshiParser) { if (!Hawk.isBuilt()) { @@ -191,10 +182,7 @@ class StorageHelper(context: Context, jsonParser: HawkMoshiParser) : KoinCompone @Suppress("UNCHECKED_CAST") override fun fromString(value: String, dataInfo: DataInfo?): T { - return jsonParser.fromJson( - value, - MigrationLocalUser::class.java - ) as T + return jsonParser.fromJson(value, MigrationLocalUser::class.java) as T } } } diff --git a/src/main/kotlin/me/proxer/app/util/extension/StrictModeExtensions.kt b/src/main/kotlin/me/proxer/app/util/extension/StrictModeExtensions.kt deleted file mode 100644 index c37d3110..00000000 --- a/src/main/kotlin/me/proxer/app/util/extension/StrictModeExtensions.kt +++ /dev/null @@ -1,23 +0,0 @@ -package me.proxer.app.util.extension - -import android.os.StrictMode -import me.proxer.app.BuildConfig - -inline fun permitDisk(action: () -> T): T { - if (BuildConfig.DEBUG) { - val oldThreadPolicy = StrictMode.getThreadPolicy() - val newThreadPolicy = StrictMode.ThreadPolicy.Builder(oldThreadPolicy) - .permitDiskWrites() - .build() - - StrictMode.setThreadPolicy(newThreadPolicy) - - val result = action() - - StrictMode.setThreadPolicy(oldThreadPolicy) - - return result - } else { - return action() - } -}