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
*/
protected static final int REQUEST_IMAGE = 0x0AA0383C;
protected static final int REQUEST_IMAGE = 0x383C;
/**
* 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
*/
protected static final int REQUEST_PROFILE = 0x5E03D636;
protected static final int REQUEST_PROFILE = 0xD636;
/**
* 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
*/
protected static final int REQUEST_STORE_IMG = 0x1B0558D3;
protected static final int REQUEST_STORE_IMG = 0x58D3;
@Nullable
private ImageSaver imageTask;
@ -164,7 +164,10 @@ public abstract class MediaActivity extends AppCompatActivity implements Locatio
int index = cursor.getColumnIndex(GET_MEDIA[0]);
if (index >= 0) {
String path = cursor.getString(index);
if (path != null) {
onMediaFetched(reqCode, path);
// todo add error handling if no media returned
}
}
}
cursor.close();
@ -360,5 +363,5 @@ public abstract class MediaActivity extends AppCompatActivity implements Locatio
* @param resultType type of media call
* @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.VideoView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
@ -325,7 +326,7 @@ public class MediaViewer extends MediaActivity implements OnImageClickListener,
@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.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
@ -112,7 +113,7 @@ public class MessageEditor extends MediaActivity implements OnClickListener, OnD
@Override
protected void onMediaFetched(int resultType, String path) {
protected void onMediaFetched(int resultType, @NonNull String path) {
if (resultType == REQUEST_IMAGE) {
preview.setVisibility(VISIBLE);
media.setVisibility(GONE);

View File

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

View File

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

View File

@ -37,15 +37,22 @@ public class MessageUpdater extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String[] param) {
try {
long mediaId = mTwitter.uploadImage(param[2]);
if (!isCancelled())
// upload media first if any
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);
return true;
}
} catch (EngineException twException) {
this.twException = twException;
}
return false;
}
return true;
}
@Override

View File

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

View File

@ -5,6 +5,7 @@ import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.nuclearfog.twidda.backend.holder.ListHolder;
@ -1077,7 +1078,7 @@ public class TwitterEngine {
* @return media ID
* @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 {
File file = new File(path);
UploadedMedia media = twitter.uploadMedia(file.getName(), new FileInputStream(file));
@ -1096,7 +1097,7 @@ public class TwitterEngine {
* @return media ID
* @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 {
File file = new File(path);
UploadedMedia media = twitter.uploadMediaChunked(file.getName(), new FileInputStream(file));

View File

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