Replace AsyncTask with Executor in FetchGfycatOrRedgifsVideoLinks.

This commit is contained in:
Alex Ning 2021-06-23 20:22:29 +08:00
parent c7f9343d00
commit ade1097f30
4 changed files with 42 additions and 65 deletions

View File

@ -1,12 +1,14 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import android.os.AsyncTask; import android.os.Handler;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.concurrent.Executor;
import ml.docilealligator.infinityforreddit.apis.GfycatAPI; import ml.docilealligator.infinityforreddit.apis.GfycatAPI;
import ml.docilealligator.infinityforreddit.utils.JSONUtils; import ml.docilealligator.infinityforreddit.utils.JSONUtils;
import retrofit2.Call; import retrofit2.Call;
@ -16,8 +18,6 @@ import retrofit2.Retrofit;
public class FetchGfycatOrRedgifsVideoLinks { public class FetchGfycatOrRedgifsVideoLinks {
private FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener; private FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener;
private ParseGfycatVideoLinksAsyncTask parseGfycatVideoLinksAsyncTask;
Retrofit gfycatRetrofit;
Call<String> gfycatCall; Call<String> gfycatCall;
public interface FetchGfycatOrRedgifsVideoLinksListener { public interface FetchGfycatOrRedgifsVideoLinksListener {
@ -29,13 +29,14 @@ public class FetchGfycatOrRedgifsVideoLinks {
this.fetchGfycatOrRedgifsVideoLinksListener = fetchGfycatOrRedgifsVideoLinksListener; this.fetchGfycatOrRedgifsVideoLinksListener = fetchGfycatOrRedgifsVideoLinksListener;
} }
public static void fetchGfycatOrRedgifsVideoLinks(Retrofit gfycatRetrofit, String gfycatId, public static void fetchGfycatOrRedgifsVideoLinks(Executor executor, Handler handler, Retrofit gfycatRetrofit,
String gfycatId,
FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) { FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) {
gfycatRetrofit.create(GfycatAPI.class).getGfycatData(gfycatId).enqueue(new Callback<String>() { gfycatRetrofit.create(GfycatAPI.class).getGfycatData(gfycatId).enqueue(new Callback<String>() {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
new ParseGfycatVideoLinksAsyncTask(response.body(), fetchGfycatOrRedgifsVideoLinksListener).execute(); parseGfycatVideoLinks(executor, handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener);
} else { } else {
fetchGfycatOrRedgifsVideoLinksListener.failed(response.code()); fetchGfycatOrRedgifsVideoLinksListener.failed(response.code());
} }
@ -48,7 +49,8 @@ public class FetchGfycatOrRedgifsVideoLinks {
}); });
} }
public void fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(Retrofit gfycatRetrofit, Retrofit redgifsRetrofit, public void fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(Executor executor, Handler handler,
Retrofit gfycatRetrofit, Retrofit redgifsRetrofit,
String gfycatId, boolean isGfycatVideo, String gfycatId, boolean isGfycatVideo,
boolean automaticallyTryRedgifs) { boolean automaticallyTryRedgifs) {
gfycatCall = (isGfycatVideo ? gfycatRetrofit : redgifsRetrofit).create(GfycatAPI.class).getGfycatData(gfycatId); gfycatCall = (isGfycatVideo ? gfycatRetrofit : redgifsRetrofit).create(GfycatAPI.class).getGfycatData(gfycatId);
@ -56,11 +58,11 @@ public class FetchGfycatOrRedgifsVideoLinks {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
parseGfycatVideoLinksAsyncTask = new ParseGfycatVideoLinksAsyncTask(response.body(), fetchGfycatOrRedgifsVideoLinksListener); parseGfycatVideoLinks(executor, handler, response.body(), fetchGfycatOrRedgifsVideoLinksListener);
parseGfycatVideoLinksAsyncTask.execute();
} else { } else {
if (response.code() == 404 && isGfycatVideo && automaticallyTryRedgifs) { if (response.code() == 404 && isGfycatVideo && automaticallyTryRedgifs) {
fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(gfycatRetrofit, redgifsRetrofit, gfycatId, false, false); fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(executor, handler, gfycatRetrofit,
redgifsRetrofit, gfycatId, false, false);
} else { } else {
fetchGfycatOrRedgifsVideoLinksListener.failed(response.code()); fetchGfycatOrRedgifsVideoLinksListener.failed(response.code());
} }
@ -78,34 +80,19 @@ public class FetchGfycatOrRedgifsVideoLinks {
if (gfycatCall != null && !gfycatCall.isCanceled()) { if (gfycatCall != null && !gfycatCall.isCanceled()) {
gfycatCall.cancel(); gfycatCall.cancel();
} }
if (parseGfycatVideoLinksAsyncTask != null && !parseGfycatVideoLinksAsyncTask.isCancelled()) {
parseGfycatVideoLinksAsyncTask.cancel(true);
}
} }
private static class ParseGfycatVideoLinksAsyncTask extends AsyncTask<Void, Void, Void> { private static void parseGfycatVideoLinks(Executor executor, Handler handler, String response,
FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) {
private String response;
private String webm;
private String mp4;
private boolean parseFailed = false;
private FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener;
ParseGfycatVideoLinksAsyncTask(String response, FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) {
this.response = response;
this.fetchGfycatOrRedgifsVideoLinksListener = fetchGfycatOrRedgifsVideoLinksListener;
}
@Override
protected Void doInBackground(Void... voids) {
try { try {
JSONObject jsonObject = new JSONObject(response); JSONObject jsonObject = new JSONObject(response);
mp4 = jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).has(JSONUtils.MP4_URL_KEY) ? String mp4 = jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).has(JSONUtils.MP4_URL_KEY) ?
jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).getString(JSONUtils.MP4_URL_KEY) jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).getString(JSONUtils.MP4_URL_KEY)
: jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY) : jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY)
.getJSONObject(JSONUtils.CONTENT_URLS_KEY) .getJSONObject(JSONUtils.CONTENT_URLS_KEY)
.getJSONObject(JSONUtils.MP4_KEY) .getJSONObject(JSONUtils.MP4_KEY)
.getString(JSONUtils.URL_KEY); .getString(JSONUtils.URL_KEY);
String webm;
if (jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).has(JSONUtils.WEBM_URL_KEY)) { if (jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).has(JSONUtils.WEBM_URL_KEY)) {
webm = jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).getString(JSONUtils.WEBM_URL_KEY); webm = jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).getString(JSONUtils.WEBM_URL_KEY);
} else if (jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).getJSONObject(JSONUtils.CONTENT_URLS_KEY).has(JSONUtils.WEBM_KEY)) { } else if (jsonObject.getJSONObject(JSONUtils.GFY_ITEM_KEY).getJSONObject(JSONUtils.CONTENT_URLS_KEY).has(JSONUtils.WEBM_KEY)) {
@ -116,22 +103,10 @@ public class FetchGfycatOrRedgifsVideoLinks {
} else { } else {
webm = mp4; webm = mp4;
} }
handler.post(() -> fetchGfycatOrRedgifsVideoLinksListener.success(webm, mp4));
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
parseFailed = true; handler.post(() -> fetchGfycatOrRedgifsVideoLinksListener.failed(-1));
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (parseFailed) {
fetchGfycatOrRedgifsVideoLinksListener.failed(-1);
} else {
fetchGfycatOrRedgifsVideoLinksListener.success(webm, mp4);
}
} }
} }
} }

