CircleToSearch with multi search engine support for all android
Kotlin
added save to gallery feature
AKS Labs042389cc7db7
diff --git a/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.kt b/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.ktindex 8441c04..de7e284 100644--- a/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.kt+++ b/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.kt@@ -96,6 +96,7 @@ import androidx.compose.material3.ExperimentalMaterial3ExpressiveApiimport androidx.compose.material3.Iconimport androidx.compose.material3.IconButtonimport androidx.compose.material3.LoadingIndicator+import androidx.compose.material3.VerticalDividerimport androidx.compose.material3.MaterialThemeimport androidx.compose.material3.ModalBottomSheetimport androidx.compose.material3.ScrollableTabRow@@ -224,7 +225,12 @@ fun CircleToSearchScreen(// Friendly Message Statevar friendlyMessage by remember { mutableStateOf("") }var isMessageVisible by remember { mutableStateOf(false) }-+ var isCopyTextTriggered by remember { mutableStateOf(false) }++ // Resizing state+ var isResizing by remember { mutableStateOf(false) }+ var activeHandle by remember { mutableStateOf<String?>(null) } // "tl", "tr", "bl", "br"+LaunchedEffect(Unit) {if (uiPreferences.isShowFriendlyMessages()) {val manager = FriendlyMessageManager(context)@@ -869,58 +875,93 @@ fun CircleToSearchScreen(}// 3. Drawing Canvas (Interactive Layer)- if (!isCopyMode) {- Canvas(+ if (!isCopyMode) { Canvas(modifier = Modifier.fillMaxSize().pointerInput(Unit) {detectDragGestures(onDragStart = { offset ->- // Clear previous state+ val rect = selectionRect+ if (rect != null && selectionAnim.value == 1f) {+ val handleSize = 64f // px for hit testing+ val tl = Offset(rect.left.toFloat(), rect.top.toFloat())+ val tr = Offset(rect.right.toFloat(), rect.top.toFloat())+ val bl = Offset(rect.left.toFloat(), rect.bottom.toFloat())+ val br = Offset(rect.right.toFloat(), rect.bottom.toFloat())++ when {+ (offset - tl).getDistance() < handleSize -> { isResizing = true; activeHandle = "tl" }+ (offset - tr).getDistance() < handleSize -> { isResizing = true; activeHandle = "tr" }+ (offset - bl).getDistance() < handleSize -> { isResizing = true; activeHandle = "bl" }+ (offset - br).getDistance() < handleSize -> { isResizing = true; activeHandle = "br" }+ else -> { isResizing = false; activeHandle = null }+ }++ if (isResizing) return@detectDragGestures+ }++ // Clear previous state if starting new drawcurrentPathPoints.clear()currentPathPoints.add(offset)selectionRect = nullscope.launch { selectionAnim.snapTo(0f) }},onDrag = { change, _ ->- val offset = change.position- currentPathPoints.add(offset)+ if (isResizing && activeHandle != null) {+ val rect = selectionRect ?: return@detectDragGestures+ val pos = change.position+ val newRect = android.graphics.Rect(rect)+ when (activeHandle) {+ "tl" -> { newRect.left = pos.x.toInt(); newRect.top = pos.y.toInt() }+ "tr" -> { newRect.right = pos.x.toInt(); newRect.top = pos.y.toInt() }+ "bl" -> { newRect.left = pos.x.toInt(); newRect.bottom = pos.y.toInt() }+ "br" -> { newRect.right = pos.x.toInt(); newRect.bottom = pos.y.toInt() }+ }+ // Basic validation (min size)+ if (newRect.width() > 20 && newRect.height() > 20) {+ selectionRect = newRect+ }+ } else {+ currentPathPoints.add(change.position)+ }},onDragEnd = {- if (currentPathPoints.isNotEmpty()) {+ if (isResizing) {+ isResizing = false+ activeHandle = null+ // Update cropped bitmap after resize+ if (screenshot != null && selectionRect != null) {+ selectedBitmap = ImageUtils.cropBitmap(screenshot, selectionRect!!)+ }+ } else if (currentPathPoints.isNotEmpty()) {var minX = Float.MAX_VALUEvar minY = Float.MAX_VALUEvar maxX = Float.MIN_VALUEvar maxY = Float.MIN_VALUE-currentPathPoints.forEach { p ->- minX = min(minX, p.x)- minY = min(minY, p.y)- maxX = max(maxX, p.x)- maxY = max(maxY, p.y)+ minX = kotlin.math.min(minX, p.x)+ minY = kotlin.math.min(minY, p.y)+ maxX = kotlin.math.max(maxX, p.x)+ maxY = kotlin.math.max(maxY, p.y)}-- val rect = Rect(- minX.toInt(),- minY.toInt(),- maxX.toInt(),- maxY.toInt()++ val border = 20+ val rect = android.graphics.Rect(+ (minX - border).toInt().coerceAtLeast(0),+ (minY - border).toInt().coerceAtLeast(0),+ (maxX + border).toInt().coerceAtMost(screenshot?.width ?: 0),+ (maxY + border).toInt().coerceAtMost(screenshot?.height ?: 0))-- selectionRect = rect- // Clear points to remove the drawn line and show the lens rect- currentPathPoints.clear()-- scope.launch {- selectionAnim.animateTo(- targetValue = 1f,- animationSpec = tween(600)- )- android.util.Log.d("CircleToSearch", "Selection rect: ${rect.left},${rect.top},${rect.right},${rect.bottom}")- selectedBitmap = ImageUtils.cropBitmap(screenshot!!, rect)- android.util.Log.d("CircleToSearch", "Cropped bitmap size: ${selectedBitmap!!.width}x${selectedBitmap!!.height}")- isSearching = true- // Sheet expands automatically in LaunchedEffect++ if (rect.width() > 10 && rect.height() > 10) {+ selectionRect = rect+ if (screenshot != null) {+ selectedBitmap = ImageUtils.cropBitmap(screenshot!!, rect)+ }+ scope.launch {+ selectionAnim.animateTo(1f, tween(600))+ isSearching = true+ }}}}@@ -1488,46 +1529,85 @@ fun CircleToSearchScreen(tonalElevation = 4.dp,shadowElevation = 8.dp) {- androidx.compose.material3.FilledTonalButton(- onClick = {- if (selectedBitmap != null) {- scope.launch {- try {- val fileName = "selection_${java.util.UUID.randomUUID()}.png"- val path = ImageUtils.saveBitmap(context, selectedBitmap!!, fileName)- val file = java.io.File(path)- val uri = androidx.core.content.FileProvider.getUriForFile(context, "com.akslabs.circletosearch.fileprovider", file)- val shareIntent = android.content.Intent(android.content.Intent.ACTION_SEND).apply {- type = "image/png"- putExtra(android.content.Intent.EXTRA_STREAM, uri)- clipData = android.content.ClipData.newRawUri("Selection", uri)- addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION)- addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)+ Row(verticalAlignment = Alignment.CenterVertically) {+ // 1. SHARE BUTTON+ androidx.compose.material3.FilledTonalButton(+ onClick = {+ if (selectedBitmap != null) {+ scope.launch {+ try {+ val fileName = "selection_${java.util.UUID.randomUUID()}.png"+ val path = ImageUtils.saveBitmap(context, selectedBitmap!!, fileName)+ val file = java.io.File(path)+ val uri = androidx.core.content.FileProvider.getUriForFile(context, "com.akslabs.circletosearch.fileprovider", file)+ val shareIntent = android.content.Intent(android.content.Intent.ACTION_SEND).apply {+ type = "image/png"+ putExtra(android.content.Intent.EXTRA_STREAM, uri)+ clipData = android.content.ClipData.newRawUri("Selection", uri)+ addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION)+ addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)+ }+ context.startActivity(android.content.Intent.createChooser(shareIntent, "Share Selection").apply {+ addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)+ })+ } catch (e: Exception) {+ android.util.Log.e("CircleToSearch", "Failed to share selection", e)}- context.startActivity(android.content.Intent.createChooser(shareIntent, "Share Selection").apply {- addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)- })- } catch (e: Exception) {- android.util.Log.e("CircleToSearch", "Failed to share selection", e)}}- }- },- modifier = Modifier.height(48.dp),- shape = CircleShape,- colors = androidx.compose.material3.ButtonDefaults.filledTonalButtonColors(- containerColor = MaterialTheme.colorScheme.surfaceContainer,- contentColor = MaterialTheme.colorScheme.onSurface- ),- elevation = null, // Surface handles shadow/tonal- contentPadding = androidx.compose.foundation.layout.PaddingValues(horizontal = 24.dp)- ) {- Text(- "Share",- style = MaterialTheme.typography.labelLarge.copy(- fontWeight = androidx.compose.ui.text.font.FontWeight.Normal+ },+ modifier = Modifier.height(48.dp),+ shape = CircleShape,+ colors = androidx.compose.material3.ButtonDefaults.filledTonalButtonColors(+ containerColor = Color.Transparent, // Parent surface handles color+ contentColor = MaterialTheme.colorScheme.onSurface+ ),+ elevation = null,+ contentPadding = androidx.compose.foundation.layout.PaddingValues(horizontal = 20.dp)+ ) {+ Text(+ "Share",+ style = MaterialTheme.typography.labelLarge.copy(+ fontWeight = androidx.compose.ui.text.font.FontWeight.Normal+ ))+ }++ // Separator+ androidx.compose.material3.VerticalDivider(+ modifier = Modifier.height(24.dp).padding(horizontal = 2.dp),+ color = MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.5f))++ // 2. SAVE BUTTON+ androidx.compose.material3.FilledTonalButton(+ onClick = {+ if (selectedBitmap != null) {+ val success = ImageUtils.saveToGallery(context, selectedBitmap!!)+ if (success) {+ haptic.performHapticFeedback(androidx.compose.ui.hapticfeedback.HapticFeedbackType.LongPress)+ android.widget.Toast.makeText(context, "Saved to Gallery", android.widget.Toast.LENGTH_SHORT).show()+ } else {+ android.widget.Toast.makeText(context, "Failed to save", android.widget.Toast.LENGTH_SHORT).show()+ }+ }+ },+ modifier = Modifier.height(48.dp),+ shape = CircleShape,+ colors = androidx.compose.material3.ButtonDefaults.filledTonalButtonColors(+ containerColor = Color.Transparent,+ contentColor = MaterialTheme.colorScheme.onSurface+ ),+ elevation = null,+ contentPadding = androidx.compose.foundation.layout.PaddingValues(horizontal = 20.dp)+ ) {+ Text(+ "Save",+ style = MaterialTheme.typography.labelLarge.copy(+ fontWeight = androidx.compose.ui.text.font.FontWeight.Normal+ )+ )+ }}}}diff --git a/app/src/main/java/com/akslabs/circletosearch/utils/ImageUtils.kt b/app/src/main/java/com/akslabs/circletosearch/utils/ImageUtils.ktindex 8950f0d..36fa8ac 100644--- a/app/src/main/java/com/akslabs/circletosearch/utils/ImageUtils.kt+++ b/app/src/main/java/com/akslabs/circletosearch/utils/ImageUtils.kt@@ -66,4 +66,40 @@ object ImageUtils {return source}}++ fun saveToGallery(context: Context, bitmap: Bitmap): Boolean {+ try {+ val filename = "CircleSelection_${System.currentTimeMillis()}.png"+ val contentValues = android.content.ContentValues().apply {+ put(android.provider.MediaStore.MediaColumns.DISPLAY_NAME, filename)+ put(android.provider.MediaStore.MediaColumns.MIME_TYPE, "image/png")+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {+ put(android.provider.MediaStore.MediaColumns.RELATIVE_PATH, android.os.Environment.DIRECTORY_PICTURES + "/CircleToSearch")+ put(android.provider.MediaStore.MediaColumns.IS_PENDING, 1)+ }+ }++ val contentResolver = context.contentResolver+ val imageUri = contentResolver.insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)++ if (imageUri == null) return false++ contentResolver.openOutputStream(imageUri).use { out ->+ if (out != null) {+ bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)+ }+ }++ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {+ contentValues.clear()+ contentValues.put(android.provider.MediaStore.MediaColumns.IS_PENDING, 0)+ contentResolver.update(imageUri, contentValues, null, null)+ }++ return true+ } catch (e: Exception) {+ android.util.Log.e("ImageUtils", "Failed to save to gallery", e)+ return false+ }+ }}diff --git a/build_errors_34.txt b/build_errors_34.txtdeleted file mode 100644index 5e291a1..0000000Binary files a/build_errors_34.txt and /dev/null differ