From 62216ec55a1657262cf770dc96c9ddd859f2d6d5 Mon Sep 17 00:00:00 2001 From: Stevan Medic Date: Wed, 23 Mar 2016 22:17:16 +0100 Subject: [PATCH] [01] Added support for devices older than Lollipop - minSdk=16 --- app/src/main/AndroidManifest.xml | 6 ++++ .../smedic/tubtub/BackgroundAudioService.java | 34 ++++++++++--------- .../smedic/tubtub/database/YouTubeSqlDb.java | 3 ++ .../receivers/MediaButtonIntentReceiver.java | 16 +++++++++ 4 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/com/smedic/tubtub/receivers/MediaButtonIntentReceiver.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index cbccb8b..39e8c20 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -35,5 +35,11 @@ android:name=".BackgroundAudioService" android:process=":youtube"> + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/smedic/tubtub/BackgroundAudioService.java b/app/src/main/java/com/smedic/tubtub/BackgroundAudioService.java index 5d27e17..c33d9af 100644 --- a/app/src/main/java/com/smedic/tubtub/BackgroundAudioService.java +++ b/app/src/main/java/com/smedic/tubtub/BackgroundAudioService.java @@ -18,6 +18,7 @@ import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; @@ -34,6 +35,7 @@ import android.util.Log; import android.util.SparseArray; +import com.smedic.tubtub.receivers.MediaButtonIntentReceiver; import com.smedic.tubtub.utils.Config; import com.squareup.picasso.Picasso; import com.squareup.picasso.Target; @@ -130,7 +132,6 @@ private void handleIntent(Intent intent) { * @param intent */ private void handleMedia(Intent intent) { - int intentMediaType = intent.getIntExtra(Config.YOUTUBE_MEDIA_TYPE, Config.YOUTUBE_NO_NEW_REQUEST); switch (intentMediaType) { case Config.YOUTUBE_NO_NEW_REQUEST: //video is paused,so no new playback requests should be processed @@ -166,7 +167,17 @@ private void initMediaSessions() { // permission in AndroidManifest.xml. mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK); - mSession = new MediaSessionCompat(getApplicationContext(), "simple player session"); + ComponentName eventReceiver = new ComponentName(getApplicationContext().getPackageName(), + MediaButtonIntentReceiver.class.getName()); + PendingIntent buttonReceiverIntent = PendingIntent.getBroadcast( + getApplicationContext(), + 0, + new Intent(Intent.ACTION_MEDIA_BUTTON), + PendingIntent.FLAG_UPDATE_CURRENT + ); + + mSession = new MediaSessionCompat(getApplicationContext(), "simple player session", + eventReceiver, buttonReceiverIntent); try { mController = new MediaControllerCompat(getApplicationContext(), mSession.getSessionToken()); @@ -181,6 +192,7 @@ public void onPlay() { @Override public void onPause() { + super.onPause(); pauseVideo(); buildNotification(generateAction(android.R.drawable.ic_media_play, "Play", ACTION_PLAY)); @@ -252,6 +264,7 @@ private void buildNotification(NotificationCompat.Action action) { builder.setShowWhen(false); builder.setContentIntent(clickPendingIntent); builder.setDeleteIntent(stopPendingIntent); + builder.setOngoing(false); builder.setStyle(style); //load bitmap for largeScreen @@ -274,19 +287,9 @@ public void onPrepareLoad(Drawable drawable) { }); } - if (mediaType == Config.YOUTUBE_PLAYLIST) { - builder.addAction(generateAction(android.R.drawable.ic_media_previous, "Previous", ACTION_PREVIOUS)); - } else { - builder.addAction(0, null, null); - } - + builder.addAction(generateAction(android.R.drawable.ic_media_previous, "Previous", ACTION_PREVIOUS)); builder.addAction(action); - - if (mediaType == Config.YOUTUBE_PLAYLIST) { - builder.addAction(generateAction(android.R.drawable.ic_media_next, "Next", ACTION_NEXT)); - } else { - builder.addAction(0, null, null); - } + builder.addAction(generateAction(android.R.drawable.ic_media_next, "Next", ACTION_NEXT)); style.setShowActionsInCompactView(0, 1, 2); @@ -328,7 +331,6 @@ private NotificationCompat.Action generateAction(int icon, String title, String * Plays next video */ private void playNext() { - if (previousWasCalled) { previousWasCalled = false; iterator.next(); @@ -347,7 +349,6 @@ private void playNext() { * Plays previous video */ private void playPrevious() { - if (nextWasCalled) { iterator.previous(); nextWasCalled = false; @@ -398,6 +399,7 @@ private void stopPlayer() { * Extracts link from youtube video ID, so mediaPlayer can play it */ private void extractUrlAndPlay() { + Log.d(TAG, "extract url"); String youtubeLink = "http://youtube.com/watch?v=" + videoItem.getId(); YouTubeUriExtractor ytEx = new YouTubeUriExtractor(this) { @Override diff --git a/app/src/main/java/com/smedic/tubtub/database/YouTubeSqlDb.java b/app/src/main/java/com/smedic/tubtub/database/YouTubeSqlDb.java index a16c35b..d4ed7b6 100644 --- a/app/src/main/java/com/smedic/tubtub/database/YouTubeSqlDb.java +++ b/app/src/main/java/com/smedic/tubtub/database/YouTubeSqlDb.java @@ -125,6 +125,9 @@ private Videos(String tableName) { * @return */ public boolean create(YouTubeVideo video) { + if(checkIfExists(video.getId())){ + return false; + } // Gets the data repository in write mode SQLiteDatabase db = dbHelper.getWritableDatabase(); diff --git a/app/src/main/java/com/smedic/tubtub/receivers/MediaButtonIntentReceiver.java b/app/src/main/java/com/smedic/tubtub/receivers/MediaButtonIntentReceiver.java new file mode 100644 index 0000000..7a3f7ea --- /dev/null +++ b/app/src/main/java/com/smedic/tubtub/receivers/MediaButtonIntentReceiver.java @@ -0,0 +1,16 @@ +package com.smedic.tubtub.receivers; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +/** + * Created by smedic on 23.3.16.. + */ +public class MediaButtonIntentReceiver extends BroadcastReceiver{ + @Override + public void onReceive(Context context, Intent intent) { + Log.d("SMEDIC" , "onRecive"); + } +}