|
@ -5,7 +5,6 @@ import android.content.Context; |
|
|
import android.content.Intent; |
|
|
import android.content.Intent; |
|
|
import android.graphics.Rect; |
|
|
import android.graphics.Rect; |
|
|
import android.net.Uri; |
|
|
import android.net.Uri; |
|
|
import android.util.Log; |
|
|
|
|
|
import android.view.View; |
|
|
import android.view.View; |
|
|
import android.widget.ImageView; |
|
|
import android.widget.ImageView; |
|
|
|
|
|
|
|
@ -17,7 +16,10 @@ import com.google.android.exoplayer2.Player; |
|
|
import com.google.android.exoplayer2.SimpleExoPlayer; |
|
|
import com.google.android.exoplayer2.SimpleExoPlayer; |
|
|
import com.google.android.exoplayer2.source.ProgressiveMediaSource; |
|
|
import com.google.android.exoplayer2.source.ProgressiveMediaSource; |
|
|
import com.google.android.exoplayer2.ui.PlayerView; |
|
|
import com.google.android.exoplayer2.ui.PlayerView; |
|
|
|
|
|
import com.google.android.exoplayer2.upstream.DataSource; |
|
|
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; |
|
|
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; |
|
|
|
|
|
import com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory; |
|
|
|
|
|
import com.google.android.exoplayer2.upstream.cache.SimpleCache; |
|
|
|
|
|
|
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
|
|
|
|
|
@ -30,18 +32,23 @@ import awais.instagrabber.utils.Utils; |
|
|
|
|
|
|
|
|
import static awais.instagrabber.utils.Utils.settingsHelper; |
|
|
import static awais.instagrabber.utils.Utils.settingsHelper; |
|
|
|
|
|
|
|
|
// wasted around 3 hours to get this working, made from scrach, forgot to take a shower so i'm gonna go take a shower (time: May 11, 2020 @ 8:09:30 PM) |
|
|
|
|
|
public class VideoAwareRecyclerScroller extends RecyclerView.OnScrollListener { |
|
|
public class VideoAwareRecyclerScroller extends RecyclerView.OnScrollListener { |
|
|
|
|
|
private static final String TAG = "VideoAwareRecScroll"; |
|
|
private static final Object LOCK = new Object(); |
|
|
private static final Object LOCK = new Object(); |
|
|
|
|
|
|
|
|
private LinearLayoutManager layoutManager; |
|
|
private LinearLayoutManager layoutManager; |
|
|
private View firstItemView, lastItemView; |
|
|
private View firstItemView, lastItemView; |
|
|
private int videoPosShown = -1, lastVideoPos = -1, lastChangedVideoPos, lastStoppedVideoPos, lastPlayedVideoPos; |
|
|
private int videoPosShown = -1, lastVideoPos = -1, lastChangedVideoPos, lastStoppedVideoPos, lastPlayedVideoPos; |
|
|
private boolean videoAttached = false; |
|
|
private boolean videoAttached = false; |
|
|
private final List<FeedModel> feedModels; |
|
|
|
|
|
//////////////////////////////////////////////////// |
|
|
|
|
|
private SimpleExoPlayer player; |
|
|
private SimpleExoPlayer player; |
|
|
private ImageView btnMute; |
|
|
private ImageView btnMute; |
|
|
|
|
|
private CacheDataSourceFactory cacheDataSourceFactory; |
|
|
|
|
|
|
|
|
|
|
|
private final List<FeedModel> feedModels; |
|
|
private final Context context; |
|
|
private final Context context; |
|
|
|
|
|
private final VideoChangeCallback videoChangeCallback; |
|
|
|
|
|
private final DefaultDataSourceFactory dataSourceFactory; |
|
|
|
|
|
|
|
|
private final View.OnClickListener commentClickListener = new View.OnClickListener() { |
|
|
private final View.OnClickListener commentClickListener = new View.OnClickListener() { |
|
|
@Override |
|
|
@Override |
|
|
public void onClick(@NonNull final View v) { |
|
|
public void onClick(@NonNull final View v) { |
|
@ -55,33 +62,38 @@ public class VideoAwareRecyclerScroller extends RecyclerView.OnScrollListener { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
private final View.OnClickListener muteClickListener = v -> { |
|
|
private final View.OnClickListener muteClickListener = v -> { |
|
|
if (player == null) return; |
|
|
if (player == null) return; |
|
|
final float intVol = player.getVolume() == 0f ? 1f : 0f; |
|
|
final float intVol = player.getVolume() == 0f ? 1f : 0f; |
|
|
player.setVolume(intVol); |
|
|
player.setVolume(intVol); |
|
|
if (btnMute != null) btnMute.setImageResource(intVol == 0f ? R.drawable.mute : R.drawable.vol); |
|
|
|
|
|
|
|
|
if (btnMute != null) |
|
|
|
|
|
btnMute.setImageResource(intVol == 0f ? R.drawable.mute : R.drawable.vol); |
|
|
Utils.sessionVolumeFull = intVol == 1f; |
|
|
Utils.sessionVolumeFull = intVol == 1f; |
|
|
}; |
|
|
}; |
|
|
private final VideoChangeCallback videoChangeCallback; |
|
|
|
|
|
// private final ScrollerVideoCallback videoCallback; |
|
|
|
|
|
// private View lastVideoHolder; |
|
|
|
|
|
// private int videoState = -1; |
|
|
|
|
|
|
|
|
|
|
|
public VideoAwareRecyclerScroller(final Context context, final List<FeedModel> feedModels, |
|
|
public VideoAwareRecyclerScroller(final Context context, final List<FeedModel> feedModels, |
|
|
final VideoChangeCallback videoChangeCallback) { |
|
|
final VideoChangeCallback videoChangeCallback) { |
|
|
this.context = context; |
|
|
this.context = context; |
|
|
this.feedModels = feedModels; |
|
|
this.feedModels = feedModels; |
|
|
this.videoChangeCallback = videoChangeCallback; |
|
|
this.videoChangeCallback = videoChangeCallback; |
|
|
|
|
|
dataSourceFactory = new DefaultDataSourceFactory(context, "instagram"); |
|
|
|
|
|
final SimpleCache simpleCache = Utils.getSimpleCacheInstance(context); |
|
|
|
|
|
if (simpleCache != null) { |
|
|
|
|
|
cacheDataSourceFactory = new CacheDataSourceFactory(simpleCache, dataSourceFactory); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onScrolled(@NonNull final RecyclerView recyclerView, final int dx, final int dy) { |
|
|
public void onScrolled(@NonNull final RecyclerView recyclerView, final int dx, final int dy) { |
|
|
if (layoutManager == null) { |
|
|
if (layoutManager == null) { |
|
|
final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); |
|
|
final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); |
|
|
if (layoutManager instanceof LinearLayoutManager) this.layoutManager = (LinearLayoutManager) layoutManager; |
|
|
|
|
|
|
|
|
if (layoutManager instanceof LinearLayoutManager) |
|
|
|
|
|
this.layoutManager = (LinearLayoutManager) layoutManager; |
|
|
|
|
|
} |
|
|
|
|
|
if (feedModels.size() == 0 || layoutManager == null) { |
|
|
|
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (feedModels.size() > 0 && layoutManager != null) { |
|
|
|
|
|
int firstVisibleItemPos = layoutManager.findFirstCompletelyVisibleItemPosition(); |
|
|
int firstVisibleItemPos = layoutManager.findFirstCompletelyVisibleItemPosition(); |
|
|
int lastVisibleItemPos = layoutManager.findLastCompletelyVisibleItemPosition(); |
|
|
int lastVisibleItemPos = layoutManager.findLastCompletelyVisibleItemPosition(); |
|
|
|
|
|
|
|
@ -122,13 +134,16 @@ public class VideoAwareRecyclerScroller extends RecyclerView.OnScrollListener { |
|
|
lastVisibleItemHeight = visibleItemRect.height(); |
|
|
lastVisibleItemHeight = visibleItemRect.height(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (processFirstItem && firstVisibleItemHeight > lastVisibleItemHeight) videoPosShown = firstVisibleItemPos; |
|
|
|
|
|
|
|
|
if (processFirstItem && firstVisibleItemHeight > lastVisibleItemHeight) |
|
|
|
|
|
videoPosShown = firstVisibleItemPos; |
|
|
else if (processLastItem && lastVisibleItemHeight != 0) videoPosShown = lastVisibleItemPos; |
|
|
else if (processLastItem && lastVisibleItemHeight != 0) videoPosShown = lastVisibleItemPos; |
|
|
|
|
|
|
|
|
if (firstItemView != lastItemView) { |
|
|
if (firstItemView != lastItemView) { |
|
|
final int mox = lastVisibleItemHeight - firstVisibleItemHeight; |
|
|
final int mox = lastVisibleItemHeight - firstVisibleItemHeight; |
|
|
if (processLastItem && lastVisibleItemHeight > firstVisibleItemHeight) videoPosShown = lastVisibleItemPos; |
|
|
|
|
|
if ((processFirstItem || processLastItem) && mox >= 0) videoPosShown = lastVisibleItemPos; |
|
|
|
|
|
|
|
|
if (processLastItem && lastVisibleItemHeight > firstVisibleItemHeight) |
|
|
|
|
|
videoPosShown = lastVisibleItemPos; |
|
|
|
|
|
if ((processFirstItem || processLastItem) && mox >= 0) |
|
|
|
|
|
videoPosShown = lastVisibleItemPos; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (lastChangedVideoPos != -1 && lastVideoPos != -1) { |
|
|
if (lastChangedVideoPos != -1 && lastVideoPos != -1) { |
|
@ -179,7 +194,6 @@ public class VideoAwareRecyclerScroller extends RecyclerView.OnScrollListener { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private synchronized void attachVideo(final int itemPos, final RecyclerView recyclerView, final View itemView) { |
|
|
private synchronized void attachVideo(final int itemPos, final RecyclerView recyclerView, final View itemView) { |
|
|
synchronized (LOCK) { |
|
|
synchronized (LOCK) { |
|
@ -197,7 +211,11 @@ public class VideoAwareRecyclerScroller extends RecyclerView.OnScrollListener { |
|
|
player = null; |
|
|
player = null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
player = new SimpleExoPlayer.Builder(context).build(); |
|
|
|
|
|
|
|
|
final boolean shouldAutoplay = settingsHelper.getBoolean(Constants.AUTOPLAY_VIDEOS); |
|
|
|
|
|
player = new SimpleExoPlayer.Builder(context) |
|
|
|
|
|
.setUseLazyPreparation(!shouldAutoplay) |
|
|
|
|
|
.build(); |
|
|
|
|
|
player.setPlayWhenReady(shouldAutoplay); |
|
|
|
|
|
|
|
|
if (itemView != null) { |
|
|
if (itemView != null) { |
|
|
final Object tag = itemView.getTag(); |
|
|
final Object tag = itemView.getTag(); |
|
@ -218,7 +236,6 @@ public class VideoAwareRecyclerScroller extends RecyclerView.OnScrollListener { |
|
|
if (playerView == null) return; |
|
|
if (playerView == null) return; |
|
|
playerView.setPlayer(player); |
|
|
playerView.setPlayer(player); |
|
|
|
|
|
|
|
|
if (player != null) { |
|
|
|
|
|
btnMute = itemView.findViewById(R.id.btnMute); |
|
|
btnMute = itemView.findViewById(R.id.btnMute); |
|
|
|
|
|
|
|
|
float vol = settingsHelper.getBoolean(Constants.MUTED_VIDEOS) ? 0f : 1f; |
|
|
float vol = settingsHelper.getBoolean(Constants.MUTED_VIDEOS) ? 0f : 1f; |
|
@ -230,27 +247,15 @@ public class VideoAwareRecyclerScroller extends RecyclerView.OnScrollListener { |
|
|
btnMute.setImageResource(vol == 0f ? R.drawable.vol : R.drawable.mute); |
|
|
btnMute.setImageResource(vol == 0f ? R.drawable.vol : R.drawable.mute); |
|
|
btnMute.setOnClickListener(muteClickListener); |
|
|
btnMute.setOnClickListener(muteClickListener); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
player.setPlayWhenReady(settingsHelper.getBoolean(Constants.AUTOPLAY_VIDEOS)); |
|
|
|
|
|
|
|
|
|
|
|
final ProgressiveMediaSource mediaSource = new ProgressiveMediaSource.Factory(new DefaultDataSourceFactory(context, "instagram")) |
|
|
|
|
|
.createMediaSource(Uri.parse(feedModels.get(itemPos).getDisplayUrl())); |
|
|
|
|
|
|
|
|
final DataSource.Factory factory = cacheDataSourceFactory != null ? cacheDataSourceFactory : dataSourceFactory; |
|
|
|
|
|
final ProgressiveMediaSource.Factory sourceFactory = new ProgressiveMediaSource.Factory(factory); |
|
|
|
|
|
final ProgressiveMediaSource mediaSource = sourceFactory.createMediaSource(Uri.parse(feedModels.get(itemPos).getDisplayUrl())); |
|
|
|
|
|
|
|
|
player.setRepeatMode(Player.REPEAT_MODE_ALL); |
|
|
player.setRepeatMode(Player.REPEAT_MODE_ALL); |
|
|
player.prepare(mediaSource); |
|
|
player.prepare(mediaSource); |
|
|
player.setVolume(vol); |
|
|
player.setVolume(vol); |
|
|
|
|
|
|
|
|
playerView.setOnClickListener(v -> { |
|
|
|
|
|
if (player.getPlayWhenReady() == true) { |
|
|
|
|
|
player.setPlayWhenReady(false); |
|
|
|
|
|
player.getPlaybackState(); |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
player.setPlayWhenReady(true); |
|
|
|
|
|
player.getPlaybackState(); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
playerView.setOnClickListener(v -> player.setPlayWhenReady(!player.getPlayWhenReady())); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (videoChangeCallback != null) videoChangeCallback.playerChanged(itemPos, player); |
|
|
if (videoChangeCallback != null) videoChangeCallback.playerChanged(itemPos, player); |
|
|