TubeLab-App-Android/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java

1493 lines
73 KiB
Java
Raw Normal View History

2020-06-26 08:50:49 +02:00
package app.fedilab.fedilabtube;
2020-07-01 16:36:08 +02:00
/* Copyright 2020 Thomas Schneider
*
* This file is a part of TubeLab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
2020-06-26 08:50:49 +02:00
import android.Manifest;
2020-10-13 18:11:53 +02:00
import android.annotation.SuppressLint;
2020-06-26 08:50:49 +02:00
import android.app.Activity;
import android.app.Dialog;
import android.app.PictureInPictureParams;
2020-10-16 17:25:52 +02:00
import android.content.BroadcastReceiver;
import android.content.Context;
2020-06-26 08:50:49 +02:00
import android.content.Intent;
2020-10-16 17:25:52 +02:00
import android.content.IntentFilter;
2020-06-26 08:50:49 +02:00
import android.content.SharedPreferences;
2020-10-02 15:14:08 +02:00
import android.content.pm.ActivityInfo;
2020-06-26 08:50:49 +02:00
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.database.sqlite.SQLiteDatabase;
2020-06-29 17:59:36 +02:00
import android.graphics.PorterDuff;
2020-06-26 08:50:49 +02:00
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
2020-10-02 15:23:36 +02:00
import android.os.Handler;
2020-09-19 18:19:59 +02:00
import android.support.v4.media.session.MediaSessionCompat;
2020-06-26 08:50:49 +02:00
import android.text.Html;
2020-10-15 15:16:41 +02:00
import android.text.Spanned;
2020-07-21 18:49:13 +02:00
import android.view.LayoutInflater;
2020-06-26 08:50:49 +02:00
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
2020-10-15 15:16:41 +02:00
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
2020-06-26 08:50:49 +02:00
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
2020-10-24 16:36:49 +02:00
import android.widget.ImageButton;
2020-06-26 08:50:49 +02:00
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
2020-10-02 18:22:19 +02:00
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
2020-09-20 11:07:28 +02:00
import androidx.appcompat.app.AlertDialog;
2020-06-26 08:50:49 +02:00
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.PopupMenu;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
2020-06-29 17:59:36 +02:00
import androidx.core.graphics.drawable.DrawableCompat;
2020-09-07 16:57:00 +02:00
import androidx.lifecycle.ViewModelProvider;
2020-06-26 08:50:49 +02:00
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
2020-10-29 18:56:57 +01:00
import com.github.vkay94.dtpv.youtube.YouTubeOverlay;
2020-09-20 12:06:38 +02:00
import com.google.android.exoplayer2.C;
2020-10-31 10:37:17 +01:00
import com.google.android.exoplayer2.ExoPlaybackException;
2020-09-20 12:06:38 +02:00
import com.google.android.exoplayer2.Format;
2020-10-29 18:56:57 +01:00
import com.google.android.exoplayer2.MediaItem;
2020-10-31 10:37:17 +01:00
import com.google.android.exoplayer2.Player;
2020-06-26 08:50:49 +02:00
import com.google.android.exoplayer2.SimpleExoPlayer;
2020-09-19 18:19:59 +02:00
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector;
2020-09-20 12:06:38 +02:00
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.MergingMediaSource;
2020-06-26 08:50:49 +02:00
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
2020-09-20 12:06:38 +02:00
import com.google.android.exoplayer2.source.SingleSampleMediaSource;
2020-10-06 17:11:25 +02:00
import com.google.android.exoplayer2.source.hls.HlsMediaSource;
2020-09-20 12:06:38 +02:00
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelector;
2020-06-26 08:50:49 +02:00
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
import com.google.android.exoplayer2.ui.PlayerControlView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
2020-10-06 17:11:25 +02:00
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
2020-09-20 12:06:38 +02:00
import com.google.android.exoplayer2.util.MimeTypes;
2020-06-26 08:50:49 +02:00
import com.google.android.exoplayer2.util.Util;
import org.jetbrains.annotations.NotNull;
2020-09-25 18:58:04 +02:00
2020-06-26 08:50:49 +02:00
import java.util.ArrayList;
2020-10-08 18:13:04 +02:00
import java.util.HashMap;
2020-06-26 08:50:49 +02:00
import java.util.List;
2020-10-03 18:37:34 +02:00
import java.util.Map;
2020-06-26 08:50:49 +02:00
import java.util.Objects;
2020-10-26 14:38:51 +01:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2020-06-26 08:50:49 +02:00
import app.fedilab.fedilabtube.client.APIResponse;
2020-09-25 18:58:04 +02:00
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
2020-09-26 10:22:11 +02:00
import app.fedilab.fedilabtube.client.data.AccountData.Account;
import app.fedilab.fedilabtube.client.data.CaptionData.Caption;
2020-10-15 15:16:41 +02:00
import app.fedilab.fedilabtube.client.data.CommentData;
2020-09-26 10:22:11 +02:00
import app.fedilab.fedilabtube.client.data.CommentData.Comment;
2020-10-08 18:13:04 +02:00
import app.fedilab.fedilabtube.client.data.PlaylistData;
2020-09-25 18:58:04 +02:00
import app.fedilab.fedilabtube.client.data.VideoData;
import app.fedilab.fedilabtube.client.entities.File;
2020-10-01 17:42:03 +02:00
import app.fedilab.fedilabtube.client.entities.ItemStr;
2020-10-03 16:22:19 +02:00
import app.fedilab.fedilabtube.client.entities.PlaylistExist;
2020-09-26 10:22:11 +02:00
import app.fedilab.fedilabtube.client.entities.Report;
2020-10-15 15:16:41 +02:00
import app.fedilab.fedilabtube.databinding.ActivityPeertubeBinding;
2020-09-25 18:58:04 +02:00
import app.fedilab.fedilabtube.drawer.CommentListAdapter;
2020-06-26 14:34:39 +02:00
import app.fedilab.fedilabtube.helper.CacheDataSourceFactory;
2020-06-26 08:50:49 +02:00
import app.fedilab.fedilabtube.helper.FullScreenMediaController;
import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.sqlite.AccountDAO;
import app.fedilab.fedilabtube.sqlite.Sqlite;
2020-09-20 11:07:28 +02:00
import app.fedilab.fedilabtube.viewmodel.CaptionsVM;
2020-09-07 16:57:00 +02:00
import app.fedilab.fedilabtube.viewmodel.CommentVM;
import app.fedilab.fedilabtube.viewmodel.PlaylistsVM;
import app.fedilab.fedilabtube.viewmodel.PostActionsVM;
import app.fedilab.fedilabtube.viewmodel.SearchVM;
2020-09-27 16:33:43 +02:00
import app.fedilab.fedilabtube.viewmodel.TimelineVM;
2020-06-26 08:50:49 +02:00
import app.fedilab.fedilabtube.webview.CustomWebview;
import app.fedilab.fedilabtube.webview.MastalabWebChromeClient;
import app.fedilab.fedilabtube.webview.MastalabWebViewClient;
import es.dmoral.toasty.Toasty;
2020-09-26 16:46:51 +02:00
import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.ActionType.ADD_COMMENT;
import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.ActionType.RATEVIDEO;
2020-10-15 15:16:41 +02:00
import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.ActionType.REPLY;
2020-09-26 16:46:51 +02:00
import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.ActionType.REPORT_ACCOUNT;
import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.ActionType.REPORT_VIDEO;
2020-06-29 17:59:36 +02:00
import static app.fedilab.fedilabtube.helper.Helper.getAttColor;
2020-09-20 12:06:38 +02:00
import static app.fedilab.fedilabtube.helper.Helper.getLiveInstance;
2020-06-27 11:21:25 +02:00
import static app.fedilab.fedilabtube.helper.Helper.isLoggedIn;
2020-10-31 10:37:17 +01:00
import static com.google.android.exoplayer2.Player.MEDIA_ITEM_TRANSITION_REASON_AUTO;
2020-06-26 08:50:49 +02:00
2020-10-31 10:37:17 +01:00
public class PeertubeActivity extends AppCompatActivity implements CommentListAdapter.AllCommentRemoved, Player.EventListener {
2020-06-26 08:50:49 +02:00
public static String video_id;
2020-10-08 18:13:04 +02:00
private String peertubeInstance, videoUuid;
2020-06-26 08:50:49 +02:00
private FullScreenMediaController.fullscreen fullscreen;
2020-10-15 15:16:41 +02:00
private ImageView fullScreenIcon;
2020-06-26 08:50:49 +02:00
private SimpleExoPlayer player;
private boolean fullScreenMode;
private Dialog fullScreenDialog;
2020-10-15 15:16:41 +02:00
private VideoData.Video peertube;
2020-06-26 08:50:49 +02:00
private int mode;
2020-10-08 18:13:04 +02:00
private Map<String, List<PlaylistExist>> playlists;
private boolean playInMinimized, autoPlay, autoFullscreen;
private boolean onStopCalled;
2020-09-20 11:07:28 +02:00
private List<Caption> captions;
2020-10-02 18:22:19 +02:00
private String max_id;
private boolean flag_loading;
2020-10-08 17:16:42 +02:00
private boolean isMyVideo;
2020-10-02 18:22:19 +02:00
private List<Comment> comments;
2020-10-15 18:11:37 +02:00
private CommentListAdapter commentListAdapter;
private CommentListAdapter commentReplyListAdapter;
2020-10-09 18:22:05 +02:00
private boolean sepiaSearch;
2020-10-15 15:16:41 +02:00
private ActivityPeertubeBinding binding;
2020-10-15 18:11:37 +02:00
private List<Comment> commentsThread;
2020-10-16 17:25:52 +02:00
private BroadcastReceiver mPowerKeyReceiver = null;
2020-10-17 18:50:20 +02:00
private boolean isPlayInMinimized;
private Intent urlIntent;
2020-10-31 10:37:17 +01:00
public static List<String> playedVideos = new ArrayList<>();
private VideoData.Video nextVideo;
2020-10-16 17:25:52 +02:00
2020-06-26 08:50:49 +02:00
public static void hideKeyboard(Activity activity) {
if (activity != null && activity.getWindow() != null) {
activity.getWindow().getDecorView();
InputMethodManager imm = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
assert imm != null;
imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2020-10-15 15:16:41 +02:00
binding = ActivityPeertubeBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
2020-09-19 16:50:20 +02:00
2020-10-02 18:22:19 +02:00
max_id = "0";
urlIntent = null;
2020-09-19 16:50:20 +02:00
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
2020-10-01 17:42:03 +02:00
String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
2020-10-09 18:22:05 +02:00
if (Helper.isLoggedIn(PeertubeActivity.this) && !sepiaSearch) {
2020-10-02 15:14:08 +02:00
Account account = new AccountDAO(PeertubeActivity.this, db).getAccountByToken(token);
2020-10-15 15:16:41 +02:00
Helper.loadGiF(PeertubeActivity.this, account.getAvatar() != null ? account.getAvatar().getPath() : null, binding.myPp);
Helper.loadGiF(PeertubeActivity.this, account.getAvatar() != null ? account.getAvatar().getPath() : null, binding.myPpReply);
2020-10-02 15:14:08 +02:00
}
2020-06-26 08:50:49 +02:00
2020-07-22 10:55:59 +02:00
if (Helper.isTablet(PeertubeActivity.this)) {
2020-10-15 15:16:41 +02:00
2020-07-09 18:08:43 +02:00
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
0,
2.0f
);
2020-10-15 15:16:41 +02:00
binding.videoContainer.setLayoutParams(param);
2020-07-09 18:08:43 +02:00
}
2020-10-16 17:25:52 +02:00
isPlayInMinimized = false;
2020-09-19 16:50:20 +02:00
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mode = sharedpreferences.getInt(Helper.SET_VIDEO_MODE, Helper.VIDEO_MODE_NORMAL);
2020-10-26 14:38:51 +01:00
Intent intent = getIntent();
Bundle b = intent.getExtras();
2020-06-26 08:50:49 +02:00
if (b != null) {
2020-06-27 11:21:25 +02:00
peertubeInstance = b.getString("peertube_instance", Helper.getLiveInstance(PeertubeActivity.this));
2020-10-06 18:49:15 +02:00
videoUuid = b.getString("video_uuid", null);
2020-10-17 18:50:20 +02:00
isMyVideo = b.getBoolean("isMyVideo", false);
sepiaSearch = b.getBoolean("sepia_search", false);
peertube = b.getParcelable("video");
2020-06-26 08:50:49 +02:00
}
2020-06-27 11:21:25 +02:00
manageIntentUrl(intent);
2020-10-08 18:13:04 +02:00
2020-10-17 18:50:20 +02:00
if (!Helper.isLoggedIn(PeertubeActivity.this) || sepiaSearch) {
2020-10-15 19:07:58 +02:00
binding.writeCommentContainerReply.setVisibility(View.GONE);
binding.writeCommentContainer.setVisibility(View.GONE);
}
2020-09-19 16:50:20 +02:00
playInMinimized = sharedpreferences.getBoolean(getString(R.string.set_video_minimize_choice), true);
autoPlay = sharedpreferences.getBoolean(getString(R.string.set_autoplay_choice), true);
autoFullscreen = sharedpreferences.getBoolean(getString(R.string.set_fullscreen_choice), false);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O
|| !getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)) {
2020-09-19 16:50:20 +02:00
playInMinimized = false;
}
2020-10-07 09:25:58 +02:00
2020-06-26 08:50:49 +02:00
if (mode == Helper.VIDEO_MODE_WEBVIEW) {
2020-10-15 15:16:41 +02:00
binding.webviewVideo.setVisibility(View.VISIBLE);
binding.mediaVideo.setVisibility(View.GONE);
2020-10-29 18:56:57 +01:00
binding.doubleTapPlayerView.setVisibility(View.GONE);
2020-10-15 15:16:41 +02:00
CustomWebview webview_video = Helper.initializeWebview(PeertubeActivity.this, R.id.webview_video, null);
2020-06-26 08:50:49 +02:00
2020-10-15 15:16:41 +02:00
MastalabWebChromeClient mastalabWebChromeClient = new MastalabWebChromeClient(PeertubeActivity.this, webview_video, binding.mainMediaFrame, binding.videoLayout);
2020-06-26 08:50:49 +02:00
mastalabWebChromeClient.setOnToggledFullscreen(fullscreen -> {
if (fullscreen) {
2020-10-15 15:16:41 +02:00
binding.videoLayout.setVisibility(View.VISIBLE);
2020-06-26 08:50:49 +02:00
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(attrs);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
2020-10-15 15:16:41 +02:00
binding.peertubeInformationContainer.setVisibility(View.GONE);
2020-10-03 18:37:34 +02:00
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
2020-06-26 08:50:49 +02:00
} else {
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(attrs);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
2020-10-15 15:16:41 +02:00
binding.videoLayout.setVisibility(View.GONE);
binding.peertubeInformationContainer.setVisibility(View.VISIBLE);
2020-06-26 08:50:49 +02:00
}
});
2020-10-15 15:16:41 +02:00
binding.webviewVideo.getSettings().setAllowFileAccess(true);
binding.webviewVideo.setWebChromeClient(mastalabWebChromeClient);
binding.webviewVideo.getSettings().setDomStorageEnabled(true);
binding.webviewVideo.getSettings().setAppCacheEnabled(true);
binding.webviewVideo.getSettings().setMediaPlaybackRequiresUserGesture(false);
binding.webviewVideo.setWebViewClient(new MastalabWebViewClient(PeertubeActivity.this));
binding.webviewVideo.loadUrl("https://" + peertubeInstance + "/videos/embed/" + videoUuid);
2020-06-26 08:50:49 +02:00
} else {
2020-10-15 15:16:41 +02:00
binding.webviewVideo.setVisibility(View.GONE);
binding.loader.setVisibility(View.VISIBLE);
2020-06-26 08:50:49 +02:00
}
if (mode != Helper.VIDEO_MODE_WEBVIEW) {
2020-10-29 18:56:57 +01:00
binding.doubleTapPlayerView.setControllerShowTimeoutMs(1000);
binding.doubleTapPlayerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT);
2020-06-26 08:50:49 +02:00
initFullscreenDialog();
initFullscreenButton();
2020-10-29 18:56:57 +01:00
binding.doubleTapPlayerView
.setDoubleTapDelay(500);
binding.doubleTapPlayerView.setDoubleTapEnabled(true);
binding.mediaVideo.performListener(new YouTubeOverlay.PerformListener() {
@Override
public void onAnimationStart() {
binding.mediaVideo.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd() {
binding.mediaVideo.setVisibility(View.GONE);
}
}) .playerView(binding.doubleTapPlayerView).seekSeconds(10);
binding.doubleTapPlayerView.setPlayer(player);
binding.doubleTapPlayerView.controller(binding.mediaVideo);
if( player != null)
binding.mediaVideo.player(player);
2020-06-26 08:50:49 +02:00
}
2020-10-02 18:22:19 +02:00
flag_loading = true;
comments = new ArrayList<>();
2020-10-15 15:16:41 +02:00
2020-10-17 18:50:20 +02:00
binding.closeReply.setOnClickListener(v -> closeCommentThread());
2020-10-15 15:16:41 +02:00
2020-10-16 11:37:40 +02:00
commentListAdapter = new CommentListAdapter(comments, isMyVideo || Helper.isVideoOwner(PeertubeActivity.this, peertube));
2020-10-02 18:22:19 +02:00
commentListAdapter.allCommentRemoved = PeertubeActivity.this;
LinearLayoutManager mLayoutManager = new LinearLayoutManager(PeertubeActivity.this);
2020-10-15 15:16:41 +02:00
binding.peertubeComments.setLayoutManager(mLayoutManager);
binding.peertubeComments.setNestedScrollingEnabled(false);
binding.peertubeComments.setAdapter(commentListAdapter);
binding.peertubeComments.addOnScrollListener(new RecyclerView.OnScrollListener() {
2020-10-02 18:22:19 +02:00
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
if (dy > 0) {
int visibleItemCount = mLayoutManager.getChildCount();
int totalItemCount = mLayoutManager.getItemCount();
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
if (firstVisibleItem + visibleItemCount == totalItemCount) {
if (!flag_loading) {
CommentVM commentViewModel = new ViewModelProvider(PeertubeActivity.this).get(CommentVM.class);
2020-10-17 18:50:20 +02:00
commentViewModel.getThread(sepiaSearch ? peertubeInstance : null, videoUuid, max_id).observe(PeertubeActivity.this, apiresponse -> manageVIewComment(apiresponse));
2020-10-02 18:22:19 +02:00
}
}
}
}
});
2020-10-17 18:50:20 +02:00
if (peertube != null && sepiaSearch && peertube.getEmbedUrl() != null && Helper.isLoggedIn(PeertubeActivity.this)) {
SearchVM viewModelSearch = new ViewModelProvider(PeertubeActivity.this).get(SearchVM.class);
viewModelSearch.getVideos("0", peertube.getEmbedUrl()).observe(PeertubeActivity.this, this::manageVIewVideos);
2020-10-17 18:50:20 +02:00
} else {
playVideo();
}
2020-10-16 17:25:52 +02:00
registBroadcastReceiver();
2020-10-24 16:36:49 +02:00
if( autoFullscreen && autoPlay) {
openFullscreenDialog();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
2020-09-19 16:50:20 +02:00
}
2020-10-15 15:16:41 +02:00
private void manageVIewVideos(APIResponse apiResponse) {
2020-10-17 18:50:20 +02:00
if (apiResponse == null || apiResponse.getPeertubes() == null || apiResponse.getPeertubes().size() == 0) {
playVideo();
if(urlIntent != null) {
Helper.forwardToAnotherApp(PeertubeActivity.this, urlIntent);
urlIntent = null;
finish();
}
return;
}
peertube = apiResponse.getPeertubes().get(0);
sepiaSearch = false;
playVideo();
}
2020-10-02 18:22:19 +02:00
public void manageVIewComment(APIResponse apiResponse) {
flag_loading = false;
if (apiResponse == null || (apiResponse.getError() != null)) {
if (apiResponse == null)
Toasty.error(PeertubeActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
else
Toasty.error(PeertubeActivity.this, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
return;
}
int oldSize = comments.size();
int newComments = 0;
for (Comment comment : apiResponse.getComments()) {
if (comment.getText() != null && comment.getText().trim().length() > 0) {
comments.add(comment);
newComments++;
}
}
if (comments.size() > 0) {
2020-10-15 15:16:41 +02:00
binding.peertubeComments.setVisibility(View.VISIBLE);
2020-10-02 18:22:19 +02:00
commentListAdapter.notifyItemRangeInserted(oldSize, newComments);
}
}
2020-10-15 15:16:41 +02:00
public void manageVIewCommentReply(APIResponse apiResponse) {
if (apiResponse == null || apiResponse.getError() != null || apiResponse.getCommentThreadData() == null) {
if (apiResponse == null || apiResponse.getError() == null)
Toasty.error(PeertubeActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
else
Toasty.error(PeertubeActivity.this, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
return;
}
List<CommentData.CommentThreadData> commentThreadDataList = apiResponse.getCommentThreadData().getChildren();
2020-10-15 18:11:37 +02:00
commentsThread = generateCommentReply(commentThreadDataList, new ArrayList<>());
2020-10-15 18:37:50 +02:00
commentReplyListAdapter = new CommentListAdapter(commentsThread, Helper.isVideoOwner(PeertubeActivity.this, peertube));
LinearLayoutManager mLayoutManager = new LinearLayoutManager(PeertubeActivity.this);
binding.peertubeReply.setLayoutManager(mLayoutManager);
binding.peertubeReply.setNestedScrollingEnabled(false);
binding.peertubeReply.setAdapter(commentReplyListAdapter);
binding.peertubeReply.setVisibility(View.VISIBLE);
2020-10-15 18:11:37 +02:00
if (commentsThread.size() > 0) {
commentReplyListAdapter.notifyItemRangeInserted(0, commentsThread.size());
2020-10-15 15:16:41 +02:00
}
}
private List<Comment> generateCommentReply(List<CommentData.CommentThreadData> commentThreadDataList, List<Comment> comments) {
for (CommentData.CommentThreadData commentThreadData : commentThreadDataList) {
if (commentThreadData.getComment().getText() != null && commentThreadData.getComment().getText().trim().length() > 0) {
commentThreadData.getComment().setReply(true);
comments.add(commentThreadData.getComment());
}
2020-10-17 18:50:20 +02:00
if (commentThreadData.getChildren() != null && commentThreadData.getChildren().size() > 0) {
2020-10-15 15:16:41 +02:00
generateCommentReply(commentThreadData.getChildren(), comments);
}
}
return comments;
}
2020-09-19 16:50:20 +02:00
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle b = intent.getExtras();
if (b != null) {
peertubeInstance = b.getString("peertube_instance", Helper.getLiveInstance(PeertubeActivity.this));
2020-10-06 18:49:15 +02:00
videoUuid = b.getString("video_uuid", null);
2020-10-21 18:43:56 +02:00
if( comments != null && comments.size() > 0) {
int number = comments.size();
comments.clear();
commentListAdapter.notifyItemRangeRemoved(0, number);
}
2020-09-19 16:50:20 +02:00
playVideo();
2020-06-30 13:45:32 +02:00
}
manageIntentUrl(intent);
}
private void manageIntentUrl(Intent intent) {
if(intent.getData() != null) { //Comes from a link
String url = intent.getData().toString();
Pattern link = Pattern.compile("(https?://[\\da-z.-]+\\.[a-z.]{2,10})/videos/watch/(\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12})$");
Matcher matcherLink = link.matcher(url);
if (matcherLink.find()) {
2020-10-29 18:56:57 +01:00
String instance = matcherLink.group(1);
String uuid = matcherLink.group(2);
if(instance != null && uuid != null) {
peertubeInstance = instance.replace("https://", "").replace("http://", "");
sepiaSearch = true; // Sepia search flag is used because, at this time we don't know if the video is federated.
videoUuid = uuid;
peertube = new VideoData.Video();
peertube.setUuid(uuid);
peertube.setEmbedUrl(url);
urlIntent = intent;
SearchVM viewModelSearch = new ViewModelProvider(PeertubeActivity.this).get(SearchVM.class);
viewModelSearch.getVideos("0", peertube.getEmbedUrl()).observe(PeertubeActivity.this, this::manageVIewVideos);
}else {
Helper.forwardToAnotherApp(PeertubeActivity.this, intent);
finish();
}
}else{
Helper.forwardToAnotherApp(PeertubeActivity.this, intent);
finish();
}
}
2020-09-19 16:50:20 +02:00
}
private void playVideo() {
if (player != null) {
player.release();
2020-10-29 18:56:57 +01:00
player = new SimpleExoPlayer.Builder(PeertubeActivity.this).build();
binding.mediaVideo.player(player);
binding.doubleTapPlayerView.setPlayer(player);
2020-10-15 15:16:41 +02:00
binding.loader.setVisibility(View.GONE);
player.setPlayWhenReady(autoPlay);
2020-09-20 11:07:28 +02:00
captions = null;
2020-09-19 16:50:20 +02:00
}
2020-10-24 16:36:49 +02:00
if( autoFullscreen && autoPlay) {
fullscreen = FullScreenMediaController.fullscreen.ON;
setFullscreen(FullScreenMediaController.fullscreen.ON);
fullScreenMode = true;
openFullscreenDialog();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
fullscreen = FullScreenMediaController.fullscreen.OFF;
setFullscreen(FullScreenMediaController.fullscreen.OFF);
fullScreenMode = false;
}
2020-10-30 16:21:56 +01:00
updateHistory(0);
2020-10-15 15:16:41 +02:00
binding.peertubePlaylist.setVisibility(View.VISIBLE);
binding.peertubeBookmark.setVisibility(View.GONE);
2020-09-25 18:58:04 +02:00
TimelineVM feedsViewModel = new ViewModelProvider(PeertubeActivity.this).get(TimelineVM.class);
2020-10-17 18:50:20 +02:00
feedsViewModel.getVideo(sepiaSearch ? peertubeInstance : null, videoUuid, isMyVideo).observe(PeertubeActivity.this, this::manageVIewVideo);
2020-09-20 11:07:28 +02:00
CaptionsVM captionsViewModel = new ViewModelProvider(PeertubeActivity.this).get(CaptionsVM.class);
2020-10-17 18:50:20 +02:00
captionsViewModel.getCaptions(sepiaSearch ? peertubeInstance : null, videoUuid).observe(PeertubeActivity.this, this::manageCaptions);
2020-06-26 08:50:49 +02:00
}
public void change() {
if (fullscreen == FullScreenMediaController.fullscreen.ON) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Objects.requireNonNull(getSupportActionBar()).hide();
2020-10-15 15:16:41 +02:00
binding.peertubeInformationContainer.setVisibility(View.GONE);
2020-06-26 08:50:49 +02:00
} else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
Objects.requireNonNull(getSupportActionBar()).show();
2020-10-15 15:16:41 +02:00
binding.peertubeInformationContainer.setVisibility(View.VISIBLE);
2020-06-26 08:50:49 +02:00
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
2020-06-26 14:34:39 +02:00
if (item.getItemId() == android.R.id.home) {
2020-09-20 15:14:48 +02:00
if (playInMinimized && player != null) {
enterVideoMode();
} else {
finish();
}
2020-06-26 14:34:39 +02:00
return true;
2020-06-26 08:50:49 +02:00
}
2020-06-26 14:34:39 +02:00
return super.onOptionsItemSelected(item);
2020-06-26 08:50:49 +02:00
}
2020-09-26 10:22:11 +02:00
private void reportAlert(RetrofitPeertubeAPI.ActionType type, androidx.appcompat.app.AlertDialog alertDialog) {
2020-07-21 18:49:13 +02:00
androidx.appcompat.app.AlertDialog.Builder dialogBuilder = new androidx.appcompat.app.AlertDialog.Builder(PeertubeActivity.this);
LayoutInflater inflater1 = getLayoutInflater();
View dialogView = inflater1.inflate(R.layout.popup_report, new LinearLayout(PeertubeActivity.this), false);
dialogBuilder.setView(dialogView);
dialogBuilder.setNeutralButton(R.string.cancel, (dialog, id) -> dialog.dismiss());
2020-10-15 15:16:41 +02:00
EditText report_content = dialogView.findViewById(R.id.report_content);
2020-07-21 18:49:13 +02:00
dialogBuilder.setPositiveButton(R.string.report, (dialog, id) -> {
2020-07-22 10:55:59 +02:00
if (report_content.getText().toString().trim().length() == 0) {
Toasty.info(PeertubeActivity.this, getString(R.string.report_comment_size), Toasty.LENGTH_LONG).show();
} else {
2020-09-26 10:22:11 +02:00
PostActionsVM viewModel = new ViewModelProvider(PeertubeActivity.this).get(PostActionsVM.class);
if (type == RetrofitPeertubeAPI.ActionType.REPORT_VIDEO) {
Report report = new Report();
Report.VideoReport videoReport = new Report.VideoReport();
videoReport.setId(peertube.getId());
report.setVideo(videoReport);
report.setReason(report_content.getText().toString());
2020-10-17 18:50:20 +02:00
viewModel.report(report).observe(PeertubeActivity.this, apiResponse -> manageVIewPostActions(RetrofitPeertubeAPI.ActionType.REPORT_VIDEO, 0, apiResponse));
2020-07-22 10:55:59 +02:00
alertDialog.dismiss();
dialog.dismiss();
2020-09-26 10:22:11 +02:00
} else if (type == RetrofitPeertubeAPI.ActionType.REPORT_ACCOUNT) {
Report report = new Report();
Report.AccountReport accountReport = new Report.AccountReport();
accountReport.setId(peertube.getAccount().getId());
report.setAccount(accountReport);
report.setReason(report_content.getText().toString());
2020-10-17 18:50:20 +02:00
viewModel.report(report).observe(PeertubeActivity.this, apiResponse -> manageVIewPostActions(RetrofitPeertubeAPI.ActionType.REPORT_ACCOUNT, 0, apiResponse));
2020-07-22 10:55:59 +02:00
alertDialog.dismiss();
dialog.dismiss();
}
2020-07-21 18:49:13 +02:00
}
});
2020-07-22 10:55:59 +02:00
androidx.appcompat.app.AlertDialog alertDialog2 = dialogBuilder.create();
2020-07-21 18:49:13 +02:00
alertDialog2.show();
}
2020-06-26 08:50:49 +02:00
public FullScreenMediaController.fullscreen getFullscreen() {
return fullscreen;
}
public void setFullscreen(FullScreenMediaController.fullscreen fullscreen) {
this.fullscreen = fullscreen;
}
2020-09-20 11:07:28 +02:00
public void manageCaptions(APIResponse apiResponse) {
if (apiResponse == null || (apiResponse.getError() != null) || apiResponse.getCaptions() == null || apiResponse.getCaptions().size() == 0) {
return;
}
captions = apiResponse.getCaptions();
}
2020-10-31 10:37:17 +01:00
public void manageNextVideos(APIResponse apiResponse) {
if (apiResponse == null || apiResponse.getError() != null || apiResponse.getPeertubes() == null || apiResponse.getPeertubes().size() == 0) {
return;
}
List<VideoData.Video> suggestedVideos = apiResponse.getPeertubes();
for(VideoData.Video video: suggestedVideos) {
if(!playedVideos.contains(video.getId())){
TimelineVM feedsViewModel = new ViewModelProvider(PeertubeActivity.this).get(TimelineVM.class);
feedsViewModel.getVideo(null, suggestedVideos.get(0).getUuid(), false).observe(PeertubeActivity.this, this::nextVideoDetails);
return;
}
}
}
@SuppressLint("ClickableViewAccessibility")
public void nextVideoDetails(APIResponse apiResponse) {
if (apiResponse == null || (apiResponse.getError() != null) || apiResponse.getPeertubes() == null || apiResponse.getPeertubes().size() == 0) {
return;
}
int i = 0;
while (i < (apiResponse.getPeertubes().size()-1) && playedVideos.contains(apiResponse.getPeertubes().get(i).getId())) {
i++;
}
nextVideo = apiResponse.getPeertubes().get(i);
MediaItem mediaItem = new MediaItem.Builder().setUri(Uri.parse(nextVideo.getFileUrl(null, PeertubeActivity.this))).build();
player.addMediaItem(mediaItem);
}
2020-10-13 18:11:53 +02:00
@SuppressLint("ClickableViewAccessibility")
2020-09-07 16:57:00 +02:00
public void manageVIewVideo(APIResponse apiResponse) {
2020-06-26 08:50:49 +02:00
if (apiResponse == null || (apiResponse.getError() != null) || apiResponse.getPeertubes() == null || apiResponse.getPeertubes().size() == 0) {
Toasty.error(PeertubeActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
2020-10-15 15:16:41 +02:00
binding.loader.setVisibility(View.GONE);
2020-06-26 08:50:49 +02:00
return;
}
2020-09-03 19:08:53 +02:00
if (apiResponse.getPeertubes() == null || apiResponse.getPeertubes().get(0) == null || apiResponse.getPeertubes().get(0).getFileUrl(null, PeertubeActivity.this) == null) {
2020-06-26 08:50:49 +02:00
Toasty.error(PeertubeActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
2020-10-15 15:16:41 +02:00
binding.loader.setVisibility(View.GONE);
2020-06-26 08:50:49 +02:00
return;
}
2020-09-19 16:50:20 +02:00
2020-06-26 08:50:49 +02:00
peertube = apiResponse.getPeertubes().get(0);
2020-10-31 10:37:17 +01:00
if( peertube.getTags() != null && peertube.getTags().size() > 0) {
SearchVM searchViewModel = new ViewModelProvider(PeertubeActivity.this).get(SearchVM.class);
searchViewModel.searchNextVideos(peertube.getTags()).observe(PeertubeActivity.this, this::manageNextVideos);
}
2020-10-17 18:50:20 +02:00
if (sepiaSearch) {
2020-10-09 18:22:05 +02:00
peertubeInstance = peertube.getAccount().getHost();
}
2020-10-08 18:13:04 +02:00
List<String> videoIds = new ArrayList<>();
videoIds.add(peertube.getId());
PlaylistsVM viewModel = new ViewModelProvider(this).get(PlaylistsVM.class);
viewModel.videoExists(videoIds).observe(this, this::manageVIewPlaylist);
2020-10-17 18:50:20 +02:00
if (!Helper.isLoggedIn(PeertubeActivity.this) || sepiaSearch) {
2020-10-15 18:11:37 +02:00
binding.writeCommentContainer.setVisibility(View.GONE);
binding.writeCommentContainerReply.setVisibility(View.GONE);
}
2020-10-15 15:16:41 +02:00
binding.send.setOnClickListener(v -> {
2020-10-09 18:22:05 +02:00
if (isLoggedIn(PeertubeActivity.this) && !sepiaSearch) {
2020-10-17 18:50:20 +02:00
String comment = binding.addCommentWrite.getText().toString();
2020-06-27 19:08:52 +02:00
if (comment.trim().length() > 0) {
2020-10-08 18:13:04 +02:00
PostActionsVM viewModelComment = new ViewModelProvider(PeertubeActivity.this).get(PostActionsVM.class);
2020-10-15 18:11:37 +02:00
viewModelComment.comment(ADD_COMMENT, peertube.getId(), null, comment).observe(PeertubeActivity.this, apiResponse1 -> manageVIewPostActions(ADD_COMMENT, 0, apiResponse1));
2020-10-17 18:50:20 +02:00
binding.addCommentWrite.setText("");
2020-06-27 11:21:25 +02:00
}
2020-06-27 19:08:52 +02:00
} else {
2020-10-17 18:50:20 +02:00
if (sepiaSearch) {
Toasty.info(PeertubeActivity.this, getString(R.string.federation_issue), Toasty.LENGTH_SHORT).show();
2020-10-17 18:50:20 +02:00
} else {
Toasty.error(PeertubeActivity.this, getString(R.string.not_logged_in), Toast.LENGTH_SHORT).show();
}
2020-06-27 19:08:52 +02:00
}
});
2020-06-27 11:21:25 +02:00
2020-10-15 15:16:41 +02:00
binding.peertubePlaylist.setOnClickListener(v -> {
2020-10-08 18:13:04 +02:00
PlaylistsVM viewModelOwnerPlaylist = new ViewModelProvider(PeertubeActivity.this).get(PlaylistsVM.class);
viewModelOwnerPlaylist.manage(PlaylistsVM.action.GET_PLAYLISTS, null, null).observe(PeertubeActivity.this, this::manageVIewPlaylists);
2020-06-26 08:50:49 +02:00
});
if (peertube.isCommentsEnabled()) {
2020-09-07 16:57:00 +02:00
CommentVM commentViewModel = new ViewModelProvider(PeertubeActivity.this).get(CommentVM.class);
2020-10-17 18:50:20 +02:00
commentViewModel.getThread(sepiaSearch ? peertubeInstance : null, videoUuid, max_id).observe(PeertubeActivity.this, this::manageVIewComment);
if (Helper.isLoggedIn(PeertubeActivity.this) && !sepiaSearch) {
2020-10-16 11:37:40 +02:00
binding.writeCommentContainerReply.setVisibility(View.VISIBLE);
binding.writeCommentContainer.setVisibility(View.VISIBLE);
}
2020-10-21 18:43:56 +02:00
binding.peertubeComments.setVisibility(View.VISIBLE);
2020-06-26 08:50:49 +02:00
} else {
2020-10-21 18:43:56 +02:00
binding.peertubeComments.setVisibility(View.GONE);
2020-10-15 19:07:58 +02:00
binding.writeCommentContainerReply.setVisibility(View.GONE);
binding.writeCommentContainer.setVisibility(View.GONE);
2020-10-15 15:16:41 +02:00
binding.noActionText.setText(getString(R.string.comment_no_allowed_peertube));
binding.noAction.setVisibility(View.VISIBLE);
binding.writeCommentContainer.setVisibility(View.GONE);
2020-06-26 08:50:49 +02:00
}
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
2020-06-27 11:21:25 +02:00
setTitle(peertube.getName());
2020-10-13 18:11:53 +02:00
2020-10-15 15:16:41 +02:00
binding.peertubeDescription.setText(peertube.getDescription());
2020-10-13 18:11:53 +02:00
2020-10-15 15:16:41 +02:00
binding.peertubeTitle.setText(peertube.getName());
2020-10-16 11:37:40 +02:00
binding.peertubeDislikeCount.setText(Helper.withSuffix(peertube.getDislikes()));
binding.peertubeLikeCount.setText(Helper.withSuffix(peertube.getLikes()));
binding.peertubeViewCount.setText(Helper.withSuffix(peertube.getViews()));
2020-06-26 08:50:49 +02:00
video_id = peertube.getId();
changeColor();
initResolution();
2020-10-15 15:16:41 +02:00
binding.peertubeLikeCount.setOnClickListener(v -> {
2020-10-09 18:22:05 +02:00
if (isLoggedIn(PeertubeActivity.this) && !sepiaSearch) {
2020-06-27 11:21:25 +02:00
String newState = peertube.getMyRating().equals("like") ? "none" : "like";
2020-10-08 18:13:04 +02:00
PostActionsVM viewModelLike = new ViewModelProvider(PeertubeActivity.this).get(PostActionsVM.class);
2020-10-15 18:11:37 +02:00
viewModelLike.post(RATEVIDEO, peertube.getId(), newState).observe(PeertubeActivity.this, apiResponse1 -> manageVIewPostActions(RATEVIDEO, 0, apiResponse1));
2020-06-27 11:21:25 +02:00
peertube.setMyRating(newState);
2020-10-15 15:16:41 +02:00
int count = Integer.parseInt(binding.peertubeLikeCount.getText().toString());
2020-06-30 13:33:43 +02:00
if (newState.compareTo("none") == 0) {
2020-06-29 17:59:36 +02:00
count--;
2020-06-30 13:33:43 +02:00
if (count - 1 < 0) {
2020-06-29 17:59:36 +02:00
count = 0;
}
2020-06-30 13:33:43 +02:00
} else {
2020-06-29 17:59:36 +02:00
count++;
}
2020-10-15 15:16:41 +02:00
binding.peertubeLikeCount.setText(String.valueOf(count));
2020-06-27 11:21:25 +02:00
changeColor();
2020-06-27 19:08:52 +02:00
} else {
2020-10-17 18:50:20 +02:00
if (sepiaSearch) {
Toasty.info(PeertubeActivity.this, getString(R.string.federation_issue), Toasty.LENGTH_SHORT).show();
2020-10-17 18:50:20 +02:00
} else {
Toasty.error(PeertubeActivity.this, getString(R.string.not_logged_in), Toast.LENGTH_SHORT).show();
}
2020-06-27 11:21:25 +02:00
}
2020-06-26 08:50:49 +02:00
});
2020-10-15 15:16:41 +02:00
binding.peertubeDislikeCount.setOnClickListener(v -> {
2020-10-09 18:22:05 +02:00
if (isLoggedIn(PeertubeActivity.this) && !sepiaSearch) {
2020-06-27 11:21:25 +02:00
String newState = peertube.getMyRating().equals("dislike") ? "none" : "dislike";
2020-10-08 18:13:04 +02:00
PostActionsVM viewModelLike = new ViewModelProvider(PeertubeActivity.this).get(PostActionsVM.class);
2020-10-17 18:50:20 +02:00
viewModelLike.post(RATEVIDEO, peertube.getId(), newState).observe(PeertubeActivity.this, apiResponse1 -> manageVIewPostActions(RATEVIDEO, 0, apiResponse1));
2020-06-27 11:21:25 +02:00
peertube.setMyRating(newState);
2020-10-15 15:16:41 +02:00
int count = Integer.parseInt(binding.peertubeDislikeCount.getText().toString());
2020-06-30 13:33:43 +02:00
if (newState.compareTo("none") == 0) {
2020-06-29 17:59:36 +02:00
count--;
2020-06-30 13:33:43 +02:00
if (count - 1 < 0) {
2020-06-29 17:59:36 +02:00
count = 0;
}
2020-06-30 13:33:43 +02:00
} else {
2020-06-29 17:59:36 +02:00
count++;
}
2020-10-15 15:16:41 +02:00
binding.peertubeDislikeCount.setText(String.valueOf(count));
2020-06-27 11:21:25 +02:00
changeColor();
2020-06-27 19:08:52 +02:00
} else {
2020-10-17 18:50:20 +02:00
if (sepiaSearch) {
Toasty.info(PeertubeActivity.this, getString(R.string.federation_issue), Toasty.LENGTH_SHORT).show();
2020-10-17 18:50:20 +02:00
} else {
Toasty.error(PeertubeActivity.this, getString(R.string.not_logged_in), Toast.LENGTH_SHORT).show();
}
2020-06-27 11:21:25 +02:00
}
2020-06-26 08:50:49 +02:00
});
2020-06-27 11:21:25 +02:00
2020-09-26 10:22:11 +02:00
2020-09-03 19:08:53 +02:00
if (mode == Helper.VIDEO_MODE_NORMAL) {
2020-06-26 08:50:49 +02:00
int video_cache = sharedpreferences.getInt(Helper.SET_VIDEO_CACHE, Helper.DEFAULT_VIDEO_CACHE_MB);
ProgressiveMediaSource videoSource;
2020-10-29 18:56:57 +01:00
player = new SimpleExoPlayer.Builder(PeertubeActivity.this).build();
2020-10-31 10:37:17 +01:00
player.addListener(this);
2020-10-29 18:56:57 +01:00
binding.mediaVideo.player(player);
binding.doubleTapPlayerView.setPlayer(player);
2020-10-15 15:16:41 +02:00
binding.loader.setVisibility(View.GONE);
2020-10-06 17:11:25 +02:00
2020-10-06 18:49:15 +02:00
if (apiResponse.getPeertubes().get(0).getFiles() != null && apiResponse.getPeertubes().get(0).getFiles().size() > 0) {
2020-10-06 17:11:25 +02:00
if (video_cache == 0) {
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(PeertubeActivity.this,
Util.getUserAgent(PeertubeActivity.this, null), null);
2020-10-29 18:56:57 +01:00
MediaItem mediaItem = new MediaItem.Builder().setUri(Uri.parse(apiResponse.getPeertubes().get(0).getFileUrl(null, PeertubeActivity.this))).build();
videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mediaItem);
2020-10-06 17:11:25 +02:00
} else {
CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(PeertubeActivity.this);
2020-10-29 18:56:57 +01:00
MediaItem mediaItem = new MediaItem.Builder().setUri(Uri.parse(apiResponse.getPeertubes().get(0).getFileUrl(null, PeertubeActivity.this))).build();
2020-10-06 17:11:25 +02:00
videoSource = new ProgressiveMediaSource.Factory(cacheDataSourceFactory)
2020-10-29 18:56:57 +01:00
.createMediaSource(mediaItem);
2020-10-06 17:11:25 +02:00
}
2020-10-29 18:56:57 +01:00
player.setMediaSource(videoSource);
2020-10-06 18:49:15 +02:00
} else {
2020-10-29 18:56:57 +01:00
MediaItem mediaItem = new MediaItem.Builder().setUri(Uri.parse(apiResponse.getPeertubes().get(0).getStreamingPlaylists().get(0).getPlaylistUrl())).build();
2020-10-06 17:11:25 +02:00
HlsMediaSource hlsMediaSource = new HlsMediaSource.Factory(new DefaultHttpDataSourceFactory(System.getProperty("http.agent")))
2020-10-29 18:56:57 +01:00
.createMediaSource(mediaItem);
player.setMediaSource(hlsMediaSource);
2020-10-06 17:11:25 +02:00
}
2020-10-29 18:56:57 +01:00
player.prepare();
player.setPlayWhenReady(autoPlay);
2020-06-26 08:50:49 +02:00
}
2020-10-15 15:16:41 +02:00
binding.moreActions.setOnClickListener(view -> {
PopupMenu popup = new PopupMenu(PeertubeActivity.this, binding.moreActions);
2020-09-20 11:07:28 +02:00
popup.getMenuInflater()
.inflate(R.menu.main_video, popup.getMenu());
if (captions == null) {
popup.getMenu().findItem(R.id.action_captions).setEnabled(false);
2020-06-26 08:50:49 +02:00
}
2020-10-17 18:50:20 +02:00
if (!isMyVideo) {
popup.getMenu().findItem(R.id.action_edit).setVisible(false);
}
2020-09-20 11:07:28 +02:00
popup.setOnMenuItemClickListener(item -> {
2020-10-14 10:31:02 +02:00
int itemId = item.getItemId();
if (itemId == R.id.action_download) {
if (Build.VERSION.SDK_INT >= 23) {
if (ContextCompat.checkSelfPermission(PeertubeActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(PeertubeActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(PeertubeActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, Helper.EXTERNAL_STORAGE_REQUEST_CODE);
2020-09-20 11:07:28 +02:00
} else {
2020-09-25 18:58:04 +02:00
Helper.manageDownloads(PeertubeActivity.this, peertube.getFileDownloadUrl(null));
2020-09-20 11:07:28 +02:00
}
2020-10-14 10:31:02 +02:00
} else {
Helper.manageDownloads(PeertubeActivity.this, peertube.getFileDownloadUrl(null));
}
} else if (itemId == R.id.action_share) {
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.shared_via));
String url;
url = peertube.getFiles().get(0).getFileDownloadUrl();
boolean share_details = sharedpreferences.getBoolean(Helper.SET_SHARE_DETAILS, true);
String extra_text;
if (share_details) {
extra_text = "@" + peertube.getAccount().getAcct();
extra_text += "\r\n\r\n" + peertube.getName();
extra_text += "\n\n\uD83D\uDD17 " + url + "\r\n-\n";
final String contentToot;
if (peertube.getDescription() != null) {
2020-09-20 11:07:28 +02:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
contentToot = Html.fromHtml(peertube.getDescription(), Html.FROM_HTML_MODE_LEGACY).toString();
else
contentToot = Html.fromHtml(peertube.getDescription()).toString();
} else {
2020-10-14 10:31:02 +02:00
contentToot = "";
2020-09-20 11:07:28 +02:00
}
2020-10-14 10:31:02 +02:00
extra_text += contentToot;
} else {
extra_text = url;
}
sendIntent.putExtra(Intent.EXTRA_TEXT, extra_text);
sendIntent.setType("text/plain");
2020-10-17 18:50:20 +02:00
try {
startActivity(Intent.createChooser(sendIntent, getString(R.string.share_with)));
} catch (Exception e) {
Toasty.error(PeertubeActivity.this, getString(R.string.toast_error), Toasty.LENGTH_LONG).show();
}
} else if (itemId == R.id.action_edit) {
Intent intent = new Intent(PeertubeActivity.this, PeertubeEditUploadActivity.class);
Bundle b = new Bundle();
b.putString("video_id", peertube.getUuid());
intent.putExtras(b);
startActivity(intent);
2020-10-14 10:31:02 +02:00
} else if (itemId == R.id.action_captions) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(PeertubeActivity.this);
if (captions == null) {
return true;
}
String[] itemsKeyLanguage = new String[captions.size() + 1];
String[] itemsLabelLanguage = new String[captions.size() + 1];
itemsLabelLanguage[0] = getString(R.string.none);
itemsKeyLanguage[0] = "null";
int i = 1;
if (captions.size() > 0) {
for (Caption caption : captions) {
ItemStr lang = caption.getLanguage();
itemsLabelLanguage[i] = lang.getLabel();
itemsKeyLanguage[i] = lang.getId();
i++;
2020-09-20 11:07:28 +02:00
}
2020-10-14 10:31:02 +02:00
}
dialogBuilder.setSingleChoiceItems(itemsLabelLanguage, i, (dialog, which) -> {
2020-09-20 11:07:28 +02:00
2020-10-14 10:31:02 +02:00
Uri uri = null;
if (which > 0) {
if (!sepiaSearch) {
uri = Uri.parse("https://" + getLiveInstance(PeertubeActivity.this) + captions.get(which - 1).getCaptionPath());
} else {
uri = Uri.parse("https://" + peertubeInstance + captions.get(which - 1).getCaptionPath());
2020-09-20 11:07:28 +02:00
}
}
2020-09-20 12:16:10 +02:00
2020-10-14 10:31:02 +02:00
int video_cache = sharedpreferences.getInt(Helper.SET_VIDEO_CACHE, Helper.DEFAULT_VIDEO_CACHE_MB);
2020-09-20 12:16:10 +02:00
2020-10-14 10:31:02 +02:00
long position = player.getCurrentPosition();
2020-09-20 12:06:38 +02:00
2020-10-14 10:31:02 +02:00
if (player != null)
player.release();
2020-09-20 11:07:28 +02:00
2020-10-29 18:56:57 +01:00
TrackSelector trackSelector = new DefaultTrackSelector(PeertubeActivity.this, new AdaptiveTrackSelection.Factory());
player = new SimpleExoPlayer.Builder(PeertubeActivity.this).setTrackSelector(trackSelector).build();
binding.mediaVideo.player(player);
2020-10-14 10:31:02 +02:00
ProgressiveMediaSource videoSource;
MediaSource subtitleSource = null;
if (apiResponse.getPeertubes().get(0).getFiles() != null && apiResponse.getPeertubes().get(0).getFiles().size() > 0) {
if (video_cache == 0) {
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(PeertubeActivity.this,
Util.getUserAgent(PeertubeActivity.this, null), null);
2020-10-29 18:56:57 +01:00
MediaItem mediaItem = new MediaItem.Builder().setUri(Uri.parse(apiResponse.getPeertubes().get(0).getFileUrl(null, PeertubeActivity.this))).build();
2020-10-14 10:31:02 +02:00
videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
2020-10-29 18:56:57 +01:00
.createMediaSource(mediaItem);
if (uri != null) {
MediaItem.Subtitle mediaSubtitle = new MediaItem.Subtitle(uri, MimeTypes.TEXT_VTT, itemsKeyLanguage[which]);
subtitleSource = new SingleSampleMediaSource.Factory(dataSourceFactory).createMediaSource(mediaSubtitle, C.TIME_UNSET);
}
2020-10-14 10:31:02 +02:00
} else {
CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(PeertubeActivity.this);
2020-10-29 18:56:57 +01:00
MediaItem mediaItem = new MediaItem.Builder().setUri(Uri.parse(apiResponse.getPeertubes().get(0).getFileUrl(null, PeertubeActivity.this))).build();
2020-10-14 10:31:02 +02:00
videoSource = new ProgressiveMediaSource.Factory(cacheDataSourceFactory)
2020-10-29 18:56:57 +01:00
.createMediaSource(mediaItem);
if (uri != null) {
MediaItem.Subtitle mediaSubtitle = new MediaItem.Subtitle(uri, MimeTypes.TEXT_VTT, itemsKeyLanguage[which], Format.NO_VALUE);
subtitleSource = new SingleSampleMediaSource.Factory(cacheDataSourceFactory).createMediaSource(mediaSubtitle, C.TIME_UNSET);
}
2020-10-14 10:31:02 +02:00
}
2020-10-29 18:56:57 +01:00
binding.doubleTapPlayerView.setPlayer(player);
2020-10-14 10:31:02 +02:00
if (which > 0 && subtitleSource != null) {
MergingMediaSource mergedSource =
new MergingMediaSource(videoSource, subtitleSource);
2020-10-29 18:56:57 +01:00
player.setMediaSource(mergedSource);
2020-10-14 10:31:02 +02:00
}
2020-10-29 18:56:57 +01:00
player.prepare();
2020-10-14 10:31:02 +02:00
player.seekTo(0, position);
player.setPlayWhenReady(true);
}
2020-09-20 11:07:28 +02:00
2020-10-13 17:50:52 +02:00
2020-10-14 10:31:02 +02:00
dialog.dismiss();
});
dialogBuilder.setOnDismissListener(dialogInterface -> {
});
dialogBuilder.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss());
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.setTitle(getString(R.string.pickup_captions));
alertDialog.show();
} else if (itemId == R.id.action_report) {
AlertDialog alertDialog;
AlertDialog.Builder dialogBuilder;
dialogBuilder = new AlertDialog.Builder(PeertubeActivity.this);
LayoutInflater inflater1 = getLayoutInflater();
View dialogView = inflater1.inflate(R.layout.popup_report_choice, new LinearLayout(PeertubeActivity.this), false);
dialogBuilder.setView(dialogView);
2020-10-15 15:16:41 +02:00
2020-10-14 10:31:02 +02:00
dialogBuilder.setNeutralButton(R.string.cancel, (dialog, id) -> dialog.dismiss());
alertDialog = dialogBuilder.create();
alertDialog.show();
2020-10-15 15:16:41 +02:00
dialogView.findViewById(R.id.report_video).setOnClickListener(v -> reportAlert(REPORT_VIDEO, alertDialog));
dialogView.findViewById(R.id.report_account).setOnClickListener(v -> reportAlert(REPORT_ACCOUNT, alertDialog));
2020-09-20 11:07:28 +02:00
}
return true;
});
popup.show();
2020-06-26 08:50:49 +02:00
});
2020-09-20 11:07:28 +02:00
2020-06-26 08:50:49 +02:00
}
@Override
public void onConfigurationChanged(@NotNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
if (mode != Helper.VIDEO_MODE_WEBVIEW) {
openFullscreenDialog();
}
setFullscreen(FullScreenMediaController.fullscreen.ON);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
if (mode != Helper.VIDEO_MODE_WEBVIEW) {
closeFullscreenDialog();
}
setFullscreen(FullScreenMediaController.fullscreen.OFF);
}
change();
}
2020-09-20 12:06:38 +02:00
2020-06-26 08:50:49 +02:00
@Override
public void onDestroy() {
super.onDestroy();
2020-10-15 15:16:41 +02:00
binding = null;
if (player != null) {
2020-06-26 08:50:49 +02:00
player.release();
}
2020-10-16 17:25:52 +02:00
unregisterReceiver();
2020-09-19 15:06:24 +02:00
if (fullScreenDialog != null && fullScreenDialog.isShowing()) {
fullScreenDialog.dismiss();
}
2020-06-26 08:50:49 +02:00
}
@Override
protected void onPause() {
super.onPause();
2020-10-30 16:21:56 +01:00
if( player != null) {
updateHistory(player.getCurrentPosition()/1000);
}
2020-10-16 17:25:52 +02:00
if (player != null && !isPlayInMinimized) {
2020-06-26 08:50:49 +02:00
player.setPlayWhenReady(false);
}
}
2020-10-16 17:25:52 +02:00
private void registBroadcastReceiver() {
final IntentFilter theFilter = new IntentFilter();
theFilter.addAction(Intent.ACTION_SCREEN_ON);
theFilter.addAction(Intent.ACTION_SCREEN_OFF);
mPowerKeyReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String strAction = intent.getAction();
2020-10-16 17:41:07 +02:00
if (strAction.equals(Intent.ACTION_SCREEN_OFF)) {
2020-10-16 17:25:52 +02:00
if (player != null && isPlayInMinimized) {
2020-10-16 17:41:07 +02:00
player.setPlayWhenReady(false);
2020-10-16 17:25:52 +02:00
}
}
}
};
getApplicationContext().registerReceiver(mPowerKeyReceiver, theFilter);
}
private void unregisterReceiver() {
int apiLevel = Build.VERSION.SDK_INT;
if (apiLevel >= 7) {
try {
getApplicationContext().unregisterReceiver(mPowerKeyReceiver);
2020-10-17 18:50:20 +02:00
} catch (IllegalArgumentException e) {
2020-10-16 17:25:52 +02:00
mPowerKeyReceiver = null;
}
2020-10-17 18:50:20 +02:00
} else {
2020-10-16 17:25:52 +02:00
getApplicationContext().unregisterReceiver(mPowerKeyReceiver);
mPowerKeyReceiver = null;
}
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onUserLeaveHint() {
2020-09-19 16:50:20 +02:00
enterVideoMode();
}
private void enterVideoMode() {
if (playInMinimized && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && player != null) {
2020-10-16 17:25:52 +02:00
isPlayInMinimized = true;
2020-09-19 18:19:59 +02:00
MediaSessionCompat mediaSession = new MediaSessionCompat(this, getPackageName());
MediaSessionConnector mediaSessionConnector = new MediaSessionConnector(mediaSession);
mediaSessionConnector.setPlayer(player);
2020-10-29 18:56:57 +01:00
PlayerControlView controlView = binding.doubleTapPlayerView.findViewById(R.id.exo_controller);
2020-10-13 16:35:51 +02:00
controlView.hide();
2020-10-29 18:56:57 +01:00
binding.doubleTapPlayerView.setControllerAutoShow(false);
2020-09-19 18:19:59 +02:00
mediaSession.setActive(true);
PictureInPictureParams params = new PictureInPictureParams.Builder().build();
enterPictureInPictureMode(params);
}
}
2020-09-19 16:50:20 +02:00
@Override
public void onBackPressed() {
2020-10-17 18:50:20 +02:00
if (binding.replyThread.getVisibility() == View.VISIBLE) {
2020-10-15 18:11:37 +02:00
closeCommentThread();
2020-10-17 18:50:20 +02:00
} else {
2020-10-15 18:11:37 +02:00
if (playInMinimized && player != null) {
enterVideoMode();
} else {
super.onBackPressed();
}
2020-09-19 16:50:20 +02:00
}
}
@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
if (isInPictureInPictureMode) {
setFullscreen(FullScreenMediaController.fullscreen.ON);
} else {
setFullscreen(FullScreenMediaController.fullscreen.OFF);
if (onStopCalled) {
2020-10-16 17:25:52 +02:00
isPlayInMinimized = false;
2020-09-20 15:14:48 +02:00
finishAndRemoveTask();
}
}
}
@Override
public void onStop() {
super.onStop();
onStopCalled = true;
}
2020-06-26 08:50:49 +02:00
@Override
public void onResume() {
super.onResume();
onStopCalled = false;
2020-10-16 17:25:52 +02:00
if (player != null && !player.isPlaying()) {
player.setPlayWhenReady(autoPlay);
2020-06-26 08:50:49 +02:00
}
}
public void displayResolution() {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
2020-06-26 14:34:39 +02:00
AlertDialog.Builder builderSingle = new AlertDialog.Builder(PeertubeActivity.this);
2020-06-26 08:50:49 +02:00
builderSingle.setTitle(R.string.pickup_resolution);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(PeertubeActivity.this, android.R.layout.select_dialog_item);
2020-09-25 18:58:04 +02:00
for (File file : peertube.getFiles()) {
if (file.getResolutions() != null) {
2020-10-03 18:37:34 +02:00
if (file.getResolutions().getLabel().compareTo("0p") != 0) {
2020-10-02 07:03:36 +02:00
arrayAdapter.add(file.getResolutions().getLabel());
}
2020-09-25 18:58:04 +02:00
}
}
2020-06-26 08:50:49 +02:00
builderSingle.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
builderSingle.setAdapter(arrayAdapter, (dialog, which) -> {
String res = Objects.requireNonNull(arrayAdapter.getItem(which)).substring(0, Objects.requireNonNull(arrayAdapter.getItem(which)).length() - 1);
2020-10-29 18:56:57 +01:00
binding.loader.setVisibility(View.VISIBLE);
long position = player.getCurrentPosition();
PlayerControlView controlView = binding.doubleTapPlayerView.findViewById(R.id.exo_controller);
TextView resolution = controlView.findViewById(R.id.resolution);
resolution.setText(String.format("%sp", res));
if (mode == Helper.VIDEO_MODE_NORMAL) {
if (player != null)
player.release();
player = new SimpleExoPlayer.Builder(PeertubeActivity.this).build();
binding.mediaVideo.player(player);
binding.doubleTapPlayerView.setPlayer(player);
binding.loader.setVisibility(View.GONE);
int video_cache = sharedpreferences.getInt(Helper.SET_VIDEO_CACHE, Helper.DEFAULT_VIDEO_CACHE_MB);
ProgressiveMediaSource videoSource;
if (video_cache == 0) {
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(PeertubeActivity.this,
Util.getUserAgent(PeertubeActivity.this, null), null);
MediaItem mediaItem = new MediaItem.Builder().setUri(Uri.parse(peertube.getFileUrl(res, PeertubeActivity.this))).build();
videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(mediaItem);
} else {
CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(PeertubeActivity.this);
MediaItem mediaItem = new MediaItem.Builder().setUri(Uri.parse(peertube.getFileUrl(res, PeertubeActivity.this))).build();
videoSource = new ProgressiveMediaSource.Factory(cacheDataSourceFactory)
.createMediaSource(mediaItem);
2020-06-26 08:50:49 +02:00
}
2020-10-29 18:56:57 +01:00
player.setMediaSource(videoSource);
player.prepare();
player.seekTo(0, position);
player.setPlayWhenReady(true);
2020-06-26 08:50:49 +02:00
}
});
builderSingle.show();
}
2020-09-07 16:57:00 +02:00
2020-06-26 08:50:49 +02:00
private void initFullscreenDialog() {
fullScreenDialog = new Dialog(this, android.R.style.Theme_Black_NoTitleBar_Fullscreen) {
public void onBackPressed() {
if( player != null && player.isPlaying() && fullScreenMode) {
player.setPlayWhenReady(false);
}
if( fullScreenMode) {
2020-06-26 08:50:49 +02:00
closeFullscreenDialog();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Handler handler = new Handler();
handler.postDelayed(() -> setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR), 2000);
2020-10-02 15:14:08 +02:00
}
2020-06-26 08:50:49 +02:00
super.onBackPressed();
}
};
}
private void openFullscreenDialog() {
2020-10-29 18:56:57 +01:00
((ViewGroup) binding.doubleTapPlayerView.getParent()).removeView(binding.doubleTapPlayerView);
fullScreenDialog.addContentView(binding.doubleTapPlayerView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
2020-06-26 14:34:39 +02:00
fullScreenIcon.setImageDrawable(ContextCompat.getDrawable(PeertubeActivity.this, R.drawable.ic_baseline_fullscreen_exit_24));
2020-06-26 08:50:49 +02:00
fullScreenMode = true;
2020-09-19 15:06:24 +02:00
2020-06-26 08:50:49 +02:00
fullScreenDialog.show();
}
2020-10-17 18:50:20 +02:00
public void openCommentThread(Comment comment, int position) {
2020-10-15 18:11:37 +02:00
2020-10-15 15:16:41 +02:00
CommentVM commentViewModel = new ViewModelProvider(PeertubeActivity.this).get(CommentVM.class);
binding.peertubeReply.setVisibility(View.GONE);
commentViewModel.getRepliesComment(videoUuid, comment.getId()).observe(PeertubeActivity.this, this::manageVIewCommentReply);
Account account = comment.getAccount();
Helper.loadGiF(PeertubeActivity.this, account.getAvatar() != null ? account.getAvatar().getPath() : null, binding.commentAccountProfile);
binding.commentAccountDisplayname.setText(account.getDisplayName());
binding.commentAccountUsername.setText(account.getAcct());
Spanned commentSpan;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
commentSpan = Html.fromHtml(comment.getText(), Html.FROM_HTML_MODE_COMPACT);
else
commentSpan = Html.fromHtml(comment.getText());
binding.commentContent.setText(commentSpan);
binding.commentDate.setText(Helper.dateDiff(PeertubeActivity.this, comment.getCreatedAt()));
binding.replyThread.setVisibility(View.VISIBLE);
TranslateAnimation animate = new TranslateAnimation(
binding.peertubeInformationContainer.getWidth(),
0,
0,
0);
animate.setAnimationListener(new Animation.AnimationListener() {
@Override
2020-10-17 18:50:20 +02:00
public void onAnimationStart(Animation animation) {
}
2020-10-15 15:16:41 +02:00
@Override
public void onAnimationEnd(Animation animation) {
binding.peertubeInformationContainer.setVisibility(View.GONE);
}
2020-10-17 18:50:20 +02:00
2020-10-15 15:16:41 +02:00
@Override
2020-10-17 18:50:20 +02:00
public void onAnimationRepeat(Animation animation) {
}
2020-10-15 15:16:41 +02:00
});
animate.setDuration(500);
2020-10-15 18:37:50 +02:00
binding.addCommentWriteReply.setText(String.format("@%s ", comment.getAccount().getAcct()));
binding.addCommentWriteReply.setSelection(binding.addCommentWriteReply.getText().length());
2020-10-15 15:16:41 +02:00
binding.replyThread.startAnimation(animate);
binding.sendReply.setOnClickListener(null);
binding.sendReply.setOnClickListener(v -> {
if (isLoggedIn(PeertubeActivity.this) && !sepiaSearch) {
2020-10-17 18:50:20 +02:00
String commentView = binding.addCommentWriteReply.getText().toString();
2020-10-15 15:16:41 +02:00
if (commentView.trim().length() > 0) {
PostActionsVM viewModelComment = new ViewModelProvider(PeertubeActivity.this).get(PostActionsVM.class);
2020-10-15 18:11:37 +02:00
viewModelComment.comment(REPLY, peertube.getId(), comment.getId(), commentView).observe(PeertubeActivity.this, apiResponse1 -> manageVIewPostActions(REPLY, position, apiResponse1));
2020-10-15 15:16:41 +02:00
binding.addCommentWriteReply.setText("");
}
} else {
2020-10-17 18:50:20 +02:00
if (sepiaSearch) {
2020-10-15 15:16:41 +02:00
Toasty.info(PeertubeActivity.this, getString(R.string.federation_issue), Toasty.LENGTH_SHORT).show();
2020-10-17 18:50:20 +02:00
} else {
2020-10-15 15:16:41 +02:00
Toasty.error(PeertubeActivity.this, getString(R.string.not_logged_in), Toast.LENGTH_SHORT).show();
}
}
});
}
2020-10-17 18:50:20 +02:00
private void closeCommentThread() {
2020-10-15 15:16:41 +02:00
binding.peertubeInformationContainer.setVisibility(View.VISIBLE);
TranslateAnimation animate = new TranslateAnimation(
0,
binding.replyThread.getWidth(),
0,
0);
animate.setAnimationListener(new Animation.AnimationListener() {
@Override
2020-10-17 18:50:20 +02:00
public void onAnimationStart(Animation animation) {
}
2020-10-15 15:16:41 +02:00
@Override
public void onAnimationEnd(Animation animation) {
binding.replyThread.setVisibility(View.GONE);
}
2020-10-17 18:50:20 +02:00
2020-10-15 15:16:41 +02:00
@Override
2020-10-17 18:50:20 +02:00
public void onAnimationRepeat(Animation animation) {
}
2020-10-15 15:16:41 +02:00
});
animate.setDuration(500);
binding.replyThread.startAnimation(animate);
}
2020-10-15 18:11:37 +02:00
@SuppressWarnings({"unused", "RedundantSuppression"})
public void manageVIewPostActions(RetrofitPeertubeAPI.ActionType statusAction, int position, APIResponse apiResponse) {
if (peertube.isCommentsEnabled() && statusAction == ADD_COMMENT) {
2020-10-17 18:50:20 +02:00
if (apiResponse.getComments() != null && apiResponse.getComments().size() > 0) {
2020-10-15 18:11:37 +02:00
comments.add(0, apiResponse.getComments().get(0));
commentListAdapter.notifyItemInserted(0);
}
} else if (peertube.isCommentsEnabled() && statusAction == REPLY) {
2020-10-17 18:50:20 +02:00
if (apiResponse.getComments() != null && apiResponse.getComments().size() > 0) {
2020-10-15 18:11:37 +02:00
commentsThread.add(position, apiResponse.getComments().get(0));
commentReplyListAdapter.notifyItemInserted(position);
}
} else if (statusAction == RetrofitPeertubeAPI.ActionType.REPORT_ACCOUNT) {
Toasty.success(PeertubeActivity.this, getString(R.string.successful_report), Toasty.LENGTH_LONG).show();
} else if (statusAction == RetrofitPeertubeAPI.ActionType.REPORT_VIDEO) {
Toasty.success(PeertubeActivity.this, getString(R.string.successful_video_report), Toasty.LENGTH_LONG).show();
}
}
2020-06-26 08:50:49 +02:00
private void closeFullscreenDialog() {
2020-10-29 18:56:57 +01:00
((ViewGroup) binding.doubleTapPlayerView.getParent()).removeView(binding.doubleTapPlayerView);
((FrameLayout) findViewById(R.id.main_media_frame)).addView(binding.doubleTapPlayerView);
2020-06-26 08:50:49 +02:00
fullScreenMode = false;
fullScreenDialog.dismiss();
2020-06-26 14:34:39 +02:00
fullScreenIcon.setImageDrawable(ContextCompat.getDrawable(PeertubeActivity.this, R.drawable.ic_baseline_fullscreen_24));
2020-06-26 08:50:49 +02:00
}
private void initFullscreenButton() {
2020-10-29 18:56:57 +01:00
PlayerControlView controlView = binding.doubleTapPlayerView.findViewById(R.id.exo_controller);
2020-06-26 08:50:49 +02:00
fullScreenIcon = controlView.findViewById(R.id.exo_fullscreen_icon);
View fullScreenButton = controlView.findViewById(R.id.exo_fullscreen_button);
fullScreenButton.setOnClickListener(v -> {
2020-10-02 15:14:08 +02:00
if (!fullScreenMode) {
2020-06-26 08:50:49 +02:00
openFullscreenDialog();
2020-10-03 18:37:34 +02:00
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
2020-06-26 08:50:49 +02:00
closeFullscreenDialog();
2020-10-03 18:37:34 +02:00
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
2020-10-02 15:23:36 +02:00
Handler handler = new Handler();
handler.postDelayed(() -> setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR), 2000);
2020-10-02 15:14:08 +02:00
}
2020-06-26 08:50:49 +02:00
});
2020-10-24 16:36:49 +02:00
ImageButton playButton = controlView.findViewById(R.id.exo_play);
playButton.setOnClickListener(v->{
if(autoFullscreen && !fullScreenMode) {
openFullscreenDialog();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
player.setPlayWhenReady(true);
});
2020-10-31 10:37:17 +01:00
View exo_next = controlView.findViewById(R.id.exo_next);
exo_next.setOnClickListener(v -> playNextVideo());
2020-06-26 08:50:49 +02:00
}
private void initResolution() {
2020-10-29 18:56:57 +01:00
PlayerControlView controlView = binding.doubleTapPlayerView.findViewById(R.id.exo_controller);
2020-10-15 15:16:41 +02:00
TextView resolution = controlView.findViewById(R.id.resolution);
2020-10-06 18:49:15 +02:00
if (peertube.getFiles() != null && peertube.getFiles().size() > 0) {
2020-10-06 17:11:25 +02:00
resolution.setText(String.format("%s", Helper.defaultFile(PeertubeActivity.this, peertube.getFiles()).getResolutions().getLabel()));
resolution.setOnClickListener(v -> displayResolution());
2020-10-06 18:49:15 +02:00
} else {
2020-10-06 17:11:25 +02:00
resolution.setVisibility(View.GONE);
}
2020-06-26 08:50:49 +02:00
}
private void changeColor() {
2020-06-29 17:59:36 +02:00
Drawable thumbUp = ContextCompat.getDrawable(PeertubeActivity.this, R.drawable.ic_baseline_thumb_up_alt_24);
Drawable thumbDown = ContextCompat.getDrawable(PeertubeActivity.this, R.drawable.ic_baseline_thumb_down_alt_24);
int color = getAttColor(this, android.R.attr.colorControlNormal);
2020-06-30 13:33:43 +02:00
if (thumbUp != null) {
2020-06-29 17:59:36 +02:00
thumbUp.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
DrawableCompat.setTint(thumbUp, color);
}
2020-06-30 13:33:43 +02:00
if (thumbDown != null) {
2020-06-29 17:59:36 +02:00
thumbDown.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
DrawableCompat.setTint(thumbDown, color);
}
if (peertube.getMyRating() != null && peertube.getMyRating().compareTo("like") == 0) {
2020-06-30 13:33:43 +02:00
if (thumbUp != null) {
2020-06-29 17:59:36 +02:00
thumbUp.setColorFilter(getResources().getColor(R.color.positive_thumbs), PorterDuff.Mode.SRC_ATOP);
DrawableCompat.setTint(thumbUp, getResources().getColor(R.color.positive_thumbs));
}
2020-06-30 13:33:43 +02:00
} else if (peertube.getMyRating() != null && peertube.getMyRating().compareTo("dislike") == 0) {
if (thumbDown != null) {
2020-06-29 17:59:36 +02:00
thumbDown.setColorFilter(getResources().getColor(R.color.negative_thumbs), PorterDuff.Mode.SRC_ATOP);
DrawableCompat.setTint(thumbDown, getResources().getColor(R.color.negative_thumbs));
}
2020-06-26 08:50:49 +02:00
}
2020-10-15 15:16:41 +02:00
binding.peertubeLikeCount.setCompoundDrawablesWithIntrinsicBounds(null, thumbUp, null, null);
binding.peertubeDislikeCount.setCompoundDrawablesWithIntrinsicBounds(null, thumbDown, null, null);
2020-06-26 08:50:49 +02:00
}
2020-10-08 18:13:04 +02:00
public void manageVIewPlaylists(APIResponse apiResponse) {
2020-10-24 16:36:49 +02:00
if (apiResponse == null || apiResponse.getError() != null || playlists == null || peertube == null) {
2020-10-08 18:13:04 +02:00
return;
}
if (apiResponse.getPlaylists() != null && apiResponse.getPlaylists().size() > 0) {
androidx.appcompat.app.AlertDialog.Builder builder = new androidx.appcompat.app.AlertDialog.Builder(PeertubeActivity.this);
builder.setTitle(R.string.modify_playlists);
List<PlaylistData.Playlist> ownerPlaylists = apiResponse.getPlaylists();
2020-10-17 18:50:20 +02:00
if (ownerPlaylists == null) {
2020-10-08 18:13:04 +02:00
return;
}
String[] label = new String[ownerPlaylists.size()];
boolean[] checked = new boolean[ownerPlaylists.size()];
int i = 0;
List<PlaylistExist> playlistsForVideo = playlists.get(peertube.getId());
for (PlaylistData.Playlist playlist : ownerPlaylists) {
checked[i] = false;
if (playlistsForVideo != null) {
for (PlaylistExist playlistExist : playlistsForVideo) {
if (playlistExist != null && playlistExist.getPlaylistId().compareTo(playlist.getId()) == 0) {
checked[i] = true;
break;
}
}
}
label[i] = playlist.getDisplayName();
i++;
}
builder.setMultiChoiceItems(label, checked, (dialog, which, isChecked) -> {
PlaylistsVM playlistsViewModel = new ViewModelProvider(PeertubeActivity.this).get(PlaylistsVM.class);
if (isChecked) { //Add to playlist
playlistsViewModel.manage(PlaylistsVM.action.ADD_VIDEOS, ownerPlaylists.get(which), peertube.getUuid()).observe(PeertubeActivity.this, apiResponse3 -> addElement(ownerPlaylists.get(which).getId(), peertube.getId(), apiResponse3));
} else { //Remove from playlist
String elementInPlaylistId = null;
for (PlaylistExist playlistExist : peertube.getPlaylistExists()) {
if (playlistExist.getPlaylistId().compareTo(ownerPlaylists.get(which).getId()) == 0) {
elementInPlaylistId = playlistExist.getPlaylistElementId();
}
}
playlistsViewModel.manage(PlaylistsVM.action.DELETE_VIDEOS, ownerPlaylists.get(which), elementInPlaylistId);
playlists.remove(peertube.getId());
}
});
builder.setPositiveButton(R.string.close, (dialog, which) -> dialog.dismiss());
androidx.appcompat.app.AlertDialog dialog = builder.create();
dialog.show();
2020-06-26 08:50:49 +02:00
}
}
2020-10-17 18:50:20 +02:00
2020-10-08 18:13:04 +02:00
public void manageVIewPlaylist(APIResponse apiResponse) {
if (apiResponse.getError() != null || apiResponse.getVideoExistPlaylist() == null) {
return;
}
if (playlists == null) {
playlists = new HashMap<>();
}
playlists.putAll(apiResponse.getVideoExistPlaylist());
peertube.setPlaylistExists(playlists.get(peertube.getId()));
}
2020-06-26 08:50:49 +02:00
2020-10-08 18:13:04 +02:00
public void addElement(String playlistId, String videoId, APIResponse apiResponse) {
2020-10-06 18:49:15 +02:00
if (apiResponse != null && apiResponse.getActionReturn() != null) {
2020-10-08 18:13:04 +02:00
2020-10-06 18:49:15 +02:00
PlaylistExist playlistExist = new PlaylistExist();
playlistExist.setPlaylistId(playlistId);
playlistExist.setPlaylistElementId(apiResponse.getActionReturn());
2020-10-08 18:13:04 +02:00
List<PlaylistExist> playlistExistList = playlists.get(videoId);
if (playlistExistList == null) {
playlistExistList = new ArrayList<>();
2020-10-03 16:22:19 +02:00
}
2020-10-08 18:13:04 +02:00
playlistExistList.add(playlistExist);
playlists.put(videoId, playlistExistList);
2020-10-03 16:22:19 +02:00
}
}
2020-10-30 16:21:56 +01:00
private void updateHistory(long position) {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
boolean storeInHistory = sharedpreferences.getBoolean(getString(R.string.set_store_in_history), true);
if( Helper.isLoggedIn(PeertubeActivity.this) && peertube != null && storeInHistory) {
new Thread(() -> {
try {
RetrofitPeertubeAPI api = new RetrofitPeertubeAPI(PeertubeActivity.this);
api.updateHistory(peertube.getUuid(), position);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}
2020-10-08 18:13:04 +02:00
2020-10-01 17:42:03 +02:00
@Override
public void onAllCommentRemoved() {
2020-10-15 15:16:41 +02:00
binding.noActionText.setVisibility(View.VISIBLE);
2020-10-01 17:42:03 +02:00
}
2020-10-31 10:37:17 +01:00
private void playNextVideo() {
if( nextVideo != null) {
Intent intent = new Intent(PeertubeActivity.this, PeertubeActivity.class);
Bundle b = new Bundle();
b.putParcelable("video",nextVideo);
b.putString("video_id", nextVideo.getId());
b.putString("video_uuid", nextVideo.getUuid());
playedVideos.add(nextVideo.getId());
b.putBoolean("sepia_search", sepiaSearch);
intent.putExtras(b);
startActivity(intent);
}
}
@Override
public void onMediaItemTransition(MediaItem mediaItem, int reason) {
2020-10-31 18:24:52 +01:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
boolean autoplayNextVideo = sharedpreferences.getBoolean(getString(R.string.set_autoplay_next_video_choice), true);
if (reason == MEDIA_ITEM_TRANSITION_REASON_AUTO && autoplayNextVideo){
2020-10-31 10:37:17 +01:00
player.removeMediaItems(0, player.getMediaItemCount());
playNextVideo();
}
}
@Override
public void onPlayerError(ExoPlaybackException error) {
}
2020-06-26 08:50:49 +02:00
}