Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 493aa1d6 authored by Rohit Yengisetty's avatar Rohit Yengisetty Committed by linus_lee
Browse files

Eleven - Introducing the ability to filter a list items into two - localizable

and non-localizable based on a specified attribute of the item.

Udpating the SongFragment  and ArtistFragment to list localizable songs
towards the top, while pushing the others towards the bottom of the list.
Also, updated the section creator logic to *not* create sections for the non-
latin named songs.

https://cyanogen.atlassian.net/browse/MUSIC-73

Change-Id: I5aaa98ae4312a9f1d142a48aaf025bdbc7e0150b

Conflicts:
	src/com/cyngn/eleven/loaders/AlbumLoader.java
	src/com/cyngn/eleven/loaders/ArtistLoader.java
	src/com/cyngn/eleven/loaders/SongLoader.java
parent 20a4fe3d
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -183,6 +183,8 @@

    <string name="header_5_plus_albums">5+ albums</string>

    <string name="header_other">"Other"</string>

    <string name="footer_search_artists">Show all artists</string>
    <string name="footer_search_albums">Show all albums</string>
    <string name="footer_search_songs">Show all songs</string>
+22 −4
Original line number Diff line number Diff line
@@ -17,14 +17,12 @@ import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.AlbumColumns;

import com.cyngn.eleven.R;
import com.cyngn.eleven.model.Album;
import com.cyngn.eleven.sectionadapter.SectionCreator;
import com.cyngn.eleven.utils.Lists;
import com.cyngn.eleven.utils.MusicUtils;
import com.cyngn.eleven.utils.PreferenceUtils;
import com.cyngn.eleven.utils.SectionCreatorUtils;
import com.cyngn.eleven.utils.SortOrder;
import com.cyngn.eleven.utils.SortUtils;