View File

@ -390,7 +390,7 @@ public class ViewVideoActivity extends AppCompatActivity {
private void loadGfycatOrRedgifsVideo(Retrofit retrofit, String gfycatId, Bundle savedInstanceState, boolean needErrorHandling) { private void loadGfycatOrRedgifsVideo(Retrofit retrofit, String gfycatId, Bundle savedInstanceState, boolean needErrorHandling) {
progressBar.setVisibility(View.VISIBLE); progressBar.setVisibility(View.VISIBLE);
FetchGfycatOrRedgifsVideoLinks.fetchGfycatOrRedgifsVideoLinks(retrofit, gfycatId, FetchGfycatOrRedgifsVideoLinks.fetchGfycatOrRedgifsVideoLinks(mExecutor, new Handler(), retrofit, gfycatId,
new FetchGfycatOrRedgifsVideoLinks.FetchGfycatOrRedgifsVideoLinksListener() { new FetchGfycatOrRedgifsVideoLinks.FetchGfycatOrRedgifsVideoLinksListener() {
@Override @Override
public void success(String webm, String mp4) { public void success(String webm, String mp4) {

View File

@ -696,8 +696,9 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
} }
}); });
((PostDetailVideoAutoplayViewHolder) holder).fetchGfycatOrRedgifsVideoLinks ((PostDetailVideoAutoplayViewHolder) holder).fetchGfycatOrRedgifsVideoLinks
.fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mGfycatRetrofit, mRedgifsRetrofit, .fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mExecutor, new Handler(),
mPost.getGfycatId(), mPost.isGfycat(), mAutomaticallyTryRedgifs); mGfycatRetrofit, mRedgifsRetrofit, mPost.getGfycatId(),
mPost.isGfycat(), mAutomaticallyTryRedgifs);
} else { } else {
((PostDetailVideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(mPost.getVideoUrl())); ((PostDetailVideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(mPost.getVideoUrl()));
} }

View File

@ -31,7 +31,6 @@ import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet; import androidx.constraintlayout.widget.ConstraintSet;
import androidx.core.graphics.drawable.DrawableCompat; import androidx.core.graphics.drawable.DrawableCompat;
import androidx.paging.PagedListAdapter; import androidx.paging.PagedListAdapter;
import androidx.paging.PagingDataAdapter;
import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -732,8 +731,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
} }
}); });
((PostVideoAutoplayViewHolder) holder).fetchGfycatOrRedgifsVideoLinks ((PostVideoAutoplayViewHolder) holder).fetchGfycatOrRedgifsVideoLinks
.fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mGfycatRetrofit, mRedgifsRetrofit, .fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mExecutor, new Handler(),
post.getGfycatId(), post.isGfycat(), mAutomaticallyTryRedgifs); mGfycatRetrofit, mRedgifsRetrofit, post.getGfycatId(),
post.isGfycat(), mAutomaticallyTryRedgifs);
} else { } else {
((PostVideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(post.getVideoUrl())); ((PostVideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(post.getVideoUrl()));
} }
@ -855,8 +855,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
} }
}); });
((PostCard2VideoAutoplayViewHolder) holder).fetchGfycatOrRedgifsVideoLinks ((PostCard2VideoAutoplayViewHolder) holder).fetchGfycatOrRedgifsVideoLinks
.fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mGfycatRetrofit, mRedgifsRetrofit, .fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mExecutor, new Handler(),
post.getGfycatId(), post.isGfycat(), mAutomaticallyTryRedgifs); mGfycatRetrofit, mRedgifsRetrofit, post.getGfycatId(), post.isGfycat(),
mAutomaticallyTryRedgifs);
} else { } else {
((PostCard2VideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(post.getVideoUrl())); ((PostCard2VideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(post.getVideoUrl()));
} }