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 { private final Account account; private final long id; public GetTask(Account account, long id) { this.account = account; this.id = id; } @Override protected Tweet doInBackground() throws TwitterException { return Tweet.fromTwitter(account.getTwitter().tweets().showStatus(id), account.getUserId()); } } public static class CreateTask extends BackgroundTask { private static final int MEDIA_SIZE_LIMIT = 5 * 1024 * 1024; private final Account account; private final StatusUpdate update; private final String mediaPath; private String tempFilePath; private boolean resizeFlag; public CreateTask(Account account, StatusUpdate update, String mediaPath, boolean resize) { this.account = account; this.update = update; this.mediaPath = mediaPath; resizeFlag = resize; } 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 { try { if (fos != null) fos.close(); } catch (IOException e) { e.printStackTrace(); Logger.error(e); } } } return file; } @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 { private final Account account; private final long statusID; public DestroyTask(Account account, long statusID) { this.account = account; this.statusID = statusID; } @Override protected Tweet doInBackground() throws TwitterException { Tweet t = Tweet.fromTwitter(account.getTwitter().tweets().destroyStatus(statusID), account.getUserId()); // FIXME: Tweet.remove(t.getId()); return t; } } public static class GetTalkTask extends BackgroundTask, Tweet> { private final Account account; private final long statusId; public GetTalkTask(Account account, long statusId) { this.account = account; this.statusId = statusId; } @Override protected List doInBackground() { ArrayList 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; } } }