Skip to content
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
9 changes: 4 additions & 5 deletions appscan/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
<!--
~ Nextcloud - Android Client
~
~ SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
~ SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
~ SPDX-FileCopyrightText: 2023 Álvaro Brey <alvaro@alvarobrey.com>
~ SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application>
<activity
android:name=".AppScanActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.NoActionBar"
tools:ignore="LockedOrientationActivity" />
android:configChanges="orientation|screenSize|screenLayout|smallestScreenSize|uiMode|density|fontScale|keyboard|keyboardHidden"
android:theme="@style/Theme.AppCompat.NoActionBar" />
</application>

</manifest>
69 changes: 62 additions & 7 deletions appscan/src/main/java/com/nextcloud/appscan/AppScanActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@
*/
package com.nextcloud.appscan

import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.graphics.Bitmap
import android.graphics.PointF
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.view.children
import com.nextcloud.android.common.ui.util.extensions.applyEdgeToEdgeWithSystemBarPadding
import com.zynksoftware.documentscanner.R
import com.zynksoftware.documentscanner.ScanActivity
import com.zynksoftware.documentscanner.model.DocumentScannerErrorModel
import com.zynksoftware.documentscanner.model.ScannerResults
Expand All @@ -22,16 +30,57 @@ import com.zynksoftware.documentscanner.ui.DocumentScanner
class AppScanActivity : ScanActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
applyEdgeToEdgeWithSystemBarPadding()
super.onCreate(savedInstanceState)
super.onCreate(null)
DocumentScanner.init(this)
addFragmentContentLayout()
}

override fun onError(error: DocumentScannerErrorModel) {
// TODO pass this from app somehow?
println(error)
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
recreateCropFragmentView()
}

private fun recreateCropFragmentView() {
val cropFragment = supportFragmentManager.findFragmentByTag(IMAGE_CROP_FRAGMENT_TAG) ?: return
if (!cropFragment.isVisible) {
return
}

val cornerFractions = cropFragment.view?.let(::cropCornerFractions)

supportFragmentManager.beginTransaction().detach(cropFragment).commitNow()
supportFragmentManager.beginTransaction().attach(cropFragment).commitNow()

val cropView = cropFragment.view ?: return
if (cornerFractions == null) {
return
}

cropView.post { restoreCropCorners(cropView, cornerFractions) }
}

private fun cropCornerFractions(cropView: View): List<PointF>? {
val preview = cropPreviewBitmap(cropView) ?: return null
return cropCornerHandles(cropView).map { PointF(it.x / preview.width, it.y / preview.height) }
}

private fun restoreCropCorners(cropView: View, cornerFractions: List<PointF>) {
val preview = cropPreviewBitmap(cropView) ?: return
cropCornerHandles(cropView).zip(cornerFractions) { handle, fraction ->
handle.x = fraction.x * preview.width
handle.y = fraction.y * preview.height
}
cropView.findViewById<View>(R.id.polygonView).invalidate()
}

private fun cropPreviewBitmap(cropView: View): Bitmap? =
(cropView.findViewById<ImageView>(R.id.imagePreview).drawable as? BitmapDrawable)?.bitmap

private fun cropCornerHandles(cropView: View): List<View> =
cropView.findViewById<ViewGroup>(R.id.polygonView).children.toList()

override fun onError(error: DocumentScannerErrorModel) = Unit

override fun onSuccess(scannerResults: ScannerResults) {
val intent = Intent()

Expand All @@ -40,12 +89,16 @@ class AppScanActivity : ScanActivity() {
scannerResults.transformedImageFile?.absolutePath ?: scannerResults.croppedImageFile?.absolutePath
)

setResult(Activity.RESULT_OK, intent)
setResult(RESULT_OK, intent)
finish()
}

override fun onClose() {
setResult(Activity.RESULT_CANCELED)
if (isChangingConfigurations) {
return
}

setResult(RESULT_CANCELED)
finish()
}

Expand All @@ -63,5 +116,7 @@ class AppScanActivity : ScanActivity() {
val enabled: Boolean = true

const val EXTRA_FILE = "file"

private const val IMAGE_CROP_FRAGMENT_TAG = "ImageCropFragmentTag"
}
}
Loading