aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/ui/listeners/ResizePictureUserDecisionListener.java
blob: 231f738c59439d54e058ea08a97e81ff34835f68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package de.thedevstack.conversationsplus.ui.listeners;

import android.app.PendingIntent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import de.thedevstack.android.logcat.Logging;
import de.thedevstack.conversationsplus.ConversationsPlusApplication;
import de.thedevstack.conversationsplus.ConversationsPlusPreferences;
import de.thedevstack.conversationsplus.enums.UserDecision;
import de.thedevstack.conversationsplus.exceptions.UiException;
import de.thedevstack.conversationsplus.utils.FileUtils;
import de.thedevstack.conversationsplus.utils.ImageUtil;
import de.thedevstack.conversationsplus.utils.MessageUtil;
import de.thedevstack.conversationsplus.R;
import de.thedevstack.conversationsplus.entities.Conversation;
import de.thedevstack.conversationsplus.entities.DownloadableFile;
import de.thedevstack.conversationsplus.entities.Message;
import de.thedevstack.conversationsplus.persistance.FileBackend;
import de.thedevstack.conversationsplus.services.XmppConnectionService;
import de.thedevstack.conversationsplus.ui.UiCallback;
import de.thedevstack.conversationsplus.ui.XmppActivity;
import de.thedevstack.conversationsplus.utils.StreamUtil;

/**
 * Created by tzur on 31.10.2015.
 */
public class ResizePictureUserDecisionListener implements UserDecisionListener {
    protected Uri uri;
    protected final Conversation conversation;
    protected final UiCallback<Message> callback;
    protected final XmppConnectionService xmppConnectionService;
    protected final Toast prepareFileToast;
    protected final XmppActivity activity;

    public ResizePictureUserDecisionListener(XmppActivity activity, Conversation conversation, XmppConnectionService xmppConnectionService) {
        this.xmppConnectionService = xmppConnectionService;
        this.conversation = conversation;
        this.activity = activity;
        this.prepareFileToast = Toast.makeText(ConversationsPlusApplication.getAppContext(), ConversationsPlusApplication.getInstance().getText(R.string.preparing_image), Toast.LENGTH_LONG);
        this.callback = new UiCallback<Message>() {

            @Override
            public void userInputRequried(PendingIntent pi, Message object) {
                hidePrepareFileToast();
            }

            @Override
            public void success(Message message) {
                ResizePictureUserDecisionListener.this.xmppConnectionService.sendMessage(message);
            }

            @Override
            public void error(int error, Message message) {
                hidePrepareFileToast();
                //TODO Find another way to display an error dialog
                ResizePictureUserDecisionListener.this.activity.displayErrorDialog(error);
            }

            protected void hidePrepareFileToast() {
                ResizePictureUserDecisionListener.this.activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ResizePictureUserDecisionListener.this.prepareFileToast.cancel();
                    }
                });
            }
        };
    }

    public ResizePictureUserDecisionListener(XmppActivity activity, Conversation conversation, Uri uri, XmppConnectionService xmppConnectionService) {
        this(activity, conversation, xmppConnectionService);
        this.uri = uri;
    }

    public void setUri(Uri uri) {
        this.uri = uri;
    }

    protected void showPrepareFileToast() {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                prepareFileToast.show();
            }
        });
    }

    @Override
    public void onYes() {
        this.showPrepareFileToast();
        final Message message;
        if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
            message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED);
        } else {
            message = new Message(conversation, "", conversation.getNextEncryption());
        }
        message.setCounterpart(conversation.getNextCounterpart());
        message.setType(Message.TYPE_IMAGE);
        ConversationsPlusApplication.executeFileAdding(new OnYesRunnable(message, uri));
    }

    @Override
    public void onNo() {
        this.showPrepareFileToast();
        final Message message;
        if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
            message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED);
        } else {
            message = new Message(conversation, "", conversation.getNextEncryption());
        }
        message.setCounterpart(conversation.getNextCounterpart());
        message.setType(Message.TYPE_IMAGE);
        ConversationsPlusApplication.executeFileAdding(new OnNoRunnable(message, uri));
    }

    @Override
    public void onRemember(UserDecision decision) {
        ConversationsPlusPreferences.applyResizePicture(decision);
    }

    private abstract class OnClickRunnable implements Runnable {

        protected final Message message;
        protected final Uri uri;

        public OnClickRunnable(Message message, Uri uri) {
            this.message = message;
            this.uri = uri;
        }
    }

    private class OnNoRunnable extends OnClickRunnable {

        public OnNoRunnable(Message message, Uri uri) {
            super(message, uri);
        }

        @Override
        public void run() {
            InputStream is = null;
            try {
                is = StreamUtil.openInputStreamFromContentResolver(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;
                String filePath = FileUtils.getPath(uri);
                MessageUtil.updateMessageWithImageDetails(message, filePath, imageSize, imageWidth, imageHeight);
                if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
                    xmppConnectionService.getPgpEngine().encrypt(message, callback);
                } else {
                    callback.success(message);
                }
            } catch (FileNotFoundException e) {
                Logging.e("picturesending", "File not found to send not resized. " + e.getMessage());
                callback.error(R.string.error_file_not_found, message);
            } catch (IOException e) {
                Logging.e("picturesending", "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) {
                        Logging.w("picturesending", "Error while closing stream for sending not resized picture. " + e.getMessage());
                    }
                }
            }
        }
    }

    private class OnYesRunnable extends OnClickRunnable {

        public OnYesRunnable(Message message, Uri uri) {
            super(message, uri);
        }

        @Override
        public void run() {
            try {
                Bitmap resizedAndRotatedImage = ImageUtil.resizeAndRotateImage(uri);
                DownloadableFile file = FileBackend.compressImageAndCopyToPrivateStorage(message, resizedAndRotatedImage);
                String filePath = file.getAbsolutePath();
                long imageSize = file.getSize();
                int imageWidth = resizedAndRotatedImage.getWidth();
                int imageHeight = resizedAndRotatedImage.getHeight();
                MessageUtil.updateMessageWithImageDetails(message, filePath, imageSize, imageWidth, imageHeight);
                if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
                    xmppConnectionService.getPgpEngine().encrypt(message, callback);
                } else {
                    callback.success(message);
                }
            } catch (final UiException e) {
                Logging.e("pictureresizesending", "Error while sending resized picture. " + e.getMessage());
                callback.error(e.getResId(), message);
            }
        }
    }
}