add ability to share plain text

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2021-05-19 21:55:18 +02:00
parent 36fdd6d82b
commit 59ccc19ebe
No known key found for this signature in database
GPG Key ID: C793F8B59F43CE7B
4 changed files with 61 additions and 28 deletions

View File

@ -265,6 +265,7 @@ class ChatController(args: Bundle) :
var futurePreconditionFailed = false
val filesToUpload: MutableList<String> = ArrayList()
var sharedText: String
init {
setHasOptionsMenu(true)
@ -273,6 +274,7 @@ class ChatController(args: Bundle) :
this.conversationUser = args.getParcelable(BundleKeys.KEY_USER_ENTITY)
this.roomId = args.getString(BundleKeys.KEY_ROOM_ID, "")
this.roomToken = args.getString(BundleKeys.KEY_ROOM_TOKEN, "")
this.sharedText = args.getString(BundleKeys.KEY_SHARED_TEXT, "")
if (args.containsKey(BundleKeys.KEY_ACTIVE_CONVERSATION)) {
this.currentConversation =
@ -583,6 +585,7 @@ class ChatController(args: Bundle) :
}
})
messageInput?.setText(sharedText)
messageInputView?.setAttachmentsListener {
activity?.let { AttachmentDialog(it, this).show() }
}
@ -1637,7 +1640,8 @@ class ChatController(args: Bundle) :
true
}
R.id.action_reply_privately -> {
val apiVersion = ApiUtils.getConversationApiVersion(conversationUser, intArrayOf(ApiUtils.APIv4, 1))
val apiVersion =
ApiUtils.getConversationApiVersion(conversationUser, intArrayOf(ApiUtils.APIv4, 1))
val retrofitBucket = ApiUtils.getRetrofitBucketForCreateRoom(
apiVersion,
conversationUser?.baseUrl,
@ -1686,6 +1690,7 @@ class ChatController(args: Bundle) :
override fun onError(e: Throwable) {
Log.e(TAG, e.message, e)
}
override fun onComplete() {}
})
}
@ -1693,6 +1698,7 @@ class ChatController(args: Bundle) :
override fun onError(e: Throwable) {
Log.e(TAG, e.message, e)
}
override fun onComplete() {}
})
true

View File

