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
|
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
class Tweets {
class GetTask(private val account: Account, private val id: Long) : BackgroundTask<Tweet, Void>() {
override fun doInBackground(): Tweet {
return Tweet.fromTwitter(account.twitter.tweets().showStatus(id), account.userId)
}
}
class CreateTask(private val account: Account, private val update: StatusUpdate, private val mediaPath: String, private val resizeFlag: Boolean) : BackgroundTask<Tweet, Void>() {
private var tempFilePath: String? = null
private val mediaFile: File
get() {
val file = File(mediaPath)
if (file.length() >= MEDIA_SIZE_LIMIT && resizeFlag) {
val opt = BitmapFactory.Options()
opt.inJustDecodeBounds = true
BitmapFactory.decodeFile(mediaPath, opt)
tempFilePath = Environment.getExternalStorageDirectory().toString() + "/temp.jpg"
val compressedFile = File(tempFilePath)
var fos: FileOutputStream? = null
try {
fos = FileOutputStream(compressedFile)
val ratio = file.length().toFloat() / MEDIA_SIZE_LIMIT.toFloat()
val resizeOpt = BitmapFactory.Options()
resizeOpt.inPurgeable = true
resizeOpt.inSampleSize = Math.ceil(ratio.toDouble()).toInt()
val bitmap = BitmapFactory.decodeFile(mediaPath, resizeOpt)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos)
bitmap.recycle()
return compressedFile
} catch (e: Exception) {
e.printStackTrace()
Logger.error(e)
} finally {
try {
if (fos != null) fos.close()
} catch (e: IOException) {
e.printStackTrace()
Logger.error(e)
}
}
}
return file
}
override fun doInBackground(): Tweet {
return try {
if (!TextUtils.isEmpty(mediaPath)) {
val mediaFile = mediaFile
if (mediaFile.exists()) {
update.setMedia(mediaFile)
}
}
Tweet.fromTwitter(account.twitter.tweets().updateStatus(update), account.userId)
} finally {
if (tempFilePath != null) {
File(tempFilePath).delete()
}
}
}
companion object {
private val MEDIA_SIZE_LIMIT = 5 * 1024 * 1024
}
}
class DestroyTask(private val account: Account, private val statusID: Long) : BackgroundTask<Tweet, Void>() {
override fun doInBackground(): Tweet {
// FIXME: Tweet.remove(t.getId());
return Tweet.fromTwitter(account.twitter.tweets().destroyStatus(statusID), account.userId)
}
}
class GetTalkTask(private val account: Account, private val statusId: Long) : BackgroundTask<List<Tweet>, Tweet>() {
override fun doInBackground(): List<Tweet> {
val list = ArrayList<Tweet>()
var id = statusId
while (id != -1L) {
var tweet = Tweet.fetch(id)
if (tweet == null) {
try {
tweet = Tweet.fromTwitter(account.twitter.showStatus(id), account.userId)
} catch (ignored: TwitterException) {
}
}
if (tweet == null) {
break
} else {
list.add(tweet)
progress(tweet)
id = tweet.inReplyToStatusId
}
}
return list
}
}
}
|