package app.fedilab.fedilabtube; /* 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 . */ import android.Manifest; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.res.Configuration; import android.database.sqlite.SQLiteDatabase; import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.text.Html; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.ScrollView; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.AppCompatImageView; import androidx.appcompat.widget.PopupMenu; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.core.graphics.drawable.DrawableCompat; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.google.android.exoplayer2.ExoPlayerFactory; import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.source.ProgressiveMediaSource; import com.google.android.exoplayer2.ui.AspectRatioFrameLayout; import com.google.android.exoplayer2.ui.PlayerControlView; 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.util.Util; import org.jetbrains.annotations.NotNull; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.List; import java.util.Objects; import javax.net.ssl.HttpsURLConnection; import app.fedilab.fedilabtube.asynctasks.ManagePlaylistsAsyncTask; import app.fedilab.fedilabtube.asynctasks.PostActionAsyncTask; import app.fedilab.fedilabtube.asynctasks.RetrievePeertubeSingleAsyncTask; import app.fedilab.fedilabtube.asynctasks.RetrievePeertubeSingleCommentsAsyncTask; import app.fedilab.fedilabtube.client.APIResponse; import app.fedilab.fedilabtube.client.PeertubeAPI; import app.fedilab.fedilabtube.client.TLSSocketFactory; import app.fedilab.fedilabtube.client.entities.Account; import app.fedilab.fedilabtube.client.entities.Error; import app.fedilab.fedilabtube.client.entities.Peertube; import app.fedilab.fedilabtube.client.entities.Playlist; import app.fedilab.fedilabtube.client.entities.PlaylistElement; import app.fedilab.fedilabtube.client.entities.Status; import app.fedilab.fedilabtube.client.entities.StatusDrawerParams; import app.fedilab.fedilabtube.drawer.StatusListAdapter; import app.fedilab.fedilabtube.helper.CacheDataSourceFactory; import app.fedilab.fedilabtube.helper.FullScreenMediaController; import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.interfaces.OnPlaylistActionInterface; import app.fedilab.fedilabtube.interfaces.OnPostActionInterface; import app.fedilab.fedilabtube.interfaces.OnRetrievePeertubeInterface; import app.fedilab.fedilabtube.sqlite.AccountDAO; import app.fedilab.fedilabtube.sqlite.PeertubeFavoritesDAO; import app.fedilab.fedilabtube.sqlite.Sqlite; import app.fedilab.fedilabtube.webview.CustomWebview; import app.fedilab.fedilabtube.webview.MastalabWebChromeClient; import app.fedilab.fedilabtube.webview.MastalabWebViewClient; import es.dmoral.toasty.Toasty; import static app.fedilab.fedilabtube.asynctasks.ManagePlaylistsAsyncTask.action.GET_PLAYLIST; import static app.fedilab.fedilabtube.asynctasks.ManagePlaylistsAsyncTask.action.GET_PLAYLIST_FOR_VIDEO; import static app.fedilab.fedilabtube.helper.Helper.getAttColor; import static app.fedilab.fedilabtube.helper.Helper.isLoggedIn; public class PeertubeActivity extends AppCompatActivity implements OnRetrievePeertubeInterface, OnPostActionInterface, OnPlaylistActionInterface { public static String video_id; private String peertubeInstance, videoId; private FullScreenMediaController.fullscreen fullscreen; private RelativeLayout loader; private TextView peertube_view_count, peertube_playlist, peertube_bookmark, peertube_like_count, peertube_dislike_count, peertube_share, peertube_download, peertube_description, peertube_title; private ScrollView peertube_information_container; private Peertube peertube; private PlayerView playerView; private SimpleExoPlayer player; private boolean fullScreenMode; private Dialog fullScreenDialog; private AppCompatImageView fullScreenIcon; private TextView resolution; private int mode; private LinearLayout write_comment_container; private ImageView send; private TextView add_comment_read; private EditText add_comment_write; private List playlistForVideo; private List playlists; 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); SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); fullscreen = FullScreenMediaController.fullscreen.OFF; fullScreenMode = false; playlistForVideo = new ArrayList<>(); setContentView(R.layout.activity_peertube); loader = findViewById(R.id.loader); peertube_view_count = findViewById(R.id.peertube_view_count); peertube_bookmark = findViewById(R.id.peertube_bookmark); peertube_like_count = findViewById(R.id.peertube_like_count); peertube_dislike_count = findViewById(R.id.peertube_dislike_count); peertube_share = findViewById(R.id.peertube_share); peertube_download = findViewById(R.id.peertube_download); peertube_description = findViewById(R.id.peertube_description); peertube_title = findViewById(R.id.peertube_title); peertube_information_container = findViewById(R.id.peertube_information_container); CustomWebview webview_video = findViewById(R.id.webview_video); playerView = findViewById(R.id.media_video); write_comment_container = findViewById(R.id.write_comment_container); ImageView my_pp = findViewById(R.id.my_pp); add_comment_read = findViewById(R.id.add_comment_read); add_comment_write = findViewById(R.id.add_comment_write); peertube_playlist = findViewById(R.id.peertube_playlist); send = findViewById(R.id.send); peertube_playlist.setVisibility(View.VISIBLE); peertube_bookmark.setVisibility(View.GONE); if (Helper.isTablet(PeertubeActivity.this)) { RelativeLayout video_container = findViewById(R.id.video_container); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, 0, 2.0f ); video_container.setLayoutParams(param); } SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); String instance = Helper.getLiveInstance(PeertubeActivity.this); Account account = new AccountDAO(PeertubeActivity.this, db).getUniqAccount(userId, instance); Helper.loadGiF(PeertubeActivity.this, account, my_pp); Bundle b = getIntent().getExtras(); if (b != null) { peertubeInstance = b.getString("peertube_instance", Helper.getLiveInstance(PeertubeActivity.this)); videoId = b.getString("video_id", null); } if (getSupportActionBar() != null) getSupportActionBar().setDisplayHomeAsUpEnabled(true); mode = sharedpreferences.getInt(Helper.SET_VIDEO_MODE, Helper.VIDEO_MODE_NORMAL); if (mode != Helper.VIDEO_MODE_WEBVIEW && mode != Helper.VIDEO_MODE_NORMAL) mode = Helper.VIDEO_MODE_NORMAL; if (mode == Helper.VIDEO_MODE_WEBVIEW) { webview_video.setVisibility(View.VISIBLE); playerView.setVisibility(View.GONE); webview_video = Helper.initializeWebview(PeertubeActivity.this, R.id.webview_video, null); FrameLayout webview_container = findViewById(R.id.main_media_frame); final ViewGroup videoLayout = findViewById(R.id.videoLayout); MastalabWebChromeClient mastalabWebChromeClient = new MastalabWebChromeClient(PeertubeActivity.this, webview_video, webview_container, videoLayout); mastalabWebChromeClient.setOnToggledFullscreen(fullscreen -> { if (fullscreen) { videoLayout.setVisibility(View.VISIBLE); 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); peertube_information_container.setVisibility(View.GONE); } 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); videoLayout.setVisibility(View.GONE); peertube_information_container.setVisibility(View.VISIBLE); } }); webview_video.getSettings().setAllowFileAccess(true); webview_video.setWebChromeClient(mastalabWebChromeClient); webview_video.getSettings().setDomStorageEnabled(true); webview_video.getSettings().setAppCacheEnabled(true); webview_video.getSettings().setMediaPlaybackRequiresUserGesture(false); webview_video.setWebViewClient(new MastalabWebViewClient(PeertubeActivity.this)); webview_video.loadUrl("https://" + peertubeInstance + "/videos/embed/" + videoId); } else { webview_video.setVisibility(View.GONE); playerView.setVisibility(View.VISIBLE); loader.setVisibility(View.VISIBLE); } if (mode != Helper.VIDEO_MODE_WEBVIEW) { playerView.setControllerShowTimeoutMs(1000); playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT); initFullscreenDialog(); initFullscreenButton(); } if (Helper.isLoggedIn(PeertubeActivity.this)) { new ManagePlaylistsAsyncTask(PeertubeActivity.this, GET_PLAYLIST, null, null, null, PeertubeActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } new RetrievePeertubeSingleAsyncTask(PeertubeActivity.this, peertubeInstance, videoId, PeertubeActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } public void change() { if (fullscreen == FullScreenMediaController.fullscreen.ON) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); Objects.requireNonNull(getSupportActionBar()).hide(); peertube_information_container.setVisibility(View.GONE); } else { getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN); Objects.requireNonNull(getSupportActionBar()).show(); peertube_information_container.setVisibility(View.VISIBLE); } } @Override public boolean dispatchTouchEvent(MotionEvent ev) { View v = getCurrentFocus(); if ((ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) && v instanceof EditText && v.getId() == R.id.add_comment_write) { int[] scrcoords = new int[2]; v.getLocationOnScreen(scrcoords); float x = ev.getRawX() + v.getLeft() - scrcoords[0]; float y = ev.getRawY() + v.getTop() - scrcoords[1]; if (x < v.getLeft() || x > v.getRight() || y < v.getTop() || y > v.getBottom()) { add_comment_read.setVisibility(View.VISIBLE); add_comment_write.setVisibility(View.GONE); send.setVisibility(View.GONE); hideKeyboard(PeertubeActivity.this); } } return super.dispatchTouchEvent(ev); } @Override public boolean onCreateOptionsMenu(@NotNull Menu menu) { getMenuInflater().inflate(R.menu.main_peertube, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { finish(); return true; } else if (item.getItemId() == R.id.action_report) { 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_choice, new LinearLayout(PeertubeActivity.this), false); dialogBuilder.setView(dialogView); Button report_video = dialogView.findViewById(R.id.report_video); Button report_account = dialogView.findViewById(R.id.report_account); dialogBuilder.setNeutralButton(R.string.cancel, (dialog, id) -> dialog.dismiss()); androidx.appcompat.app.AlertDialog alertDialog = dialogBuilder.create(); alertDialog.show(); report_video.setOnClickListener(v -> reportAlert(PeertubeAPI.reportType.VIDEO, alertDialog)); report_account.setOnClickListener(v -> reportAlert(PeertubeAPI.reportType.ACCOUNT, alertDialog)); return true; } return super.onOptionsItemSelected(item); } private void reportAlert(PeertubeAPI.reportType type, androidx.appcompat.app.AlertDialog alertDialog) { 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); EditText report_content = dialogView.findViewById(R.id.report_content); dialogBuilder.setNeutralButton(R.string.cancel, (dialog, id) -> dialog.dismiss()); dialogBuilder.setPositiveButton(R.string.report, (dialog, id) -> { if (report_content.getText().toString().trim().length() == 0) { Toasty.info(PeertubeActivity.this, getString(R.string.report_comment_size), Toasty.LENGTH_LONG).show(); } else { if (type == PeertubeAPI.reportType.VIDEO) { new PostActionAsyncTask(PeertubeActivity.this, PeertubeAPI.StatusAction.REPORT_VIDEO, peertube.getId(), report_content.getText().toString(), PeertubeActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); alertDialog.dismiss(); dialog.dismiss(); } else if (type == PeertubeAPI.reportType.ACCOUNT) { new PostActionAsyncTask(PeertubeActivity.this, PeertubeAPI.StatusAction.REPORT_ACCOUNT, peertube.getAccount().getId(), report_content.getText().toString(), PeertubeActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); alertDialog.dismiss(); dialog.dismiss(); } } }); androidx.appcompat.app.AlertDialog alertDialog2 = dialogBuilder.create(); alertDialog2.show(); } public FullScreenMediaController.fullscreen getFullscreen() { return fullscreen; } public void setFullscreen(FullScreenMediaController.fullscreen fullscreen) { this.fullscreen = fullscreen; } @Override public void onRetrievePeertube(APIResponse apiResponse) { 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(); loader.setVisibility(View.GONE); return; } if (apiResponse.getPeertubes() == null || apiResponse.getPeertubes().get(0) == null || apiResponse.getPeertubes().get(0).getFileUrl(null, PeertubeActivity.this) == null) { Toasty.error(PeertubeActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show(); loader.setVisibility(View.GONE); return; } peertube = apiResponse.getPeertubes().get(0); //TODO: currently streaming service gives the wrong values for duration new ManagePlaylistsAsyncTask(PeertubeActivity.this, GET_PLAYLIST_FOR_VIDEO, null, peertube.getId(), null, PeertubeActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); add_comment_read.setOnClickListener(v -> { if (isLoggedIn(PeertubeActivity.this)) { add_comment_read.setVisibility(View.GONE); add_comment_write.setVisibility(View.VISIBLE); send.setVisibility(View.VISIBLE); add_comment_write.requestFocus(); add_comment_write.setSelection(add_comment_write.getText().length()); } else { Toasty.error(PeertubeActivity.this, getString(R.string.not_logged_in), Toast.LENGTH_SHORT).show(); } }); send.setOnClickListener(v -> { if (isLoggedIn(PeertubeActivity.this)) { String comment = add_comment_write.getText().toString(); if (comment.trim().length() > 0) { new PostActionAsyncTask(PeertubeActivity.this, PeertubeAPI.StatusAction.PEERTUBECOMMENT, peertube.getId(), comment, PeertubeActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); add_comment_write.setText(""); add_comment_read.setVisibility(View.VISIBLE); add_comment_write.setVisibility(View.GONE); send.setVisibility(View.GONE); add_comment_read.requestFocus(); } } else { Toasty.error(PeertubeActivity.this, getString(R.string.not_logged_in), Toast.LENGTH_SHORT).show(); } }); peertube_playlist.setOnClickListener(v -> { if (playlists != null && peertube.getId() != null) { PopupMenu popup = new PopupMenu(PeertubeActivity.this, peertube_playlist); for (Playlist playlist : playlists) { String title = null; boolean isPresent = false; String elementId = null; PlaylistElement playlistElementFinal = null; for (PlaylistElement playlistElement : playlistForVideo) { if (playlist.getId().equals(playlistElement.getPlaylistId())) { title = "✔ " + playlist.getDisplayName(); isPresent = true; elementId = playlistElement.getPlaylistElementId(); playlistElementFinal = playlistElement; break; } } if (title == null) { title = playlist.getDisplayName(); } MenuItem item = popup.getMenu().add(0, 0, Menu.NONE, title); boolean finalIsPresent = isPresent; String finalElementId = elementId; PlaylistElement finalPlaylistElementFinal = playlistElementFinal; item.setOnMenuItemClickListener(item1 -> { item1.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); item1.setActionView(new View(PeertubeActivity.this)); item1.setOnActionExpandListener(new MenuItem.OnActionExpandListener() { @Override public boolean onMenuItemActionExpand(MenuItem item1) { return false; } @Override public boolean onMenuItemActionCollapse(MenuItem item1) { return false; } }); if (finalIsPresent) { item1.setTitle(playlist.getDisplayName()); new ManagePlaylistsAsyncTask(PeertubeActivity.this, ManagePlaylistsAsyncTask.action.DELETE_VIDEOS, playlist, finalElementId, null, PeertubeActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); playlistForVideo.remove(finalPlaylistElementFinal); } else { item1.setTitle("✔ " + playlist.getDisplayName()); new ManagePlaylistsAsyncTask(PeertubeActivity.this, ManagePlaylistsAsyncTask.action.ADD_VIDEOS, playlist, peertube.getId(), null, PeertubeActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); PlaylistElement playlistElement = new PlaylistElement(); playlistElement.setPlaylistElementId(finalElementId); playlistElement.setPlaylistId(playlist.getId()); playlistForVideo.add(playlistElement); } return false; }); popup.show(); } } }); if (peertube.isCommentsEnabled()) { new RetrievePeertubeSingleCommentsAsyncTask(PeertubeActivity.this, peertubeInstance, videoId, PeertubeActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); write_comment_container.setVisibility(View.VISIBLE); } else { RelativeLayout no_action = findViewById(R.id.no_action); TextView no_action_text = findViewById(R.id.no_action_text); no_action_text.setText(getString(R.string.comment_no_allowed_peertube)); no_action.setVisibility(View.VISIBLE); write_comment_container.setVisibility(View.GONE); } SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); setTitle(peertube.getName()); peertube_description.setText(peertube.getDescription()); peertube_title.setText(peertube.getName()); peertube_dislike_count.setText(String.valueOf(peertube.getDislike())); peertube_like_count.setText(String.valueOf(peertube.getLike())); peertube_view_count.setText(String.valueOf(peertube.getView())); video_id = peertube.getId(); changeColor(); initResolution(); peertube_like_count.setOnClickListener(v -> { if (isLoggedIn(PeertubeActivity.this)) { String newState = peertube.getMyRating().equals("like") ? "none" : "like"; new PostActionAsyncTask(PeertubeActivity.this, PeertubeAPI.StatusAction.RATEVIDEO, peertube.getId(), newState, PeertubeActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); peertube.setMyRating(newState); int count = Integer.parseInt(peertube_like_count.getText().toString()); if (newState.compareTo("none") == 0) { count--; if (count - 1 < 0) { count = 0; } } else { count++; } peertube_like_count.setText(String.valueOf(count)); changeColor(); } else { Toasty.error(PeertubeActivity.this, getString(R.string.not_logged_in), Toast.LENGTH_SHORT).show(); } }); peertube_dislike_count.setOnClickListener(v -> { if (isLoggedIn(PeertubeActivity.this)) { String newState = peertube.getMyRating().equals("dislike") ? "none" : "dislike"; new PostActionAsyncTask(PeertubeActivity.this, PeertubeAPI.StatusAction.RATEVIDEO, peertube.getId(), newState, PeertubeActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); peertube.setMyRating(newState); int count = Integer.parseInt(peertube_dislike_count.getText().toString()); if (newState.compareTo("none") == 0) { count--; if (count - 1 < 0) { count = 0; } } else { count++; } peertube_dislike_count.setText(String.valueOf(count)); changeColor(); } else { Toasty.error(PeertubeActivity.this, getString(R.string.not_logged_in), Toast.LENGTH_SHORT).show(); } }); try { HttpsURLConnection.setDefaultSSLSocketFactory(new TLSSocketFactory()); } catch (KeyManagementException | NoSuchAlgorithmException e) { e.printStackTrace(); } if (mode == Helper.VIDEO_MODE_NORMAL) { 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); videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory) .createMediaSource(Uri.parse(apiResponse.getPeertubes().get(0).getFileUrl(null, PeertubeActivity.this))); } else { CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(PeertubeActivity.this); videoSource = new ProgressiveMediaSource.Factory(cacheDataSourceFactory) .createMediaSource(Uri.parse(apiResponse.getPeertubes().get(0).getFileUrl(null, PeertubeActivity.this))); } player = ExoPlayerFactory.newSimpleInstance(PeertubeActivity.this); playerView.setPlayer(player); loader.setVisibility(View.GONE); player.prepare(videoSource); player.setPlayWhenReady(true); } peertube_download.setOnClickListener(v -> { 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); } else { Helper.manageDownloads(PeertubeActivity.this, peertube.getFileDownloadUrl(null, PeertubeActivity.this)); } } else { Helper.manageDownloads(PeertubeActivity.this, peertube.getFileDownloadUrl(null, PeertubeActivity.this)); } }); SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); List peertubes = new PeertubeFavoritesDAO(PeertubeActivity.this, db).getSinglePeertube(peertube); Drawable img; if (peertubes == null || peertubes.size() == 0) img = ContextCompat.getDrawable(PeertubeActivity.this, R.drawable.ic_baseline_bookmark_border_24); else img = ContextCompat.getDrawable(PeertubeActivity.this, R.drawable.ic_baseline_bookmark_24); peertube_bookmark.setCompoundDrawablesWithIntrinsicBounds(null, img, null, null); peertube_bookmark.setOnClickListener(v -> { List peertubes1 = new PeertubeFavoritesDAO(PeertubeActivity.this, db).getSinglePeertube(peertube); if (peertubes1 == null || peertubes1.size() == 0) { new PeertubeFavoritesDAO(PeertubeActivity.this, db).insert(peertube); Toasty.success(PeertubeActivity.this, getString(R.string.bookmark_add_peertube), Toast.LENGTH_SHORT).show(); } else { new PeertubeFavoritesDAO(PeertubeActivity.this, db).remove(peertube); Toasty.success(PeertubeActivity.this, getString(R.string.bookmark_remove_peertube), Toast.LENGTH_SHORT).show(); } if (peertubes1 != null && peertubes1.size() > 0) //Was initially in cache peertube_bookmark.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(PeertubeActivity.this, R.drawable.ic_baseline_bookmark_border_24), null, null); else peertube_bookmark.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(PeertubeActivity.this, R.drawable.ic_baseline_bookmark_24), null, null); }); peertube_share.setOnClickListener(v -> { Intent sendIntent = new Intent(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.shared_via)); String url; url = "https://" + peertube.getInstance() + "/videos/watch/" + peertube.getUuid(); 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 (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(); extra_text += contentToot; } else { extra_text = url; } sendIntent.putExtra(Intent.EXTRA_TEXT, extra_text); sendIntent.setType("text/plain"); startActivity(Intent.createChooser(sendIntent, getString(R.string.share_with))); }); } @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(); } @Override public void onRetrievePeertubeComments(APIResponse apiResponse) { if (apiResponse == null || (apiResponse.getError() != null && apiResponse.getError().getStatusCode() != 404 && apiResponse.getError() != null && apiResponse.getError().getStatusCode() != 501)) { 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; } List statuses = apiResponse.getStatuses(); RecyclerView lv_comments = findViewById(R.id.peertube_comments); if (statuses != null && statuses.size() > 0) { lv_comments.setVisibility(View.VISIBLE); SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); StatusDrawerParams statusDrawerParams = new StatusDrawerParams(); statusDrawerParams.setTargetedId(userId); statusDrawerParams.setStatuses(statuses); StatusListAdapter statusListAdapter = new StatusListAdapter(statusDrawerParams); LinearLayoutManager mLayoutManager = new LinearLayoutManager(PeertubeActivity.this); lv_comments.setLayoutManager(mLayoutManager); lv_comments.setNestedScrollingEnabled(false); lv_comments.setAdapter(statusListAdapter); } } @Override public void onRetrievePeertubeChannels(APIResponse apiResponse) { } @Override public void onDestroy() { super.onDestroy(); if (player != null) player.release(); } @Override protected void onPause() { super.onPause(); if (player != null) { player.setPlayWhenReady(false); } } @Override public void onResume() { super.onResume(); if (player != null) { player.setPlayWhenReady(true); } } public void displayResolution() { SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); AlertDialog.Builder builderSingle = new AlertDialog.Builder(PeertubeActivity.this); builderSingle.setTitle(R.string.pickup_resolution); final ArrayAdapter arrayAdapter = new ArrayAdapter<>(PeertubeActivity.this, android.R.layout.select_dialog_item); for (String resolution : peertube.getResolution()) arrayAdapter.add(resolution + "p"); 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); if (playerView != null) { loader.setVisibility(View.VISIBLE); long position = player.getCurrentPosition(); PlayerControlView controlView = playerView.findViewById(R.id.exo_controller); resolution = controlView.findViewById(R.id.resolution); resolution.setText(String.format("%sp", res)); if (mode == Helper.VIDEO_MODE_NORMAL) { if (player != null) player.release(); player = ExoPlayerFactory.newSimpleInstance(PeertubeActivity.this); playerView.setPlayer(player); 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); videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory) .createMediaSource(Uri.parse(peertube.getFileUrl(res, PeertubeActivity.this))); } else { CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(PeertubeActivity.this); videoSource = new ProgressiveMediaSource.Factory(cacheDataSourceFactory) .createMediaSource(Uri.parse(peertube.getFileUrl(res, PeertubeActivity.this))); } player.prepare(videoSource); player.seekTo(0, position); player.setPlayWhenReady(true); } } }); builderSingle.show(); } @Override public void onPostAction(int statusCode, PeertubeAPI.StatusAction statusAction, String userId, Error error) { if (peertube.isCommentsEnabled() && statusAction == PeertubeAPI.StatusAction.PEERTUBECOMMENT) { new RetrievePeertubeSingleCommentsAsyncTask(PeertubeActivity.this, peertubeInstance, videoId, PeertubeActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } else if (statusAction == PeertubeAPI.StatusAction.REPORT_ACCOUNT) { Toasty.success(PeertubeActivity.this, getString(R.string.successful_report), Toasty.LENGTH_LONG).show(); } else if (statusAction == PeertubeAPI.StatusAction.REPORT_VIDEO) { Toasty.success(PeertubeActivity.this, getString(R.string.successful_video_report), Toasty.LENGTH_LONG).show(); } } private void initFullscreenDialog() { fullScreenDialog = new Dialog(this, android.R.style.Theme_Black_NoTitleBar_Fullscreen) { public void onBackPressed() { if (fullScreenMode) closeFullscreenDialog(); super.onBackPressed(); } }; } private void openFullscreenDialog() { ((ViewGroup) playerView.getParent()).removeView(playerView); fullScreenDialog.addContentView(playerView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); fullScreenIcon.setImageDrawable(ContextCompat.getDrawable(PeertubeActivity.this, R.drawable.ic_baseline_fullscreen_exit_24)); fullScreenMode = true; fullScreenDialog.show(); } private void closeFullscreenDialog() { ((ViewGroup) playerView.getParent()).removeView(playerView); ((FrameLayout) findViewById(R.id.main_media_frame)).addView(playerView); fullScreenMode = false; fullScreenDialog.dismiss(); fullScreenIcon.setImageDrawable(ContextCompat.getDrawable(PeertubeActivity.this, R.drawable.ic_baseline_fullscreen_24)); } private void initFullscreenButton() { PlayerControlView controlView = playerView.findViewById(R.id.exo_controller); fullScreenIcon = controlView.findViewById(R.id.exo_fullscreen_icon); View fullScreenButton = controlView.findViewById(R.id.exo_fullscreen_button); fullScreenButton.setOnClickListener(v -> { if (!fullScreenMode) openFullscreenDialog(); else closeFullscreenDialog(); }); } private void initResolution() { PlayerControlView controlView = playerView.findViewById(R.id.exo_controller); resolution = controlView.findViewById(R.id.resolution); resolution.setText(String.format("%sp", peertube.getResolution().get(0))); resolution.setOnClickListener(v -> displayResolution()); } private void changeColor() { 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); if (thumbUp != null) { thumbUp.setColorFilter(color, PorterDuff.Mode.SRC_ATOP); DrawableCompat.setTint(thumbUp, color); } if (thumbDown != null) { thumbDown.setColorFilter(color, PorterDuff.Mode.SRC_ATOP); DrawableCompat.setTint(thumbDown, color); } if (peertube.getMyRating() != null && peertube.getMyRating().compareTo("like") == 0) { if (thumbUp != null) { thumbUp.setColorFilter(getResources().getColor(R.color.positive_thumbs), PorterDuff.Mode.SRC_ATOP); DrawableCompat.setTint(thumbUp, getResources().getColor(R.color.positive_thumbs)); } } else if (peertube.getMyRating() != null && peertube.getMyRating().compareTo("dislike") == 0) { if (thumbDown != null) { thumbDown.setColorFilter(getResources().getColor(R.color.negative_thumbs), PorterDuff.Mode.SRC_ATOP); DrawableCompat.setTint(thumbDown, getResources().getColor(R.color.negative_thumbs)); } } peertube_like_count.setCompoundDrawablesWithIntrinsicBounds(null, thumbUp, null, null); peertube_dislike_count.setCompoundDrawablesWithIntrinsicBounds(null, thumbDown, null, null); } @Override public void onActionDone(ManagePlaylistsAsyncTask.action actionType, APIResponse apiResponse, int statusCode) { if (actionType == GET_PLAYLIST_FOR_VIDEO && apiResponse != null) { playlistForVideo = apiResponse.getPlaylistForVideos(); } else if (actionType == GET_PLAYLIST && apiResponse != null) { playlists = apiResponse.getPlaylists(); } } }