parent
a397f34ec4
commit
3c41f27298
@ -0,0 +1,165 @@
|
||||
package org.thoughtcrime.securesms.jobs;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetFileDescriptor;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.provider.ContactsContract;
|
||||
import android.util.Log;
|
||||
|
||||
import org.thoughtcrime.securesms.contacts.ContactAccessor;
|
||||
import org.thoughtcrime.securesms.contacts.ContactAccessor.ContactData;
|
||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
||||
import org.thoughtcrime.securesms.dependencies.TextSecureCommunicationModule.TextSecureMessageSenderFactory;
|
||||
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
import org.whispersystems.jobqueue.JobParameters;
|
||||
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
||||
import org.whispersystems.libaxolotl.util.guava.Optional;
|
||||
import org.whispersystems.textsecure.api.TextSecureMessageSender;
|
||||
import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException;
|
||||
import org.whispersystems.textsecure.api.messages.TextSecureAttachmentStream;
|
||||
import org.whispersystems.textsecure.api.messages.multidevice.DeviceContact;
|
||||
import org.whispersystems.textsecure.api.messages.multidevice.DeviceContactsOutputStream;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class MultiDeviceContactUpdateJob extends MasterSecretJob implements InjectableType {
|
||||
|
||||
private static final String TAG = MultiDeviceContactUpdateJob.class.getSimpleName();
|
||||
|
||||
@Inject transient TextSecureMessageSenderFactory messageSenderFactory;
|
||||
|
||||
public MultiDeviceContactUpdateJob(Context context) {
|
||||
super(context, JobParameters.newBuilder()
|
||||
.withRequirement(new NetworkRequirement(context))
|
||||
.withRequirement(new MasterSecretRequirement(context))
|
||||
.withGroupId(MultiDeviceContactUpdateJob.class.getSimpleName())
|
||||
.withPersistence()
|
||||
.create());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRun(MasterSecret masterSecret)
|
||||
throws IOException, UntrustedIdentityException, NetworkException
|
||||
{
|
||||
TextSecureMessageSender messageSender = messageSenderFactory.create(masterSecret);
|
||||
File contactDataFile = createTempFile("multidevice-contact-update");
|
||||
|
||||
try {
|
||||
DeviceContactsOutputStream out = new DeviceContactsOutputStream(new FileOutputStream(contactDataFile));
|
||||
Collection<ContactData> contacts = ContactAccessor.getInstance().getContactsWithPush(context);
|
||||
|
||||
for (ContactData contactData : contacts) {
|
||||
Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactData.id));
|
||||
String number = contactData.numbers.get(0).number;
|
||||
Optional<String> name = Optional.fromNullable(contactData.name);
|
||||
|
||||
out.write(new DeviceContact(number, name, getAvatar(contactUri)));
|
||||
}
|
||||
|
||||
out.close();
|
||||
sendUpdate(messageSender, contactDataFile);
|
||||
|
||||
} finally {
|
||||
if (contactDataFile != null) contactDataFile.delete();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onShouldRetryThrowable(Exception exception) {
|
||||
if (exception instanceof NetworkException) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdded() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCanceled() {
|
||||
|
||||
}
|
||||
|
||||
private void sendUpdate(TextSecureMessageSender messageSender, File contactsFile)
|
||||
throws IOException, UntrustedIdentityException, NetworkException
|
||||
{
|
||||
FileInputStream contactsFileStream = new FileInputStream(contactsFile);
|
||||
TextSecureAttachmentStream attachmentStream = new TextSecureAttachmentStream(contactsFileStream,
|
||||
"application/octet-stream",
|
||||
contactsFile.length());
|
||||
|
||||
try {
|
||||
messageSender.sendMultiDeviceContactsUpdate(attachmentStream);
|
||||
} catch (IOException ioe) {
|
||||
throw new NetworkException(ioe);
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<TextSecureAttachmentStream> getAvatar(Uri uri) throws IOException {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
|
||||
try {
|
||||
Uri displayPhotoUri = Uri.withAppendedPath(uri, ContactsContract.Contacts.Photo.DISPLAY_PHOTO);
|
||||
AssetFileDescriptor fd = context.getContentResolver().openAssetFileDescriptor(displayPhotoUri, "r");
|
||||
return Optional.of(new TextSecureAttachmentStream(fd.createInputStream(), "image/*", fd.getLength()));
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, e);
|
||||
}
|
||||
}
|
||||
|
||||
Uri photoUri = Uri.withAppendedPath(uri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
|
||||
|
||||
if (photoUri == null) {
|
||||
return Optional.absent();
|
||||
}
|
||||
|
||||
Cursor cursor = context.getContentResolver().query(photoUri,
|
||||
new String[] {
|
||||
ContactsContract.CommonDataKinds.Photo.PHOTO,
|
||||
ContactsContract.CommonDataKinds.Phone.MIMETYPE
|
||||
}, null, null, null);
|
||||
|
||||
try {
|
||||
if (cursor != null && cursor.moveToNext()) {
|
||||
byte[] data = cursor.getBlob(0);
|
||||
|
||||
if (data != null) {
|
||||
return Optional.of(new TextSecureAttachmentStream(new ByteArrayInputStream(data), "image/*", data.length));
|
||||
}
|
||||
}
|
||||
|
||||
return Optional.absent();
|
||||
} finally {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private File createTempFile(String prefix) throws IOException {
|
||||
File file = File.createTempFile(prefix, "tmp", context.getCacheDir());
|
||||
file.deleteOnExit();
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
private static class NetworkException extends Exception {
|
||||
|
||||
public NetworkException(Exception ioe) {
|
||||
super(ioe);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue