aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/twitter/task/TweetTask.java
blob: f665a558c3152347889942d5b06865b3b3fdd6d5 (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
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2012-2014 lacolaco.net
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

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.R;
import net.lacolaco.smileessence.entity.Account;
import net.lacolaco.smileessence.entity.Tweet;
import net.lacolaco.smileessence.logging.Logger;
import net.lacolaco.smileessence.notification.Notificator;
import net.lacolaco.smileessence.util.BackgroundTask;
import twitter4j.StatusUpdate;
import twitter4j.TwitterException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class TweetTask 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 TweetTask(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() {
        try {
            if (!TextUtils.isEmpty(mediaPath)) {
                File mediaFile = getMediaFile();
                if (mediaFile.exists()) {
                    update.setMedia(mediaFile);
                }
            }
            Tweet tweet = Tweet.fromTwitter(account.getTwitter().tweets().updateStatus(update), account.getUserId());
            if (tempFilePath != null) {
                new File(tempFilePath).delete();
            }
            if (tweet != null) {
                Notificator.getInstance().publish(R.string.notice_tweet_succeeded);
            } else {
                Notificator.getInstance().alert(R.string.notice_tweet_failed);
            }
            return tweet;
        } catch (TwitterException e) {
            e.printStackTrace();
            Logger.error(e.toString());
            return null;
        }
    }
}