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 var futurePreconditionFailed = false
val filesToUpload: MutableList<String> = ArrayList() val filesToUpload: MutableList<String> = ArrayList()
var sharedText: String
init { init {
setHasOptionsMenu(true) setHasOptionsMenu(true)
@ -273,6 +274,7 @@ class ChatController(args: Bundle) :
this.conversationUser = args.getParcelable(BundleKeys.KEY_USER_ENTITY) this.conversationUser = args.getParcelable(BundleKeys.KEY_USER_ENTITY)
this.roomId = args.getString(BundleKeys.KEY_ROOM_ID, "") this.roomId = args.getString(BundleKeys.KEY_ROOM_ID, "")
this.roomToken = args.getString(BundleKeys.KEY_ROOM_TOKEN, "") this.roomToken = args.getString(BundleKeys.KEY_ROOM_TOKEN, "")
this.sharedText = args.getString(BundleKeys.KEY_SHARED_TEXT, "")
if (args.containsKey(BundleKeys.KEY_ACTIVE_CONVERSATION)) { if (args.containsKey(BundleKeys.KEY_ACTIVE_CONVERSATION)) {
this.currentConversation = this.currentConversation =
@ -550,7 +552,7 @@ class ChatController(args: Bundle) :
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (s.length >= lengthFilter) { if (s.length >= lengthFilter) {
messageInput?.error = String.format( messageInput?.error = String.format(
Objects.requireNonNull<Resources> (resources).getString(R.string.nc_limit_hit), Objects.requireNonNull<Resources>(resources).getString(R.string.nc_limit_hit),
Integer.toString(lengthFilter) Integer.toString(lengthFilter)
) )
} else { } else {
@ -583,6 +585,7 @@ class ChatController(args: Bundle) :
} }
}) })
messageInput?.setText(sharedText)
messageInputView?.setAttachmentsListener { messageInputView?.setAttachmentsListener {
activity?.let { AttachmentDialog(it, this).show() } activity?.let { AttachmentDialog(it, this).show() }
} }
@ -1637,7 +1640,8 @@ class ChatController(args: Bundle) :
true true
} }
R.id.action_reply_privately -> { 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( val retrofitBucket = ApiUtils.getRetrofitBucketForCreateRoom(
apiVersion, apiVersion,
conversationUser?.baseUrl, conversationUser?.baseUrl,
@ -1686,6 +1690,7 @@ class ChatController(args: Bundle) :
override fun onError(e: Throwable) { override fun onError(e: Throwable) {
Log.e(TAG, e.message, e) Log.e(TAG, e.message, e)
} }
override fun onComplete() {} override fun onComplete() {}
}) })
} }
@ -1693,6 +1698,7 @@ class ChatController(args: Bundle) :
override fun onError(e: Throwable) { override fun onError(e: Throwable) {
Log.e(TAG, e.message, e) Log.e(TAG, e.message, e)
} }
override fun onComplete() {} override fun onComplete() {}
}) })
true true

View File

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

View File

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