From 4678caa92bad919201b53000e8d4100bf253c497 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 30 Apr 2020 15:29:56 +0800 Subject: Add an option for video autoplay. Rewrite some viewholders in PostRecyclerViewAdapter. --- .../Adapter/PostRecyclerViewAdapter.java | 235 +++++++++++---------- .../Event/ChangeVideoAutoplayEvent.java | 9 + .../infinityforreddit/Fragment/PostFragment.java | 17 +- .../Settings/MainPreferenceFragment.java | 19 +- .../Utils/SharedPreferencesUtils.java | 12 +- .../infinityforreddit/Utils/Utils.java | 17 ++ 6 files changed, 183 insertions(+), 126 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeVideoAutoplayEvent.java (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index 1f3335e6..88175912 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -88,8 +88,8 @@ import retrofit2.Retrofit; public class PostRecyclerViewAdapter extends PagedListAdapter implements CacheManager { private static final int VIEW_TYPE_POST_CARD_VIDEO_TYPE_AUTOPLAY = 1; - private static final int VIEW_TYPE_POST_CARD_GIF_TYPE = 2; - private static final int VIEW_TYPE_POST_CARD_IMAGE_TYPE = 3; + private static final int VIEW_TYPE_POST_CARD_VIDEO_AND_GIF_PREVIEW_TYPE = 2; + private static final int VIEW_TYPE_POST_CARD_IMAGE_AND_GIF_AUTOPLAY_TYPE = 3; private static final int VIEW_TYPE_POST_CARD_LINK_TYPE = 4; private static final int VIEW_TYPE_POST_CARD_NO_PREVIEW_LINK_TYPE = 5; private static final int VIEW_TYPE_POST_CARD_TEXT_TYPE = 6; @@ -153,7 +153,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - Intent intent = new Intent(mActivity, ViewGIFActivity.class); - intent.setData(gifVideoUri); - intent.putExtra(ViewGIFActivity.FILE_NAME_KEY, subredditName - + "-" + id + ".gif"); - intent.putExtra(ViewGIFActivity.GIF_URL_KEY, post.getVideoUrl()); - intent.putExtra(ViewGIFActivity.POST_TITLE_KEY, post.getTitle()); - mActivity.startActivity(intent); - }); - - ((PostBaseViewHolder) holder).playButtonImageView.setVisibility(View.VISIBLE);*/ - - - /*final Uri videoUri = Uri.parse(post.getVideoUrl()); - ((PostBaseViewHolder) holder).imageView.setOnClickListener(view -> { - Intent intent = new Intent(mActivity, ViewVideoActivity.class); - intent.setData(videoUri); - intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl()); - intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, subredditName); - intent.putExtra(ViewVideoActivity.EXTRA_ID, fullName); - intent.putExtra(ViewVideoActivity.EXTRA_POST_TITLE, post.getTitle()); - mActivity.startActivity(intent); - }); - - ((PostBaseViewHolder) holder).playButtonImageView.setVisibility(View.VISIBLE);*/ - /*((PostVideoAutoplayViewHolder) holder).videoPlayer.setRatio( - (float) post.getPreviewHeight() / post.getPreviewWidth() - );*/ ((PostVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio((float) post.getPreviewWidth() / post.getPreviewHeight()); ((PostVideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(post.getVideoUrl())); - } else if (holder instanceof PostGifTypeViewHolder) { - ((PostGifTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); - ((PostGifTypeViewHolder) holder).imageView + } else if (holder instanceof PostGifAndVideoPreviewViewHolder) { + if (post.getPostType() == Post.VIDEO_TYPE) { + ((PostGifAndVideoPreviewViewHolder) holder).typeTextView.setText(mActivity.getString(R.string.video)); + } else { + ((PostGifAndVideoPreviewViewHolder) holder).typeTextView.setText(mActivity.getString(R.string.gif)); + } + ((PostGifAndVideoPreviewViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + ((PostGifAndVideoPreviewViewHolder) holder).imageView .setRatio((float) post.getPreviewHeight() / post.getPreviewWidth()); loadImage(holder, post); if (post.getPreviewWidth() <= 0 || post.getPreviewHeight() <= 0) { - ((PostGifTypeViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); - ((PostGifTypeViewHolder) holder).imageView.getLayoutParams().height = (int) (400 * mScale); + ((PostGifAndVideoPreviewViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); + ((PostGifAndVideoPreviewViewHolder) holder).imageView.getLayoutParams().height = (int) (400 * mScale); } - } else if (holder instanceof PostImageTypeViewHolder) { - ((PostImageTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); - ((PostImageTypeViewHolder) holder).imageView + } else if (holder instanceof PostImageAndGifAutoplayTypeViewHolder) { + if (post.getPostType() == Post.GIF_TYPE) { + ((PostImageAndGifAutoplayTypeViewHolder) holder).typeTextView.setText(mActivity.getString(R.string.gif)); + } else { + ((PostImageAndGifAutoplayTypeViewHolder) holder).typeTextView.setText(mActivity.getString(R.string.image)); + } + ((PostImageAndGifAutoplayTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + ((PostImageAndGifAutoplayTypeViewHolder) holder).imageView .setRatio((float) post.getPreviewHeight() / post.getPreviewWidth()); loadImage(holder, post); if (post.getPreviewWidth() <= 0 || post.getPreviewHeight() <= 0) { - ((PostImageTypeViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); - ((PostImageTypeViewHolder) holder).imageView.getLayoutParams().height = (int) (400 * mScale); + ((PostImageAndGifAutoplayTypeViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); + ((PostImageAndGifAutoplayTypeViewHolder) holder).imageView.getLayoutParams().height = (int) (400 * mScale); } } else if (holder instanceof PostLinkTypeViewHolder) { ((PostLinkTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); @@ -1104,15 +1097,16 @@ public class PostRecyclerViewAdapter extends PagedListAdapter imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener() { + if (holder instanceof PostImageAndGifAutoplayTypeViewHolder) { + String url = mAutoplay && post.getPostType() == Post.GIF_TYPE ? post.getUrl() : post.getPreviewUrl(); + RequestBuilder imageRequestBuilder = mGlide.load(url).listener(new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - ((PostImageTypeViewHolder) holder).progressBar.setVisibility(View.GONE); - ((PostImageTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); - ((PostImageTypeViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { - ((PostImageTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); - ((PostImageTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((PostImageAndGifAutoplayTypeViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostImageAndGifAutoplayTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); + ((PostImageAndGifAutoplayTypeViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { + ((PostImageAndGifAutoplayTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + ((PostImageAndGifAutoplayTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); loadImage(holder, post); }); return false; @@ -1120,28 +1114,27 @@ public class PostRecyclerViewAdapter extends PagedListAdapter target, DataSource dataSource, boolean isFirstResource) { - ((PostImageTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - ((PostImageTypeViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostImageAndGifAutoplayTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((PostImageAndGifAutoplayTypeViewHolder) holder).progressBar.setVisibility(View.GONE); return false; } }); if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) { imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) - .into(((PostImageTypeViewHolder) holder).imageView); + .into(((PostImageAndGifAutoplayTypeViewHolder) holder).imageView); } else { - imageRequestBuilder.into(((PostImageTypeViewHolder) holder).imageView); + imageRequestBuilder.into(((PostImageAndGifAutoplayTypeViewHolder) holder).imageView); } - } else if (holder instanceof PostGifTypeViewHolder) { - String url = mAutoplay ? post.getUrl() : post.getPreviewUrl(); - RequestBuilder imageRequestBuilder = mGlide.load(url).listener(new RequestListener() { + } else if (holder instanceof PostGifAndVideoPreviewViewHolder) { + RequestBuilder imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - ((PostGifTypeViewHolder) holder).progressBar.setVisibility(View.GONE); - ((PostGifTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); - ((PostGifTypeViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { - ((PostGifTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); - ((PostGifTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((PostGifAndVideoPreviewViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostGifAndVideoPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); + ((PostGifAndVideoPreviewViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { + ((PostGifAndVideoPreviewViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + ((PostGifAndVideoPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); loadImage(holder, post); }); return false; @@ -1149,17 +1142,17 @@ public class PostRecyclerViewAdapter extends PagedListAdapter target, DataSource dataSource, boolean isFirstResource) { - ((PostGifTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - ((PostGifTypeViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostGifAndVideoPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((PostGifAndVideoPreviewViewHolder) holder).progressBar.setVisibility(View.GONE); return false; } }); if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) { imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) - .into(((PostGifTypeViewHolder) holder).imageView); + .into(((PostGifAndVideoPreviewViewHolder) holder).imageView); } else { - imageRequestBuilder.into(((PostGifTypeViewHolder) holder).imageView); + imageRequestBuilder.into(((PostGifAndVideoPreviewViewHolder) holder).imageView); } } else if (holder instanceof PostLinkTypeViewHolder) { RequestBuilder imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener() { @@ -1300,16 +1293,19 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { Post post = getItem(getAdapterPosition()); if (post != null) { - Intent intent = new Intent(mActivity, ViewImageActivity.class); - intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, post.getUrl()); - intent.putExtra(ViewImageActivity.FILE_NAME_KEY, post.getSubredditName() - + "-" + post.getId() + ".jpg"); - intent.putExtra(ViewImageActivity.POST_TITLE_KEY, post.getTitle()); - mActivity.startActivity(intent); + if (post.getPostType() == Post.VIDEO_TYPE) { + Intent intent = new Intent(mActivity, ViewVideoActivity.class); + intent.setData(Uri.parse(post.getVideoUrl())); + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl()); + intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, post.getSubredditName()); + intent.putExtra(ViewVideoActivity.EXTRA_ID, post.getFullName()); + intent.putExtra(ViewVideoActivity.EXTRA_POST_TITLE, post.getTitle()); + mActivity.startActivity(intent); + } else if (post.getPostType() == Post.GIF_TYPE) { + Intent intent = new Intent(mActivity, ViewGIFActivity.class); + intent.setData(Uri.parse(post.getVideoUrl())); + intent.putExtra(ViewGIFActivity.FILE_NAME_KEY, post.getSubredditName() + + "-" + post.getId() + ".gif"); + intent.putExtra(ViewGIFActivity.GIF_URL_KEY, post.getVideoUrl()); + intent.putExtra(ViewGIFActivity.POST_TITLE_KEY, post.getTitle()); + mActivity.startActivity(intent); + } } }); } } - class PostImageTypeViewHolder extends PostBaseViewHolder { + class PostImageAndGifAutoplayTypeViewHolder extends PostBaseViewHolder { @BindView(R.id.card_view_item_post_image_type) MaterialCardView cardView; @BindView(R.id.icon_gif_image_view_item_post_image_type) @@ -2095,7 +2102,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { + EventBus.getDefault().post(new ChangeVideoAutoplayEvent((String) newValue)); + return true; + }); + } if (confirmToExitSwitch != null) { - confirmToExitSwitch.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - EventBus.getDefault().post(new RecreateActivityEvent()); - return true; - } + confirmToExitSwitch.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new RecreateActivityEvent()); + return true; }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java index ec523487..63ab550e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -76,6 +76,10 @@ public class SharedPreferencesUtils { public static final String VOLUME_KEYS_NAVIGATE_POSTS = "volume_keys_navigate_posts"; public static final String MUTE_VIDEO = "mute_video"; public static final String OPEN_LINK_IN_APP = "open_link_in_app"; + public static final String VIDEO_AUTOPLAY = "video_autoplay"; + public static final String VIDEO_AUTOPLAY_VALUE_ALWAYS_ON = "2"; + public static final String VIDEO_AUTOPLAY_VALUE_ON_WIFI = "1"; + public static final String VIDEO_AUTOPLAY_VALUE_NEVER = "0"; public static final String LOCK_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON = "lock_jump_to_next_top_level_comment_button"; public static final String SWIPE_UP_TO_HIDE_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON = "swipe_up_to_hide_jump_to_next_top_level_comments_button"; public static final String SHOW_TOP_LEVEL_COMMENTS_FIRST = "show_top_level_comments_first"; @@ -89,10 +93,10 @@ public class SharedPreferencesUtils { public static final String CUSTOMIZE_DARK_THEME = "customize_dark_theme"; public static final String CUSTOMIZE_AMOLED_THEME = "customize_amoled_theme"; public static final String MANAGE_THEMES = "manage_themes"; - public static final String DELETE_ALL_SUBREDDITS_DATA_IN_DATABASE= "delete_all_subreddits_data_in_database"; - public static final String DELETE_ALL_USERS_DATA_IN_DATABASE= "delete_all_users_data_in_database"; - public static final String DELETE_ALL_SORT_TYPE_DATA_IN_DATABASE= "delete_all_sort_type_data_in_database"; - public static final String DELETE_ALL_POST_LAYOUT_DATA_IN_DATABASE= "delete_all_post_layout_data_in_database"; + public static final String DELETE_ALL_SUBREDDITS_DATA_IN_DATABASE = "delete_all_subreddits_data_in_database"; + public static final String DELETE_ALL_USERS_DATA_IN_DATABASE = "delete_all_users_data_in_database"; + public static final String DELETE_ALL_SORT_TYPE_DATA_IN_DATABASE = "delete_all_sort_type_data_in_database"; + public static final String DELETE_ALL_POST_LAYOUT_DATA_IN_DATABASE = "delete_all_post_layout_data_in_database"; public static final String DELETE_ALL_THEMES_IN_DATABASE = "delete_all_themes_in_database"; public static final String RESET_ALL_SETTINGS = "reset_all_settings"; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java index f3788ff8..e6726cbb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java @@ -1,6 +1,9 @@ package ml.docilealligator.infinityforreddit.Utils; import android.content.Context; +import android.net.ConnectivityManager; +import android.net.Network; +import android.net.NetworkInfo; import android.text.Html; import android.text.Spannable; import android.widget.TextView; @@ -87,4 +90,18 @@ public class Utils { } textView.setText(html); } + + public static boolean isConnectedToWifi(Context context) { + ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + if (connMgr != null) { + for (Network network : connMgr.getAllNetworks()) { + NetworkInfo networkInfo = connMgr.getNetworkInfo(network); + if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { + return networkInfo.isConnected(); + } + } + } + + return false; + } } -- cgit v1.2.3