aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/twitter/task/Tweets.java
blob: 016f699cb0933db16402a88413cb4c36ee1559a5 (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
package net.lacolaco.smileessence.twitter.task;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.text.TextUtils;
import net.lacolaco.smileessence.data.Account;
import net.lacolaco.smileessence.entity.Tweet;
import net.lacolaco.smileessence.logging.Logger;
import net.lacolaco.smileessence.util.BackgroundTask;
import twitter4j.StatusUpdate;
import twitter4j.TwitterException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Tweets {
    public static class GetTask extends BackgroundTask<Tweet, Void> {

        // ------------------------------ FIELDS ------------------------------

        private final Account account;
        private final long id;

        // --------------------------- CONSTRUCTORS ---------------------------

        public GetTask(Account account, long id) {
            this.account = account;
            this.id = id;
        }

        // ------------------------ OVERRIDE METHODS ------------------------

        @Override
        protected Tweet doInBackground() throws TwitterException {
            return Tweet.fromTwitter(account.getTwitter().tweets().showStatus(id), account.getUserId());
        }
    }

    public static class CreateTask extends BackgroundTask<Tweet, Void> {

        // ------------------------------ FIELDS ------------------------------

        private static final int MEDIA_SIZE_LIMIT = 2 * 1024 * 1024;
        private final Account account;
        private final StatusUpdate update;
        private final String mediaPath;
        private String tempFilePath;
        private boolean resizeFlag;

        // --------------------------- CONSTRUCTORS ---------------------------

        public CreateTask(Account account, StatusUpdate update, String mediaPath, boolean resize) {
            this.account = account;
            this.update = update;
            this.mediaPath = mediaPath;
            resizeFlag = resize;
        }

        // --------------------- GETTER / SETTER METHODS ---------------------

        private File getMediaFile() {
            File file = new File(mediaPath);
            if (file.length() >= MEDIA_SIZE_LIMIT && resizeFlag) {
                BitmapFactory.Options opt = new BitmapFactory.Options();
                opt.inJustDecodeBounds = true; //decoder is not return bitmap but set option
                BitmapFactory.decodeFile(mediaPath, opt);
                tempFilePath = Environment.getExternalStorageDirectory() + "/temp.jpg";
                File compressedFile = new File(tempFilePath);
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(compressedFile);
                    float ratio = (float) file.length() / (float) MEDIA_SIZE_LIMIT;
                    BitmapFactory.Options resizeOpt = new BitmapFactory.Options();
                    resizeOpt.inPurgeable = true;
                    resizeOpt.inSampleSize = (int) Math.ceil(ratio);
                    Bitmap bitmap = BitmapFactory.decodeFile(mediaPath, resizeOpt);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                    bitmap.recycle();
                    return compressedFile;
                } catch (Exception e) {
                    e.printStackTrace();
                    Logger.error(e);
                } finally {
                    if (fos != null) {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                            Logger.error(e);
                        }
                    }
                }
            }
            return file;
        }

        // ------------------------ OVERRIDE METHODS ------------------------

        @Override
        protected Tweet doInBackground() throws TwitterException {
            try {
                if (!TextUtils.isEmpty(mediaPath)) {
                    File mediaFile = getMediaFile();
                    if (mediaFile.exists()) {
                        update.setMedia(mediaFile);
                    }
                }
                return Tweet.fromTwitter(account.getTwitter().tweets().updateStatus(update), account.getUserId());
            } finally {
                if (tempFilePath != null) {
                    new File(tempFilePath).delete();
                }
            }
        }
    }

    public static class DestroyTask extends BackgroundTask<Tweet, Void> {

        // ------------------------------ FIELDS ------------------------------

        private final Account account;
        private final long statusID;

        // --------------------------- CONSTRUCTORS ---------------------------

        public DestroyTask(Account account, long statusID) {
            this.account = account;
            this.statusID = statusID;
        }

        // ------------------------ OVERRIDE METHODS ------------------------

        @Override
        protected Tweet doInBackground() throws TwitterException {
            Tweet t = Tweet.fromTwitter(account.getTwitter().tweets().destroyStatus(statusID), account.getUserId());
            Tweet.remove(t.getId());
            return t;
        }
    }

    public static class GetTalkTask extends BackgroundTask<List<Tweet>, Tweet> {
        private final Account account;
        private final long statusId;

        public GetTalkTask(Account account, long statusId) {
            this.account = account;
            this.statusId = statusId;
        }

        @Override
        protected List<Tweet> doInBackground() {
            ArrayList<Tweet> list = new ArrayList<>();
            long id = statusId;
            while (id != -1) {
                Tweet tweet = Tweet.fetch(id);
                if (tweet == null) {
                    try {
                        tweet = Tweet.fromTwitter(account.getTwitter().showStatus(id), account.getUserId());
                    } catch (TwitterException ignored) {
                    }
                }

                if (tweet == null) {
                    break;
                } else {
                    list.add(tweet);
                    progress(tweet);
                    id = tweet.getInReplyToStatusId();
                }
            }
            return list;
        }
    }
}