Skip to content

Commit

Permalink
Fix for Crash on generating AnnotationPreviews on PdfScreen. “width a…
Browse files Browse the repository at this point in the history
…nd height must be > 0” Issue #206

Upping versionCode to 126
  • Loading branch information
Dima-Android committed Dec 31, 2024
1 parent b45d6e7 commit 86f86b4
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 1 deletion.
10 changes: 10 additions & 0 deletions app/src/main/java/org/zotero/android/architecture/Defaults.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ open class Defaults @Inject constructor(
private val webDavScheme = "webDavScheme"
private val webDavPassword = "webDavPassword"

private val needsZeroWidthOrHeightAnnotationsFix = "needsZeroWidthOrHeightAnnotationsFix2"

private val sharedPreferences: SharedPreferences by lazy {
context.getSharedPreferences(
sharedPrefsFile,
Expand Down Expand Up @@ -395,6 +397,14 @@ open class Defaults @Inject constructor(
return sharedPreferences.getInt(performFullSyncGuardKey, 1)
}

fun needsZeroWidthOrHeightAnnotationsFix(): Boolean {
return sharedPreferences.getBoolean(needsZeroWidthOrHeightAnnotationsFix, true)
}

fun setNeedsZeroWidthOrHeightAnnotationsFix(value: Boolean) {
sharedPreferences.edit { putBoolean(needsZeroWidthOrHeightAnnotationsFix, value) }
}

fun reset() {
setUsername("")
setDisplayName("")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.zotero.android.database.requests

import io.realm.Realm
import io.realm.kotlin.where
import org.zotero.android.database.DbRequest
import org.zotero.android.database.objects.AnnotationType
import org.zotero.android.database.objects.ItemTypes
import org.zotero.android.database.objects.RItem

class FixSquareAnnotationsWithZeroWidthOrHeightDbRequest : DbRequest {
override val needsWrite: Boolean
get() = true

override fun process(database: Realm) {
val targetTypes = listOf(AnnotationType.image.name)
val results = database.where<RItem>()
.item(type = ItemTypes.annotation)
.deleted(false)
.`in`("annotationType", targetTypes.toTypedArray())
.findAll()

for (rItem in results) {
val rect = rItem.rects.getOrNull(0) ?: continue
val libraryId = rItem.libraryId ?: continue
val width = rect.maxX - rect.minX
val height = rect.maxY - rect.minY
if (width == 0.0 || height == 0.0) {
MarkObjectsAsDeletedDbRequest(
clazz = RItem::class,
keys = listOf(rItem.key),
libraryId = libraryId
).process(database)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ class AnnotationPreviewManager @Inject constructor(
val resultScale = scaleX.coerceAtLeast(scaleY)
val resultVideoViewWidth = (width / resultScale).toInt()
val resultVideoViewHeight = (height / resultScale).toInt()
if (resultVideoViewWidth == 0 || resultVideoViewHeight == 0) {
throw Exception("An attempt to create an AnnotationPreview for annotation of type ${annotation.type} resulting in a zero scale dimension. width = $width, height = $height, maxSide = $maxSide, resultVideoViewWidth = $resultVideoViewWidth, resultVideoViewHeight = $resultVideoViewHeight")
}

return rawDocumentBitmap.scale(resultVideoViewWidth, resultVideoViewHeight, true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,18 @@ class PdfReaderViewModel @Inject constructor(
onAnnotationUpdatedListener = object :
AnnotationProvider.OnAnnotationUpdatedListener {
override fun onAnnotationCreated(annotation: Annotation) {
//Don't allow Square Annotations with zero width or height from being added.
val annotationRect = annotation.boundingBox
val width = (annotationRect.right - annotationRect.left).toInt()
val height = (annotationRect.top - annotationRect.bottom).toInt()
if (annotation.type == AnnotationType.SQUARE && (width == 0 || height == 0)) {
Timber.w("PdfReaderViewModel: Prevented an annotation of type ${annotation.type} from being created with width=$width and height=$height")
this@PdfReaderViewModel.document.annotationProvider.removeAnnotationFromPage(
annotation
)
return
}

processAnnotationObserving(annotation, emptyList(), PdfReaderNotification.PSPDFAnnotationsAdded)
}

Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/org/zotero/android/sync/UserControllers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import org.zotero.android.architecture.coroutines.Dispatchers
import org.zotero.android.attachmentdownloader.AttachmentDownloader
import org.zotero.android.database.DbWrapperMain
import org.zotero.android.database.requests.CleanupUnusedTags
import org.zotero.android.database.requests.FixSquareAnnotationsWithZeroWidthOrHeightDbRequest
import org.zotero.android.files.FileStore
import org.zotero.android.websocket.ChangeWsResponse
import timber.log.Timber
import javax.inject.Inject
Expand All @@ -32,6 +34,7 @@ class UserControllers @Inject constructor(
private val changeWsResponseKindEventStream: ChangeWsResponseKindEventStream,
private val fileDownloader: AttachmentDownloader,
private val defaults: Defaults,
private val fileStore: FileStore
) {

private lateinit var changeObserver: ObjectUserChangeObserver
Expand All @@ -55,6 +58,14 @@ class UserControllers @Inject constructor(
dbWrapperMain.realmDbStorage.perform(coordinatorAction = { coordinator ->
isFirstLaunch = coordinator.perform(InitializeCustomLibrariesDbRequest())
coordinator.perform(CleanupUnusedTags())

if (defaults.needsZeroWidthOrHeightAnnotationsFix()) {
fileStore.annotationPreviews.deleteRecursively()
fileStore.pageThumbnails.deleteRecursively()
coordinator.perform(FixSquareAnnotationsWithZeroWidthOrHeightDbRequest())
defaults.setNeedsZeroWidthOrHeightAnnotationsFix(false)
}

coordinator.invalidate()
})
}
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/BuildConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ object BuildConfig {
const val compileSdkVersion = 34
const val targetSdk = 34

val versionCode = 125 // Must be updated on every build
val versionCode = 126 // Must be updated on every build
val version = Version(
major = 1,
minor = 0,
Expand Down

0 comments on commit 86f86b4

Please sign in to comment.