2017-07-21 15:59:27 -07:00
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-06-11 10:38:11 -07:00
|
|
|
import android.database.Cursor;
|
2017-07-21 15:59:27 -07:00
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.NetworkInfo;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2018-03-15 11:17:40 -07:00
|
|
|
import android.support.annotation.WorkerThread;
|
2017-07-21 15:59:27 -07:00
|
|
|
import android.text.TextUtils;
|
|
|
|
|
2018-03-15 11:17:40 -07:00
|
|
|
import org.thoughtcrime.securesms.attachments.AttachmentId;
|
|
|
|
import org.thoughtcrime.securesms.attachments.DatabaseAttachment;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2018-06-11 10:38:11 -07:00
|
|
|
import org.thoughtcrime.securesms.database.model.MessageRecord;
|
2019-04-17 10:21:30 -04:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2017-07-21 15:59:27 -07:00
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public class AttachmentUtil {
|
|
|
|
|
|
|
|
private static final String TAG = AttachmentUtil.class.getSimpleName();
|
|
|
|
|
2018-06-11 11:08:25 -07:00
|
|
|
@WorkerThread
|
2018-06-11 10:38:11 -07:00
|
|
|
public static boolean isAutoDownloadPermitted(@NonNull Context context, @Nullable DatabaseAttachment attachment) {
|
2017-07-21 15:59:27 -07:00
|
|
|
if (attachment == null) {
|
|
|
|
Log.w(TAG, "attachment was null, returning vacuous true");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-11 10:38:11 -07:00
|
|
|
if (isFromUnknownContact(context, attachment)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-21 15:59:27 -07:00
|
|
|
Set<String> allowedTypes = getAllowedAutoDownloadTypes(context);
|
|
|
|
String contentType = attachment.getContentType();
|
|
|
|
|
2019-04-17 10:21:30 -04:00
|
|
|
if (attachment.isVoiceNote() ||
|
|
|
|
(MediaUtil.isAudio(attachment) && TextUtils.isEmpty(attachment.getFileName())) ||
|
|
|
|
MediaUtil.isLongTextType(attachment.getContentType()) ||
|
|
|
|
attachment.isSticker())
|
|
|
|
{
|
2017-07-21 15:59:27 -07:00
|
|
|
return true;
|
|
|
|
} else if (isNonDocumentType(contentType)) {
|
|
|
|
return allowedTypes.contains(MediaUtil.getDiscreteMimeType(contentType));
|
|
|
|
} else {
|
|
|
|
return allowedTypes.contains("documents");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 11:17:40 -07:00
|
|
|
/**
|
|
|
|
* Deletes the specified attachment. If its the only attachment for its linked message, the entire
|
|
|
|
* message is deleted.
|
|
|
|
*/
|
|
|
|
@WorkerThread
|
|
|
|
public static void deleteAttachment(@NonNull Context context,
|
|
|
|
@NonNull DatabaseAttachment attachment)
|
|
|
|
{
|
|
|
|
AttachmentId attachmentId = attachment.getAttachmentId();
|
|
|
|
long mmsId = attachment.getMmsId();
|
|
|
|
int attachmentCount = DatabaseFactory.getAttachmentDatabase(context)
|
|
|
|
.getAttachmentsForMessage(mmsId)
|
|
|
|
.size();
|
|
|
|
|
|
|
|
if (attachmentCount <= 1) {
|
|
|
|
DatabaseFactory.getMmsDatabase(context).delete(mmsId);
|
|
|
|
} else {
|
|
|
|
DatabaseFactory.getAttachmentDatabase(context).deleteAttachment(attachmentId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-21 15:59:27 -07:00
|
|
|
private static boolean isNonDocumentType(String contentType) {
|
|
|
|
return
|
|
|
|
MediaUtil.isImageType(contentType) ||
|
|
|
|
MediaUtil.isVideoType(contentType) ||
|
|
|
|
MediaUtil.isAudioType(contentType);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static @NonNull Set<String> getAllowedAutoDownloadTypes(@NonNull Context context) {
|
|
|
|
if (isConnectedWifi(context)) return TextSecurePreferences.getWifiMediaDownloadAllowed(context);
|
|
|
|
else if (isConnectedRoaming(context)) return TextSecurePreferences.getRoamingMediaDownloadAllowed(context);
|
|
|
|
else if (isConnectedMobile(context)) return TextSecurePreferences.getMobileMediaDownloadAllowed(context);
|
|
|
|
else return Collections.emptySet();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static NetworkInfo getNetworkInfo(@NonNull Context context) {
|
|
|
|
return ServiceUtil.getConnectivityManager(context).getActiveNetworkInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isConnectedWifi(@NonNull Context context) {
|
|
|
|
final NetworkInfo info = getNetworkInfo(context);
|
|
|
|
return info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isConnectedMobile(@NonNull Context context) {
|
|
|
|
final NetworkInfo info = getNetworkInfo(context);
|
|
|
|
return info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isConnectedRoaming(@NonNull Context context) {
|
|
|
|
final NetworkInfo info = getNetworkInfo(context);
|
|
|
|
return info != null && info.isConnected() && info.isRoaming() && info.getType() == ConnectivityManager.TYPE_MOBILE;
|
|
|
|
}
|
|
|
|
|
2018-06-11 11:08:25 -07:00
|
|
|
@WorkerThread
|
2018-06-11 10:38:11 -07:00
|
|
|
private static boolean isFromUnknownContact(@NonNull Context context, @NonNull DatabaseAttachment attachment) {
|
|
|
|
try (Cursor messageCursor = DatabaseFactory.getMmsDatabase(context).getMessage(attachment.getMmsId())) {
|
|
|
|
final MessageRecord message = DatabaseFactory.getMmsDatabase(context).readerFor(messageCursor).getNext();
|
2017-07-21 15:59:27 -07:00
|
|
|
|
2018-06-22 09:40:32 -07:00
|
|
|
if (message == null || (!message.getRecipient().isSystemContact() && !message.isOutgoing() && !Util.isOwnNumber(context, message.getRecipient().getAddress()))) {
|
2018-06-11 10:38:11 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-21 15:59:27 -07:00
|
|
|
}
|