Converted DownloadUtils to a normal kotlin class, removed static methods.

MAILAND-1337
This commit is contained in:
Tomasz Giszczak 2021-02-08 09:58:18 +01:00
parent 811ca1b6b1
commit df916afeeb
6 changed files with 94 additions and 101 deletions

View File

@ -111,6 +111,8 @@ public class AddAttachmentsActivity extends BaseStoragePermissionActivity implem
@Inject
WorkManager workManager;
@Inject
DownloadUtils downloadUtils;
private String mPathToPhoto;
private String mDraftId;
@ -400,7 +402,7 @@ public class AddAttachmentsActivity extends BaseStoragePermissionActivity implem
public void onDownloadAttachmentEvent(DownloadedAttachmentEvent event) {
//once attachment has been downloaded
if (event.getStatus().equals(Status.SUCCESS)) {
DownloadUtils.viewAttachment(this, event.getFilename(), event.getAttachmentUri());
downloadUtils.viewAttachment(this, event.getFilename(), event.getAttachmentUri());
TextExtensions.showToast(this, String.format(getString(R.string.attachment_download_success), event.getFilename()), Toast.LENGTH_SHORT);
} else {
TextExtensions.showToast(this, String.format(getString(R.string.attachment_download_failed), event.getFilename()), Toast.LENGTH_SHORT);

View File

@ -85,7 +85,6 @@ import ch.protonmail.android.jobs.PostUnreadJob
import ch.protonmail.android.jobs.ReportPhishingJob
import ch.protonmail.android.utils.AppUtil
import ch.protonmail.android.utils.CustomLocale
import ch.protonmail.android.utils.DownloadUtils
import ch.protonmail.android.utils.Event
import ch.protonmail.android.utils.MessageUtils
import ch.protonmail.android.utils.UiUtil
@ -619,7 +618,7 @@ internal class MessageDetailsActivity :
attachmentsListAdapter.setIsPgpEncrypted(viewModel.isPgpEncrypted())
attachmentsListAdapter.setDownloaded(eventAttachmentId, isDownloaded)
if (isDownloaded) {
DownloadUtils.viewAttachment(this, event.filename, event.attachmentUri)
viewModel.viewAttachment(this, event.filename, event.attachmentUri)
} else {
showToast(R.string.downloading)
}

View File

@ -19,6 +19,7 @@
package ch.protonmail.android.activities.messageDetails.viewmodel
import android.content.Context
import android.net.Uri
import android.print.PrintManager
import androidx.hilt.Assisted
import androidx.hilt.lifecycle.ViewModelInject
@ -89,6 +90,7 @@ internal class MessageDetailsViewModel @ViewModelInject constructor(
private val attachmentsWorker: DownloadEmbeddedAttachmentsWorker.Enqueuer,
private val dispatchers: DispatcherProvider,
private val attachmentsHelper: AttachmentsHelper,
private val downloadUtils: DownloadUtils,
messageRendererFactory: MessageRenderer.Factory,
verifyConnection: VerifyConnection,
networkConfigurator: NetworkConfigurator
@ -489,7 +491,7 @@ internal class MessageDetailsViewModel @ViewModelInject constructor(
attachmentMetadataDatabase.deleteAttachmentMetadata(metadata)
attachmentsWorker.enqueue(messageId, userManager.username, attachmentToDownloadId)
} else {
DownloadUtils.viewAttachment(context, metadata.name, metadata.uri)
viewAttachment(context, metadata.name, metadata.uri)
}
} else {
attachmentsWorker.enqueue(messageId, userManager.username, attachmentToDownloadId)
@ -497,6 +499,9 @@ internal class MessageDetailsViewModel @ViewModelInject constructor(
}
}
fun viewAttachment(context: Context, filename: String?, uri: Uri?) =
downloadUtils.viewAttachment(context, filename, uri)
fun remoteContentDisplayed() {
remoteContentDisplayed = true
}

View File

@ -153,6 +153,8 @@ public class ProtonMailApplication extends Application implements androidx.work.
NetworkConfigurator networkConfigurator;
@Inject
NetworkSwitcher networkSwitcher;
@Inject
DownloadUtils downloadUtils;
private Bus mBus;
private boolean mIsInitialized;
@ -411,7 +413,7 @@ public class ProtonMailApplication extends Application implements androidx.work.
public void onDownloadAttachmentEvent(DownloadedAttachmentEvent event) {
final Status status = event.getStatus();
if (status != Status.FAILED) {
DownloadUtils.viewAttachment(this, event.getFilename(), event.getAttachmentUri(), !event.isOfflineLoaded());
downloadUtils.viewAttachmentNotification(this, event.getFilename(), event.getAttachmentUri(), !event.isOfflineLoaded());
}
}

View File

@ -1,96 +0,0 @@
/*
* Copyright (c) 2020 Proton Technologies AG
*
* This file is part of ProtonMail.
*
* ProtonMail is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ProtonMail is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ProtonMail. If not, see https://www.gnu.org/licenses/.
*/
package ch.protonmail.android.utils;
import android.app.NotificationManager;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;
import android.webkit.MimeTypeMap;
import androidx.annotation.Nullable;
import ch.protonmail.android.core.Constants;
import ch.protonmail.android.servers.notification.INotificationServer;
import ch.protonmail.android.servers.notification.NotificationServer;
import timber.log.Timber;
public class DownloadUtils {
public static void viewAttachment(Context context, String filename, @Nullable Uri uri) {
if (uri != null) {
String mimeType = "";
final ContentResolver resolver = context.getContentResolver();
if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
mimeType = resolver.getType(uri);
Cursor cursor = resolver.query(uri, null, null, null, null);
cursor.close();
} else if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
String extension = MimeTypeMap.getFileExtensionFromUrl(filename);
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
if (mimeType == null) {
mimeType = Constants.MIME_TYPE_UNKNOWN_FILE;
}
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType(mimeType);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(uri, mimeType);
try {
context.startActivity(intent);
} catch (ActivityNotFoundException notFoundException) {
Timber.i(notFoundException, "Unable to view attachment");
}
}
}
public static void viewAttachment(Context context, String fileName, @Nullable Uri uri, boolean showNotification) {
if (uri != null) {
String mimeType = "";
final ContentResolver resolver = context.getContentResolver();
if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
mimeType = resolver.getType(uri);
Cursor cursor = resolver.query(uri, null, null, null, null);
cursor.close();
} else if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
if (!TextUtils.isEmpty(extension)) {
extension = extension.toLowerCase();
}
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
if (mimeType == null) {
mimeType = Constants.MIME_TYPE_UNKNOWN_FILE;
}
}
NotificationManager notifyManager = (NotificationManager) context.getSystemService(
Context.NOTIFICATION_SERVICE);
INotificationServer notificationServer = new NotificationServer(context, notifyManager);
notificationServer.notifyAboutAttachment(fileName, uri, mimeType, showNotification);
}
}
}

