OSDN Git Service

bef40fb047c0802940dd689257421853c7a3880a
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / utils / GenreFetcher.java
1 package com.cyanogenmod.eleven.utils;
2
3 import android.content.Context;
4 import android.database.Cursor;
5 import android.os.Bundle;
6 import android.provider.MediaStore;
7 import android.support.v4.app.FragmentActivity;
8 import android.support.v4.app.LoaderManager;
9 import android.support.v4.app.LoaderManager.LoaderCallbacks;
10 import android.support.v4.content.CursorLoader;
11 import android.support.v4.content.Loader;
12 import android.view.View;
13 import android.widget.TextView;
14
15 public class GenreFetcher implements LoaderCallbacks<Cursor> {
16     private static final String[] GENRE_PROJECTION = new String[] { MediaStore.Audio.Genres.NAME };
17
18     private Context mContext;
19     private int mSongId;
20     private TextView mTextView;
21
22     public static void fetch(FragmentActivity activity, int songId, TextView textView) {
23         LoaderManager lm = activity.getSupportLoaderManager();
24         lm.initLoader(0, null, new GenreFetcher(activity, songId, textView));
25     }
26
27     private GenreFetcher(Context context, int songId, TextView textView) {
28         mContext = context;
29         mSongId = songId;
30         mTextView = textView;
31     }
32
33     @Override
34     public Loader<Cursor> onCreateLoader(int id, Bundle args) {
35         return new CursorLoader(mContext,
36             MediaStore.Audio.Genres.getContentUriForAudioId("external", mSongId),
37             GENRE_PROJECTION, null, null, null);
38     }
39
40     @Override
41     public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
42         if(mTextView != null && cursor.moveToFirst()) {
43             String genre = cursor.getString(0);
44             if(!MusicUtils.isBlank(genre)) {
45                 mTextView.setText(genre);
46                 mTextView.setVisibility(View.VISIBLE);
47                 return;
48             }
49         }
50         // no displayable genre found
51         mTextView.setVisibility(View.GONE);
52     }
53
54     @Override
55     public void onLoaderReset(Loader<Cursor> loader) {}
56 }