Skip to content

Commit

Permalink
build(deps): bump screengrab from 1.2.0 to 2.1.0 in /app (#4080)
Browse files Browse the repository at this point in the history
* build(deps): bump screengrab from 1.2.0 to 2.1.0 in /app

Bumps [screengrab](https://github.com/fastlane/fastlane) from 1.2.0 to 2.1.0.
- [Release notes](https://github.com/fastlane/fastlane/releases)
- [Commits](fastlane/fastlane@1.2.0...2.1.0)

---
updated-dependencies:
- dependency-name: tools.fastlane:screengrab
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* fix: screenshot tests now compiling

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: VaiTon <eyadlorenzo@gmail.com>
  • Loading branch information
dependabot[bot] and VaiTon committed Aug 29, 2021
1 parent 7caa086 commit eef9a9f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
4 changes: 3 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ dependencies {
exclude(module = "recyclerview-v7")
}
androidTestImplementation("com.jraska:falcon:2.2.0")
androidTestImplementation("tools.fastlane:screengrab:1.2.0")
androidTestImplementation("tools.fastlane:screengrab:2.1.0")
androidTestImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:${coroutinesVersion}")


Expand Down Expand Up @@ -276,6 +276,8 @@ android {
applicationIdSuffix = ".debug"
isDebuggable = true

defaultConfig.minSdk = 18

// Uncomment to use dev server
// buildConfigField("String", "HOST", "\"https://ssl-api.openfoodfacts.net\"")
// buildConfigField("String", "OFWEBSITE", "\"https://www.openfoodfacts.net/\"")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
package openfoodfacts.github.scrachx.openfood.test

import android.graphics.Bitmap
import android.graphics.Bitmap.CompressFormat
import android.graphics.Bitmap.CompressFormat.PNG
import android.os.Environment
import android.util.Log
import openfoodfacts.github.scrachx.openfood.BuildConfig
import tools.fastlane.screengrab.ScreenshotCallback
import tools.fastlane.screengrab.file.Chmod
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.nio.file.Path
import java.nio.file.attribute.PosixFilePermissions
import java.nio.file.attribute.PosixFilePermissions.asFileAttribute
import java.text.SimpleDateFormat
import java.util.*
import kotlin.io.path.*

//
/**
* Write screennshot files.
*/
class FileWritingScreenshotCallback internal constructor(
private val screenshotParameter: ScreenshotParameter
private val screenshotParameter: ScreenshotParameter
) : ScreenshotCallback {

override fun screenshotCaptured(screenshotName: String, screenshot: Bitmap) {
try {
val screenshotDirectory = getFilesDirectory()

val screenshotFile = getScreenshotFile(
screenshotDirectory,
"${screenshotParameter.locale.country}_${screenshotParameter.locale.language}-$screenshotName"
screenshotDirectory,
"${screenshotParameter.locale.country}_${screenshotParameter.locale.language}-$screenshotName"
)

try {
BufferedOutputStream(FileOutputStream(screenshotFile)).use { fos ->
screenshot.compress(CompressFormat.PNG, 100, fos)
Chmod.chmodPlusR(screenshotFile)
screenshotFile.outputStream().buffered().use { fos ->
screenshot.compress(PNG, 100, fos)
}
} finally {
screenshot.recycle()
Expand All @@ -43,53 +44,48 @@ class FileWritingScreenshotCallback internal constructor(
}
}

private fun getScreenshotFile(dir: File, name: String) =
File(dir, "${name}_${dateFormat.format(Date())}.png")
private fun getScreenshotFile(dir: Path, name: String): Path =
dir.resolve("${name}_${dateFormat.format(Date())}.png")

@Throws(IOException::class)
private fun getFilesDirectory(): File {
private fun getFilesDirectory(): Path {
if (!isExternalStorageWritable()) {
Log.e(LOG_TAG, "Can't write to external storage check your installation")
throw IOException("Can't write to external storage")
}
val targetDirectory = File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
BuildConfig.FLAVOR + "Screenshots"
)
val internalDir = File(targetDirectory, screenshotParameter.locale.country)
val targetDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toPath()
.resolve(BuildConfig.FLAVOR + "Screenshots")

val internalDir = targetDirectory.resolve(screenshotParameter.locale.country)
val directory = initializeDirectory(internalDir)

return if (directory == null) {
throw IOException("Unable to get a screenshot storage directory")
} else {
Log.d(LOG_TAG, "Using screenshot storage directory: ${directory.absolutePath}")
Log.d(LOG_TAG, "Using screenshot storage directory: ${directory.absolutePathString()}")
directory
}
}

companion object {
private val LOG_TAG = FileWritingScreenshotCallback::class.simpleName
private val dateFormat = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss")
private fun initializeDirectory(dir: File): File? {

private fun initializeDirectory(dir: Path): Path? {
try {
createPathTo(dir)
if (dir.isDirectory && dir.canWrite()) return dir
if (dir.isDirectory() && dir.isWritable()) return dir
} catch (exception: IOException) {
Log.e(LOG_TAG, "Failed to initialize directory: ${dir.absolutePath}", exception)
Log.e(LOG_TAG, "Failed to initialize directory: ${dir.absolutePathString()}", exception)
}
return null
}

/* Checks if external storage is available for read and write */
private fun isExternalStorageWritable() =
Environment.MEDIA_MOUNTED == Environment.getExternalStorageState()
Environment.MEDIA_MOUNTED == Environment.getExternalStorageState()

@Throws(IOException::class)
private fun createPathTo(dir: File) {
if (dir.exists() || dir.mkdirs()) {
Chmod.chmodPlusRWX(dir)
} else {
throw IOException("Unable to create output dir: ${dir.absolutePath}")
}
private fun createPathTo(dir: Path) {
dir.createDirectories(asFileAttribute(PosixFilePermissions.fromString("rwxr-xr--")))
}
}
}

0 comments on commit eef9a9f

Please sign in to comment.