From d338b9be36855a1f1c1f392430c6efede7192a03 Mon Sep 17 00:00:00 2001 From: stom79 Date: Sun, 6 Jan 2019 16:06:18 +0100 Subject: [PATCH] Webview --- .../mastodon/activities/PeertubeActivity.java | 193 ++++++++++++------ .../mastodon/activities/WebviewActivity.java | 2 +- app/src/main/res/layout/activity_peertube.xml | 13 +- 3 files changed, 143 insertions(+), 65 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/PeertubeActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/PeertubeActivity.java index 164284bef..e6da8666c 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/PeertubeActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/PeertubeActivity.java @@ -46,6 +46,7 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; +import android.webkit.WebView; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.FrameLayout; @@ -104,6 +105,8 @@ import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrievePeertubeInterface; import fr.gouv.etalab.mastodon.sqlite.PeertubeFavoritesDAO; import fr.gouv.etalab.mastodon.sqlite.Sqlite; +import fr.gouv.etalab.mastodon.webview.MastalabWebChromeClient; +import fr.gouv.etalab.mastodon.webview.MastalabWebViewClient; import static fr.gouv.etalab.mastodon.helper.Helper.THEME_LIGHT; import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor; @@ -135,6 +138,9 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube private AppCompatImageView fullScreenIcon; private TextView resolution; private DefaultTrackSelector trackSelector; + private WebView webview_video; + private int mode; + @Override protected void onCreate(Bundle savedInstanceState) { @@ -167,18 +173,27 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube peertube_description = findViewById(R.id.peertube_description); peertube_title = findViewById(R.id.peertube_title); peertube_information_container = findViewById(R.id.peertube_information_container); - - - + webview_video = findViewById(R.id.webview_video); playerView = findViewById(R.id.media_video); - playerView.setControllerShowTimeoutMs(1000); - playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT); - initFullscreenDialog(); - initFullscreenButton(); + mode = sharedpreferences.getInt(Helper.SET_VIDEO_MODE, Helper.VIDEO_MODE_TORRENT); + if( mode == Helper.VIDEO_MODE_WEBVIEW){ + webview_video.setVisibility(View.VISIBLE); + playerView.setVisibility(View.GONE); + }else { + webview_video.setVisibility(View.GONE); + playerView.setVisibility(View.VISIBLE); + } + + + if( mode != Helper.VIDEO_MODE_WEBVIEW){ + playerView.setControllerShowTimeoutMs(1000); + playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT); + initFullscreenDialog(); + initFullscreenButton(); + } - loader.setVisibility(View.VISIBLE); Bundle b = getIntent().getExtras(); if(b != null) { peertubeInstance = b.getString("peertube_instance", null); @@ -394,8 +409,6 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube }); } - //Uri uri = Uri.parse(apiResponse.getPeertubes().get(0).getFileUrl(null)); - Uri uri = Uri.parse(apiResponse.getPeertubes().get(0).getTorrentUrl(null)); try { HttpsURLConnection.setDefaultSSLSocketFactory(new TLSSocketFactory()); } catch (KeyManagementException e) { @@ -404,73 +417,127 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube e.printStackTrace(); } - - TorrentOptions torrentOptions = new TorrentOptions.Builder() - .saveLocation(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)) - .removeFilesAfterStop(true) - .build(); + if( mode == Helper.VIDEO_MODE_TORRENT){ + TorrentOptions torrentOptions = new TorrentOptions.Builder() + .saveLocation(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)) + .removeFilesAfterStop(true) + .build(); - DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); - TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter); - trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); + DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); + TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter); + trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); - DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getApplicationContext(), - Util.getUserAgent(getApplicationContext(), "Mastalab"), null); + DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getApplicationContext(), + Util.getUserAgent(getApplicationContext(), "Mastalab"), null); - ExtractorMediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory) - .createMediaSource(Uri.parse(apiResponse.getPeertubes().get(0).getTorrentUrl(null))); + ExtractorMediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory) + .createMediaSource(Uri.parse(apiResponse.getPeertubes().get(0).getTorrentUrl(null))); - player = ExoPlayerFactory.newSimpleInstance(PeertubeActivity.this, trackSelector); - playerView.setPlayer(player); - torrentStream = TorrentStream.init(torrentOptions); - player.prepare(videoSource); - torrentStream.startStream(apiResponse.getPeertubes().get(0).getTorrentUrl(null)); - torrentListener = new TorrentListener() { - @Override - public void onStreamPrepared(Torrent torrent) { - Log.v(Helper.TAG,"onStreamPrepared"); - } + player = ExoPlayerFactory.newSimpleInstance(PeertubeActivity.this, trackSelector); + playerView.setPlayer(player); + torrentStream = TorrentStream.init(torrentOptions); + player.prepare(videoSource); + torrentStream.startStream(apiResponse.getPeertubes().get(0).getTorrentUrl(null)); + torrentListener = new TorrentListener() { + @Override + public void onStreamPrepared(Torrent torrent) { + Log.v(Helper.TAG,"onStreamPrepared"); + } - @Override - public void onStreamStarted(Torrent torrent) { - Log.v(Helper.TAG,"onStreamStarted"); - } + @Override + public void onStreamStarted(Torrent torrent) { + Log.v(Helper.TAG,"onStreamStarted"); + } - @Override - public void onStreamError(Torrent torrent, Exception e) { - Log.v(Helper.TAG,"onStreamError"); - e.printStackTrace(); - } + @Override + public void onStreamError(Torrent torrent, Exception e) { + Log.v(Helper.TAG,"onStreamError"); + e.printStackTrace(); + } - @Override - public void onStreamReady(Torrent torrent) { - Log.v(Helper.TAG,"onStreamReady"); - loader.setVisibility(View.GONE); - DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getApplicationContext(), - Util.getUserAgent(getApplicationContext(), "Mastalab"), bandwidthMeter); + @Override + public void onStreamReady(Torrent torrent) { + Log.v(Helper.TAG,"onStreamReady"); + loader.setVisibility(View.GONE); + DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getApplicationContext(), + Util.getUserAgent(getApplicationContext(), "Mastalab"), bandwidthMeter); - ExtractorMediaSource extractorMediaSource = new ExtractorMediaSource.Factory(dataSourceFactory) - .createMediaSource(Uri.fromFile(torrent.getVideoFile())); - player.prepare(extractorMediaSource); - player.setPlayWhenReady(true); + ExtractorMediaSource extractorMediaSource = new ExtractorMediaSource.Factory(dataSourceFactory) + .createMediaSource(Uri.fromFile(torrent.getVideoFile())); + player.prepare(extractorMediaSource); + player.setPlayWhenReady(true); - } + } - @Override - public void onStreamProgress(Torrent torrent, StreamStatus status) { - } + @Override + public void onStreamProgress(Torrent torrent, StreamStatus status) { + } + + @Override + public void onStreamStopped() { + Log.v(Helper.TAG,"onStreamStopped"); + loader.setVisibility(View.GONE); + player.release(); + } + }; + torrentStream.addListener(torrentListener); + }else if( mode == Helper.VIDEO_MODE_DIRECT){ + DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); + TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter); + trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); + + DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getApplicationContext(), + Util.getUserAgent(getApplicationContext(), "Mastalab"), null); + + ExtractorMediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory) + .createMediaSource(Uri.parse(apiResponse.getPeertubes().get(0).getTorrentUrl(null))); + + player = ExoPlayerFactory.newSimpleInstance(PeertubeActivity.this); + playerView.setPlayer(player); + loader.setVisibility(View.GONE); + + ExtractorMediaSource extractorMediaSource = new ExtractorMediaSource.Factory(dataSourceFactory) + .createMediaSource(Uri.parse(apiResponse.getPeertubes().get(0).getTorrentUrl(null))); + player.prepare(extractorMediaSource); + player.setPlayWhenReady(true); + }else { + webview_video = Helper.initializeWebview(PeertubeActivity.this, R.id.webview_video); + setTitle(""); + FrameLayout webview_container = findViewById(R.id.main_media_frame); + final ViewGroup videoLayout = findViewById(R.id.videoLayout); // Your own view, read class comments + + MastalabWebChromeClient mastalabWebChromeClient = new MastalabWebChromeClient(PeertubeActivity.this, webview_video, webview_container, videoLayout); + mastalabWebChromeClient.setOnToggledFullscreen(new MastalabWebChromeClient.ToggledFullscreenCallback() { + @Override + public void toggledFullscreen(boolean 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.setWebChromeClient(mastalabWebChromeClient); + webview_video.setWebViewClient(new MastalabWebViewClient(PeertubeActivity.this)); + webview_video.loadUrl(peertube.getFileUrl(null)); + } - @Override - public void onStreamStopped() { - Log.v(Helper.TAG,"onStreamStopped"); - loader.setVisibility(View.GONE); - player.release(); - } - }; - torrentStream.addListener(torrentListener); peertube_download.setOnClickListener(new View.OnClickListener() { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/WebviewActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/WebviewActivity.java index cc191d39b..495675a6f 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/WebviewActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/WebviewActivity.java @@ -95,7 +95,7 @@ public class WebviewActivity extends BaseActivity { if( getSupportActionBar() != null) getSupportActionBar().setDisplayHomeAsUpEnabled(true); - webView = Helper.initializeWebview(WebviewActivity.this, R.id.webview); + webView = Helper.initializeWebview(WebviewActivity.this, R.id.webview); setTitle(""); FrameLayout webview_container = findViewById(R.id.webview_container); final ViewGroup videoLayout = findViewById(R.id.videoLayout); // Your own view, read class comments diff --git a/app/src/main/res/layout/activity_peertube.xml b/app/src/main/res/layout/activity_peertube.xml index 684edbc00..e9b2b01f7 100644 --- a/app/src/main/res/layout/activity_peertube.xml +++ b/app/src/main/res/layout/activity_peertube.xml @@ -48,6 +48,11 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" /> + @@ -206,6 +211,12 @@ - + +