fixed crash, restructured tweet media upload

Signed-off-by: nuclearfog <hatespirit666@gmail.com>
This commit is contained in:
nuclearfog 2021-05-11 00:50:56 +02:00
parent 2a44c1eca2
commit d694ee40bb
No known key found for this signature in database
GPG Key ID: AA0271FBE406DB98
9 changed files with 57 additions and 45 deletions

View File

@ -90,27 +90,27 @@ public abstract class MediaActivity extends AppCompatActivity implements Locatio
/** /**
* request code to pick an image * request code to pick an image
*/ */
protected static final int REQUEST_IMAGE = 0x0AA0383C; protected static final int REQUEST_IMAGE = 0x383C;
/** /**
* request code to pick image or video * request code to pick image or video
*/ */
protected static final int REQUEST_IMG_VID = 0x191B6B1A; protected static final int REQUEST_IMG_VID = 0x6B1A;
/** /**
* request code to pick an image for a profile picture * request code to pick an image for a profile picture
*/ */
protected static final int REQUEST_PROFILE = 0x5E03D636; protected static final int REQUEST_PROFILE = 0xD636;
/** /**
* request code to pick an image for a profile banner * request code to pick an image for a profile banner
*/ */
protected static final int REQUEST_BANNER = 0x4349E7E3; protected static final int REQUEST_BANNER = 0xE7E3;
/** /**
* request code to store image into storage * request code to store image into storage
*/ */
protected static final int REQUEST_STORE_IMG = 0x1B0558D3; protected static final int REQUEST_STORE_IMG = 0x58D3;
@Nullable @Nullable
private ImageSaver imageTask; private ImageSaver imageTask;
@ -164,7 +164,10 @@ public abstract class MediaActivity extends AppCompatActivity implements Locatio
int index = cursor.getColumnIndex(GET_MEDIA[0]); int index = cursor.getColumnIndex(GET_MEDIA[0]);
if (index >= 0) { if (index >= 0) {
String path = cursor.getString(index); String path = cursor.getString(index);
onMediaFetched(reqCode, path); if (path != null) {
onMediaFetched(reqCode, path);
// todo add error handling if no media returned
}
} }
} }
cursor.close(); cursor.close();
@ -360,5 +363,5 @@ public abstract class MediaActivity extends AppCompatActivity implements Locatio
* @param resultType type of media call * @param resultType type of media call
* @param path local path to the media file * @param path local path to the media file
*/ */
protected abstract void onMediaFetched(int resultType, String path); protected abstract void onMediaFetched(int resultType, @NonNull String path);
} }

View File

@ -24,6 +24,7 @@ import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import android.widget.VideoView; import android.widget.VideoView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -325,7 +326,7 @@ public class MediaViewer extends MediaActivity implements OnImageClickListener,
@Override @Override
protected void onMediaFetched(int resultType, String path) { protected void onMediaFetched(int resultType, @NonNull String path) {
} }

View File