import java.util.ArrayList;
import java.util.List;
@@ -40,7 +38,7 @@ public class AlbumLoader extends SectionCreator.SimpleListLoader<Album> {
    /**
     * The result
     */
    private final ArrayList<Album> mAlbumsList = Lists.newArrayList();
    private ArrayList<Album> mAlbumsList = Lists.newArrayList();

    /**
     * The {@link Cursor} used to run the query.
@@ -99,9 +97,29 @@ public class AlbumLoader extends SectionCreator.SimpleListLoader<Album> {
            mCursor = null;
        }

        // requested album ordering
        String albumSortOrder = PreferenceUtils.getInstance(mContext).getAlbumSortOrder();

        // run a custom localized sort to try to fit items in to header buckets more nicely
        if (shouldEvokeCustomSortRoutine(albumSortOrder)) {
            mAlbumsList = SortUtils.localizeSortList(mAlbumsList, albumSortOrder);
        }

        return mAlbumsList;
    }

    /**
     * Evoke custom sorting routine if the sorting attribute is a String. MediaProvider's sort
     * can be trusted in other instances
     * @param sortOrder
     * @return
     */
    private boolean shouldEvokeCustomSortRoutine(String sortOrder) {
        return sortOrder.equals(SortOrder.AlbumSortOrder.ALBUM_A_Z) ||
               sortOrder.equals(SortOrder.AlbumSortOrder.ALBUM_Z_A) ||
               sortOrder.equals(SortOrder.AlbumSortOrder.ALBUM_ARTIST);
    }

    /**
     * Creates the {@link Cursor} used to run the query.
     * 
+22 −3
Original line number Diff line number Diff line
@@ -17,11 +17,12 @@ import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.ArtistColumns;

import com.cyngn.eleven.R;
import com.cyngn.eleven.model.Artist;
import com.cyngn.eleven.sectionadapter.SectionCreator;
import com.cyngn.eleven.utils.Lists;
import com.cyngn.eleven.utils.PreferenceUtils;
import com.cyngn.eleven.utils.SortOrder;
import com.cyngn.eleven.utils.SortUtils;

import java.util.ArrayList;
import java.util.List;
@@ -37,7 +38,7 @@ public class ArtistLoader extends SectionCreator.SimpleListLoader<Artist> {
    /**
     * The result
     */
    private final ArrayList<Artist> mArtistsList = Lists.newArrayList();
    private ArrayList<Artist> mArtistsList = Lists.newArrayList();

    /**
     * The {@link Cursor} used to run the query.
@@ -83,7 +84,6 @@ public class ArtistLoader extends SectionCreator.SimpleListLoader<Artist> {
                // Create a new artist
                final Artist artist = new Artist(id, artistName, songCount, albumCount);

                // Add everything up
                mArtistsList.add(artist);
            } while (mCursor.moveToNext());
        }
@@ -92,9 +92,28 @@ public class ArtistLoader extends SectionCreator.SimpleListLoader<Artist> {
            mCursor.close();
            mCursor = null;
        }

        // requested artist ordering
        String artistSortOrder = PreferenceUtils.getInstance(mContext).getArtistSortOrder();
        // run a custom localized sort to try to fit items in to header buckets more nicely
        if (shouldEvokeCustomSortRoutine(artistSortOrder)) {
            mArtistsList = SortUtils.localizeSortList(mArtistsList, artistSortOrder);
        }

        return mArtistsList;
    }

    /**
     * Evoke custom sorting routine if the sorting attribute is a String. MediaProvider's sort
     * can be trusted in other instances
     * @param sortOrder
     * @return
     */
    private boolean shouldEvokeCustomSortRoutine(String sortOrder) {
        return sortOrder.equals(SortOrder.ArtistSortOrder.ARTIST_A_Z) ||
               sortOrder.equals(SortOrder.ArtistSortOrder.ARTIST_Z_A);
    }

    /**
     * Creates the {@link Cursor} used to run the query.
     * 
+28 −3
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@ import com.cyngn.eleven.sectionadapter.SectionCreator;
import com.cyngn.eleven.utils.Lists;
import com.cyngn.eleven.utils.MusicUtils;
import com.cyngn.eleven.utils.PreferenceUtils;
import com.cyngn.eleven.utils.SortOrder;
import com.cyngn.eleven.utils.SortUtils;

import java.util.ArrayList;
import java.util.List;
@@ -37,7 +39,7 @@ public class SongLoader extends SectionCreator.SimpleListLoader<Song> {
    /**
     * The result
     */
    protected final ArrayList<Song> mSongList = Lists.newArrayList();
    protected ArrayList<Song> mSongList = Lists.newArrayList();

    /**
     * The {@link Cursor} used to run the query.
@@ -60,6 +62,7 @@ public class SongLoader extends SectionCreator.SimpleListLoader<Song> {
    public List<Song> loadInBackground() {
        // Create the Cursor
        mCursor = getCursor();

        // Gather the data
        if (mCursor != null && mCursor.moveToFirst()) {
            do {
@@ -88,9 +91,9 @@ public class SongLoader extends SectionCreator.SimpleListLoader<Song> {
                final int year = mCursor.getInt(6);

                // Create a new song
                final Song song = new Song(id, songName, artist, album, albumId, durationInSecs, year);
                final Song song = new Song(id, songName, artist, album, albumId,
                                            durationInSecs, year);

                // Add everything up
                mSongList.add(song);
            } while (mCursor.moveToNext());
        }
@@ -99,9 +102,31 @@ public class SongLoader extends SectionCreator.SimpleListLoader<Song> {
            mCursor.close();
            mCursor = null;
        }

        // requested ordering of songs
        String songSortOrder = PreferenceUtils.getInstance(mContext).getSongSortOrder();

        // run a custom localized sort to try to fit items in to header buckets more nicely
        if (shouldEvokeCustomSortRoutine(songSortOrder)) {
            mSongList = SortUtils.localizeSortList(mSongList, songSortOrder);
        }

        return mSongList;
    }

    /**
     * We are choosing to custom sort the song list for a cleaner look on the UI side for a few
     * sort options
     * @param sortOrder the song ordering preference selected by the user
     * @return
     */
    private boolean shouldEvokeCustomSortRoutine(String sortOrder) {
        return sortOrder.equals(SortOrder.SongSortOrder.SONG_A_Z) ||
               sortOrder.equals(SortOrder.SongSortOrder.SONG_Z_A) ||
               sortOrder.equals(SortOrder.SongSortOrder.SONG_ALBUM) ||
               sortOrder.equals(SortOrder.SongSortOrder.SONG_ARTIST);
    }

    /**
     * Gets the cursor for the loader - can be overriden
     * @return cursor to load
+59 −5
Original line number Diff line number Diff line
@@ -49,7 +49,10 @@ import com.cyngn.eleven.loaders.PlaylistSongLoader;
import com.cyngn.eleven.loaders.SongLoader;
import com.cyngn.eleven.loaders.TopTracksLoader;
import com.cyngn.eleven.menu.FragmentMenuItems;
import com.cyngn.eleven.model.Album;
import com.cyngn.eleven.model.AlbumArtistDetails;
import com.cyngn.eleven.model.Artist;
import com.cyngn.eleven.model.Song;
import com.cyngn.eleven.provider.RecentStore;
import com.cyngn.eleven.provider.SongPlayCount;
import com.cyngn.eleven.service.MusicPlaybackTrack;
@@ -1688,7 +1691,7 @@ public final class MusicUtils {
            // not quite sorted
            if (lbl != null && lbl.length() > 0) {
                char ch = lbl.charAt(0);
                if (ch < 'A' && ch > 'Z' && ch != '#') {
                if ((ch < 'A' || ch > 'Z') && ch != '#') {
                    return null;
                }
            }
@@ -1730,4 +1733,55 @@ public final class MusicUtils {
        removeFromCache(activity, key);
        MusicUtils.refresh();
    }

    /**
     * Determines the correct item attribute to use for a given sort request and generates the
     * localized bucket for that attribute
     * @param item
     * @param sortOrder
     * @param <T>
     * @return
     */
    public static <T> String getLocalizedBucketLetterByAttribute(T item, String sortOrder) {
        if (item instanceof Song) {
            // we aren't 'trimming' certain attributes - a flag for such attributes
            boolean trimName = true;
            String attributeToLocalize = ((Song)item).mSongName;

            // select Song attribute based on the sort order
            if (sortOrder.equals(SortOrder.SongSortOrder.SONG_ARTIST) ) {
                attributeToLocalize = ((Song)item).mArtistName;
                trimName = false;
            } else if (sortOrder.equals(SortOrder.SongSortOrder.SONG_ALBUM) ) {
                attributeToLocalize = ((Song)item).mAlbumName;
            }

            return getLocalizedBucketLetter(attributeToLocalize, trimName);
        } else if (item instanceof Artist) {
            return getLocalizedBucketLetter(((Artist)item).mArtistName, true);
        } else if (item instanceof Album) {
            // we aren't 'trimming' certain attributes - a flag for such attributes
            boolean trimName = true;
            String attributeToLocalize = ((Album)item).mAlbumName;

            if (sortOrder.equals(SortOrder.AlbumSortOrder.ALBUM_ARTIST) ) {
                attributeToLocalize = ((Album)item).mArtistName;
                trimName = false;
            }

            return getLocalizedBucketLetter(attributeToLocalize, trimName);
        }

        return null;
    }

    /**
     *
     * @param sortOrder values are mostly derived from SortOrder.class or could also be any sql
     *                  order clause
     * @return
     */
    public static boolean isSortOrderDesending(String sortOrder) {
        return sortOrder.endsWith(" DESC");
    }
}
Loading