Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Commit

Permalink
Performance Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Sogolumbo committed Jul 22, 2019
1 parent 7380f4c commit 04fc392
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
final ArrayList<Song> songs = getArguments().getParcelableArrayList("songs");

int[] songIds = new int[songs.size()];
boolean[][] isSongInPlaylist = new boolean[playlists.size()][songs.size()];
if(songs != null)
{

Expand All @@ -60,9 +59,12 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
for (int i = 0; i < playlists.size(); i++) {
int playlistId = playlists.get(i).id;

isSongInPlaylist [i] = PlaylistsUtil.doPlaylistContains(getActivity(), playlistId, songIds);
boolean isAnySongInPlaylist = PlaylistsUtil.playlistContainsAnySong(getActivity(), isSongInPlaylist[i]);
boolean areAllSongsInPlaylist = PlaylistsUtil.playlistContainsAllSongs(getActivity(), isSongInPlaylist[i]);
long startTime = System.currentTimeMillis();//TODO: remove stopwatch

boolean isAnySongInPlaylist = PlaylistsUtil.doPlaylistContainsAnySong(getActivity(), playlistId, songIds);
long stopTime1 = System.currentTimeMillis();//TODO: remove stopwatch
boolean areAllSongsInPlaylist = PlaylistsUtil.doPlaylistContainsAllSongs(getActivity(), playlistId, songIds);
long stopTime2 = System.currentTimeMillis();//TODO: remove stopwatch

//TODO: display checkboxes instead of checkmark
if (isAnySongInPlaylist) {
Expand All @@ -73,6 +75,15 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
playlistNames[i + 1] = playlists.get(i).name + " (\u2713)"; //Add checkmark in brackets
}
}

long stopTimeTotal = System.currentTimeMillis();//TODO: remove stopwatch
long Time1 = stopTime1 - startTime;
long Time2 = stopTime2 - stopTime1;
long Time3 = stopTimeTotal - stopTime2;
long TotalTime = stopTimeTotal - startTime;//TODO: remove stopwatch
long unecessary = 7;//TODO: remove stopwatch
unecessary++;
long c = unecessary;
}
}

Expand All @@ -87,7 +98,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
CreatePlaylistDialog.create(songs).show(getActivity().getSupportFragmentManager(), "ADD_TO_PLAYLIST");
} else {
materialDialog.dismiss();
PlaylistsUtil.addToPlaylistWithoutDuplicates(getActivity(), songs, playlists.get(i - 1).id, isSongInPlaylist[i-1], true);
PlaylistsUtil.addToPlaylistWithoutDuplicates(getActivity(), songs, songIds, playlists.get(i - 1).id, true);
}
})
.build();
Expand Down
156 changes: 150 additions & 6 deletions app/src/main/java/com/kabouzeid/gramophone/util/PlaylistsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,24 +272,54 @@ else if(songIdsSorted[songIndex] == playlistSong)
}
} catch (SecurityException ignored) {
}

