mirror of
https://github.com/oxen-io/session-android.git
synced 2025-10-24 18:19:38 +00:00
Eliminate MediaNetworkRequirement style attachment job handling
// FREEBIE
This commit is contained in:
@@ -12,13 +12,14 @@ import org.thoughtcrime.securesms.crypto.AsymmetricMasterSecret;
|
||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||
import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
|
||||
import org.thoughtcrime.securesms.crypto.MediaKey;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.AttachmentDatabase;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
||||
import org.thoughtcrime.securesms.events.PartProgressEvent;
|
||||
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
||||
import org.thoughtcrime.securesms.jobs.requirements.MediaNetworkRequirement;
|
||||
import org.thoughtcrime.securesms.mms.MmsException;
|
||||
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
||||
import org.thoughtcrime.securesms.util.AttachmentUtil;
|
||||
import org.thoughtcrime.securesms.util.Hex;
|
||||
import org.whispersystems.jobqueue.JobParameters;
|
||||
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
||||
@@ -36,31 +37,30 @@ import java.io.InputStream;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.thoughtcrime.securesms.mms.MmsException;
|
||||
|
||||
public class AttachmentDownloadJob extends MasterSecretJob implements InjectableType {
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 2L;
|
||||
private static final int MAX_ATTACHMENT_SIZE = 150 * 1024 * 1024;
|
||||
private static final String TAG = AttachmentDownloadJob.class.getSimpleName();
|
||||
|
||||
@Inject transient SignalServiceMessageReceiver messageReceiver;
|
||||
|
||||
private final long messageId;
|
||||
private final long partRowId;
|
||||
private final long partUniqueId;
|
||||
private final long messageId;
|
||||
private final long partRowId;
|
||||
private final long partUniqueId;
|
||||
private final boolean manual;
|
||||
|
||||
public AttachmentDownloadJob(Context context, long messageId, AttachmentId attachmentId) {
|
||||
public AttachmentDownloadJob(Context context, long messageId, AttachmentId attachmentId, boolean manual) {
|
||||
super(context, JobParameters.newBuilder()
|
||||
.withGroupId(AttachmentDownloadJob.class.getCanonicalName())
|
||||
.withRequirement(new MasterSecretRequirement(context))
|
||||
.withRequirement(new NetworkRequirement(context))
|
||||
.withRequirement(new MediaNetworkRequirement(context, messageId, attachmentId))
|
||||
.withPersistence()
|
||||
.create());
|
||||
|
||||
this.messageId = messageId;
|
||||
this.partRowId = attachmentId.getRowId();
|
||||
this.partUniqueId = attachmentId.getUniqueId();
|
||||
this.manual = manual;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -69,8 +69,9 @@ public class AttachmentDownloadJob extends MasterSecretJob implements Injectable
|
||||
|
||||
@Override
|
||||
public void onRun(MasterSecret masterSecret) throws IOException {
|
||||
final AttachmentId attachmentId = new AttachmentId(partRowId, partUniqueId);
|
||||
final Attachment attachment = DatabaseFactory.getAttachmentDatabase(context).getAttachment(masterSecret, attachmentId);
|
||||
final AttachmentDatabase database = DatabaseFactory.getAttachmentDatabase(context);
|
||||
final AttachmentId attachmentId = new AttachmentId(partRowId, partUniqueId);
|
||||
final Attachment attachment = database.getAttachment(masterSecret, attachmentId);
|
||||
|
||||
if (attachment == null) {
|
||||
Log.w(TAG, "attachment no longer exists.");
|
||||
@@ -82,7 +83,13 @@ public class AttachmentDownloadJob extends MasterSecretJob implements Injectable
|
||||
return;
|
||||
}
|
||||
|
||||
if (!manual && !AttachmentUtil.isAutoDownloadPermitted(context, attachment)) {
|
||||
Log.w(TAG, "Attachment can't be auto downloaded...");
|
||||
return;
|
||||
}
|
||||
|
||||
Log.w(TAG, "Downloading push part " + attachmentId);
|
||||
database.setTransferState(messageId, attachmentId, AttachmentDatabase.TRANSFER_PROGRESS_STARTED);
|
||||
|
||||
retrieveAttachment(masterSecret, messageId, attachmentId, attachment);
|
||||
MessageNotifier.updateNotification(context, masterSecret);
|
||||
|
||||
@@ -508,8 +508,7 @@ public class PushDecryptJob extends ContextJob {
|
||||
for (DatabaseAttachment attachment : attachments) {
|
||||
ApplicationContext.getInstance(context)
|
||||
.getJobManager()
|
||||
.add(new AttachmentDownloadJob(context, insertResult.get().getMessageId(),
|
||||
attachment.getAttachmentId()));
|
||||
.add(new AttachmentDownloadJob(context, insertResult.get().getMessageId(), attachment.getAttachmentId(), false));
|
||||
|
||||
if (!masterSecret.getMasterSecret().isPresent()) {
|
||||
ApplicationContext.getInstance(context)
|
||||
@@ -579,7 +578,7 @@ public class PushDecryptJob extends ContextJob {
|
||||
for (DatabaseAttachment attachment : DatabaseFactory.getAttachmentDatabase(context).getAttachmentsForMessage(null, messageId)) {
|
||||
ApplicationContext.getInstance(context)
|
||||
.getJobManager()
|
||||
.add(new AttachmentDownloadJob(context, messageId, attachment.getAttachmentId()));
|
||||
.add(new AttachmentDownloadJob(context, messageId, attachment.getAttachmentId(), false));
|
||||
}
|
||||
|
||||
if (smsMessageId.isPresent()) {
|
||||
|
||||
@@ -1,120 +0,0 @@
|
||||
package org.thoughtcrime.securesms.jobs.requirements;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import org.thoughtcrime.securesms.attachments.Attachment;
|
||||
import org.thoughtcrime.securesms.attachments.AttachmentId;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.AttachmentDatabase;
|
||||
import org.thoughtcrime.securesms.util.MediaUtil;
|
||||
import org.thoughtcrime.securesms.util.ServiceUtil;
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
import org.whispersystems.jobqueue.dependencies.ContextDependent;
|
||||
import org.whispersystems.jobqueue.requirements.Requirement;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
public class MediaNetworkRequirement implements Requirement, ContextDependent {
|
||||
private static final long serialVersionUID = 0L;
|
||||
private static final String TAG = MediaNetworkRequirement.class.getSimpleName();
|
||||
|
||||
private transient Context context;
|
||||
|
||||
private final long messageId;
|
||||
private final long partRowId;
|
||||
private final long partUniqueId;
|
||||
|
||||
public MediaNetworkRequirement(Context context, long messageId, AttachmentId attachmentId) {
|
||||
this.context = context;
|
||||
this.messageId = messageId;
|
||||
this.partRowId = attachmentId.getRowId();
|
||||
this.partUniqueId = attachmentId.getUniqueId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContext(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
private NetworkInfo getNetworkInfo() {
|
||||
return ServiceUtil.getConnectivityManager(context).getActiveNetworkInfo();
|
||||
}
|
||||
|
||||
public boolean isConnectedWifi() {
|
||||
final NetworkInfo info = getNetworkInfo();
|
||||
return info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI;
|
||||
}
|
||||
|
||||
public boolean isConnectedMobile() {
|
||||
final NetworkInfo info = getNetworkInfo();
|
||||
return info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE;
|
||||
}
|
||||
|
||||
public boolean isConnectedRoaming() {
|
||||
final NetworkInfo info = getNetworkInfo();
|
||||
return info != null && info.isConnected() && info.isRoaming() && info.getType() == ConnectivityManager.TYPE_MOBILE;
|
||||
}
|
||||
|
||||
private @NonNull Set<String> getAllowedAutoDownloadTypes() {
|
||||
if (isConnectedWifi()) {
|
||||
return TextSecurePreferences.getWifiMediaDownloadAllowed(context);
|
||||
} else if (isConnectedRoaming()) {
|
||||
return TextSecurePreferences.getRoamingMediaDownloadAllowed(context);
|
||||
} else if (isConnectedMobile()) {
|
||||
return TextSecurePreferences.getMobileMediaDownloadAllowed(context);
|
||||
} else {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPresent() {
|
||||
final AttachmentId attachmentId = new AttachmentId(partRowId, partUniqueId);
|
||||
final AttachmentDatabase db = DatabaseFactory.getAttachmentDatabase(context);
|
||||
final Attachment attachment = db.getAttachment(null, attachmentId);
|
||||
|
||||
if (attachment == null) {
|
||||
Log.w(TAG, "attachment was null, returning vacuous true");
|
||||
return true;
|
||||
}
|
||||
|
||||
Log.w(TAG, "part transfer progress is " + attachment.getTransferState());
|
||||
switch (attachment.getTransferState()) {
|
||||
case AttachmentDatabase.TRANSFER_PROGRESS_STARTED:
|
||||
return true;
|
||||
case AttachmentDatabase.TRANSFER_PROGRESS_AUTO_PENDING:
|
||||
final Set<String> allowedTypes = getAllowedAutoDownloadTypes();
|
||||
final String contentType = attachment.getContentType();
|
||||
|
||||
boolean isAllowed;
|
||||
|
||||
if (attachment.isVoiceNote() || (MediaUtil.isAudio(attachment) && TextUtils.isEmpty(attachment.getFileName()))) {
|
||||
isAllowed = isConnectedWifi() || isConnectedMobile();
|
||||
} else if (isNonDocumentType(contentType)) {
|
||||
isAllowed = allowedTypes.contains(MediaUtil.getDiscreteMimeType(contentType));
|
||||
} else {
|
||||
isAllowed = allowedTypes.contains("documents");
|
||||
}
|
||||
|
||||
/// XXX WTF -- This is *hella* gross. A requirement shouldn't have the side effect of
|
||||
// *modifying the database* just by calling isPresent().
|
||||
if (isAllowed) db.setTransferState(messageId, attachmentId, AttachmentDatabase.TRANSFER_PROGRESS_STARTED);
|
||||
return isAllowed;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isNonDocumentType(String contentType) {
|
||||
return
|
||||
MediaUtil.isImageType(contentType) ||
|
||||
MediaUtil.isVideoType(contentType) ||
|
||||
MediaUtil.isAudioType(contentType);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package org.thoughtcrime.securesms.jobs.requirements;
|
||||
|
||||
import org.whispersystems.jobqueue.requirements.RequirementListener;
|
||||
import org.whispersystems.jobqueue.requirements.RequirementProvider;
|
||||
|
||||
public class MediaNetworkRequirementProvider implements RequirementProvider {
|
||||
|
||||
private RequirementListener listener;
|
||||
|
||||
public void notifyMediaControlEvent() {
|
||||
if (listener != null) listener.onRequirementStatusChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setListener(RequirementListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user