Skip to content

Commit

Permalink
Bugfix/issue 108/notification bar issue (#109)
Browse files Browse the repository at this point in the history
Issue-108 -  1)  removed the kill notification service \n
2) added a permissions fix \n
3) added MyPlayerNotificationManagerTest \n
4) removed fragment custom constructor.  \n
5) Removed custom thread call to the speed changer \n
  • Loading branch information
goldy1992 authored Nov 24, 2019
1 parent 03c5f8d commit 008e6f5
Show file tree
Hide file tree
Showing 24 changed files with 210 additions and 123 deletions.
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<application
android:name=".MikesMp3Player"
Expand Down Expand Up @@ -76,8 +77,6 @@
</intent-filter>
</service>

<service android:name=".service.KillNotificationService"/>

<receiver android:name="androidx.media.session.MediaButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

import android.app.Application;

/**
* Declared in case need in the future
*/
public class MikesMp3Player extends Application {
/**
* Declared in case need in the future
*/
@Override
public void onCreate() {
super.onCreate();
// startService(new Intent(this, KillNotificationService.class));
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private boolean validPosition(long position) {
}

private float getPositionAsFraction() {
Log.e(LOG_TAG, "current pos: " + currentPosition + ", seekerbar max" + seekerBar.getMax());
//Log.d(LOG_TAG, "current pos: " + currentPosition + ", seekerbar max" + seekerBar.getMax());
return currentPosition / (float) seekerBar.getMax();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void onConnected() {
super.onConnected();
MediaItem mediaItem = getIntent().getParcelableExtra(MEDIA_ITEM);
String itemLibraryId = MediaItemUtils.getLibraryId(mediaItem);
this.viewPageFragment = new SongListFragment(MediaItemType.FOLDER, itemLibraryId, getMediaActivityCompatComponent());
this.viewPageFragment = SongListFragment.newInstance(MediaItemType.FOLDER, itemLibraryId, getMediaActivityCompatComponent());
initialiseView(R.layout.activity_folder);
getSupportFragmentManager().beginTransaction().add(R.id.songListFragment, viewPageFragment).commit();
getSupportActionBar().setTitle(MediaItemUtils.getDirectoryName(mediaItem));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ public void onChildrenLoaded(@NonNull String parentId, @NonNull ArrayList<MediaB
MediaItemType category = (MediaItemType) MediaItemUtils.getExtra(ROOT_ITEM_TYPE, mediaItem);
MediaItemListFragment mediaItemListFragment;
switch (category) {
case SONGS: mediaItemListFragment = new SongListFragment(category, id, getMediaActivityCompatComponent());
case SONGS: mediaItemListFragment = SongListFragment.newInstance(category, id, getMediaActivityCompatComponent());
break;
case FOLDERS: mediaItemListFragment = new FolderListFragment(category, id, getMediaActivityCompatComponent());
case FOLDERS: mediaItemListFragment = FolderListFragment.newInstance(category, id, getMediaActivityCompatComponent());
break;
default:
mediaItemListFragment = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.github.goldy1992.mp3player.dagger.components.DaggerMediaActivityCompatComponent;
import com.github.goldy1992.mp3player.dagger.components.MediaActivityCompatComponent;
import com.github.goldy1992.mp3player.dagger.components.SplashScreenEntryActivityComponent;
import com.github.goldy1992.mp3player.service.MediaPlaybackServiceInjector;

import org.apache.commons.lang3.exception.ExceptionUtils;

Expand Down Expand Up @@ -149,6 +150,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
public void onPermissionGranted() {
Log.i(LOG_TAG, "permission granted");
setPermissionGranted(true);
createService();
Runnable r = new Thread(() -> mediaBrowserAdapter.connect());
runOnUiThread(r);
}
Expand Down Expand Up @@ -193,6 +195,10 @@ public void setMediaBrowserAdapter(MediaBrowserAdapter mediaBrowserAdapter) {
this.mediaBrowserAdapter = mediaBrowserAdapter;
}

private void createService() {
startService(new Intent(getApplicationContext(), MediaPlaybackServiceInjector.class));
}

private void initialiseDependencies() {
MediaActivityCompatComponent component = DaggerMediaActivityCompatComponent
.factory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
import com.github.goldy1992.mp3player.commons.MediaItemType;
import com.github.goldy1992.mp3player.dagger.components.MediaActivityCompatComponent;

import javax.inject.Inject;

import static com.github.goldy1992.mp3player.commons.Constants.MEDIA_ITEM;

public class FolderListFragment extends MediaItemListFragment {

@Inject
public FolderListFragment(MediaItemType mediaItemType, String id, MediaActivityCompatComponent component) {
super(mediaItemType, id, component);
public static FolderListFragment newInstance(MediaItemType mediaItemType, String id, MediaActivityCompatComponent component) {
FolderListFragment folderListFragment = new FolderListFragment();
folderListFragment.init(mediaItemType, id, component);
return folderListFragment;
}

@Override
public void itemSelected(MediaBrowserCompat.MediaItem item) {
Intent intent = intentMapper.getIntent(this.parentItemType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public abstract class MediaItemListFragment extends Fragment implements MyGeneri
private AlbumArtPainter albumArtPainter;
private MyGenericItemTouchListener myGenericItemTouchListener;

public MediaItemListFragment(MediaItemType mediaItemType, String id, MediaActivityCompatComponent component) {
public void init(MediaItemType mediaItemType, String id, MediaActivityCompatComponent component) {
this.parentItemType = mediaItemType;
this.parentItemTypeId = id;
injectDependencies(component);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
import com.github.goldy1992.mp3player.commons.MediaItemUtils;
import com.github.goldy1992.mp3player.dagger.components.MediaActivityCompatComponent;

import javax.inject.Inject;

public class SongListFragment extends MediaItemListFragment {

@Inject
public SongListFragment(MediaItemType mediaItemType, String id, MediaActivityCompatComponent compatComponent) {
super(mediaItemType, id, compatComponent);
public static SongListFragment newInstance(MediaItemType mediaItemType, String id, MediaActivityCompatComponent component) {
SongListFragment songListFragment = new SongListFragment();
songListFragment.init(mediaItemType, id, component);
return songListFragment;
}

@Override
public void itemSelected(MediaBrowserCompat.MediaItem item) {
mediaControllerAdapter.playFromMediaId(MediaItemUtils.getLibraryId(item), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.github.goldy1992.mp3player.dagger.modules.service.MediaSessionConnectorModule;
import com.github.goldy1992.mp3player.dagger.modules.service.SearchDatabaseModule;
import com.github.goldy1992.mp3player.service.MediaPlaybackService;
import com.google.android.exoplayer2.ui.PlayerNotificationManager.NotificationListener;

import javax.inject.Singleton;

Expand All @@ -31,7 +32,7 @@ public interface ServiceComponent {
@Component.Factory
interface Factory {
ServiceComponent create(@BindsInstance Context context,
@BindsInstance MediaPlaybackService mediaPlaybackService,
@BindsInstance NotificationListener notificationListener,
@BindsInstance String workerId);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.github.goldy1992.mp3player.service;

import android.app.Notification;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.v4.media.MediaBrowserCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.media.MediaBrowserServiceCompat;

import com.github.goldy1992.mp3player.service.library.ContentManager;
import com.github.goldy1992.mp3player.service.player.PlayerNotificationManagerCreator;
import com.google.android.exoplayer2.ui.PlayerNotificationManager.NotificationListener;

import java.util.List;

Expand All @@ -22,13 +26,12 @@
/**
* Created by Mike on 24/09/2017.
*/
public abstract class MediaPlaybackService extends MediaBrowserServiceCompat {
public abstract class MediaPlaybackService extends MediaBrowserServiceCompat implements NotificationListener {

private static final String LOG_TAG = "MEDIA_PLAYBACK_SERVICE";
private ContentManager contentManager;
private HandlerThread worker;
private Handler handler;
private PlayerNotificationManagerCreator playerNotificationManagerCreator;
private MediaSessionConnectorCreator mediaSessionConnectorCreator;
private MediaSessionCompat mediaSession;
private RootAuthenticator rootAuthenticator;
Expand All @@ -38,16 +41,23 @@ public abstract class MediaPlaybackService extends MediaBrowserServiceCompat {
@Override
public void onCreate() {
super.onCreate();
this.playerNotificationManagerCreator.create();
this.mediaSessionConnectorCreator.create();
setSessionToken(mediaSession.getSessionToken());
}

@Override
public int onStartCommand (Intent intent,
int flags,
int startId) {

Log.i(LOG_TAG, "breakpoint, on start command called");
return START_STICKY;
}


@Override
public BrowserRoot onGetRoot(String clientPackageName, int clientUid,
Bundle rootHints) {
public BrowserRoot onGetRoot(@NonNull String clientPackageName, int clientUid,
@Nullable Bundle rootHints) {
return rootAuthenticator.authenticate(clientPackageName, clientUid, rootHints);
}

Expand All @@ -73,6 +83,27 @@ public void onLoadChildren(@NonNull String parentId, @NonNull Result<List<MediaB
});
}

/**
* Called each time after the notification has been posted.
*
* <p>For a service, the {@code ongoing} flag can be used as an indicator as to whether it
* should be in the foreground.
*
* @param notificationId The id of the notification which has been posted.
* @param notification The {@link Notification}.
* @param ongoing Whether the notification is ongoing.
*/
@Override
public void onNotificationPosted(
int notificationId, Notification notification, boolean ongoing) {
// fix to make notifications removable on versions < oreo.
if (!ongoing) {
stopForeground(false);
} else {
startForeground(notificationId, notification);
}
}

@Override
public void onSearch(@NonNull String query, Bundle extras,
@NonNull Result<List<MediaBrowserCompat.MediaItem>> result) {
Expand All @@ -84,22 +115,6 @@ public void onSearch(@NonNull String query, Bundle extras,
});
}


@Override
public void onDestroy() {
super.onDestroy();
stopForeground(true);
mediaSession.release();
worker.quitSafely();
}

@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
mediaSession.release();
stopSelf();
}

public MediaSessionCompat getMediaSession() {
return mediaSession;
}
Expand Down Expand Up @@ -134,11 +149,6 @@ public void setMediaSessionConnectorCreator(MediaSessionConnectorCreator mediaSe
this.mediaSessionConnectorCreator = mediaSessionConnectorCreator;
}

@Inject
public void setPlayerNotificationManagerCreator(PlayerNotificationManagerCreator playerNotificationManagerCreator) {
this.playerNotificationManagerCreator = playerNotificationManagerCreator;
}

@VisibleForTesting
public HandlerThread getWorker() {
return worker;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.goldy1992.mp3player.service;

import com.github.goldy1992.mp3player.service.player.AudioBecomingNoisyBroadcastReceiver;
import com.github.goldy1992.mp3player.service.player.MyPlayerNotificationManager;
import com.google.android.exoplayer2.DefaultControlDispatcher;
import com.google.android.exoplayer2.Player;

Expand All @@ -10,15 +11,22 @@ public class MyControlDispatcher extends DefaultControlDispatcher {

private final AudioBecomingNoisyBroadcastReceiver audioBecomingNoisyBroadcastReceiver;

private final MyPlayerNotificationManager playerNotificationManager;

@Inject
public MyControlDispatcher(AudioBecomingNoisyBroadcastReceiver audioBecomingNoisyBroadcastReceiver) {
public MyControlDispatcher(AudioBecomingNoisyBroadcastReceiver audioBecomingNoisyBroadcastReceiver,
MyPlayerNotificationManager playerNotificationManager) {
this.audioBecomingNoisyBroadcastReceiver = audioBecomingNoisyBroadcastReceiver;
this.playerNotificationManager = playerNotificationManager;
}

@Override
public boolean dispatchSetPlayWhenReady(Player player, boolean playWhenReady) {
if (playWhenReady) {
audioBecomingNoisyBroadcastReceiver.register();
if (!this.playerNotificationManager.isActive()) {
this.playerNotificationManager.activate();
}
} else {
audioBecomingNoisyBroadcastReceiver.unregister();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package com.github.goldy1992.mp3player.service.player;

import android.os.Handler;
import android.support.v4.media.session.PlaybackStateCompat;

import com.github.goldy1992.mp3player.R;
import com.google.android.exoplayer2.Player;

import javax.inject.Inject;
import javax.inject.Named;

import static com.github.goldy1992.mp3player.commons.Constants.DECREASE_PLAYBACK_SPEED;

public class DecreaseSpeedProvider extends SpeedChangeActionProviderBase {

@Inject
public DecreaseSpeedProvider(@Named("worker") Handler handler) {
super(handler);
public DecreaseSpeedProvider() {
super();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package com.github.goldy1992.mp3player.service.player;

import android.os.Handler;
import android.support.v4.media.session.PlaybackStateCompat;

import com.github.goldy1992.mp3player.R;
import com.google.android.exoplayer2.Player;

import javax.inject.Inject;
import javax.inject.Named;

import static com.github.goldy1992.mp3player.commons.Constants.INCREASE_PLAYBACK_SPEED;

public class IncreaseSpeedProvider extends SpeedChangeActionProviderBase {

@Inject
public IncreaseSpeedProvider(@Named("worker")Handler handler) {
super(handler);
public IncreaseSpeedProvider() {
super();
}

@Override
Expand Down
Loading

0 comments on commit 008e6f5

Please sign in to comment.