decoupled media upload from tweet/message upload. Added webp support
Signed-off-by: nuclearfog <hatespirit666@gmail.com>
This commit is contained in:
parent
5193e52184
commit
2197d7273f
|
@ -221,6 +221,7 @@ public class TweetEditor extends MediaActivity implements OnClickListener, Dialo
|
||||||
switch (extension) {
|
switch (extension) {
|
||||||
case "jpg":
|
case "jpg":
|
||||||
case "jpeg":
|
case "jpeg":
|
||||||
|
case "webp":
|
||||||
case "png":
|
case "png":
|
||||||
if (selectedFormat == MediaType.NONE)
|
if (selectedFormat == MediaType.NONE)
|
||||||
selectedFormat = MediaType.IMAGE;
|
selectedFormat = MediaType.IMAGE;
|
||||||
|
|
|
@ -37,7 +37,9 @@ public class MessageUpdater extends AsyncTask<String, Void, Boolean> {
|
||||||
@Override
|
@Override
|
||||||
protected Boolean doInBackground(String[] param) {
|
protected Boolean doInBackground(String[] param) {
|
||||||
try {
|
try {
|
||||||
mTwitter.sendMessage(param[0], param[1], param[2]);
|
long mediaId = mTwitter.uploadImage(param[2]);
|
||||||
|
if (!isCancelled())
|
||||||
|
mTwitter.sendDirectMessage(param[0], param[1], mediaId);
|
||||||
return true;
|
return true;
|
||||||
} catch (EngineException twException) {
|
} catch (EngineException twException) {
|
||||||
this.twException = twException;
|
this.twException = twException;
|
||||||
|
|
|
@ -37,8 +37,25 @@ public class TweetUpdater extends AsyncTask<TweetHolder, Void, Boolean> {
|
||||||
@Override
|
@Override
|
||||||
protected Boolean doInBackground(TweetHolder[] param) {
|
protected Boolean doInBackground(TweetHolder[] param) {
|
||||||
try {
|
try {
|
||||||
|
long[] mediaIds;
|
||||||
TweetHolder tweet = param[0];
|
TweetHolder tweet = param[0];
|
||||||
mTwitter.uploadStatus(tweet);
|
if (tweet.getMediaType() == TweetHolder.MediaType.IMAGE) {
|
||||||
|
String[] mediaLinks = tweet.getMediaPaths();
|
||||||
|
mediaIds = new long[mediaLinks.length];
|
||||||
|
for (int i = 0; i < mediaLinks.length; i++) {
|
||||||
|
mediaIds[i] = mTwitter.uploadImage(mediaLinks[i]);
|
||||||
|
if (isCancelled()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (tweet.getMediaType() == TweetHolder.MediaType.VIDEO) {
|
||||||
|
mediaIds = new long[]{mTwitter.uploadVideo(tweet.getMediaPath())};
|
||||||
|
} else {
|
||||||
|
mediaIds = new long[0];
|
||||||
|
}
|
||||||
|
if (!isCancelled()) {
|
||||||
|
mTwitter.uploadStatus(tweet, mediaIds);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
} catch (EngineException twException) {
|
} catch (EngineException twException) {
|
||||||
this.twException = twException;
|
this.twException = twException;
|
||||||
|
|
|
@ -126,7 +126,6 @@ public class TwitterEngine {
|
||||||
ProxySetup.setConnection(settings);
|
ProxySetup.setConnection(settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get singleton instance
|
* get singleton instance
|
||||||
*
|
*
|
||||||
|
@ -146,7 +145,6 @@ public class TwitterEngine {
|
||||||
return mTwitter;
|
return mTwitter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* reset Twitter state
|
* reset Twitter state
|
||||||
*/
|
*/
|
||||||
|
@ -156,7 +154,6 @@ public class TwitterEngine {
|
||||||
mTwitter.aToken = null; //
|
mTwitter.aToken = null; //
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request Registration Website
|
* Request Registration Website
|
||||||
*
|
*
|
||||||
|
@ -173,7 +170,6 @@ public class TwitterEngine {
|
||||||
return reqToken.getAuthenticationURL();
|
return reqToken.getAuthenticationURL();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Access-Token, store and initialize Twitter
|
* Get Access-Token, store and initialize Twitter
|
||||||
*
|
*
|
||||||
|
@ -197,7 +193,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Home Timeline
|
* Get Home Timeline
|
||||||
*
|
*
|
||||||
|
@ -221,7 +216,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Mention Tweets
|
* Get Mention Tweets
|
||||||
*
|
*
|
||||||
|
@ -245,7 +239,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Tweet search result
|
* Get Tweet search result
|
||||||
*
|
*
|
||||||
|
@ -273,7 +266,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Trending Hashtags
|
* Get Trending Hashtags
|
||||||
*
|
*
|
||||||
|
@ -294,7 +286,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get available locations
|
* get available locations
|
||||||
*
|
*
|
||||||
|
@ -313,7 +304,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get User search result
|
* Get User search result
|
||||||
*
|
*
|
||||||
|
@ -339,7 +329,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get User Tweets
|
* Get User Tweets
|
||||||
*
|
*
|
||||||
|
@ -363,7 +352,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get User Tweets
|
* Get User Tweets
|
||||||
*
|
*
|
||||||
|
@ -387,7 +375,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get User Favs
|
* Get User Favs
|
||||||
*
|
*
|
||||||
|
@ -411,7 +398,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get User Favs
|
* Get User Favs
|
||||||
*
|
*
|
||||||
|
@ -436,7 +422,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get User
|
* Get User
|
||||||
*
|
*
|
||||||
|
@ -467,7 +452,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Efficient Access of Connection Information
|
* Efficient Access of Connection Information
|
||||||
*
|
*
|
||||||
|
@ -498,7 +482,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unfollow Twitter user
|
* Unfollow Twitter user
|
||||||
*
|
*
|
||||||
|
@ -514,7 +497,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Block Twitter user
|
* Block Twitter user
|
||||||
*
|
*
|
||||||
|
@ -530,7 +512,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unblock Twitter user
|
* Unblock Twitter user
|
||||||
*
|
*
|
||||||
|
@ -546,7 +527,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mute Twitter user
|
* Mute Twitter user
|
||||||
*
|
*
|
||||||
|
@ -562,9 +542,8 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unmute Twitter user
|
* Un-mute Twitter user
|
||||||
*
|
*
|
||||||
* @param UserID User ID
|
* @param UserID User ID
|
||||||
* @return Twitter User
|
* @return Twitter User
|
||||||
|
@ -578,7 +557,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get Following User List
|
* get Following User List
|
||||||
*
|
*
|
||||||
|
@ -603,7 +581,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get Follower
|
* get Follower
|
||||||
*
|
*
|
||||||
|
@ -628,34 +605,27 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* send tweet
|
* send tweet
|
||||||
*
|
*
|
||||||
* @param tweet Tweet holder
|
* @param tweet Tweet holder
|
||||||
* @throws EngineException if twitter service is unavailable or media was not found
|
* @throws EngineException if twitter service is unavailable
|
||||||
*/
|
*/
|
||||||
public void uploadStatus(TweetHolder tweet) throws EngineException {
|
public void uploadStatus(TweetHolder tweet, long[] mediaIds) throws EngineException {
|
||||||
try {
|
try {
|
||||||
StatusUpdate mStatus = new StatusUpdate(tweet.getText());
|
StatusUpdate mStatus = new StatusUpdate(tweet.getText());
|
||||||
if (tweet.isReply())
|
if (tweet.isReply())
|
||||||
mStatus.setInReplyToStatusId(tweet.getReplyId());
|
mStatus.setInReplyToStatusId(tweet.getReplyId());
|
||||||
if (tweet.hasLocation())
|
if (tweet.hasLocation())
|
||||||
mStatus.setLocation(new GeoLocation(tweet.getLatitude(), tweet.getLongitude()));
|
mStatus.setLocation(new GeoLocation(tweet.getLatitude(), tweet.getLongitude()));
|
||||||
if (tweet.getMediaType() == TweetHolder.MediaType.IMAGE) {
|
if (mediaIds.length > 0)
|
||||||
long[] ids = uploadImages(tweet.getMediaPaths());
|
mStatus.setMediaIds(mediaIds);
|
||||||
mStatus.setMediaIds(ids);
|
|
||||||
} else if (tweet.getMediaType() == TweetHolder.MediaType.VIDEO) {
|
|
||||||
long id = uploadVideo(tweet.getMediaPath());
|
|
||||||
mStatus.setMediaIds(id);
|
|
||||||
}
|
|
||||||
twitter.updateStatus(mStatus);
|
twitter.updateStatus(mStatus);
|
||||||
} catch (Exception err) {
|
} catch (Exception err) {
|
||||||
throw new EngineException(err);
|
throw new EngineException(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Tweet
|
* Get Tweet
|
||||||
*
|
*
|
||||||
|
@ -672,7 +642,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get replies to a specific tweet
|
* Get replies to a specific tweet
|
||||||
*
|
*
|
||||||
|
@ -858,16 +827,15 @@ public class TwitterEngine {
|
||||||
* send direct message to an user
|
* send direct message to an user
|
||||||
*
|
*
|
||||||
* @param username screen name of the user
|
* @param username screen name of the user
|
||||||
* @param message message text
|
* @param message message text
|
||||||
* @param mediaPath path to the image or null
|
* @param mediaId media ID referenced by Twitter
|
||||||
* @throws EngineException if access is unavailable
|
* @throws EngineException if access is unavailable
|
||||||
*/
|
*/
|
||||||
public void sendMessage(String username, String message, String mediaPath) throws EngineException {
|
public void sendDirectMessage(String username, String message, long mediaId) throws EngineException {
|
||||||
try {
|
try {
|
||||||
if (mediaPath != null && !mediaPath.isEmpty()) {
|
if (mediaId > 0) {
|
||||||
long[] mediaId = uploadImages(new String[]{mediaPath});
|
|
||||||
long userId = twitter.showUser(username).getId();
|
long userId = twitter.showUser(username).getId();
|
||||||
twitter.sendDirectMessage(userId, message, mediaId[0]);
|
twitter.sendDirectMessage(userId, message, mediaId);
|
||||||
} else {
|
} else {
|
||||||
twitter.sendDirectMessage(username, message);
|
twitter.sendDirectMessage(username, message);
|
||||||
}
|
}
|
||||||
|
@ -1061,7 +1029,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get tweets of a lists
|
* get tweets of a lists
|
||||||
*
|
*
|
||||||
|
@ -1085,7 +1052,6 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* download image from Twitter
|
* download image from Twitter
|
||||||
*
|
*
|
||||||
|
@ -1104,6 +1070,44 @@ public class TwitterEngine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* upload image to twitter and return unique media ID
|
||||||
|
*
|
||||||
|
* @param path path to the media file
|
||||||
|
* @return media ID
|
||||||
|
* @throws EngineException if twitter service is unavailable or media not found
|
||||||
|
*/
|
||||||
|
public long uploadImage(String path) throws EngineException {
|
||||||
|
try {
|
||||||
|
File file = new File(path);
|
||||||
|
UploadedMedia media = twitter.uploadMedia(file.getName(), new FileInputStream(file));
|
||||||
|
return media.getMediaId();
|
||||||
|
} catch (FileNotFoundException err) {
|
||||||
|
throw new EngineException(EngineException.InternalErrorType.FILENOTFOUND);
|
||||||
|
} catch (Exception err) {
|
||||||
|
throw new EngineException(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upload video to twitter and return unique media ID
|
||||||
|
*
|
||||||
|
* @param path path to the media file
|
||||||
|
* @return media ID
|
||||||
|
* @throws EngineException if twitter service is unavailable or media not found
|
||||||
|
*/
|
||||||
|
public long uploadVideo(String path) throws EngineException {
|
||||||
|
try {
|
||||||
|
File file = new File(path);
|
||||||
|
UploadedMedia media = twitter.uploadMediaChunked(file.getName(), new FileInputStream(file));
|
||||||
|
return media.getMediaId();
|
||||||
|
} catch (FileNotFoundException err) {
|
||||||
|
throw new EngineException(EngineException.InternalErrorType.FILENOTFOUND);
|
||||||
|
} catch (Exception err) {
|
||||||
|
throw new EngineException(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* creates an user list
|
* creates an user list
|
||||||
*
|
*
|
||||||
|
@ -1169,7 +1173,6 @@ public class TwitterEngine {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* convert #twitter4j.Status to Tweet List
|
* convert #twitter4j.Status to Tweet List
|
||||||
*
|
*
|
||||||
|
@ -1183,9 +1186,8 @@ public class TwitterEngine {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param dm Twitter4J directmessage
|
* @param dm Twitter4J direct message
|
||||||
* @return dm item
|
* @return dm item
|
||||||
* @throws EngineException if Access is unavailable
|
* @throws EngineException if Access is unavailable
|
||||||
*/
|
*/
|
||||||
|
@ -1202,49 +1204,4 @@ public class TwitterEngine {
|
||||||
throw new EngineException(err);
|
throw new EngineException(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Upload image to twitter and return unique media IDs
|
|
||||||
*
|
|
||||||
* @param paths Image Paths
|
|
||||||
* @return Media ID array
|
|
||||||
* @throws EngineException if twitter service is unavailable or media not found
|
|
||||||
*/
|
|
||||||
private long[] uploadImages(String[] paths) throws EngineException {
|
|
||||||
try {
|
|
||||||
long[] ids = new long[paths.length];
|
|
||||||
int i = 0;
|
|
||||||
for (String path : paths) {
|
|
||||||
File file = new File(path);
|
|
||||||
UploadedMedia media = twitter.uploadMedia(file.getName(), new FileInputStream(file));
|
|
||||||
ids[i++] = media.getMediaId();
|
|
||||||
}
|
|
||||||
return ids;
|
|
||||||
} catch (FileNotFoundException err) {
|
|
||||||
throw new EngineException(EngineException.InternalErrorType.FILENOTFOUND);
|
|
||||||
} catch (Exception err) {
|
|
||||||
throw new EngineException(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Upload video or gif to twitter and return unique media ID
|
|
||||||
*
|
|
||||||
* @param path path of video or gif
|
|
||||||
* @return media ID
|
|
||||||
* @throws EngineException if twitter service is unavailable or media not found
|
|
||||||
*/
|
|
||||||
private long uploadVideo(String path) throws EngineException {
|
|
||||||
try {
|
|
||||||
File file = new File(path);
|
|
||||||
UploadedMedia media = twitter.uploadMediaChunked(file.getName(), new FileInputStream(file));
|
|
||||||
return media.getMediaId();
|
|
||||||
} catch (FileNotFoundException err) {
|
|
||||||
throw new EngineException(EngineException.InternalErrorType.FILENOTFOUND);
|
|
||||||
} catch (Exception err) {
|
|
||||||
throw new EngineException(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue