Added kotlinx-serialization plugin

This commit is contained in:
merkost 2023-07-05 11:02:32 +10:00
parent af9ab19185
commit aff820a84a
4 changed files with 13 additions and 24 deletions

View File

@ -2,6 +2,7 @@ plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'org.jetbrains.kotlin.plugin.serialization' version "$kotlin_version"
}
def keystorePropertiesFile = rootProject.file("keystore.properties")
@ -67,4 +68,5 @@ dependencies {
implementation 'com.github.SimpleMobileTools:Simple-Commons:35d685c042'
implementation 'com.github.tibbi:IndicatorFastScroll:4524cd0b61'
implementation 'me.grantland:autofittextview:0.2.1'
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.1"
}

View File

@ -7,10 +7,6 @@ import android.os.Build
import android.os.Bundle
import android.view.Menu
import androidx.activity.result.contract.ActivityResultContracts
import com.google.gson.Gson
import com.google.gson.JsonParseException
import com.google.gson.JsonSyntaxException
import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.activities.ManageBlockedNumbersActivity
import com.simplemobiletools.commons.dialogs.ChangeDateTimeFormatDialog
import com.simplemobiletools.commons.dialogs.FeatureLockedDialog
@ -25,6 +21,10 @@ import com.simplemobiletools.dialer.extensions.config
import com.simplemobiletools.dialer.helpers.RecentsHelper
import com.simplemobiletools.dialer.models.RecentCall
import kotlinx.android.synthetic.main.activity_settings.*
import kotlinx.serialization.SerializationException
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.util.*
import kotlin.system.exitProcess
@ -308,8 +308,7 @@ class SettingsActivity : SimpleActivity() {
inputStream.bufferedReader().readText()
}
val recentsType = object : TypeToken<List<RecentCall>>() {}.type
val objects = Gson().fromJson<List<RecentCall>>(jsonString, recentsType).orEmpty()
val objects = Json.decodeFromString<List<RecentCall>>(jsonString)
if (objects.isEmpty()) {
toast(R.string.no_entries_for_importing)
@ -319,9 +318,9 @@ class SettingsActivity : SimpleActivity() {
RecentsHelper(this).restoreRecentCalls(this, objects) {
toast(R.string.importing_successful)
}
} catch (_: JsonParseException) {
} catch (_: SerializationException) {
toast(R.string.invalid_file_format)
} catch (_: JsonSyntaxException) {
} catch (_: IllegalArgumentException) {
toast(R.string.invalid_file_format)
} catch (e: Exception) {
showErrorToast(e)
@ -335,7 +334,7 @@ class SettingsActivity : SimpleActivity() {
try {
val outputStream = contentResolver.openOutputStream(uri)!!
val jsonString = Gson().toJson(recents)
val jsonString = Json.encodeToString(recents)
outputStream.use {
it.write(jsonString.toByteArray())
}

View File

@ -2,12 +2,12 @@ package com.simplemobiletools.dialer.models
import android.telephony.PhoneNumberUtils
import com.simplemobiletools.commons.extensions.normalizePhoneNumber
import java.io.Serializable
/**
* Used at displaying recent calls.
* For contacts with multiple numbers specify the number and type
*/
@kotlinx.serialization.Serializable
data class RecentCall(
val id: Int,
val phoneNumber: String,
@ -21,7 +21,7 @@ data class RecentCall(
val specificNumber: String,
val specificType: String,
val isUnknownNumber: Boolean,
) : Serializable {
) {
fun doesContainPhoneNumber(text: String): Boolean {
val normalizedText = text.normalizePhoneNumber()
return PhoneNumberUtils.compare(phoneNumber.normalizePhoneNumber(), normalizedText) ||
@ -29,5 +29,4 @@ data class RecentCall(
phoneNumber.normalizePhoneNumber().contains(normalizedText) ||
phoneNumber.contains(normalizedText)
}
}

View File

@ -16,7 +16,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/medium_margin"
android:hint="@string/filename"
android:hint="@string/filename_without_json"
android:paddingStart="@dimen/activity_margin"
android:paddingEnd="@dimen/activity_margin">
@ -30,16 +30,5 @@
</com.simplemobiletools.commons.views.MyTextInputLayout>
<ImageView
android:id="@+id/export_call_history_divider"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginStart="@dimen/activity_margin"
android:layout_marginTop="@dimen/medium_margin"
android:layout_marginEnd="@dimen/activity_margin"
android:layout_marginBottom="@dimen/medium_margin"
android:background="@color/divider_grey"
android:importantForAccessibility="no" />
</LinearLayout>
</ScrollView>