-
Notifications
You must be signed in to change notification settings - Fork 2
[MS-1493] Image injection in Debug versions for E2E tests #1780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alexandr-simprints
wants to merge
7
commits into
main
Choose a base branch
from
feature/MS-1493-image-injection-impl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c2bfb59
[MS-1493] Initial image injection broadcast receiver implementation
alexandr-simprints 9d3a710
[MS-1493] Image injection broadcast is now correctly scaled to the ca…
alexandr-simprints fc6ee6e
MS-1493 Limit image cache and pre-processing to debug builds
luhmirin-s 9f46391
MS-1493 Clear cached injected image when receiving faulty or empty pa…
luhmirin-s 0d8fbb4
[MS-1493] updating log messages
alexandr-simprints b79aa64
[MS-1493] Creating test for injected image pre-processor
alexandr-simprints afcbb4e
[MS-1493] Annotating the UI class with @ExcludedFromGeneratedTestCove…
alexandr-simprints File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,13 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
|
||
| <application> | ||
| <receiver | ||
| android:name=".ImageInjectionReceiver" | ||
| android:exported="true"> | ||
| <intent-filter> | ||
| <action android:name="com.simprints.automation.INJECT_FRAME" /> | ||
| </intent-filter> | ||
| </receiver> | ||
| </application> | ||
| </manifest> |
54 changes: 54 additions & 0 deletions
54
infra/camera/src/debug/java/com/simprints/infra/camera/ImageInjectionReceiver.kt
This file contains hidden or 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,54 @@ | ||
| package com.simprints.infra.camera | ||
|
|
||
| import android.content.BroadcastReceiver | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.graphics.BitmapFactory | ||
| import android.widget.Toast | ||
| import com.simprints.core.ExcludedFromGeneratedTestCoverageReports | ||
| import com.simprints.infra.camera.repository.InjectedImageCache | ||
| import com.simprints.infra.logging.Simber | ||
| import dagger.hilt.android.AndroidEntryPoint | ||
| import java.io.File | ||
| import javax.inject.Inject | ||
|
|
||
| @AndroidEntryPoint | ||
| @ExcludedFromGeneratedTestCoverageReports("Wrapper for E2E test image injection broadcast receiver") | ||
| class ImageInjectionReceiver : BroadcastReceiver() { | ||
| @Inject | ||
| lateinit var cache: InjectedImageCache | ||
|
|
||
| override fun onReceive( | ||
| context: Context, | ||
| intent: Intent, | ||
| ) { | ||
| val extras = intent.extras | ||
| Simber.d("Image injection broadcast received. Extras: ${extras?.keySet()?.joinToString { "$it=${extras[it]}" }}") | ||
| val filename = intent.getStringExtra(EXTRA_FILE) | ||
| if (filename.isNullOrBlank()) { | ||
| cache.injectedImage = null | ||
| Simber.d("Image injection broadcast does not contain '$EXTRA_FILE' extra. Image injection cleared") | ||
| return | ||
| } | ||
| val dir = context.applicationContext.getExternalFilesDir(null) | ||
| if (dir == null) { | ||
| cache.injectedImage = null | ||
| Simber.d("External files dir cannot be resolved. Image injection cleared.") | ||
| return | ||
| } | ||
| val bitmap = BitmapFactory.decodeFile(File(dir, filename).absolutePath) | ||
| if (bitmap != null) { | ||
| cache.injectedImage = bitmap | ||
| val message = "Image injected: $filename" | ||
| Simber.d(message) | ||
| Toast.makeText(context.applicationContext, message, Toast.LENGTH_LONG).show() | ||
| } else { | ||
| cache.injectedImage = null | ||
| Simber.d("failed to decode '$filename'. Image injection cleared") | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private const val EXTRA_FILE = "frame" | ||
| } | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
infra/camera/src/debug/java/com/simprints/infra/camera/repository/InjectedImageCache.kt
This file contains hidden or 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,12 @@ | ||
| package com.simprints.infra.camera.repository | ||
|
|
||
| import android.graphics.Bitmap | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class InjectedImageCache @Inject constructor() { | ||
| @Volatile | ||
| var injectedImage: Bitmap? = null | ||
| internal set | ||
| } |
67 changes: 67 additions & 0 deletions
67
infra/camera/src/debug/java/com/simprints/infra/camera/usecase/FramePreProcessUseCase.kt
This file contains hidden or 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,67 @@ | ||
| package com.simprints.infra.camera.usecase | ||
|
|
||
| import android.graphics.Bitmap | ||
| import android.graphics.Canvas | ||
| import android.graphics.Color | ||
| import android.graphics.Rect | ||
| import androidx.core.graphics.createBitmap | ||
| import com.simprints.infra.camera.Frame | ||
| import com.simprints.infra.camera.repository.InjectedImageCache | ||
| import javax.inject.Inject | ||
|
|
||
| internal class FramePreProcessUseCase @Inject constructor( | ||
| private val injectedImageCache: InjectedImageCache, | ||
| ) { | ||
| /** | ||
| * Wraps the provided parameters with a [Frame] object. | ||
| * | ||
| * If an injected image is available, it will be used instead of the provided [bitmap]. | ||
| * Fits injected image centred within [targetRect], preserving aspect ratio, and expanded to [previewRect]. The remaining area is | ||
| * filled with black color. This is done so that the injected image is properly displayed on the screen, and post-processing can | ||
| * crop the area of interest as if it was a real frame from the camera. | ||
| */ | ||
| operator fun invoke( | ||
| bitmap: Bitmap, | ||
| rotation: Int, | ||
| previewRect: Rect, | ||
| targetRect: Rect, | ||
| ): Frame { | ||
| val injected = injectedImageCache.injectedImage ?: return Frame( | ||
| bitmap = bitmap, | ||
| rotation = rotation, | ||
| previewBounds = previewRect, | ||
| targetBounds = targetRect, | ||
| ) | ||
|
|
||
| val result = createBitmap(previewRect.width(), previewRect.height()) | ||
| val canvas = Canvas(result) | ||
| canvas.drawColor(Color.BLACK) | ||
|
|
||
| val scale = if (targetRect.width() > targetRect.height()) { | ||
| targetRect.height().toFloat() / injected.height | ||
| } else { | ||
| targetRect.width().toFloat() / injected.width | ||
| } | ||
|
alexandr-simprints marked this conversation as resolved.
|
||
|
|
||
| val scaledWidth = (injected.width * scale).toInt() | ||
| val scaledHeight = (injected.height * scale).toInt() | ||
|
|
||
| val left = targetRect.left + (targetRect.width() - scaledWidth) / 2 | ||
| val top = targetRect.top + (targetRect.height() - scaledHeight) / 2 | ||
|
|
||
| canvas.drawBitmap( | ||
| injected, | ||
| null, | ||
| Rect(left, top, left + scaledWidth, top + scaledHeight), | ||
| null, | ||
| ) | ||
|
|
||
| return Frame( | ||
| bitmap = result, | ||
| rotation = 0, | ||
| previewBounds = previewRect, | ||
| targetBounds = targetRect, | ||
| isInjected = true, | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or 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
32 changes: 32 additions & 0 deletions
32
infra/camera/src/main/java/com/simprints/infra/camera/CameraPreviewView.kt
This file contains hidden or 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,32 @@ | ||
| package com.simprints.infra.camera | ||
|
|
||
| import android.content.Context | ||
| import android.util.AttributeSet | ||
| import android.view.View.IMPORTANT_FOR_ACCESSIBILITY_NO | ||
| import android.widget.FrameLayout | ||
| import android.widget.ImageView | ||
| import androidx.camera.view.PreviewView | ||
| import com.simprints.core.ExcludedFromGeneratedTestCoverageReports | ||
|
|
||
| @ExcludedFromGeneratedTestCoverageReports("UI class") | ||
| class CameraPreviewView @JvmOverloads constructor( | ||
| context: Context, | ||
| attrs: AttributeSet? = null, | ||
| ) : FrameLayout(context, attrs) { | ||
| val previewView: PreviewView = PreviewView(context).apply { | ||
| layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) | ||
| importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_NO | ||
| } | ||
|
|
||
| val injectionOverlay: ImageView = ImageView(context).apply { | ||
| layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) | ||
| scaleType = ImageView.ScaleType.FIT_CENTER | ||
| importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_NO | ||
| visibility = GONE | ||
| } | ||
|
|
||
| init { | ||
| addView(previewView) | ||
| addView(injectionOverlay) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.