@ -11,6 +11,7 @@ import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
@ -112,7 +113,7 @@ public class MessageEditor extends MediaActivity implements OnClickListener, OnD
@Override @Override
protected void onMediaFetched(int resultType, String path) { protected void onMediaFetched(int resultType, @NonNull String path) {
if (resultType == REQUEST_IMAGE) { if (resultType == REQUEST_IMAGE) {
preview.setVisibility(VISIBLE); preview.setVisibility(VISIBLE);
media.setVisibility(GONE); media.setVisibility(GONE);

View File

@ -174,7 +174,7 @@ public class ProfileEditor extends MediaActivity implements OnClickListener, OnP
@Override @Override
protected void onMediaFetched(int resultType, String path) { protected void onMediaFetched(int resultType, @NonNull String path) {
// Add image as profile image // Add image as profile image
if (resultType == REQUEST_PROFILE) { if (resultType == REQUEST_PROFILE) {
Bitmap image = BitmapFactory.decodeFile(path); Bitmap image = BitmapFactory.decodeFile(path);

View File

@ -11,6 +11,7 @@ import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
@ -214,7 +215,7 @@ public class TweetEditor extends MediaActivity implements OnClickListener, OnPro
@Override @Override
protected void onMediaFetched(int resultType, String path) { protected void onMediaFetched(int resultType, @NonNull String path) {
String extension = path.substring(path.lastIndexOf('.') + 1).toLowerCase(); String extension = path.substring(path.lastIndexOf('.') + 1).toLowerCase();
switch (extension) { switch (extension) {
case "jpg": case "jpg":
@ -337,8 +338,8 @@ public class TweetEditor extends MediaActivity implements OnClickListener, OnPro
if (location != null) if (location != null)
tweet.addLocation(location); tweet.addLocation(location);
// send tweet // send tweet
uploaderAsync = new TweetUpdater(this); uploaderAsync = new TweetUpdater(this, tweet);
uploaderAsync.execute(tweet); uploaderAsync.execute();
if (!loadingCircle.isShowing()) { if (!loadingCircle.isShowing()) {
loadingCircle.show(); loadingCircle.show();
} }

View File

@ -37,14 +37,21 @@ public class MessageUpdater extends AsyncTask<String, Void, Boolean> {
@Override @Override
protected Boolean doInBackground(String[] param) { protected Boolean doInBackground(String[] param) {
try { try {
long mediaId = mTwitter.uploadImage(param[2]); // upload media first if any
if (!isCancelled()) long mediaId = -1;
String mediaPath = param[2];
if (mediaPath != null && !mediaPath.isEmpty()) {
mediaId = mTwitter.uploadImage(param[2]);
}
// upload message and media ID if defined
if (!isCancelled()) {
mTwitter.sendDirectMessage(param[0], param[1], mediaId); mTwitter.sendDirectMessage(param[0], param[1], mediaId);
return true; }
} catch (EngineException twException) { } catch (EngineException twException) {
this.twException = twException; this.twException = twException;
return false;
} }
return false; return true;
} }

View File

@ -15,44 +15,51 @@ import java.lang.ref.WeakReference;
* @author nuclearfog * @author nuclearfog
* @see TweetEditor * @see TweetEditor
*/ */
public class TweetUpdater extends AsyncTask<TweetHolder, Void, Boolean> { public class TweetUpdater extends AsyncTask<Void, Void, Boolean> {
private EngineException twException; private EngineException twException;
private final WeakReference<TweetEditor> callback;
private final TwitterEngine mTwitter; private final TwitterEngine mTwitter;
private final WeakReference<TweetEditor> callback;
private TweetHolder tweet;
/** /**
* initialize task * initialize task
* *
* @param callback Activity context * @param callback Activity context
*/ */
public TweetUpdater(TweetEditor callback) { public TweetUpdater(TweetEditor callback, TweetHolder tweet) {
super(); super();
this.callback = new WeakReference<>(callback);
mTwitter = TwitterEngine.getInstance(callback); mTwitter = TwitterEngine.getInstance(callback);
this.callback = new WeakReference<>(callback);
this.tweet = tweet;
} }
@Override @Override
protected Boolean doInBackground(TweetHolder[] param) { protected Boolean doInBackground(Void[] v) {
try { try {
long[] mediaIds; long[] mediaIds = {};
TweetHolder tweet = param[0]; String[] mediaLinks = tweet.getMediaPaths();
if (tweet.getMediaType() == TweetHolder.MediaType.IMAGE) { if (mediaLinks != null && mediaLinks.length > 0) {
String[] mediaLinks = tweet.getMediaPaths();
mediaIds = new long[mediaLinks.length]; mediaIds = new long[mediaLinks.length];
for (int i = 0; i < mediaLinks.length; i++) {
mediaIds[i] = mTwitter.uploadImage(mediaLinks[i]); // upload image
if (isCancelled()) { if (tweet.getMediaType() == TweetHolder.MediaType.IMAGE) {
break; for (int i = 0; i < mediaLinks.length; i++) {
mediaIds[i] = mTwitter.uploadImage(mediaLinks[i]);
if (isCancelled()) {
break;
}
} }
} }
} else if (tweet.getMediaType() == TweetHolder.MediaType.VIDEO) { // upload video file
mediaIds = new long[]{mTwitter.uploadVideo(tweet.getMediaPath())}; else if (tweet.getMediaType() == TweetHolder.MediaType.VIDEO) {
} else { mediaIds[0] = mTwitter.uploadVideo(mediaLinks[0]);
mediaIds = new long[0]; }
} }
// upload tweet
if (!isCancelled()) { if (!isCancelled()) {
mTwitter.uploadStatus(tweet, mediaIds); mTwitter.uploadStatus(tweet, mediaIds);
} }

View File

@ -5,6 +5,7 @@ import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.os.Build; import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import org.nuclearfog.twidda.backend.holder.ListHolder; import org.nuclearfog.twidda.backend.holder.ListHolder;
@ -1077,7 +1078,7 @@ public class TwitterEngine {
* @return media ID * @return media ID
* @throws EngineException if twitter service is unavailable or media not found * @throws EngineException if twitter service is unavailable or media not found
*/ */
public long uploadImage(String path) throws EngineException { public long uploadImage(@NonNull String path) throws EngineException {
try { try {
File file = new File(path); File file = new File(path);
UploadedMedia media = twitter.uploadMedia(file.getName(), new FileInputStream(file)); UploadedMedia media = twitter.uploadMedia(file.getName(), new FileInputStream(file));
@ -1096,7 +1097,7 @@ public class TwitterEngine {
* @return media ID * @return media ID
* @throws EngineException if twitter service is unavailable or media not found * @throws EngineException if twitter service is unavailable or media not found
*/ */
public long uploadVideo(String path) throws EngineException { public long uploadVideo(@NonNull String path) throws EngineException {
try { try {
File file = new File(path); File file = new File(path);
UploadedMedia media = twitter.uploadMediaChunked(file.getName(), new FileInputStream(file)); UploadedMedia media = twitter.uploadMediaChunked(file.getName(), new FileInputStream(file));

View File

@ -96,15 +96,6 @@ public class TweetHolder {
return mediaPaths; return mediaPaths;
} }
/**
* get first media path
*
* @return path string
*/
public String getMediaPath() {
return mediaPaths[0];
}
/** /**
* get longitude of the location * get longitude of the location
* *