diff options
Diffstat (limited to 'app/src/main/java/net/lacolaco/smileessence/twitter/task/Tweets.java')
-rw-r--r-- | app/src/main/java/net/lacolaco/smileessence/twitter/task/Tweets.java | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/app/src/main/java/net/lacolaco/smileessence/twitter/task/Tweets.java b/app/src/main/java/net/lacolaco/smileessence/twitter/task/Tweets.java new file mode 100644 index 00000000..016f699c --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/twitter/task/Tweets.java @@ -0,0 +1,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; + } + } +} |