Skip to content

Commit

Permalink
Add message sender
Browse files Browse the repository at this point in the history
Add message signature
Add save image to album
Fix bugs

添加信息发送者
添加信息署名
添加保存图片到相册
修复bugs

Signed-off-by: gohj99 <[email protected]>
  • Loading branch information
gohj99 committed Aug 21, 2024
1 parent 8823687 commit 5876248
Show file tree
Hide file tree
Showing 16 changed files with 354 additions and 123 deletions.
15 changes: 13 additions & 2 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ android {
//noinspection OldTargetApi
targetSdk = 34
versionCode = 1
versionName = "1.0.6"
versionName = "1.0.7"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down Expand Up @@ -107,7 +107,6 @@ dependencies {
implementation(libs.androidx.material3)
implementation(libs.coil.compose)
implementation(libs.androidx.compose.foundation)
implementation(libs.zoomable)
implementation(libs.gson)
testImplementation(libs.junit)
implementation(libs.zxing.core)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/gohj99/telewatch/AboutActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fun SplashAboutScreen(appVersion: String, buildDate: String) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(top = 25.dp)
.padding(top = 20.dp)
.background(Color.Black),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
Expand Down Expand Up @@ -139,7 +139,7 @@ fun SplashAboutScreen(appVersion: String, buildDate: String) {
textAlign = TextAlign.Center
)

Spacer(modifier = Modifier.height(8.dp))
Spacer(modifier = Modifier.height(12.dp))

Text(
text = stringResource(id = R.string.Build_Date) + " $buildDate",
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/com/gohj99/telewatch/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ class ChatActivity : ComponentActivity() {
SplashChatScreen(
chatTitle = chat!!.title,
chatList = chatList,
chatId = chat!!.id,
currentUserId = currentUserId.value,
goToChat = { chat ->
startActivity(
Intent(this@ChatActivity, ChatActivity::class.java).apply {
putExtra("chat", chat)
}
)
},
sendCallback = { messageText ->
tgApi?.sendMessage(
chatId = chat!!.id,
Expand Down
90 changes: 87 additions & 3 deletions app/src/main/java/com/gohj99/telewatch/ImgViewActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@

package com.gohj99.telewatch

import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
Expand Down Expand Up @@ -42,6 +52,7 @@ import net.engawapg.lib.zoomable.rememberZoomState
import net.engawapg.lib.zoomable.zoomable
import org.drinkless.td.libcore.telegram.TdApi
import java.io.File
import java.io.FileOutputStream

class ImgViewActivity : ComponentActivity() {
private var tgApi: TgApi? = null
Expand Down Expand Up @@ -132,7 +143,9 @@ class ImgViewActivity : ComponentActivity() {
private fun showImage(photoPath: String) {
setContent {
TelewatchTheme {
SplashImgView(photoPath)
SplashImgView(photoPath, {
Toast.makeText(this, saveImageToExternalStorage(this, photoPath), Toast.LENGTH_SHORT).show()
})
}
}
}
Expand All @@ -157,8 +170,73 @@ class ImgViewActivity : ComponentActivity() {
}
}

fun saveImageToExternalStorage(context: Context, photoPath: String): String {
// 获取图片文件名
val imageName = File(photoPath).nameWithoutExtension

// 从内部存储读取图片
val bitmap = BitmapFactory.decodeFile(photoPath) ?: return context.getString(R.string.Read_failed)

return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
// Android 10及以上使用MediaStore
val selection = "${MediaStore.Images.Media.DISPLAY_NAME} = ?"
val selectionArgs = arrayOf("$imageName.jpg")
val queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
val cursor = context.contentResolver.query(queryUri, null, selection, selectionArgs, null)

if (cursor != null && cursor.count > 0) {
cursor.close()
return context.getString(R.string.Same_name_image_exists)
}
cursor?.close()

val contentValues = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, "$imageName.jpg")
put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/Telewatch")
put(MediaStore.Images.Media.IS_PENDING, 1) // 设置IS_PENDING状态
}

val uri = context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
?: return context.getString(R.string.failling)

context.contentResolver.openOutputStream(uri)?.use { outputStream ->
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outputStream)
outputStream.flush()
}

// 更新IS_PENDING状态
contentValues.clear()
contentValues.put(MediaStore.Images.Media.IS_PENDING, 0)
context.contentResolver.update(uri, contentValues, null, null)

return uri.toString()
} else {
// Android 8和9使用传统方式保存到外部存储
val imagesDir = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Telewatch")
if (!imagesDir.exists()) {
imagesDir.mkdirs()
}

val file = File(imagesDir, "$imageName.jpg")

if (file.exists()) {
return context.getString(R.string.Same_name_image_exists)
}

FileOutputStream(file).use { outputStream ->
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outputStream)
}

// 通知系统图库更新
context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)))

return file.absolutePath
}
}

@Composable
private fun SplashImgView(photoPath: String) {
private fun SplashImgView(photoPath: String, saveImage: () -> Unit) {
Box(
modifier = Modifier
.padding(16.dp)
Expand All @@ -169,7 +247,13 @@ private fun SplashImgView(photoPath: String) {
Image(
painter = rememberAsyncImagePainter(model = photoPath),
contentDescription = null,
modifier = Modifier.zoomable(rememberZoomState()),
modifier = Modifier
.zoomable(
zoomState = rememberZoomState(),
onLongPress = {
saveImage()
}
),
)
}
}
4 changes: 1 addition & 3 deletions app/src/main/java/com/gohj99/telewatch/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ class MainActivity : ComponentActivity() {
this@MainActivity,
chatsList = chatsList
)
TgApiManager.tgApi?.getChats(
limit = 10
)
TgApiManager.tgApi?.loadChats(15)
launch(Dispatchers.Main) {
setContent {
TelewatchTheme {
Expand Down
Loading

0 comments on commit 5876248

Please sign in to comment.