View File

@ -0,0 +1,81 @@
/*
* Copyright (c) 2020 Proton Technologies AG
*
* This file is part of ProtonMail.
*
* ProtonMail is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ProtonMail is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ProtonMail. If not, see https://www.gnu.org/licenses/.
*/
package ch.protonmail.android.utils
import android.app.NotificationManager
import android.content.ActivityNotFoundException
import android.content.ContentResolver
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.webkit.MimeTypeMap
import ch.protonmail.android.core.Constants
import ch.protonmail.android.servers.notification.INotificationServer
import ch.protonmail.android.servers.notification.NotificationServer
import timber.log.Timber
import java.util.Locale
import javax.inject.Inject
class DownloadUtils @Inject constructor() {
fun viewAttachment(context: Context, filename: String?, uri: Uri?) {
if (uri != null) {
val mimeType = getMimeType(uri, context, filename)
Timber.d("viewAttachment mimeType: $mimeType uri: $uri uriScheme: ${uri.scheme}")
val intent = Intent(Intent.ACTION_VIEW).apply {
type = mimeType
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or
Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_GRANT_READ_URI_PERMISSION
setDataAndType(uri, mimeType)
}
try {
context.startActivity(intent)
} catch (notFoundException: ActivityNotFoundException) {
Timber.i(notFoundException, "Unable to view attachment")
}
}
}
fun viewAttachmentNotification(context: Context, fileName: String, uri: Uri?, showNotification: Boolean) {
if (uri != null) {
val mimeType = getMimeType(uri, context, fileName)
Timber.d("viewAttachmentNotification mimeType: $mimeType uri: $uri")
val notifyManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notificationServer: INotificationServer = NotificationServer(context, notifyManager)
notificationServer.notifyAboutAttachment(fileName, uri, mimeType, showNotification)
}
}
private fun getMimeType(uri: Uri, context: Context, filename: String?) = when {
ContentResolver.SCHEME_CONTENT == uri.scheme -> {
val resolver = context.contentResolver
resolver.getType(uri)
}
ContentResolver.SCHEME_FILE == uri.scheme -> {
val extension = MimeTypeMap.getFileExtensionFromUrl(filename)
MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase(Locale.ENGLISH))
}
else -> {
Constants.MIME_TYPE_UNKNOWN_FILE
}
}
}