Skip to content

Commit

Permalink
支持从其他应用分享文件、图片等到野火IM fix #893
Browse files Browse the repository at this point in the history
  • Loading branch information
imndx committed Jan 2, 2025
1 parent ebcc6c5 commit ac50a88
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
4 changes: 2 additions & 2 deletions chat/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@
<intent-filter android:label="分享到野火IM">
<action android:name="android.intent.action.SENDTO" />
<action android:name="android.intent.action.SEND" />

<!-- <action android:name="android.intent.action.SEND_MULTIPLE" />-->
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
<data android:mimeType="*/*" />
</intent-filter>
</activity>

Expand Down
67 changes: 62 additions & 5 deletions chat/src/main/java/cn/wildfire/chat/app/main/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,19 @@
import cn.wildfire.chat.kit.user.ChangeMyNameActivity;
import cn.wildfire.chat.kit.user.UserInfoActivity;
import cn.wildfire.chat.kit.user.UserViewModel;
import cn.wildfire.chat.kit.utils.FileUtils;
import cn.wildfire.chat.kit.viewmodel.MessageViewModel;
import cn.wildfire.chat.kit.voip.conference.ConferenceInfoActivity;
import cn.wildfire.chat.kit.workspace.WebViewFragment;
import cn.wildfirechat.chat.R;
import cn.wildfirechat.client.ConnectionStatus;
import cn.wildfirechat.message.FileMessageContent;
import cn.wildfirechat.message.ImageMessageContent;
import cn.wildfirechat.message.LinkMessageContent;
import cn.wildfirechat.message.Message;
import cn.wildfirechat.message.MessageContent;
import cn.wildfirechat.message.TextMessageContent;
import cn.wildfirechat.message.VideoMessageContent;
import cn.wildfirechat.message.core.MessageContentType;
import cn.wildfirechat.message.core.MessageStatus;
import cn.wildfirechat.model.Conversation;
Expand Down Expand Up @@ -189,8 +193,13 @@ protected void afterViews() {
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action)) {
if ("text/plain".equals(type)) {
handleSend(intent);
handleSendText(intent);
} else {
Uri fileUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
handleSendFile(fileUri);
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
handleSendMultiple(intent);
}

requestMandatoryPermissions();
Expand All @@ -203,8 +212,13 @@ protected void onNewIntent(Intent intent) {
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action)) {
if ("text/plain".equals(type)) {
handleSend(intent);
handleSendText(intent);
} else {
Uri fileUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
handleSendFile(fileUri);
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
handleSendMultiple(intent); // Handle multiple items being sent
}
}

Expand Down Expand Up @@ -352,7 +366,7 @@ private void initView() {
switch (item.getItemId()) {
case R.id.conversation_list:
long now = System.currentTimeMillis();
if(now - lastSelectConversatonListItemTimestamp < 200){
if (now - lastSelectConversatonListItemTimestamp < 200) {
conversationListFragment.scrollToNextUnreadConversation();
}
lastSelectConversatonListItemTimestamp = now;
Expand Down Expand Up @@ -617,8 +631,8 @@ public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which)
}

// 分享
private void handleSend(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_INTENT);
private void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (!TextUtils.isEmpty(sharedText)) {
MessageContent content = new TextMessageContent(sharedText);
shareMessage(content);
Expand All @@ -642,6 +656,49 @@ private void handleSend(Intent intent) {
}
}

private void handleSendMultiple(Intent intent) {
// TODO 暂不支持一次分享多个文件,分享页面不支持,没有相关 UI
// ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
// if (imageUris != null) {
// for (Uri uri : imageUris) {
// handleSendFile(uri);
// }
// }
}

private void handleSendFile(Uri fileUri) {
if (fileUri == null) {
return;
}
String filePath = FileUtils.getPath(this, fileUri);
if (TextUtils.isEmpty(filePath)) {
Toast.makeText(this, "Error selecting file", Toast.LENGTH_SHORT).show();
return;
}
String suffix = filePath.substring(filePath.lastIndexOf("."));
MessageContent content;
switch (suffix) {
case ".png":
case ".jpg":
case ".jpeg":
case ".gif":
content = new ImageMessageContent(filePath);
break;
case ".3gp":
case ".mpg":
case ".mpeg":
case ".mpe":
case ".mp4":
case ".avi":
content = new VideoMessageContent(filePath);
break;
default:
content = new FileMessageContent(filePath);
break;
}
shareMessage(content);
}

private void shareMessage(MessageContent content) {
ArrayList<Message> msgs = new ArrayList<>();
Message message = new Message();
Expand Down

0 comments on commit ac50a88

Please sign in to comment.