diff --git a/uikit/src/main/java/cn/wildfire/chat/kit/conversation/file/FileDetailActivity.java b/uikit/src/main/java/cn/wildfire/chat/kit/conversation/file/FileDetailActivity.java new file mode 100644 index 0000000000000000000000000000000000000000..9b99d8ef957035a9c833a7dbe64593b9e7738272 --- /dev/null +++ b/uikit/src/main/java/cn/wildfire/chat/kit/conversation/file/FileDetailActivity.java @@ -0,0 +1,238 @@ +/* + * Copyright (c) 2020 WildFireChat. All rights reserved. + */ + +package cn.wildfire.chat.kit.conversation.file; + +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.text.TextUtils; +import android.view.View; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.annotation.Nullable; + +import java.io.File; + +import cn.wildfire.chat.kit.R; +import cn.wildfire.chat.kit.WfcBaseActivity; +import cn.wildfire.chat.kit.utils.DownloadManager; +import cn.wildfire.chat.kit.utils.FileUtils; +import cn.wildfirechat.message.FileMessageContent; +import cn.wildfirechat.message.Message; +import cn.wildfirechat.model.Conversation; +import cn.wildfirechat.remote.ChatManager; + +public class FileDetailActivity extends WfcBaseActivity implements View.OnClickListener { + + private ImageView fileIconImageView; + private TextView fileNameTextView; + private TextView fileSizeTextView; + private Button saveButton; + private Button openWithOtherAppButton; + + private Message message; + private FileMessageContent fileMessageContent; + + @Override + protected int contentLayout() { + return R.layout.activity_file_detail; + } + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + initData(); + } + + @Override + protected void bindViews() { + super.bindViews(); + fileIconImageView = findViewById(R.id.fileIconImageView); + fileNameTextView = findViewById(R.id.fileNameTextView); + fileSizeTextView = findViewById(R.id.fileSizeTextView); + saveButton = findViewById(R.id.saveButton); + openWithOtherAppButton = findViewById(R.id.openWithOtherAppButton); + } + + @Override + protected void bindEvents() { + super.bindEvents(); + saveButton.setOnClickListener(this); + openWithOtherAppButton.setOnClickListener(this); + } + + private void initData() { + message = getIntent().getParcelableExtra("message"); + if (message == null || !(message.content instanceof FileMessageContent)) { + finish(); + return; + } + + fileMessageContent = (FileMessageContent) message.content; + + // 设置文件信息 + fileNameTextView.setText(fileMessageContent.getName()); + fileSizeTextView.setText(FileUtils.getReadableFileSize(fileMessageContent.getSize())); + fileIconImageView.setImageResource(FileUtils.getFileTypeImageResId(fileMessageContent.getName())); + + // 设置标题 + setTitle(fileMessageContent.getName()); + } + + @Override + public void onClick(View v) { + if (v.getId() == R.id.saveButton) { + saveFile(); + } else if (v.getId() == R.id.openWithOtherAppButton) { + openWithOtherApp(); + } + } + + private void saveFile() { + File file = DownloadManager.mediaMessageContentFile(message); + if (file == null) { + Toast.makeText(this, "文件路径无效", Toast.LENGTH_SHORT).show(); + return; + } + + if (file.exists()) { + // 文件已存在,复制到下载目录 + copyFileToDownloads(file); + } else { + // 下载文件 + downloadFile(file); + } + } + + private void copyFileToDownloads(File sourceFile) { + try { + File downloadsDir = FileUtils.getDownloadsDir(); + File targetFile = new File(downloadsDir, fileMessageContent.getName()); + + // 如果文件已存在,生成新的文件名 + targetFile = FileUtils.generateFileName(fileMessageContent.getName(), downloadsDir); + + if (targetFile != null && FileUtils.copyFile(sourceFile, targetFile)) { + Toast.makeText(this, "文件已保存到下载目录", Toast.LENGTH_SHORT).show(); + } else { + Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show(); + } + } catch (Exception e) { + e.printStackTrace(); + Toast.makeText(this, "保存失败: " + e.getMessage(), Toast.LENGTH_SHORT).show(); + } + } + + private void downloadFile(File file) { + String fileUrl; + if (message.conversation.type == Conversation.ConversationType.SecretChat) { + fileUrl = DownloadManager.buildSecretChatMediaUrl(message); + } else { + fileUrl = fileMessageContent.remoteUrl; + } + + if (TextUtils.isEmpty(fileUrl)) { + Toast.makeText(this, "文件下载地址无效", Toast.LENGTH_SHORT).show(); + return; + } + + Toast.makeText(this, "开始下载文件...", Toast.LENGTH_SHORT).show(); + + DownloadManager.download(fileUrl, FileUtils.getDownloadsDir().getAbsolutePath(), + fileMessageContent.getName(), new DownloadManager.OnDownloadListener() { + @Override + public void onSuccess(File file) { + runOnUiThread(() -> { + Toast.makeText(FileDetailActivity.this, "文件已保存到下载目录", Toast.LENGTH_SHORT).show(); + }); + } + + @Override + public void onProgress(int progress) { + // 可以在这里更新进度 + } + + @Override + public void onFail() { + runOnUiThread(() -> { + Toast.makeText(FileDetailActivity.this, "下载失败", Toast.LENGTH_SHORT).show(); + }); + } + }); + } + + private void openWithOtherApp() { + File file = DownloadManager.mediaMessageContentFile(message); + if (file == null) { + Toast.makeText(this, "文件路径无效", Toast.LENGTH_SHORT).show(); + return; + } + + ChatManager.Instance().setMediaMessagePlayed(message.messageId); + + if (file.exists()) { + tryOpenWithOtherApp(file); + } else { + // 下载文件后再打开 + downloadAndOpen(file); + } + } + + private void tryOpenWithOtherApp(File file) { + try { + Intent intent = FileUtils.getViewIntent(this, file); + startActivity(intent); + } catch (Exception e) { + Toast.makeText(this, "找不到能打开此文件的应用", Toast.LENGTH_SHORT).show(); + } + } + + private void downloadAndOpen(File file) { + String fileUrl; + if (message.conversation.type == Conversation.ConversationType.SecretChat) { + fileUrl = DownloadManager.buildSecretChatMediaUrl(message); + } else { + fileUrl = fileMessageContent.remoteUrl; + } + + if (TextUtils.isEmpty(fileUrl)) { + Toast.makeText(this, "文件下载地址无效", Toast.LENGTH_SHORT).show(); + return; + } + + Toast.makeText(this, "文件下载中,请稍候...", Toast.LENGTH_SHORT).show(); + + DownloadManager.download(fileUrl, file.getParent(), file.getName(), + new DownloadManager.OnDownloadListener() { + @Override + public void onSuccess(File downloadedFile) { + runOnUiThread(() -> { + tryOpenWithOtherApp(downloadedFile); + }); + } + + @Override + public void onProgress(int progress) { + // 可以在这里更新进度 + } + + @Override + public void onFail() { + runOnUiThread(() -> { + Toast.makeText(FileDetailActivity.this, "下载失败", Toast.LENGTH_SHORT).show(); + }); + } + }); + } + + public static void start(Context context, Message message) { + Intent intent = new Intent(context, FileDetailActivity.class); + intent.putExtra("message", message); + context.startActivity(intent); + } +} \ No newline at end of file