Skip to content

Commit

Permalink
potential bug fix for app update + added option to disable saving the…
Browse files Browse the repository at this point in the history
… image in gallery
  • Loading branch information
rosenpin committed Dec 24, 2023
1 parent 26c032e commit 8088351
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 19 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
</application>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@ import android.widget.ImageView
import android.widget.Toast
import com.tomer.screenshotsharer.R
import com.tomer.screenshotsharer.prefs.DB_NAME
import com.tomer.screenshotsharer.prefs.KEY_SAVE_SCREENSHOT
import com.tomer.screenshotsharer.prefs.KEY_SHOW_PREVIEW
import java.io.File
import java.io.FileOutputStream
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale


class AssistLoggerSession(context: Context) : VoiceInteractionSession(context) {
private val showPreview by lazy {
private val prefs by lazy {
context.getSharedPreferences(DB_NAME, Context.MODE_PRIVATE)
.getBoolean(KEY_SHOW_PREVIEW, false)
}
private val showPreview by lazy {
prefs.getBoolean(KEY_SHOW_PREVIEW, false)
}
private val saveScreenshot by lazy {
prefs.getBoolean(KEY_SAVE_SCREENSHOT, true)
}

override fun onHandleAssist(state: AssistState) {
Expand Down Expand Up @@ -82,9 +90,33 @@ class AssistLoggerSession(context: Context) : VoiceInteractionSession(context) {
})
}

private fun saveImage(bitmap: Bitmap?, fileName: String): String? {
if (bitmap == null)
return null
private fun saveImage(
context: Context,
bitmap: Bitmap?,
fileName: String,
saveScreenshotInStorage: Boolean
): String? {
if (bitmap == null) return null

return if (saveScreenshotInStorage) {
saveToExternalStorage(context, bitmap, fileName)
} else {
saveToCache(context, bitmap, fileName)
}
}

private fun saveToCache(context: Context, bitmap: Bitmap, fileName: String): String {
val directory = context.cacheDir
val imageFile = File(directory, fileName)

FileOutputStream(imageFile).use { fos ->
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fos)
}

return imageFile.absolutePath
}

private fun saveToExternalStorage(context: Context, bitmap: Bitmap, fileName: String): String? {
val contentValues = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, fileName)
put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
Expand All @@ -97,16 +129,12 @@ class AssistLoggerSession(context: Context) : VoiceInteractionSession(context) {
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
contentValues
)

uri?.let {
context.contentResolver.openOutputStream(it).use { outputStream ->
outputStream?.let { stream ->
bitmap.compress(Bitmap.CompressFormat.PNG, 85, stream)
stream.flush()
stream.close()

// Add pic to gallery
MediaScannerConnection.scanFile(context, arrayOf(uri.toString()), null, null)
}
context.contentResolver.openOutputStream(it)?.use { outputStream ->
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outputStream)

MediaScannerConnection.scanFile(context, arrayOf(uri.toString()), null, null)
}
return uri.toString()
}
Expand All @@ -118,7 +146,7 @@ class AssistLoggerSession(context: Context) : VoiceInteractionSession(context) {
val time = SimpleDateFormat("yyyy-MM-dd_HH_mm_ss", Locale.getDefault()).format(Date())
val fileName = "screenshot-$time.png"

val path = saveImage(bitmap, fileName) ?: run {
val path = saveImage(context, bitmap, fileName, saveScreenshot) ?: run {
Log.e("Screenshot", "Failed to save image")
return
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/tomer/screenshotsharer/prefs/Keys.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.tomer.screenshotsharer.prefs

const val KEY_SHOW_PREVIEW = "show_preview"
const val KEY_SAVE_SCREENSHOT = "save_screenshot"
const val DB_NAME = "ScreenshotSharerSettings"
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import android.content.Intent
import com.tomer.screenshotsharer.assist.AssistLoggerService

class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED)
context.startService(Intent(context, AssistLoggerService::class.java))
}
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED ||
intent.action == Intent.ACTION_MY_PACKAGE_REPLACED ||
intent.action == Intent.ACTION_LOCKED_BOOT_COMPLETED
) {
context.startService(Intent(context, AssistLoggerService::class.java))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewModel
import com.tomer.screenshotsharer.R
import com.tomer.screenshotsharer.assist.AssistLoggerService
import com.tomer.screenshotsharer.prefs.KEY_SAVE_SCREENSHOT
import com.tomer.screenshotsharer.prefs.KEY_SHOW_PREVIEW
import com.tomer.screenshotsharer.prefs.PrefsRepository
import com.tomer.screenshotsharer.prefs.PrefsRepositoryMock
Expand Down Expand Up @@ -134,6 +135,12 @@ fun Content(viewModel: MainViewModel) {
onClick = {
viewModel.saveSetting(KEY_SHOW_PREVIEW, it)
})
SwitchSetting(
title = context.getString(R.string.save_screenshot),
checked = viewModel.saveScreenshot,
onClick = {
viewModel.saveSetting(KEY_SAVE_SCREENSHOT, it)
})
}
}
}
Expand Down Expand Up @@ -163,13 +170,15 @@ class MainViewModel @Inject constructor(

val isAssistantEnabled = mutableStateOf(false)
val showPreviewEnabled = mutableStateOf(false)
val saveScreenshot = mutableStateOf(true)

fun checkAssistantState(context: Context) {
isAssistantEnabled.value = isAssistant(context)
}

fun loadSettings() {
showPreviewEnabled.value = prefsRepository.getBoolean(KEY_SHOW_PREVIEW, false)
saveScreenshot.value = prefsRepository.getBoolean(KEY_SAVE_SCREENSHOT, true)
}

fun saveSetting(key: String, value: Boolean) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<string name="app_name">Screenshot sharer</string>
<string name="enable_service">Enable Service</string>
<string name="show_preview">Show Preview</string>
<string name="save_screenshot">Save screenshot in gallery</string>
<string name="missing_permission_can_t_access_screenshots">Missing Permission! Can\'t access screenshots</string>
<string name="storage_access_permission_granted">Storage access permission granted</string>
<string name="Screenshot">Screenshot</string>
Expand Down

0 comments on commit 8088351

Please sign in to comment.