Skip to content

Commit

Permalink
Feature/issue 110/content observer (#113)
Browse files Browse the repository at this point in the history
Issue-110 - added functionality to react to changes in audio files, i.e. the add/deletion of files and reacted by updating the search database accordingly. Also fixed bug viewing songs from folders.
  • Loading branch information
goldy1992 authored Dec 1, 2019
1 parent 852bc18 commit 54058de
Show file tree
Hide file tree
Showing 44 changed files with 1,179 additions and 223 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
applicationId "com.github.goldy1992.mp3player"
minSdkVersion MARSHMALLOW
targetSdkVersion PIE
versionCode 3
versionName "1.0.2"
versionCode 4
versionName "1.1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ public abstract class MyGenericRecycleViewAdapter extends MediaItemRecyclerViewA
private static final String EMPTY_MEDIA_ID = "EMPTY_MEDIA_ID";
final int EMPTY_VIEW_TYPE = -1;



private boolean isInitialised = false;
private final MediaItem EMPTY_LIST_ITEM = buildEmptyListMediaItem();

public MyGenericRecycleViewAdapter(AlbumArtPainter albumArtPainter, Handler mainHandler) {
Expand All @@ -45,15 +42,14 @@ public int getItemCount() {

@Override
public void onChildrenLoaded(@NonNull String parentId, @NonNull ArrayList<MediaItem> children) {
if (!isInitialised && children.isEmpty()) {
addNoChildrenFoundItem();
}


if (!children.isEmpty()) {
this.items.addAll(children);
this.items = children;
mainHandler.post(this::notifyDataSetChanged);
} else {
addNoChildrenFoundItem();
}
this.isInitialised = true;
}
@Override
public MediaItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public MediaItemBuilder setMediaUri(Uri mediaUri) {
return this;
}

public MediaItemBuilder setFile(File file) {
public MediaItemBuilder setDirectoryFile(File file) {
extras.putSerializable(META_DATA_DIRECTORY, file);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.goldy1992.mp3player.commons;

import androidx.annotation.NonNull;

import org.apache.commons.lang3.StringUtils;

public final class Normaliser {

private Normaliser() {

}

public static String normalise(@NonNull String query) {
query = StringUtils.stripAccents(query);
return query.trim().toUpperCase();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.app.Notification;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
Expand All @@ -16,6 +15,8 @@
import androidx.media.MediaBrowserServiceCompat;

import com.github.goldy1992.mp3player.service.library.ContentManager;
import com.github.goldy1992.mp3player.service.library.content.observers.MediaStoreObservers;
import com.github.goldy1992.mp3player.service.library.search.managers.SearchDatabaseManagers;
import com.google.android.exoplayer2.ui.PlayerNotificationManager.NotificationListener;

import java.util.List;
Expand All @@ -29,27 +30,31 @@
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 MediaSessionConnectorCreator mediaSessionConnectorCreator;
private MediaSessionCompat mediaSession;
private RootAuthenticator rootAuthenticator;
private MediaStoreObservers mediaStoreObservers;
private SearchDatabaseManagers searchDatabaseManagers;

abstract void initialiseDependencies();

@Override
public void onCreate() {
super.onCreate();
this.mediaSessionConnectorCreator.create();
setSessionToken(mediaSession.getSessionToken());
this.setSessionToken(mediaSession.getSessionToken());
this.mediaStoreObservers.init(this);
this.searchDatabaseManagers.reindexAll();
}

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

int flags,
int startId) {
Log.i(LOG_TAG, "breakpoint, on start command called");
return START_STICKY;
}
Expand Down Expand Up @@ -94,14 +99,15 @@ public void onLoadChildren(@NonNull String parentId, @NonNull Result<List<MediaB
* @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);
}
public void onNotificationPosted(int notificationId,
Notification notification,
boolean ongoing) {
// fix to make notifications removable
if (!ongoing) {
stopForeground(false);
} else {
startForeground(notificationId, notification);
}
}

@Override
Expand All @@ -115,6 +121,12 @@ public void onSearch(@NonNull String query, Bundle extras,
});
}

@Override
public void onDestroy() {
super.onDestroy();
mediaStoreObservers.unregisterAll();
}

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

@Inject
public void setMediaStoreObservers(MediaStoreObservers mediaStoreObservers) {
this.mediaStoreObservers = mediaStoreObservers;
}

@Inject
public void setSearchDatabaseManagers(SearchDatabaseManagers searchDatabaseManagers) {
this.searchDatabaseManagers = searchDatabaseManagers;
}

@VisibleForTesting
public HandlerThread getWorker() {
return worker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@
import com.github.goldy1992.mp3player.service.library.content.request.ContentRequest;
import com.github.goldy1992.mp3player.service.library.content.request.ContentRequestParser;
import com.github.goldy1992.mp3player.service.library.content.retriever.ContentRetriever;
import com.github.goldy1992.mp3player.service.library.content.retriever.MediaItemFromIdRetriever;
import com.github.goldy1992.mp3player.service.library.content.retriever.RootRetriever;
import com.github.goldy1992.mp3player.service.library.content.retriever.SongFromUriRetriever;
import com.github.goldy1992.mp3player.service.library.content.searcher.ContentSearcher;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

import static android.support.v4.media.MediaBrowserCompat.MediaItem;
import static com.github.goldy1992.mp3player.commons.Normaliser.normalise;

@Singleton
public class ContentManager {

public static final String CONTENT_SCHEME = "content";
Expand All @@ -34,17 +37,20 @@ public class ContentManager {
private final RootRetriever rootRetriever;
private final ContentRequestParser contentRequestParser;
private final SongFromUriRetriever songFromUriRetriever;
private final MediaItemFromIdRetriever mediaItemFromIdRetriever;

@Inject
public ContentManager(ContentRetrievers contentRetrievers,
ContentSearchers contentSearchers,
ContentRequestParser contentRequestParser,
SongFromUriRetriever songFromUriRetriever) {
SongFromUriRetriever songFromUriRetriever,
MediaItemFromIdRetriever mediaItemFromIdRetriever) {
this.contentSearchers = contentSearchers;
this.contentRetrievers = contentRetrievers;
this.contentRequestParser = contentRequestParser;
this.rootRetriever = contentRetrievers.getRoot();
this.songFromUriRetriever = songFromUriRetriever;
this.mediaItemFromIdRetriever = mediaItemFromIdRetriever;
}
/**
* The id is in the following format
Expand Down Expand Up @@ -88,16 +94,21 @@ public MediaItem getItem(@NonNull Uri uri) {
}
/**
*
* @param id
* @return
*/
@Nullable
public MediaItem getItem(long id) {
/* TODO: in the future the content manager will need to know which type of URI will need
* to be parsed, e.g. song, album, artist etc. */
return mediaItemFromIdRetriever.getItem(id);
}
/**
*
* @param id the id of the playlist
* @return the playlist
*/
public List<MediaItem> getPlaylist(String id) {
return getChildren(id);
}

private String normalise(@NonNull String query) {
query = StringUtils.stripAccents(query);
return query.trim().toUpperCase();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

import javax.inject.Inject;

public class FoldersResultFilter extends ResultsFilter {
public class FolderSearchResultsFilter implements ResultsFilter {

@Inject
public FoldersResultFilter() {
public FolderSearchResultsFilter() {
super();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import java.util.List;

public abstract class ResultsFilter {
public interface ResultsFilter {

public abstract List<MediaItem> filter(@NonNull String query, @NonNull List<MediaItem> results);
List<MediaItem> filter(@NonNull String query, @NonNull List<MediaItem> results);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.goldy1992.mp3player.service.library.content.filter;

import android.support.v4.media.MediaBrowserCompat.MediaItem;

import androidx.annotation.NonNull;

import com.github.goldy1992.mp3player.commons.MediaItemUtils;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import javax.inject.Inject;

public class SongsFromFolderResultsFilter implements ResultsFilter {


@Inject
public SongsFromFolderResultsFilter() {
/* Empty constructor declared for dagger insert */
}


@Override
public List<MediaItem> filter(@NonNull String query,
@NonNull List<MediaItem> results) {
File queryPath = new File(query);
Iterator<MediaItem> iterator = results.listIterator();

while(iterator.hasNext()) {
MediaItem currentItem = iterator.next();
String directoryPath = MediaItemUtils.getDirectoryPath(currentItem);
if (directoryPath == null || !directoryPath.equalsIgnoreCase(queryPath.getAbsolutePath().toUpperCase())) {
iterator.remove();
}
}
return results;
}
}
Loading

0 comments on commit 54058de

Please sign in to comment.