Skip to content

Commit

Permalink
[01] Added support for devices older than Lollipop - minSdk=16
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevan Medic committed Mar 23, 2016
1 parent b8f78c0 commit 62216ec
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,11 @@
android:name=".BackgroundAudioService"
android:process=":youtube"></service>

<receiver android:name="receivers.MediaButtonIntentReceiver" android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>

</application>
</manifest>
34 changes: 18 additions & 16 deletions app/src/main/java/com/smedic/tubtub/BackgroundAudioService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand All @@ -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));
Expand Down Expand Up @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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();
Expand All @@ -347,7 +349,6 @@ private void playNext() {
* Plays previous video
*/
private void playPrevious() {

if (nextWasCalled) {
iterator.previous();
nextWasCalled = false;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit 62216ec

Please sign in to comment.