Skip to content
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

build(deps): bump screengrab from 1.2.0 to 2.1.0 in /app #4080

Merged
merged 2 commits into from
Aug 29, 2021
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
4 changes: 3 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,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 @@ -274,6 +274,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--")))
}
}
}