//long[] partTimes = new long[songIds.length];//TODO: remove stopwatch
//long startTime = System.currentTimeMillis();//TODO: remove stopwatch
//Revert the sorting to return the right results
PlaylistContainsSongOriginalOrder = PlaylistContainsSongSorted.clone();
for(int i = 0; i < songIdsSorted.length; i++){
//partTimes[i] = System.currentTimeMillis();//TODO: remove stopwatch
if(songIdsOriginalOrder[i] == songIdsSorted[i]){
continue;
}
else{
int indexOfSongInSortedArray = -1;
for (int j = 0; j < songIdsSorted.length; j++) {
if (songIdsOriginalOrder[i] == songIdsSorted[j]) {
indexOfSongInSortedArray = j;
break;
int indexOfSongInSortedArray = i;
int compareValue;

int max = songIds.length;
int min = -1;
while(max - min > 15){ //this is a random number which I hope will result in a good efficiency. Tests showed it should be below 100.
compareValue = Integer.compare(songIdsOriginalOrder[i], songIdsSorted[indexOfSongInSortedArray]);
if(compareValue > 0){
min = indexOfSongInSortedArray;
}
else{
max = indexOfSongInSortedArray;
}
indexOfSongInSortedArray = (min + max)/2;
}
indexOfSongInSortedArray = min + 1;
while (songIdsOriginalOrder[i] != songIdsSorted[indexOfSongInSortedArray]){
indexOfSongInSortedArray++;
}
PlaylistContainsSongOriginalOrder[i] = PlaylistContainsSongSorted[indexOfSongInSortedArray];
}
}
/*
long stopTimeTotal = System.currentTimeMillis();//TODO: remove stopwatch
long[] diffs = new long[songIds.length];//TODO: remove stopwatch
diffs[songIds.length-1] = stopTimeTotal - partTimes[songIds.length-1];
long min = diffs[songIds.length-1]; //TODO: remove stopwatch
long max = diffs[songIds.length-1]; //TODO: remove stopwatch
long sum = diffs[songIds.length-1]; //TODO: remove stopwatch
for (int i = 0; i < songIdsSorted.length - 1; i++) {
diffs[i] = partTimes[i+1] - partTimes[i];
sum += diffs[i];
min = Math.min(diffs[i], min);
max = Math.max(diffs[i], max);
}
float mean = (float)sum / partTimes.length;
long TotalTime = stopTimeTotal - startTime;//TODO: remove stopwatch
*/
return PlaylistContainsSongOriginalOrder;
}
else{
Expand All @@ -308,6 +338,63 @@ public static int playlistContainsCount(@NonNull final Context context, boolean[
}
return count;
}
public static boolean doPlaylistContainsAnySong(@NonNull final Context context, final long playlistId, final int[] songIds) {
if (playlistId != -1) {
int[] songIdsSorted = songIds.clone();
java.util.Arrays.sort(songIdsSorted);

boolean result = false;

try {
Cursor playlistSongs = context.getContentResolver().query(
MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId),
new String[]{MediaStore.Audio.Playlists.Members.AUDIO_ID}, null, new String[]{}, MediaStore.Audio.Playlists.Members.AUDIO_ID + " ASC");

if (playlistSongs != null && playlistSongs.getCount() > 0) {
playlistSongs.moveToNext(); //goes to first element

int songIndex = 0;
int playlistIndex = 0;
while (true)
{
if(songIndex >= songIdsSorted.length){
break;
}
int playlistSong = playlistSongs.getInt(0);
if(songIdsSorted[songIndex] > playlistSong)
{
playlistIndex++;
if(playlistIndex < playlistSongs.getCount())
{
playlistSongs.moveToNext();
}
else
{
break;
}
}
else if (songIdsSorted[songIndex] < playlistSong)
{
songIndex++;
}
else if(songIdsSorted[songIndex] == playlistSong)
{
result = true;
songIndex++;
break;
}
}
playlistSongs.close();
}
} catch (SecurityException ignored) {
}

return result;
}
else{
throw new IllegalArgumentException("Must be a non-negative integer");
}
}
public static boolean playlistContainsAnySong(@NonNull final Context context, boolean[] isSongInPlaylist) {
for(boolean isThisSongInPlaylist : isSongInPlaylist){
if(isThisSongInPlaylist){
Expand All @@ -316,6 +403,63 @@ public static boolean playlistContainsAnySong(@NonNull final Context context, bo
}
return false;
}
public static boolean doPlaylistContainsAllSongs(@NonNull final Context context, final long playlistId, final int[] songIds) {
if (playlistId != -1) {
int[] songIdsSorted = songIds.clone();
java.util.Arrays.sort(songIdsSorted);

boolean result = true;

try {
Cursor playlistSongs = context.getContentResolver().query(
MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId),
new String[]{MediaStore.Audio.Playlists.Members.AUDIO_ID}, null, new String[]{}, MediaStore.Audio.Playlists.Members.AUDIO_ID + " ASC");

if (playlistSongs != null && playlistSongs.getCount() > 0) {
playlistSongs.moveToNext(); //goes to first element

int songIndex = 0;
int playlistIndex = 0;
while (true)
{
if(songIndex >= songIdsSorted.length){
break;
}
int playlistSong = playlistSongs.getInt(0);
if(songIdsSorted[songIndex] > playlistSong)
{
playlistIndex++;
if(playlistIndex < playlistSongs.getCount())
{
playlistSongs.moveToNext();
}
else
{
break;
}
}
else if (songIdsSorted[songIndex] < playlistSong)
{
result = false;
songIndex++;
break;
}
else if(songIdsSorted[songIndex] == playlistSong)
{
songIndex++;
}
}
playlistSongs.close();
}
} catch (SecurityException ignored) {
}

return result;
}
else{
throw new IllegalArgumentException("Must be a non-negative integer");
}
}
public static boolean playlistContainsAllSongs(@NonNull final Context context, boolean[] isSongInPlaylist) {
for(boolean isThisSongInPlaylist : isSongInPlaylist){
if(!isThisSongInPlaylist){
Expand Down

0 comments on commit 04fc392

Please sign in to comment.