From ae30ab708eac8f72008217ca7e7047125dcd502d Mon Sep 17 00:00:00 2001 From: Olivier Bouillet Date: Fri, 25 Sep 2020 21:55:25 +0200 Subject: [PATCH 1/2] [Crash] : avoid crash when getFiles is empty --- .../fragment/VideoPlayerFragment.java | 23 ++++++++----- .../schueller/peertube/intents/Intents.java | 33 +++++++++++-------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/net/schueller/peertube/fragment/VideoPlayerFragment.java b/app/src/main/java/net/schueller/peertube/fragment/VideoPlayerFragment.java index 229ca2b..397913c 100644 --- a/app/src/main/java/net/schueller/peertube/fragment/VideoPlayerFragment.java +++ b/app/src/main/java/net/schueller/peertube/fragment/VideoPlayerFragment.java @@ -243,17 +243,22 @@ public class VideoPlayerFragment extends Fragment implements VideoRendererEventL Integer videoQuality = sharedPref.getInt(getString(R.string.pref_quality_key), 0); //get video qualities - String urlToPlay = video.getFiles().get(0).getFileUrl(); - for (File file : video.getFiles()) { - // Set quality if it matches - if (file.getResolution().getId().equals(videoQuality)) { - urlToPlay = file.getFileUrl(); + /// # + if (video.getFiles().size() > 0) { + String urlToPlay = video.getFiles().get( 0 ).getFileUrl(); + for ( File file : video.getFiles() ) { + // Set quality if it matches + if ( file.getResolution().getId().equals( videoQuality ) ) { + urlToPlay = file.getFileUrl(); + } } + mService.setCurrentStreamUrl( urlToPlay ); + torrentStatus.setVisibility(View.GONE); + startPlayer(); + } else { + stopVideo(); + Toast.makeText(context, R.string.api_error, Toast.LENGTH_LONG).show(); } - mService.setCurrentStreamUrl(urlToPlay); - - torrentStatus.setVisibility(View.GONE); - startPlayer(); } Log.v(TAG, "end of load Video"); diff --git a/app/src/main/java/net/schueller/peertube/intents/Intents.java b/app/src/main/java/net/schueller/peertube/intents/Intents.java index 6b2dfe3..c8eb87e 100644 --- a/app/src/main/java/net/schueller/peertube/intents/Intents.java +++ b/app/src/main/java/net/schueller/peertube/intents/Intents.java @@ -27,6 +27,7 @@ import android.os.Build; import android.os.Environment; import android.webkit.MimeTypeMap; import android.webkit.URLUtil; +import android.widget.Toast; import com.github.se_bastiaan.torrentstream.TorrentOptions; @@ -40,6 +41,7 @@ import androidx.core.app.ActivityCompat; public class Intents { + private static final String TAG = "Intents"; /** * https://troll.tv/videos/watch/6edbd9d1-e3c5-4a6c-8491-646e2020469c @@ -68,20 +70,25 @@ public class Intents { // TODO, offer which version to download public static void Download(Context context, Video video) { - String url = video.getFiles().get(0).getFileDownloadUrl(); - // make sure it is a valid filename - String destFilename = video.getName().replaceAll("[^a-zA-Z0-9]", "_") + "." + MimeTypeMap.getFileExtensionFromUrl(URLUtil.guessFileName(url,null,null)); + if (video.getFiles().size() > 0) + { + String url = video.getFiles().get( 0 ).getFileDownloadUrl(); + // make sure it is a valid filename + String destFilename = video.getName().replaceAll( "[^a-zA-Z0-9]", "_" ) + "." + MimeTypeMap.getFileExtensionFromUrl( URLUtil.guessFileName( url, null, null ) ); - //Toast.makeText(context, destFilename, Toast.LENGTH_LONG).show(); - DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); - request.setDescription(video.getDescription()); - request.setTitle(video.getName()); - request.allowScanningByMediaScanner(); - request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); - request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, destFilename); + //Toast.makeText(context, destFilename, Toast.LENGTH_LONG).show(); + DownloadManager.Request request = new DownloadManager.Request( Uri.parse( url ) ); + request.setDescription( video.getDescription() ); + request.setTitle( video.getName() ); + request.allowScanningByMediaScanner(); + request.setNotificationVisibility( DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED ); + request.setDestinationInExternalPublicDir( Environment.DIRECTORY_DOWNLOADS, destFilename ); - // get download service and enqueue file - DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); - manager.enqueue(request); + // get download service and enqueue file + DownloadManager manager = (DownloadManager) context.getSystemService( Context.DOWNLOAD_SERVICE ); + manager.enqueue( request ); + } else { + Toast.makeText( context, R.string.api_error, Toast.LENGTH_LONG ).show(); + } } } From cdcfa8c28e22652d488d7c71c36579557c410da7 Mon Sep 17 00:00:00 2001 From: Olivier Bouillet Date: Fri, 25 Sep 2020 21:56:09 +0200 Subject: [PATCH 2/2] [Crash] : avoid crash when leaving the application --- .../peertube/service/VideoPlayerService.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java b/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java index 96e71b1..94414d7 100644 --- a/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java +++ b/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java @@ -113,8 +113,7 @@ public class VideoPlayerService extends Service { if (playbackState == ACTION_PLAY) { // this means that play is available, hence the audio is paused or stopped Log.v(TAG, "ACTION_PAUSE: " + playbackState); - unregisterReceiver(myNoisyAudioStreamReceiver); - myNoisyAudioStreamReceiver = null; + safeUnregisterReceiver(); } } }); @@ -137,14 +136,9 @@ public class VideoPlayerService extends Service { if (playerNotificationManager != null) { playerNotificationManager.setPlayer(null); } - //Was seeing an error when exiting the program about about not unregistering the receiver. - try { - if (null != myNoisyAudioStreamReceiver) { - this.unregisterReceiver(myNoisyAudioStreamReceiver); - } - } catch (Exception e) { - Log.e("VideoPlayerService", "attempted to unregister a nonregistered service"); - } + //Was seeing an error when exiting the program about not unregistering the receiver. + safeUnregisterReceiver(); + if (player != null) { player.release(); player = null; @@ -152,6 +146,15 @@ public class VideoPlayerService extends Service { super.onDestroy(); } + private void safeUnregisterReceiver() + { + try { + unregisterReceiver(myNoisyAudioStreamReceiver); + } catch (Exception e) { + Log.e("VideoPlayerService", "attempted to unregister a nonregistered service"); + } + } + @Nullable @Override public IBinder onBind(Intent intent) {