Skip to content

Commit

Permalink
[android] refactor. less aggresive locking.
Browse files Browse the repository at this point in the history
  • Loading branch information
gubatron committed Feb 17, 2016
1 parent 15fcb77 commit f3e4886
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 40 deletions.
24 changes: 15 additions & 9 deletions android/apollo/src/com/andrew/apollo/MusicPlaybackService.java
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ public class MusicPlaybackService extends Service {
*/
private Cursor mCursor;

private final Object cursorLock = new Object();

/**
* The cursor used to retrieve info on the album the current track is
* part of, if any.
Expand Down Expand Up @@ -1024,7 +1026,7 @@ private void updateCursor(final long trackId) {
}

private void updateCursor(final String selection, final String[] selectionArgs) {
synchronized (this) {
synchronized (cursorLock) {
closeCursor();
mCursor = openCursorAndGoToFirst(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
PROJECTION, selection, selectionArgs);
Expand All @@ -1033,7 +1035,7 @@ private void updateCursor(final String selection, final String[] selectionArgs)
}

private void updateCursor(final Uri uri) {
synchronized (this) {
synchronized (cursorLock) {
closeCursor();
mCursor = openCursorAndGoToFirst(uri, PROJECTION, null, null);
}
Expand Down Expand Up @@ -1071,13 +1073,15 @@ private Cursor openCursorAndGoToFirst(Uri uri, String[] projection,
}

private void closeCursor() {
if (mCursor != null) {
mCursor.close();
mCursor = null;
}
if (mAlbumCursor != null) {
mAlbumCursor.close();
mAlbumCursor = null;
synchronized (cursorLock) {
if (mCursor != null) {
mCursor.close();
mCursor = null;
}
if (mAlbumCursor != null) {
mAlbumCursor.close();
mAlbumCursor = null;
}
}
}

Expand All @@ -1097,7 +1101,9 @@ private void openCurrentAndNext() {
* otherwise.
*/
private void openCurrentAndMaybeNext(final boolean openNext) {
Log.d(TAG, "openCurrentAndMaybeNext() waiting for synchronized(this)");
synchronized (this) {
Log.d(TAG, "openCurrentAndMaybeNext() DONE waiting for synchronized(this)\n");
closeCursor();

if (mPlayListLen == 0 || mPlayList == null) {
Expand Down
67 changes: 36 additions & 31 deletions android/apollo/src/com/andrew/apollo/utils/MusicUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -688,12 +688,8 @@ public static void playAll(final long[] list, int position,
}
final long currentId = mService.getAudioId();
final int currentQueuePosition = getQueuePosition();
if (position != -1 && currentQueuePosition == position && currentId == list[position]) {
final long[] playlist = getQueue();
if (Arrays.equals(list, playlist)) {
mService.play();
return;
}
if (continuedPlayingCurrentQueue(list, position, currentId, currentQueuePosition)) {
return;
}
if (position < 0) {
position = 0;
Expand All @@ -706,6 +702,23 @@ public static void playAll(final long[] list, int position,
}
}

private static boolean continuedPlayingCurrentQueue(long[] list, int position, long currentId, int currentQueuePosition) {
if (position != -1 && currentQueuePosition == position && currentId == list[position]) {
final long[] playlist = getQueue();
if (Arrays.equals(list, playlist)) {
try {
mService.play();
} catch (Throwable ignored) {
ignored.printStackTrace();

return false;
}
return true;
}
}
return false;
}

/**
* @param list The list to enqueue.
*/
Expand Down Expand Up @@ -735,13 +748,9 @@ public static void shuffleAll(final Context context) {
mService.setShuffleMode(MusicPlaybackService.SHUFFLE_NORMAL);
final long mCurrentId = mService.getAudioId();
final int mCurrentQueuePosition = getQueuePosition();
if (position != -1 && mCurrentQueuePosition == position
&& mCurrentId == mTrackList[position]) {
final long[] mPlaylist = getQueue();
if (Arrays.equals(mTrackList, mPlaylist)) {
mService.play();
return;
}

if (continuedPlayingCurrentQueue(mTrackList, position, mCurrentId, mCurrentQueuePosition)) {
return;
}

if (mTrackList.length > 0) {
Expand Down Expand Up @@ -1079,21 +1088,25 @@ public static String getSongCountForAlbum(final Context context, final long id)
Cursor cursor = context.getContentResolver().query(uri, new String[]{
AlbumColumns.NUMBER_OF_SONGS
}, null, null, null);
String songCount = null;
if (cursor != null) {
cursor.moveToFirst();
if (!cursor.isAfterLast()) {
songCount = cursor.getString(0);
}
cursor.close();
}
return songCount;
return getFirstStringResult(cursor);
} catch (Throwable e) {
e.printStackTrace();
return null;
}
}

private static String getFirstStringResult(Cursor cursor) {
String result = null;
if (cursor != null) {
cursor.moveToFirst();
if (!cursor.isAfterLast()) {
result = cursor.getString(0);
}
cursor.close();
}
return result;
}

/**
* @param context The {@link Context} to use.
* @param id The id of the album.
Expand All @@ -1108,15 +1121,7 @@ public static String getReleaseDateForAlbum(final Context context, final long id
Cursor cursor = context.getContentResolver().query(uri, new String[]{
AlbumColumns.FIRST_YEAR
}, null, null, null);
String releaseDate = null;
if (cursor != null) {
cursor.moveToFirst();
if (!cursor.isAfterLast()) {
releaseDate = cursor.getString(0);
}
cursor.close();
}
return releaseDate;
return getFirstStringResult(cursor);
} catch (Throwable e) {
// ignore this error since it's not critical
LOG.error("Error getting release date for album", e);
Expand Down

0 comments on commit f3e4886

Please sign in to comment.