@ -25,6 +25,7 @@ package com.nextcloud.talk.controllers;
import android.animation.AnimatorInflater;
import android.annotation.SuppressLint;
import android.app.SearchManager;
import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
@ -58,7 +59,6 @@ import com.facebook.imagepipeline.core.ImagePipeline;
import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.request.ImageRequest;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.kennyc.bottomsheet.BottomSheet;
@ -98,13 +98,11 @@ import org.apache.commons.lang3.builder.CompareToBuilder;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;
import org.parceler.Parcels;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.inject.Inject;
@ -202,6 +200,8 @@ public class ConversationsListController extends BaseController implements Searc
private ArrayList<String> filesToShare;
private Conversation selectedConversation;
private String textToPaste = "";
public ConversationsListController() {
super();
setHasOptionsMenu(true);
@ -749,7 +749,7 @@ public class ConversationsListController extends BaseController implements Searc
if (selectedConversation != null && getActivity() != null) {
if (showShareToScreen) {
shareToScreenWasShown = true;
showShareToConfirmDialog();
handleSharedData();
} else {
openConversation();
}
@ -757,10 +757,19 @@ public class ConversationsListController extends BaseController implements Searc
return true;
}
private void showShareToConfirmDialog() {
if (UploadAndShareFilesWorker.Companion.isStoragePermissionGranted(context)) {
collectFilesToShareFromIntent();
private void handleSharedData() {
collectDataFromIntent();
if (!textToPaste.isEmpty()) {
openConversation(textToPaste);
} else if (filesToShare != null && !filesToShare.isEmpty()) {
showSendFilesConfirmDialog();
} else {
Toast.makeText(context, context.getResources().getString(R.string.nc_common_error_sorry), Toast.LENGTH_LONG).show();
}
}
private void showSendFilesConfirmDialog() {
if (UploadAndShareFilesWorker.Companion.isStoragePermissionGranted(context)) {
StringBuilder fileNamesWithLineBreaks = new StringBuilder("\n");
for (String file : filesToShare) {
@ -824,21 +833,33 @@ public class ConversationsListController extends BaseController implements Searc
}
}
private void collectFilesToShareFromIntent() {
private void collectDataFromIntent() {
filesToShare = new ArrayList<>();
if (getActivity() != null && getActivity().getIntent() != null) {
Intent intent = getActivity().getIntent();
if (Intent.ACTION_SEND.equals(intent.getAction())
|| Intent.ACTION_SEND_MULTIPLE.equals(intent.getAction())) {
try {
if (intent.getClipData() != null) {
for (int i = 0; i < intent.getClipData().getItemCount(); i++) {
ClipData.Item item = intent.getClipData().getItemAt(i);
if (item.getUri() != null) {
filesToShare.add(intent.getClipData().getItemAt(i).getUri().toString());
} else if (item.getText() != null) {
textToPaste = intent.getClipData().getItemAt(i).getText().toString();
break;
} else {
Log.w(TAG, "datatype not yet implemented for share-to");
}
}
} else {
filesToShare.add(intent.getData().toString());
}
if (filesToShare.isEmpty()) {
Log.e(TAG, "failed to get files from intent");
if (filesToShare.isEmpty() && textToPaste.isEmpty()) {
Log.e(TAG, "failed to get data from intent");
}
} catch (Exception e) {
Log.e(TAG, "Something went wrong when extracting data from intent");
}
}
}
@ -881,17 +902,22 @@ public class ConversationsListController extends BaseController implements Searc
grantResults.length > 0 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "upload starting after permissions were granted");
showShareToConfirmDialog();
showSendFilesConfirmDialog();
} else {
Toast.makeText(context, context.getString(R.string.read_storage_no_permission), Toast.LENGTH_LONG).show();
}
}
private void openConversation() {
openConversation("");
}
private void openConversation(String textToPaste) {
Bundle bundle = new Bundle();
bundle.putParcelable(BundleKeys.INSTANCE.getKEY_USER_ENTITY(), currentUser);
bundle.putString(BundleKeys.INSTANCE.getKEY_ROOM_TOKEN(), selectedConversation.getToken());
bundle.putString(BundleKeys.INSTANCE.getKEY_ROOM_ID(), selectedConversation.getRoomId());
bundle.putString(BundleKeys.INSTANCE.getKEY_SHARED_TEXT(), textToPaste);
if (selectedConversation.hasPassword && selectedConversation.participantType ==
Participant.ParticipantType.GUEST ||

View File

@ -75,10 +75,10 @@ class UploadAndShareFilesWorker(val context: Context, workerParameters: WorkerPa
if (!isStoragePermissionGranted(context)) {
Log.w(
TAG, "Storage permission is not granted. As a developer please make sure you check for permissions " +
"via UploadAndShareFilesWorker.isStoragePermissionGranted() and UploadAndShareFilesWorker" +
".requestStoragePermission() beforehand. If you already did but end up with this warning, the user " +
"most likely revoked the permission"
TAG, "Storage permission is not granted. As a developer please make sure you check for" +
"permissions via UploadAndShareFilesWorker.isStoragePermissionGranted() and " +
"UploadAndShareFilesWorker.requestStoragePermission() beforehand. If you already " +
"did but end up with this warning, the user most likely revoked the permission"
)
}

View File

@ -65,4 +65,5 @@ object BundleKeys {
val KEY_ACCOUNT = "KEY_ACCOUNT"
val KEY_FILE_ID = "KEY_FILE_ID"
val KEY_NOTIFICATION_ID = "KEY_NOTIFICATION_ID"
val KEY_SHARED_TEXT = "KEY_SHARED_TEXT"
}