mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-11-10 12:47:26 +01:00
Finish replacing AsyncTask with Executor in ParsePost.
This commit is contained in:
parent
260bd45433
commit
b01c9b9e2b
@ -3,9 +3,12 @@ package ml.docilealligator.infinityforreddit.activities;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
||||
@ -32,6 +35,8 @@ public class FetchRandomSubredditOrPostActivity extends BaseActivity {
|
||||
SharedPreferences mSharedPreferences;
|
||||
@Inject
|
||||
CustomThemeWrapper mCustomThemeWrapper;
|
||||
@Inject
|
||||
Executor mExecutor;
|
||||
private boolean isCanceled = false;
|
||||
|
||||
@Override
|
||||
@ -44,7 +49,7 @@ public class FetchRandomSubredditOrPostActivity extends BaseActivity {
|
||||
|
||||
int option = getIntent().getIntExtra(EXTRA_RANDOM_OPTION, RandomBottomSheetFragment.RANDOM_SUBREDDIT);
|
||||
|
||||
FetchPost.fetchRandomPost(mRetrofit, option == RandomBottomSheetFragment.RANDOM_NSFW_SUBREDDIT
|
||||
FetchPost.fetchRandomPost(mExecutor, new Handler(), mRetrofit, option == RandomBottomSheetFragment.RANDOM_NSFW_SUBREDDIT
|
||||
|| option == RandomBottomSheetFragment.RANDOM_NSFW_POST, new FetchPost.FetchRandomPostListener() {
|
||||
@Override
|
||||
public void fetchRandomPostSuccess(String postId, String subredditName) {
|
||||
|
@ -11,6 +11,7 @@ import android.media.AudioManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.text.Html;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
@ -51,6 +52,7 @@ import com.thefuntasty.hauler.HaulerView;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
@ -152,6 +154,9 @@ public class ViewVideoActivity extends AppCompatActivity {
|
||||
@Named("default")
|
||||
SharedPreferences mSharedPreferences;
|
||||
|
||||
@Inject
|
||||
Executor mExecutor;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@ -430,54 +435,55 @@ public class ViewVideoActivity extends AppCompatActivity {
|
||||
List<String> segments = redirectUri.getPathSegments();
|
||||
int commentsIndex = segments.lastIndexOf("comments");
|
||||
String postId = segments.get(commentsIndex + 1);
|
||||
FetchPost.fetchPost(retrofit, postId, null, new FetchPost.FetchPostListener() {
|
||||
@Override
|
||||
public void fetchPostSuccess(Post post) {
|
||||
if (post.isGfycat()) {
|
||||
videoType = VIDEO_TYPE_GFYCAT;
|
||||
String gfycatId = post.getGfycatId();
|
||||
if (gfycatId != null && gfycatId.contains("-")) {
|
||||
gfycatId = gfycatId.substring(0, gfycatId.indexOf('-'));
|
||||
}
|
||||
if (videoType == VIDEO_TYPE_GFYCAT) {
|
||||
videoFileName = "Gfycat-" + gfycatId + ".mp4";
|
||||
} else {
|
||||
videoFileName = "Redgifs-" + gfycatId + ".mp4";
|
||||
}
|
||||
loadGfycatOrRedgifsVideo(gfycatRetrofit, gfycatId, savedInstanceState, true);
|
||||
} else if (post.isRedgifs()) {
|
||||
videoType = VIDEO_TYPE_REDGIFS;
|
||||
String gfycatId = post.getGfycatId();
|
||||
if (gfycatId != null && gfycatId.contains("-")) {
|
||||
gfycatId = gfycatId.substring(0, gfycatId.indexOf('-'));
|
||||
}
|
||||
if (videoType == VIDEO_TYPE_GFYCAT) {
|
||||
videoFileName = "Gfycat-" + gfycatId + ".mp4";
|
||||
} else {
|
||||
videoFileName = "Redgifs-" + gfycatId + ".mp4";
|
||||
}
|
||||
loadGfycatOrRedgifsVideo(redgifsRetrofit, gfycatId, savedInstanceState, false);
|
||||
} else {
|
||||
progressBar.setVisibility(View.INVISIBLE);
|
||||
mVideoUri = Uri.parse(post.getVideoUrl());
|
||||
subredditName = post.getSubredditName();
|
||||
id = post.getId();
|
||||
videoDownloadUrl = post.getVideoDownloadUrl();
|
||||
FetchPost.fetchPost(mExecutor, new Handler(), retrofit, postId, null,
|
||||
new FetchPost.FetchPostListener() {
|
||||
@Override
|
||||
public void fetchPostSuccess(Post post) {
|
||||
if (post.isGfycat()) {
|
||||
videoType = VIDEO_TYPE_GFYCAT;
|
||||
String gfycatId = post.getGfycatId();
|
||||
if (gfycatId != null && gfycatId.contains("-")) {
|
||||
gfycatId = gfycatId.substring(0, gfycatId.indexOf('-'));
|
||||
}
|
||||
if (videoType == VIDEO_TYPE_GFYCAT) {
|
||||
videoFileName = "Gfycat-" + gfycatId + ".mp4";
|
||||
} else {
|
||||
videoFileName = "Redgifs-" + gfycatId + ".mp4";
|
||||
}
|
||||
loadGfycatOrRedgifsVideo(gfycatRetrofit, gfycatId, savedInstanceState, true);
|
||||
} else if (post.isRedgifs()) {
|
||||
videoType = VIDEO_TYPE_REDGIFS;
|
||||
String gfycatId = post.getGfycatId();
|
||||
if (gfycatId != null && gfycatId.contains("-")) {
|
||||
gfycatId = gfycatId.substring(0, gfycatId.indexOf('-'));
|
||||
}
|
||||
if (videoType == VIDEO_TYPE_GFYCAT) {
|
||||
videoFileName = "Gfycat-" + gfycatId + ".mp4";
|
||||
} else {
|
||||
videoFileName = "Redgifs-" + gfycatId + ".mp4";
|
||||
}
|
||||
loadGfycatOrRedgifsVideo(redgifsRetrofit, gfycatId, savedInstanceState, false);
|
||||
} else {
|
||||
progressBar.setVisibility(View.INVISIBLE);
|
||||
mVideoUri = Uri.parse(post.getVideoUrl());
|
||||
subredditName = post.getSubredditName();
|
||||
id = post.getId();
|
||||
videoDownloadUrl = post.getVideoDownloadUrl();
|
||||
|
||||
videoFileName = subredditName + "-" + id + ".mp4";
|
||||
// Produces DataSource instances through which media data is loaded.
|
||||
dataSourceFactory = new DefaultHttpDataSourceFactory(Util.getUserAgent(ViewVideoActivity.this, "Infinity"));
|
||||
// Prepare the player with the source.
|
||||
player.prepare(new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri));
|
||||
preparePlayer(savedInstanceState);
|
||||
}
|
||||
}
|
||||
videoFileName = subredditName + "-" + id + ".mp4";
|
||||
// Produces DataSource instances through which media data is loaded.
|
||||
dataSourceFactory = new DefaultHttpDataSourceFactory(Util.getUserAgent(ViewVideoActivity.this, "Infinity"));
|
||||
// Prepare the player with the source.
|
||||
player.prepare(new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri));
|
||||
preparePlayer(savedInstanceState);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fetchPostFailed() {
|
||||
Toast.makeText(ViewVideoActivity.this, R.string.error_fetching_v_redd_it_video_cannot_get_post, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
@Override
|
||||
public void fetchPostFailed() {
|
||||
Toast.makeText(ViewVideoActivity.this, R.string.error_fetching_v_redd_it_video_cannot_get_post, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
Toast.makeText(ViewVideoActivity.this, R.string.error_fetching_v_redd_it_video_cannot_get_post_id, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import android.graphics.Canvas;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.HapticFeedbackConstants;
|
||||
import android.view.LayoutInflater;
|
||||
@ -1103,7 +1104,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
|
||||
mSwipeRefreshLayout.setRefreshing(false);
|
||||
|
||||
if (response.isSuccessful()) {
|
||||
ParsePost.parsePost(response.body(), new ParsePost.ParsePostListener() {
|
||||
ParsePost.parsePost(mExecutor, new Handler(), response.body(), new ParsePost.ParsePostListener() {
|
||||
@Override
|
||||
public void onParsePostSuccess(Post post) {
|
||||
mPost = post;
|
||||
@ -1420,7 +1421,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
|
||||
} else {
|
||||
retrofit = mOauthRetrofit;
|
||||
}
|
||||
FetchPost.fetchPost(retrofit, mPost.getId(), mAccessToken,
|
||||
FetchPost.fetchPost(mExecutor, new Handler(), retrofit, mPost.getId(), mAccessToken,
|
||||
new FetchPost.FetchPostListener() {
|
||||
@Override
|
||||
public void fetchPostSuccess(Post post) {
|
||||
|
@ -1,7 +1,11 @@
|
||||
package ml.docilealligator.infinityforreddit.post;
|
||||
|
||||
import android.os.Handler;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
|
||||
import ml.docilealligator.infinityforreddit.utils.APIUtils;
|
||||
import retrofit2.Call;
|
||||
@ -10,7 +14,8 @@ import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class FetchPost {
|
||||
public static void fetchPost(Retrofit retrofit, String id, String accessToken, FetchPostListener fetchPostListener) {
|
||||
public static void fetchPost(Executor executor, Handler handler, Retrofit retrofit, String id, String accessToken,
|
||||
FetchPostListener fetchPostListener) {
|
||||
Call<String> postCall;
|
||||
if (accessToken == null) {
|
||||
postCall = retrofit.create(RedditAPI.class).getPost(id);
|
||||
@ -21,7 +26,7 @@ public class FetchPost {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
||||
if (response.isSuccessful()) {
|
||||
ParsePost.parsePost(response.body(), new ParsePost.ParsePostListener() {
|
||||
ParsePost.parsePost(executor, handler, response.body(), new ParsePost.ParsePostListener() {
|
||||
@Override
|
||||
public void onParsePostSuccess(Post post) {
|
||||
fetchPostListener.fetchPostSuccess(post);
|
||||
@ -44,7 +49,8 @@ public class FetchPost {
|
||||
});
|
||||
}
|
||||
|
||||
public static void fetchRandomPost(Retrofit retrofit, boolean isNSFW, FetchRandomPostListener fetchRandomPostListener) {
|
||||
public static void fetchRandomPost(Executor executor, Handler handler, Retrofit retrofit, boolean isNSFW,
|
||||
FetchRandomPostListener fetchRandomPostListener) {
|
||||
Call<String> call;
|
||||
if (isNSFW) {
|
||||
call = retrofit.create(RedditAPI.class).getRandomNSFWPost();
|
||||
@ -56,18 +62,18 @@ public class FetchPost {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
||||
if (response.isSuccessful()) {
|
||||
new ParsePost.ParseRandomPostAsyncTask(response.body(), isNSFW, new ParsePost.ParseRandomPostListener() {
|
||||
ParsePost.parseRandomPost(executor, handler, response.body(), isNSFW,
|
||||
new ParsePost.ParseRandomPostListener() {
|
||||
@Override
|
||||
public void onParseRandomPostSuccess(String postId, String subredditName) {
|
||||
fetchRandomPostListener.fetchRandomPostSuccess(postId, subredditName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onParseRandomPostSuccess(String postId, String subredditName) {
|
||||
fetchRandomPostListener.fetchRandomPostSuccess(postId, subredditName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onParseRandomPostFailed() {
|
||||
fetchRandomPostListener.fetchRandomPostFailed();
|
||||
}
|
||||
}).execute();
|
||||
@Override
|
||||
public void onParseRandomPostFailed() {
|
||||
fetchRandomPostListener.fetchRandomPostFailed();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
fetchRandomPostListener.fetchRandomPostFailed();
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package ml.docilealligator.infinityforreddit.post;
|
||||
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Handler;
|
||||
import android.text.Html;
|
||||
|
||||
@ -31,13 +30,11 @@ public class ParsePost {
|
||||
PostFilter postFilter, List<ReadPost> readPostList,
|
||||
ParsePostsListingListener parsePostsListingListener) {
|
||||
executor.execute(() -> {
|
||||
boolean parseFailed = false;
|
||||
LinkedHashSet<Post> newPosts = new LinkedHashSet<>();
|
||||
String lastItem = null;
|
||||
try {
|
||||
JSONObject jsonResponse = new JSONObject(response);
|
||||
JSONArray allData = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
|
||||
lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
|
||||
String lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
|
||||
|
||||
//Posts listing
|
||||
int size;
|
||||
@ -67,24 +64,59 @@ public class ParsePost {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
handler.post(() -> parsePostsListingListener.onParsePostsListingSuccess(newPosts, lastItem));
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
parseFailed = true;
|
||||
}
|
||||
|
||||
if (!parseFailed) {
|
||||
String finalLastItem = lastItem;
|
||||
handler.post(() -> parsePostsListingListener.onParsePostsListingSuccess(newPosts, finalLastItem));
|
||||
} else {
|
||||
handler.post(parsePostsListingListener::onParsePostsListingFail);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void parsePost(String response, ParsePostListener parsePostListener) {
|
||||
public static void parsePost(Executor executor, Handler handler, String response, ParsePostListener parsePostListener) {
|
||||
PostFilter postFilter = new PostFilter();
|
||||
postFilter.allowNSFW = true;
|
||||
new ParsePostDataAsyncTask(response, postFilter, parsePostListener).execute();
|
||||
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
JSONArray allData = new JSONArray(response).getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
|
||||
if (allData.length() == 0) {
|
||||
handler.post(parsePostListener::onParsePostFail);
|
||||
return;
|
||||
}
|
||||
JSONObject data = allData.getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY);
|
||||
Post post = parseBasicData(data);
|
||||
handler.post(() -> parsePostListener.onParsePostSuccess(post));
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
handler.post(parsePostListener::onParsePostFail);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void parseRandomPost(Executor executor, Handler handler, String response, boolean isNSFW,
|
||||
ParseRandomPostListener parseRandomPostListener) {
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
JSONArray postsArray = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
|
||||
if (postsArray.length() == 0) {
|
||||
handler.post(parseRandomPostListener::onParseRandomPostFailed);
|
||||
} else {
|
||||
JSONObject post = postsArray.getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY);
|
||||
String subredditName = post.getString(JSONUtils.SUBREDDIT_KEY);
|
||||
String postId;
|
||||
if (isNSFW) {
|
||||
postId = post.getString(JSONUtils.ID_KEY);
|
||||
} else {
|
||||
postId = post.getString(JSONUtils.LINK_ID_KEY).substring("t3_".length());
|
||||
}
|
||||
handler.post(() -> parseRandomPostListener.onParseRandomPostSuccess(postId, subredditName));
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
handler.post(parseRandomPostListener::onParseRandomPostFailed);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static Post parseBasicData(JSONObject data) throws JSONException {
|
||||
@ -561,13 +593,11 @@ public class ParsePost {
|
||||
|
||||
public interface ParsePostsListingListener {
|
||||
void onParsePostsListingSuccess(LinkedHashSet<Post> newPostData, String lastItem);
|
||||
|
||||
void onParsePostsListingFail();
|
||||
}
|
||||
|
||||
public interface ParsePostListener {
|
||||
void onParsePostSuccess(Post post);
|
||||
|
||||
void onParsePostFail();
|
||||
}
|
||||
|
||||
@ -575,166 +605,4 @@ public class ParsePost {
|
||||
void onParseRandomPostSuccess(String postId, String subredditName);
|
||||
void onParseRandomPostFailed();
|
||||
}
|
||||
|
||||
private static class ParsePostDataAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||
private JSONArray allData;
|
||||
private int nPosts;
|
||||
private PostFilter postFilter;
|
||||
private List<ReadPost> readPostList;
|
||||
private ParsePostsListingListener parsePostsListingListener;
|
||||
private ParsePostListener parsePostListener;
|
||||
private LinkedHashSet<Post> newPosts;
|
||||
private Post post;
|
||||
private String lastItem;
|
||||
private boolean parseFailed;
|
||||
|
||||
ParsePostDataAsyncTask(String response, int nPosts, PostFilter postFilter, List<ReadPost> readPostList,
|
||||
ParsePostsListingListener parsePostsListingListener) {
|
||||
this.parsePostsListingListener = parsePostsListingListener;
|
||||
try {
|
||||
JSONObject jsonResponse = new JSONObject(response);
|
||||
allData = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
|
||||
lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
|
||||
this.nPosts = nPosts;
|
||||
this.postFilter = postFilter;
|
||||
this.readPostList = readPostList;
|
||||
newPosts = new LinkedHashSet<>();
|
||||
parseFailed = false;
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
parseFailed = true;
|
||||
}
|
||||
}
|
||||
|
||||
ParsePostDataAsyncTask(String response, PostFilter postFilter,
|
||||
ParsePostListener parsePostListener) {
|
||||
this.parsePostListener = parsePostListener;
|
||||
try {
|
||||
allData = new JSONArray(response).getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
|
||||
this.postFilter = postFilter;
|
||||
parseFailed = false;
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
parseFailed = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
if (parseFailed) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (newPosts == null) {
|
||||
//Only one post
|
||||
if (allData.length() == 0) {
|
||||
parseFailed = true;
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
JSONObject data = allData.getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY);
|
||||
post = parseBasicData(data);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
parseFailed = true;
|
||||
}
|
||||
} else {
|
||||
//Posts listing
|
||||
int size;
|
||||
if (nPosts < 0 || nPosts > allData.length()) {
|
||||
size = allData.length();
|
||||
} else {
|
||||
size = nPosts;
|
||||
}
|
||||
|
||||
HashSet<ReadPost> readPostHashSet = null;
|
||||
if (readPostList != null) {
|
||||
readPostHashSet = new HashSet<>(readPostList);
|
||||
}
|
||||
for (int i = 0; i < size; i++) {
|
||||
try {
|
||||
if (allData.getJSONObject(i).getString(JSONUtils.KIND_KEY).equals("t3")) {
|
||||
JSONObject data = allData.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY);
|
||||
Post post = parseBasicData(data);
|
||||
if (readPostHashSet != null && readPostHashSet.contains(ReadPost.convertPost(post))) {
|
||||
post.markAsRead(false);
|
||||
}
|
||||
if (PostFilter.isPostAllowed(post, postFilter)) {
|
||||
newPosts.add(post);
|
||||
}
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void aVoid) {
|
||||
if (!parseFailed) {
|
||||
if (newPosts != null) {
|
||||
parsePostsListingListener.onParsePostsListingSuccess(newPosts, lastItem);
|
||||
} else {
|
||||
parsePostListener.onParsePostSuccess(post);
|
||||
}
|
||||
} else {
|
||||
if (parsePostsListingListener != null) {
|
||||
parsePostsListingListener.onParsePostsListingFail();
|
||||
} else {
|
||||
parsePostListener.onParsePostFail();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class ParseRandomPostAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||
|
||||
private String response;
|
||||
private boolean isNSFW;
|
||||
private ParseRandomPostListener parseRandomPostListener;
|
||||
private String subredditName;
|
||||
private String postId;
|
||||
private boolean parseFailed = false;
|
||||
|
||||
ParseRandomPostAsyncTask(String response, boolean isNSFW, ParseRandomPostListener parseRandomPostListener) {
|
||||
this.response = response;
|
||||
this.isNSFW = isNSFW;
|
||||
this.parseRandomPostListener = parseRandomPostListener;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
try {
|
||||
JSONArray postsArray = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
|
||||
if (postsArray.length() == 0) {
|
||||
parseFailed = true;
|
||||
} else {
|
||||
JSONObject post = postsArray.getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY);
|
||||
subredditName = post.getString(JSONUtils.SUBREDDIT_KEY);
|
||||
if (isNSFW) {
|
||||
postId = post.getString(JSONUtils.ID_KEY);
|
||||
} else {
|
||||
postId = post.getString(JSONUtils.LINK_ID_KEY).substring("t3_".length());
|
||||
}
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
parseFailed = true;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void aVoid) {
|
||||
super.onPostExecute(aVoid);
|
||||
if (parseFailed) {
|
||||
parseRandomPostListener.onParseRandomPostFailed();
|
||||
} else {
|
||||
parseRandomPostListener.onParseRandomPostSuccess(postId, subredditName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package ml.docilealligator.infinityforreddit.post;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Handler;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
@ -18,6 +19,7 @@ import java.io.StringReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import ml.docilealligator.infinityforreddit.Flair;
|
||||
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
|
||||
@ -31,21 +33,21 @@ import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class SubmitPost {
|
||||
public static void submitTextOrLinkPost(Retrofit oauthRetrofit, String accessToken,
|
||||
public static void submitTextOrLinkPost(Executor executor, Handler handler, Retrofit oauthRetrofit, String accessToken,
|
||||
Locale locale, String subredditName, String title, String content,
|
||||
Flair flair, boolean isSpoiler, boolean isNSFW, String kind,
|
||||
SubmitPostListener submitPostListener) {
|
||||
submitPost(oauthRetrofit, accessToken, subredditName, title, content,
|
||||
submitPost(executor, handler, oauthRetrofit, accessToken, subredditName, title, content,
|
||||
flair, isSpoiler, isNSFW, kind, null, submitPostListener);
|
||||
}
|
||||
|
||||
public static void submitImagePost(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
|
||||
public static void submitImagePost(Executor executor, Handler handler, Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
|
||||
String accessToken, String subredditName, String title, Bitmap image,
|
||||
Flair flair, boolean isSpoiler, boolean isNSFW, SubmitPostListener submitPostListener) {
|
||||
try {
|
||||
String imageUrlOrError = uploadImage(oauthRetrofit, uploadMediaRetrofit, accessToken, image);
|
||||
if (imageUrlOrError != null && !imageUrlOrError.startsWith("Error: ")) {
|
||||
submitPost(oauthRetrofit, accessToken,
|
||||
submitPost(executor, handler, oauthRetrofit, accessToken,
|
||||
subredditName, title, imageUrlOrError, flair, isSpoiler, isNSFW,
|
||||
APIUtils.KIND_IMAGE, null, submitPostListener);
|
||||
} else {
|
||||
@ -57,7 +59,7 @@ public class SubmitPost {
|
||||
}
|
||||
}
|
||||
|
||||
public static void submitVideoPost(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
|
||||
public static void submitVideoPost(Executor executor, Handler handler, Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
|
||||
Retrofit uploadVideoRetrofit, String accessToken,
|
||||
String subredditName, String title, File buffer, String mimeType,
|
||||
Bitmap posterBitmap, Flair flair, boolean isSpoiler, boolean isNSFW,
|
||||
@ -96,11 +98,11 @@ public class SubmitPost {
|
||||
String imageUrlOrError = uploadImage(oauthRetrofit, uploadMediaRetrofit, accessToken, posterBitmap);
|
||||
if (imageUrlOrError != null && !imageUrlOrError.startsWith("Error: ")) {
|
||||
if (fileType.equals("gif")) {
|
||||
submitPost(oauthRetrofit, accessToken,
|
||||
submitPost(executor, handler, oauthRetrofit, accessToken,
|
||||
subredditName, title, url, flair, isSpoiler, isNSFW,
|
||||
APIUtils.KIND_VIDEOGIF, imageUrlOrError, submitPostListener);
|
||||
} else {
|
||||
submitPost(oauthRetrofit, accessToken,
|
||||
submitPost(executor, handler, oauthRetrofit, accessToken,
|
||||
subredditName, title, url, flair, isSpoiler, isNSFW,
|
||||
APIUtils.KIND_VIDEO, imageUrlOrError, submitPostListener);
|
||||
}
|
||||
@ -119,15 +121,15 @@ public class SubmitPost {
|
||||
}
|
||||
}
|
||||
|
||||
public static void submitCrosspost(Retrofit oauthRetrofit, String accessToken,
|
||||
public static void submitCrosspost(Executor executor, Handler handler, Retrofit oauthRetrofit, String accessToken,
|
||||
String subredditName, String title, String crosspostFullname,
|
||||
Flair flair, boolean isSpoiler, boolean isNSFW, String kind,
|
||||
SubmitPostListener submitPostListener) {
|
||||
submitPost(oauthRetrofit, accessToken, subredditName, title, crosspostFullname,
|
||||
submitPost(executor, handler, oauthRetrofit, accessToken, subredditName, title, crosspostFullname,
|
||||
flair, isSpoiler, isNSFW, kind, null, submitPostListener);
|
||||
}
|
||||
|
||||
private static void submitPost(Retrofit oauthRetrofit, String accessToken,
|
||||
private static void submitPost(Executor executor, Handler handler, Retrofit oauthRetrofit, String accessToken,
|
||||
String subredditName, String title, String content,
|
||||
Flair flair, boolean isSpoiler, boolean isNSFW, String kind,
|
||||
@Nullable String posterUrl, SubmitPostListener submitPostListener) {
|
||||
@ -172,7 +174,8 @@ public class SubmitPost {
|
||||
try {
|
||||
Response<String> response = submitPostCall.execute();
|
||||
if (response.isSuccessful()) {
|
||||
getSubmittedPost(response.body(), kind, oauthRetrofit, accessToken, submitPostListener);
|
||||
getSubmittedPost(executor, handler, response.body(), kind, oauthRetrofit, accessToken,
|
||||
submitPostListener);
|
||||
} else {
|
||||
submitPostListener.submitFailed(response.message());
|
||||
}
|
||||
@ -216,8 +219,9 @@ public class SubmitPost {
|
||||
}
|
||||
}
|
||||
|
||||
private static void getSubmittedPost(String response, String kind, Retrofit oauthRetrofit,
|
||||
String accessToken, SubmitPostListener submitPostListener) throws JSONException, IOException {
|
||||
private static void getSubmittedPost(Executor executor, Handler handler, String response, String kind,
|
||||
Retrofit oauthRetrofit, String accessToken,
|
||||
SubmitPostListener submitPostListener) throws JSONException, IOException {
|
||||
JSONObject responseObject = new JSONObject(response).getJSONObject(JSONUtils.JSON_KEY);
|
||||
if (responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() != 0) {
|
||||
JSONArray error = responseObject.getJSONArray(JSONUtils.ERRORS_KEY)
|
||||
@ -245,7 +249,7 @@ public class SubmitPost {
|
||||
Call<String> getPostCall = api.getPostOauth(postId, APIUtils.getOAuthHeader(accessToken));
|
||||
Response<String> getPostCallResponse = getPostCall.execute();
|
||||
if (getPostCallResponse.isSuccessful()) {
|
||||
ParsePost.parsePost(getPostCallResponse.body(), new ParsePost.ParsePostListener() {
|
||||
ParsePost.parsePost(executor, handler, getPostCallResponse.body(), new ParsePost.ParsePostListener() {
|
||||
@Override
|
||||
public void onParsePostSuccess(Post post) {
|
||||
submitPostListener.submitSuccessful(post);
|
||||
|
@ -33,6 +33,7 @@ import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
@ -77,6 +78,9 @@ public class SubmitPostService extends Service {
|
||||
Retrofit mUploadVideoRetrofit;
|
||||
@Inject
|
||||
CustomThemeWrapper mCustomThemeWrapper;
|
||||
@Inject
|
||||
Executor mExecutor;
|
||||
private Handler handler;
|
||||
private ServiceHandler serviceHandler;
|
||||
|
||||
public SubmitPostService() {
|
||||
@ -111,7 +115,8 @@ public class SubmitPostService extends Service {
|
||||
submitTextOrLinkPost(accessToken, subredditName, title, content, flair, isSpoiler, isNSFW, kind);
|
||||
} else if (postType == EXTRA_POST_TYPE_CROSSPOST) {
|
||||
String content = bundle.getString(EXTRA_CONTENT);
|
||||
submitCrosspost(accessToken, subredditName, title, content, flair, isSpoiler, isNSFW);
|
||||
submitCrosspost(mExecutor, handler, accessToken, subredditName, title, content,
|
||||
flair, isSpoiler, isNSFW);
|
||||
} else if (postType == EXTRA_POST_TYPE_IMAGE) {
|
||||
Uri mediaUri = Uri.parse(bundle.getString(EXTRA_MEDIA_URI));
|
||||
submitImagePost(accessToken, mediaUri, subredditName, title, flair, isSpoiler, isNSFW);
|
||||
@ -129,6 +134,7 @@ public class SubmitPostService extends Service {
|
||||
// separate thread because the service normally runs in the process's
|
||||
// main thread, which we don't want to block. We also make it
|
||||
// background priority so CPU-intensive work doesn't disrupt our UI.
|
||||
handler = new Handler();
|
||||
HandlerThread thread = new HandlerThread("ServiceStartArguments",
|
||||
Process.THREAD_PRIORITY_BACKGROUND);
|
||||
thread.start();
|
||||
@ -185,8 +191,9 @@ public class SubmitPostService extends Service {
|
||||
}
|
||||
|
||||
private void submitTextOrLinkPost(String accessToken, String subredditName, String title, String content, Flair flair, boolean isSpoiler, boolean isNSFW, String kind) {
|
||||
SubmitPost.submitTextOrLinkPost(mOauthRetrofit, accessToken, getResources().getConfiguration().locale,
|
||||
subredditName, title, content, flair, isSpoiler, isNSFW, kind, new SubmitPost.SubmitPostListener() {
|
||||
SubmitPost.submitTextOrLinkPost(mExecutor, handler, mOauthRetrofit, accessToken,
|
||||
getResources().getConfiguration().locale, subredditName, title, content, flair, isSpoiler,
|
||||
isNSFW, kind, new SubmitPost.SubmitPostListener() {
|
||||
@Override
|
||||
public void submitSuccessful(Post post) {
|
||||
EventBus.getDefault().post(new SubmitTextOrLinkPostEvent(true, post, null));
|
||||
@ -203,9 +210,10 @@ public class SubmitPostService extends Service {
|
||||
});
|
||||
}
|
||||
|
||||
private void submitCrosspost(String accessToken, String subredditName, String title, String content, Flair flair, boolean isSpoiler, boolean isNSFW) {
|
||||
SubmitPost.submitCrosspost(mOauthRetrofit, accessToken, subredditName, title, content, flair, isSpoiler,
|
||||
isNSFW, APIUtils.KIND_CROSSPOST, new SubmitPost.SubmitPostListener() {
|
||||
private void submitCrosspost(Executor executor, Handler handler, String accessToken, String subredditName,
|
||||
String title, String content, Flair flair, boolean isSpoiler, boolean isNSFW) {
|
||||
SubmitPost.submitCrosspost(executor, handler, mOauthRetrofit, accessToken, subredditName, title,
|
||||
content, flair, isSpoiler, isNSFW, APIUtils.KIND_CROSSPOST, new SubmitPost.SubmitPostListener() {
|
||||
@Override
|
||||
public void submitSuccessful(Post post) {
|
||||
EventBus.getDefault().post(new SubmitCrosspostEvent(true, post, null));
|
||||
@ -225,8 +233,9 @@ public class SubmitPostService extends Service {
|
||||
private void submitImagePost(String accessToken, Uri mediaUri, String subredditName, String title, Flair flair, boolean isSpoiler, boolean isNSFW) {
|
||||
try {
|
||||
Bitmap resource = Glide.with(this).asBitmap().load(mediaUri).submit().get();
|
||||
SubmitPost.submitImagePost(mOauthRetrofit, mUploadMediaRetrofit, accessToken, subredditName, title, resource,
|
||||
flair, isSpoiler, isNSFW, new SubmitPost.SubmitPostListener() {
|
||||
SubmitPost.submitImagePost(mExecutor, handler, mOauthRetrofit, mUploadMediaRetrofit,
|
||||
accessToken, subredditName, title, resource, flair, isSpoiler, isNSFW,
|
||||
new SubmitPost.SubmitPostListener() {
|
||||
@Override
|
||||
public void submitSuccessful(Post post) {
|
||||
EventBus.getDefault().post(new SubmitImagePostEvent(true, null));
|
||||
@ -266,9 +275,9 @@ public class SubmitPostService extends Service {
|
||||
Bitmap resource = Glide.with(this).asBitmap().load(mediaUri).submit().get();
|
||||
|
||||
if (type != null) {
|
||||
SubmitPost.submitVideoPost(mOauthRetrofit, mUploadMediaRetrofit, mUploadVideoRetrofit,
|
||||
accessToken, subredditName, title, new File(cacheFilePath), type, resource, flair,
|
||||
isSpoiler, isNSFW, new SubmitPost.SubmitPostListener() {
|
||||
SubmitPost.submitVideoPost(mExecutor, handler, mOauthRetrofit, mUploadMediaRetrofit,
|
||||
mUploadVideoRetrofit, accessToken, subredditName, title, new File(cacheFilePath),
|
||||
type, resource, flair, isSpoiler, isNSFW, new SubmitPost.SubmitPostListener() {
|
||||
@Override
|
||||
public void submitSuccessful(Post post) {
|
||||
EventBus.getDefault().post(new SubmitVideoOrGifPostEvent(true, false, null));
|
||||
|
Loading…
Reference in New Issue
Block a user