From 35504ec023d771f2fa12e214e8d823624f4fd3f0 Mon Sep 17 00:00:00 2001 From: DenBond7 Date: Fri, 5 May 2023 09:39:06 +0300 Subject: [PATCH] Added using Content app if 'RESTRICT_ANDROID_ATTACHMENT_HANDLING' is present. Refactored code.| #2137 --- .../java/com/flowcrypt/email/Constants.kt | 2 ++ .../AttachmentDownloadManagerService.kt | 23 +++++++++++-------- .../AttachmentNotificationManager.kt | 13 ++++++----- .../DownloadAttachmentTaskResult.kt | 2 +- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/Constants.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/Constants.kt index d3f77b69ee..4586761527 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/Constants.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/Constants.kt @@ -145,5 +145,7 @@ class Constants { */ const val REQUEST_KEY_BUTTON_CLICK = "REQUEST_KEY_BUTTON_CLICK" const val REQUEST_KEY_INFO_BUTTON_CLICK = "REQUEST_KEY_INFO_BUTTON_CLICK" + + const val APP_PACKAGE_CONTENT = "com.airwatch.contentlocker" } } diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/service/attachment/AttachmentDownloadManagerService.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/service/attachment/AttachmentDownloadManagerService.kt index f620430ef3..ab67f6c5d0 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/service/attachment/AttachmentDownloadManagerService.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/service/attachment/AttachmentDownloadManagerService.kt @@ -161,7 +161,7 @@ class AttachmentDownloadManagerService : Service() { } private interface OnDownloadAttachmentListener { - fun onAttDownloaded(attInfo: AttachmentInfo, uri: Uri, canBeOpened: Boolean = true) + fun onAttDownloaded(attInfo: AttachmentInfo, uri: Uri, useContentApp: Boolean = false) fun onCanceled(attInfo: AttachmentInfo) @@ -186,7 +186,7 @@ class AttachmentDownloadManagerService : Service() { val attDownloadManagerService = weakRef.get() val notificationManager = attDownloadManagerService?.attsNotificationManager - val (attInfo, exception, uri, progressInPercentage, timeLeft, isLast, canBeOpened) + val (attInfo, exception, uri, progressInPercentage, timeLeft, isLast, useContentApp) = message.obj as DownloadAttachmentTaskResult when (message.what) { @@ -208,7 +208,7 @@ class AttachmentDownloadManagerService : Service() { context = attDownloadManagerService, attInfo = attInfo!!, uri = uri!!, - canBeOpened = canBeOpened + useContentApp = useContentApp ) LogsUtil.d(TAG, attInfo?.getSafeName() + " is downloaded") } @@ -373,7 +373,7 @@ class AttachmentDownloadManagerService : Service() { } } - override fun onAttDownloaded(attInfo: AttachmentInfo, uri: Uri, canBeOpened: Boolean) { + override fun onAttDownloaded(attInfo: AttachmentInfo, uri: Uri, useContentApp: Boolean) { attsInfoMap.remove(attInfo.id) futureMap.remove(attInfo.uniqueStringId) try { @@ -381,7 +381,7 @@ class AttachmentDownloadManagerService : Service() { attInfo = attInfo, uri = uri, isLast = isLast, - canBeOpened = canBeOpened + useContentApp = useContentApp ) messenger.send(Message.obtain(null, ReplyHandler.MESSAGE_ATTACHMENT_DOWNLOAD, result)) } catch (remoteException: RemoteException) { @@ -463,7 +463,7 @@ class AttachmentDownloadManagerService : Service() { listener?.onAttDownloaded( attInfo = att.copy(name = finalFileName), uri = uri, - canBeOpened = !account.hasClientConfigurationProperty( + useContentApp = account.hasClientConfigurationProperty( ClientConfiguration.ConfigurationProperty.RESTRICT_ANDROID_ATTACHMENT_HANDLING ) ) @@ -488,7 +488,7 @@ class AttachmentDownloadManagerService : Service() { ).use { inputStream -> handleAttachmentInputStream( inputStream = inputStream, - canBeOpened = !account.hasClientConfigurationProperty( + useContentApp = account.hasClientConfigurationProperty( ClientConfiguration.ConfigurationProperty.RESTRICT_ANDROID_ATTACHMENT_HANDLING ) ) @@ -517,7 +517,7 @@ class AttachmentDownloadManagerService : Service() { )?.inputStream?.let { inputStream -> handleAttachmentInputStream( inputStream = inputStream, - canBeOpened = !account.hasClientConfigurationProperty( + useContentApp = account.hasClientConfigurationProperty( ClientConfiguration.ConfigurationProperty.RESTRICT_ANDROID_ATTACHMENT_HANDLING ) ) @@ -534,7 +534,10 @@ class AttachmentDownloadManagerService : Service() { } } - private fun handleAttachmentInputStream(inputStream: InputStream, canBeOpened: Boolean = true) { + private fun handleAttachmentInputStream( + inputStream: InputStream, + useContentApp: Boolean = false + ) { downloadFile(attTempFile, inputStream) if (Thread.currentThread().isInterrupted) { @@ -548,7 +551,7 @@ class AttachmentDownloadManagerService : Service() { listener?.onAttDownloaded( attInfo = att.copy(name = finalFileName), uri = uri, - canBeOpened = canBeOpened + useContentApp = useContentApp ) } } diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/service/attachment/AttachmentNotificationManager.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/service/attachment/AttachmentNotificationManager.kt index d4704badfe..f1f720efd7 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/service/attachment/AttachmentNotificationManager.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/service/attachment/AttachmentNotificationManager.kt @@ -15,6 +15,7 @@ import android.text.TextUtils import android.text.format.DateUtils import androidx.core.app.NotificationCompat import com.flowcrypt.email.BuildConfig +import com.flowcrypt.email.Constants import com.flowcrypt.email.R import com.flowcrypt.email.api.email.model.AttachmentInfo import com.flowcrypt.email.security.pgp.PgpDecryptAndOrVerify @@ -88,18 +89,18 @@ class AttachmentNotificationManager(context: Context) : CustomNotificationManage * @param context Interface to global information about an application environment. * @param attInfo [AttachmentInfo] object which contains a detail information about an attachment. * @param uri The [Uri] of the downloaded attachment. - * @param canBeOpened A flag that indicates can we open an attachment after downloading. + * @param useContentApp A flag that indicates should we use the Content app as a viewer. */ fun downloadCompleted( context: Context, attInfo: AttachmentInfo, uri: Uri, - canBeOpened: Boolean = true + useContentApp: Boolean = false ) { - val intent = if (canBeOpened) { - GeneralUtil.genViewAttachmentIntent(uri, attInfo) + val intent = if (useContentApp) { + GeneralUtil.genViewAttachmentIntent(uri, attInfo).setPackage(Constants.APP_PACKAGE_CONTENT) } else { - Intent() + GeneralUtil.genViewAttachmentIntent(uri, attInfo) } val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE) val builder = genDefBuilder(context, attInfo) @@ -198,7 +199,7 @@ class AttachmentNotificationManager(context: Context) : CustomNotificationManage * @param attachmentInfo The [AttachmentInfo] object. * @return The prepared title. */ - private fun prepareContentTitle(attachmentInfo: AttachmentInfo): String? { + private fun prepareContentTitle(attachmentInfo: AttachmentInfo): String { var contentTitle = attachmentInfo.getSafeName() if (!TextUtils.isEmpty(contentTitle) && contentTitle.length > MAX_CONTENT_TITLE_LENGTH) { contentTitle = contentTitle.substring(0, MAX_CONTENT_TITLE_LENGTH) + "..." diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/service/attachment/DownloadAttachmentTaskResult.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/service/attachment/DownloadAttachmentTaskResult.kt index cd692820c2..577777ab2b 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/service/attachment/DownloadAttachmentTaskResult.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/service/attachment/DownloadAttachmentTaskResult.kt @@ -20,5 +20,5 @@ data class DownloadAttachmentTaskResult constructor( val progressInPercentage: Int = 0, val timeLeft: Long = 0, val isLast: Boolean = false, - val canBeOpened: Boolean = true + val useContentApp: Boolean = false )