diff options
author | steckbrief <steckbrief@chefmail.de> | 2015-11-03 21:09:36 +0100 |
---|---|---|
committer | steckbrief <steckbrief@chefmail.de> | 2015-11-03 21:09:36 +0100 |
commit | b22d863c362bb6492240700c0f69f1a5d926f46b (patch) | |
tree | acf185af5312a97f18819efd3927c46b12a797d1 /src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java | |
parent | 11659eadb02b382804845d4e01a8536e02698468 (diff) |
Implements FS#26: Introduction of dialog to choose whether to resize a picture or not
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java')
-rw-r--r-- | src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java | 118 |
1 files changed, 88 insertions, 30 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java b/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java index 25d6a389..62ed2781 100644 --- a/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java +++ b/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java @@ -1,13 +1,16 @@ package de.thedevstack.conversationsplus.services; import android.annotation.SuppressLint; +import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.database.ContentObserver; +import android.database.Cursor; import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; @@ -20,6 +23,7 @@ import android.os.PowerManager; import android.os.PowerManager.WakeLock; import android.os.SystemClock; import android.provider.ContactsContract; +import android.provider.MediaStore; import android.util.Log; import android.util.LruCache; @@ -32,6 +36,10 @@ import net.java.otr4j.session.SessionStatus; import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpServiceConnection; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; import java.math.BigInteger; import java.security.SecureRandom; import java.util.ArrayList; @@ -49,6 +57,10 @@ import java.util.concurrent.CopyOnWriteArrayList; import de.duenndns.ssl.MemorizingTrustManager; import de.thedevstack.conversationsplus.ConversationsPlusApplication; import de.thedevstack.conversationsplus.ConversationsPlusPreferences; +import de.thedevstack.conversationsplus.enums.UserDecision; +import de.thedevstack.conversationsplus.ui.dialogs.UserDecisionDialog; +import de.thedevstack.conversationsplus.ui.listeners.ResizePictureUserDecisionListener; +import de.thedevstack.conversationsplus.utils.FileHelper; import de.tzur.conversations.Settings; import de.thedevstack.conversationsplus.Config; import de.thedevstack.conversationsplus.R; @@ -122,7 +134,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } }; - private final SerialSingleThreadExecutor mFileAddingExecutor = new SerialSingleThreadExecutor(); + private final SerialSingleThreadExecutor mFileAddingExecutor = new SerialSingleThreadExecutor(); private final SerialSingleThreadExecutor mDatabaseExecutor = new SerialSingleThreadExecutor(); private final IBinder mBinder = new XmppConnectionBinder(); @@ -401,38 +413,84 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } } - public void attachImageToConversation(final Conversation conversation, - final Uri uri, final UiCallback<Message> callback) { - final Message message; + public void attachImageToConversationWithoutResizing(final Conversation conversation, final Uri uri, final UiCallback<Message> callback) { + final Message message; final boolean forceEncryption = ConversationsPlusPreferences.forceEncryption(); - if (conversation.getNextEncryption(forceEncryption) == Message.ENCRYPTION_PGP) { - message = new Message(conversation, "", - Message.ENCRYPTION_DECRYPTED); - } else { - message = new Message(conversation, "", - conversation.getNextEncryption(forceEncryption)); - } - message.setCounterpart(conversation.getNextCounterpart()); - message.setType(Message.TYPE_IMAGE); - mFileAddingExecutor.execute(new Runnable() { + if (conversation.getNextEncryption(forceEncryption) == Message.ENCRYPTION_PGP) { + message = new Message(conversation, "", + Message.ENCRYPTION_DECRYPTED); + } else { + message = new Message(conversation, "", + conversation.getNextEncryption(forceEncryption)); + } + message.setCounterpart(conversation.getNextCounterpart()); + message.setType(Message.TYPE_IMAGE); + mFileAddingExecutor.execute(new Runnable() { + @Override + public void run() { + InputStream is = null; + try { + is = ConversationsPlusApplication.getInstance().getContentResolver().openInputStream(uri); + long imageSize = is.available(); + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inJustDecodeBounds = true; + BitmapFactory.decodeStream(is, null, options); + int imageHeight = options.outHeight; + int imageWidth = options.outWidth; + message.setRelativeFilePath(FileHelper.getRealPathFromUri(uri)); + message.setBody(Long.toString(imageSize) + '|' + imageWidth + '|' + imageHeight); + callback.success(message); + } catch (FileNotFoundException e) { + Log.e("pictureresize", "File not found to send not resized. " + e.getMessage()); + callback.error(R.string.error_file_not_found, message); + } catch (IOException e) { + Log.e("pictureresize", "Error while sending not resized picture. " + e.getMessage()); + callback.error(R.string.error_io_exception, message); + } finally { + if (null != is) { + try { + is.close(); + } catch (IOException e) { + Log.w("pictureresize", "Error while closing stream for sending not resized picture. " + e.getMessage()); + } + } + } + } + }); + } - @Override - public void run() { - try { - getFileBackend().copyImageToPrivateStorage(message, uri); - if (conversation.getNextEncryption(forceEncryption) == Message.ENCRYPTION_PGP) { - getPgpEngine().encrypt(message, callback); - } else { - callback.success(message); - } - } catch (final FileBackend.FileCopyException e) { - callback.error(e.getResId(), message); - } - } - }); - } + public void attachImageToConversation(final Conversation conversation, + final Uri uri, final UiCallback<Message> callback) { + final Message message; + final boolean forceEncryption = ConversationsPlusPreferences.forceEncryption(); + if (conversation.getNextEncryption(forceEncryption) == Message.ENCRYPTION_PGP) { + message = new Message(conversation, "", + Message.ENCRYPTION_DECRYPTED); + } else { + message = new Message(conversation, "", + conversation.getNextEncryption(forceEncryption)); + } + message.setCounterpart(conversation.getNextCounterpart()); + message.setType(Message.TYPE_IMAGE); + mFileAddingExecutor.execute(new Runnable() { + + @Override + public void run() { + try { + getFileBackend().copyImageToPrivateStorage(message, uri); + if (conversation.getNextEncryption(forceEncryption) == Message.ENCRYPTION_PGP) { + getPgpEngine().encrypt(message, callback); + } else { + callback.success(message); + } + } catch (final FileBackend.FileCopyException e) { + callback.error(e.getResId(), message); + } + } + }); + } - public Conversation find(Bookmark bookmark) { + public Conversation find(Bookmark bookmark) { return find(bookmark.getAccount(), bookmark.getJid()); } |