-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Potential fix for intermittent crash “kotlin.UninitializedPropertyAcc…
…essException” that seems to happen when the app is in the background for a long time on Pdf Screen. Upping versionCode to 51
- Loading branch information
1 parent
f763c5f
commit 2f248a8
Showing
18 changed files
with
182 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
app/src/main/java/org/zotero/android/architecture/navigation/NavigationParamsMarshaller.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.zotero.android.architecture.navigation | ||
|
||
import com.google.gson.Gson | ||
import org.apache.commons.codec.binary.Base64 | ||
import org.apache.commons.io.IOUtils | ||
import java.nio.charset.StandardCharsets | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class NavigationParamsMarshaller @Inject constructor(val gson: Gson) { | ||
|
||
fun encodeObjectToBase64(data: Any): String { | ||
val json = gson.toJson(data) | ||
val encodedJson = encodeJsonToBase64(json) | ||
return encodedJson | ||
} | ||
|
||
private fun encodeJsonToBase64(stringToEncode: String): String { | ||
val bytes = IOUtils.toByteArray(stringToEncode); | ||
val encoded: ByteArray = Base64.encodeBase64(bytes) | ||
return String(encoded, StandardCharsets.US_ASCII) | ||
} | ||
|
||
inline fun <reified T> decodeObjectFromBase64(encodedJson: String): T { | ||
val decodedJson = decodeJsonFromBase64Binary(encodedJson) | ||
return unmarshal(decodedJson) | ||
} | ||
|
||
inline fun <reified T> unmarshal(data: String): T { | ||
return gson.fromJson(data, T::class.java) | ||
} | ||
|
||
fun decodeJsonFromBase64Binary(encodedJson: String): String { | ||
val bytes = IOUtils.toByteArray(encodedJson); | ||
val decodedJson: ByteArray = Base64.decodeBase64(bytes) | ||
return String(decodedJson, StandardCharsets.US_ASCII) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
app/src/main/java/org/zotero/android/architecture/serialization/SealedClassTypeAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.zotero.android.architecture.serialization | ||
|
||
import com.google.gson.Gson | ||
import com.google.gson.TypeAdapter | ||
import com.google.gson.stream.JsonReader | ||
import com.google.gson.stream.JsonWriter | ||
import kotlin.reflect.KClass | ||
|
||
class SealedClassTypeAdapter<T : Any>(val kclass: KClass<Any>, val gson: Gson) : TypeAdapter<T>() { | ||
override fun read(jsonReader: JsonReader): T? { | ||
jsonReader.beginObject() | ||
val nextName = jsonReader.nextName() | ||
val innerClass = kclass.sealedSubclasses.firstOrNull { | ||
it.simpleName!!.contains(nextName) | ||
} | ||
?: throw Exception("$nextName is not found to be a data class of the sealed class ${kclass.qualifiedName}") | ||
val x = gson.fromJson<T>(jsonReader, innerClass.javaObjectType) | ||
jsonReader.endObject() | ||
return innerClass.objectInstance as T? ?: x | ||
} | ||
|
||
override fun write(out: JsonWriter, value: T) { | ||
val jsonString = gson.toJson(value) | ||
out.beginObject() | ||
out.name(value.javaClass.canonicalName.splitToSequence(".").last()).jsonValue(jsonString) | ||
out.endObject() | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
app/src/main/java/org/zotero/android/architecture/serialization/UriTypeAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.zotero.android.architecture.serialization | ||
|
||
import android.net.Uri | ||
import com.google.gson.JsonDeserializationContext | ||
import com.google.gson.JsonDeserializer | ||
import com.google.gson.JsonElement | ||
import com.google.gson.JsonPrimitive | ||
import com.google.gson.JsonSerializationContext | ||
import com.google.gson.JsonSerializer | ||
import java.lang.reflect.Type | ||
|
||
object UriTypeAdapter : JsonDeserializer<Uri>, JsonSerializer<Uri> { | ||
|
||
override fun deserialize( | ||
json: JsonElement?, | ||
typeOfT: Type?, | ||
context: JsonDeserializationContext? | ||
): Uri { | ||
return Uri.parse(json?.asString.toString()) | ||
} | ||
|
||
override fun serialize( | ||
src: Uri?, | ||
typeOfSrc: Type?, | ||
context: JsonSerializationContext? | ||
): JsonElement { | ||
return JsonPrimitive(src.toString()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.