summaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/eleven/utils/GenreFetcher.java
blob: bef40fb047c0802940dd689257421853c7a3880a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.cyanogenmod.eleven.utils;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.TextView;

public class GenreFetcher implements LoaderCallbacks<Cursor> {
    private static final String[] GENRE_PROJECTION = new String[] { MediaStore.Audio.Genres.NAME };

    private Context mContext;
    private int mSongId;
    private TextView mTextView;

    public static void fetch(FragmentActivity activity, int songId, TextView textView) {
        LoaderManager lm = activity.getSupportLoaderManager();
        lm.initLoader(0, null, new GenreFetcher(activity, songId, textView));
    }

    private GenreFetcher(Context context, int songId, TextView textView) {
        mContext = context;
        mSongId = songId;
        mTextView = textView;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new CursorLoader(mContext,
            MediaStore.Audio.Genres.getContentUriForAudioId("external", mSongId),
            GENRE_PROJECTION, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        if(mTextView != null && cursor.moveToFirst()) {
            String genre = cursor.getString(0);
            if(!MusicUtils.isBlank(genre)) {
                mTextView.setText(genre);
                mTextView.setVisibility(View.VISIBLE);
                return;
            }
        }
        // no displayable genre found
        mTextView.setVisibility(View.GONE);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {}
}