2014-11-03 15:16:04 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-08-09 10:15:43 -04:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.SafeData;
|
2018-08-01 11:09:24 -04:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2018-08-09 10:15:43 -04:00
|
|
|
|
|
|
|
import android.support.annotation.NonNull;
|
2014-11-03 15:16:04 -08:00
|
|
|
import android.util.Pair;
|
|
|
|
|
2017-05-08 15:32:59 -07:00
|
|
|
import com.google.android.mms.pdu_alt.GenericPdu;
|
|
|
|
import com.google.android.mms.pdu_alt.NotificationInd;
|
|
|
|
import com.google.android.mms.pdu_alt.PduHeaders;
|
|
|
|
import com.google.android.mms.pdu_alt.PduParser;
|
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
2017-07-26 09:59:15 -07:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.MmsDatabase;
|
2018-06-18 12:27:04 -07:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobParameters;
|
2017-08-01 08:56:00 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2018-08-09 10:15:43 -04:00
|
|
|
import org.thoughtcrime.securesms.util.Base64;
|
2015-06-09 07:37:20 -07:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import androidx.work.Data;
|
2018-11-27 12:34:42 -08:00
|
|
|
import androidx.work.WorkerParameters;
|
2018-08-09 10:15:43 -04:00
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
public class MmsReceiveJob extends ContextJob {
|
|
|
|
|
2016-02-05 16:10:33 -08:00
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
private static final String TAG = MmsReceiveJob.class.getSimpleName();
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private static final String KEY_DATA = "data";
|
|
|
|
private static final String KEY_SUBSCRIPTION_ID = "subscription_id";
|
|
|
|
|
|
|
|
private byte[] data;
|
|
|
|
private int subscriptionId;
|
|
|
|
|
2018-11-27 12:34:42 -08:00
|
|
|
public MmsReceiveJob(@NonNull Context context, @NonNull WorkerParameters workerParameters) {
|
|
|
|
super(context, workerParameters);
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2016-02-05 16:10:33 -08:00
|
|
|
public MmsReceiveJob(Context context, byte[] data, int subscriptionId) {
|
2018-08-09 10:15:43 -04:00
|
|
|
super(context, JobParameters.newBuilder().create());
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2016-02-05 16:10:33 -08:00
|
|
|
this.data = data;
|
|
|
|
this.subscriptionId = subscriptionId;
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-09 10:15:43 -04:00
|
|
|
protected void initialize(@NonNull SafeData data) {
|
|
|
|
try {
|
|
|
|
this.data = Base64.decode(data.getString(KEY_DATA));
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
subscriptionId = data.getInt(KEY_SUBSCRIPTION_ID);
|
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
@Override
|
|
|
|
protected @NonNull Data serialize(@NonNull Data.Builder dataBuilder) {
|
|
|
|
return dataBuilder.putString(KEY_DATA, Base64.encodeBytes(data))
|
|
|
|
.putInt(KEY_SUBSCRIPTION_ID, subscriptionId)
|
|
|
|
.build();
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun() {
|
|
|
|
if (data == null) {
|
|
|
|
Log.w(TAG, "Received NULL pdu, ignoring...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-31 09:12:22 -07:00
|
|
|
PduParser parser = new PduParser(data);
|
|
|
|
GenericPdu pdu = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
pdu = parser.parse();
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-06-09 07:37:20 -07:00
|
|
|
if (isNotification(pdu) && !isBlocked(pdu)) {
|
2014-11-03 15:16:04 -08:00
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
2016-02-05 16:10:33 -08:00
|
|
|
Pair<Long, Long> messageAndThreadId = database.insertMessageInbox((NotificationInd)pdu, subscriptionId);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2018-08-02 09:25:33 -04:00
|
|
|
Log.i(TAG, "Inserted received MMS notification...");
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getJobManager()
|
|
|
|
.add(new MmsDownloadJob(context,
|
|
|
|
messageAndThreadId.first,
|
|
|
|
messageAndThreadId.second,
|
|
|
|
true));
|
2015-06-09 07:37:20 -07:00
|
|
|
} else if (isNotification(pdu)) {
|
|
|
|
Log.w(TAG, "*** Received blocked MMS, ignoring...");
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-11 21:11:57 -08:00
|
|
|
public boolean onShouldRetry(Exception exception) {
|
2014-11-03 15:16:04 -08:00
|
|
|
return false;
|
|
|
|
}
|
2015-06-09 07:37:20 -07:00
|
|
|
|
|
|
|
private boolean isBlocked(GenericPdu pdu) {
|
|
|
|
if (pdu.getFrom() != null && pdu.getFrom().getTextString() != null) {
|
2017-08-21 18:32:38 -07:00
|
|
|
Recipient recipients = Recipient.from(context, Address.fromExternal(context, Util.toIsoString(pdu.getFrom().getTextString())), false);
|
2015-06-09 07:37:20 -07:00
|
|
|
return recipients.isBlocked();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isNotification(GenericPdu pdu) {
|
|
|
|
return pdu != null && pdu.getMessageType() == PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND;
|
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|