OSDN Git Service

Eleven: Add loading dialogs to all fragments
[android-x86/packages-apps-Eleven.git] / src / com / cyngn / eleven / ui / fragments / SongFragment.java
index bd55b0a..0024314 100644 (file)
 
 package com.cyngn.eleven.ui.fragments;
 
-import android.app.Activity;
 import android.content.Context;
 import android.database.Cursor;
 import android.os.Bundle;
-import android.os.SystemClock;
-import android.support.v4.app.Fragment;
-import android.support.v4.app.LoaderManager.LoaderCallbacks;
+import android.support.v4.app.LoaderManager;
 import android.support.v4.content.Loader;
-import android.text.TextUtils;
-import android.view.ContextMenu;
-import android.view.ContextMenu.ContextMenuInfo;
-import android.view.LayoutInflater;
-import android.view.Menu;
-import android.view.SubMenu;
-import android.view.View;
-import android.view.ViewGroup;
-import android.widget.AdapterView;
-import android.widget.AdapterView.AdapterContextMenuInfo;
-import android.widget.AdapterView.OnItemClickListener;
-import android.widget.ListView;
-import android.widget.TextView;
 
-import com.cyngn.eleven.MusicStateListener;
-import com.cyngn.eleven.R;
-import com.cyngn.eleven.adapters.SongAdapter;
+import com.cyngn.eleven.adapters.PagerAdapter;
 import com.cyngn.eleven.loaders.SongLoader;
-import com.cyngn.eleven.menu.CreateNewPlaylist;
-import com.cyngn.eleven.menu.DeleteDialog;
-import com.cyngn.eleven.menu.FragmentMenuItems;
 import com.cyngn.eleven.model.Song;
-import com.cyngn.eleven.provider.FavoritesStore;
-import com.cyngn.eleven.recycler.RecycleHolder;
-import com.cyngn.eleven.sectionadapter.SectionAdapter;
 import com.cyngn.eleven.sectionadapter.SectionCreator;
 import com.cyngn.eleven.sectionadapter.SectionListContainer;
-import com.cyngn.eleven.ui.activities.BaseActivity;
+import com.cyngn.eleven.ui.fragments.profile.BasicSongFragment;
 import com.cyngn.eleven.utils.MusicUtils;
-import com.cyngn.eleven.utils.NavUtils;
 import com.cyngn.eleven.utils.SectionCreatorUtils;
 import com.viewpagerindicator.TitlePageIndicator;
 
-import java.util.List;
-
 /**
  * This class is used to display all of the songs on a user's device.
- * 
+ *
  * @author Andrew Neal (andrewdneal@gmail.com)
  */
-public class SongFragment extends Fragment implements LoaderCallbacks<SectionListContainer<Song>>,
-        OnItemClickListener, MusicStateListener {
-
-    /**
-     * Used to keep context menu items from bleeding into other fragments
-     */
-    private static final int GROUP_ID = 4;
-
-    /**
-     * LoaderCallbacks identifier
-     */
-    private static final int LOADER = 0;
-
-    /**
-     * Fragment UI
-     */
-    private ViewGroup mRootView;
-
-    /**
-     * The adapter for the list
-     */
-    private SectionAdapter<Song, SongAdapter> mAdapter;
-
-    /**
-     * The list view
-     */
-    private ListView mListView;
-
-    /**
-     * Represents a song
-     */
-    private Song mSong;
-
-    /**
-     * Position of a context menu item
-     */
-    private int mSelectedPosition;
-
-    /**
-     * Id of a context menu item
-     */
-    private long mSelectedId;
-
-    /**
-     * Song, album, and artist name used in the context menu
-     */
-    private String mSongName, mAlbumName, mArtistName;
-
-    /**
-     * True if the list should execute {@code #restartLoader()}.
-     */
-    private boolean mShouldRefresh = false;
-
-    /**
-     * Empty constructor as per the {@link Fragment} documentation
-     */
-    public SongFragment() {
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void onAttach(final Activity activity) {
-        super.onAttach(activity);
-        // Register the music status listener
-        ((BaseActivity)activity).setMusicStateListenerListener(this);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void onCreate(final Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-        // Create the adpater
-        mAdapter = new SectionAdapter<Song, SongAdapter>(getActivity(), new SongAdapter(getActivity(), R.layout.list_item_simple));
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
-            final Bundle savedInstanceState) {
-        // The View for the fragment's UI
-        mRootView = (ViewGroup)inflater.inflate(R.layout.list_base, null);
-        // Initialize the list
-        mListView = (ListView)mRootView.findViewById(R.id.list_base);
-        // Set the data behind the list
-        mListView.setAdapter(mAdapter);
-        // Release any references to the recycled Views
-        mListView.setRecyclerListener(new RecycleHolder());
-        // Listen for ContextMenus to be created
-        mListView.setOnCreateContextMenuListener(this);
-        // Play the selected song
-        mListView.setOnItemClickListener(this);
-        return mRootView;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void onActivityCreated(final Bundle savedInstanceState) {
-        super.onActivityCreated(savedInstanceState);
-        // Enable the options menu
-        setHasOptionsMenu(true);
-        // Start the loader
-        getLoaderManager().initLoader(LOADER, null, this);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void onCreateContextMenu(final ContextMenu menu, final View v,
-            final ContextMenuInfo menuInfo) {
-        super.onCreateContextMenu(menu, v, menuInfo);
-        // Get the position of the selected item
-        final AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
-        mSelectedPosition = info.position;
-        // Creat a new song
-        mSong = mAdapter.getTItem(mSelectedPosition);
-        mSelectedId = mSong.mSongId;
-        mSongName = mSong.mSongName;
-        mAlbumName = mSong.mAlbumName;
-        mArtistName = mSong.mArtistName;
-
-        // Play the song
-        menu.add(GROUP_ID, FragmentMenuItems.PLAY_SELECTION, Menu.NONE,
-                getString(R.string.context_menu_play_selection));
-
-        // Play next
-        menu.add(GROUP_ID, FragmentMenuItems.PLAY_NEXT, Menu.NONE,
-                getString(R.string.context_menu_play_next));
-
-        // Add the song to the queue
-        menu.add(GROUP_ID, FragmentMenuItems.ADD_TO_QUEUE, Menu.NONE,
-                getString(R.string.add_to_queue));
-
-        // Add the song to a playlist
-        final SubMenu subMenu = menu.addSubMenu(GROUP_ID, FragmentMenuItems.ADD_TO_PLAYLIST,
-                Menu.NONE, R.string.add_to_playlist);
-        MusicUtils.makePlaylistMenu(getActivity(), GROUP_ID, subMenu, true);
-
-        // View more content by the song artist
-        menu.add(GROUP_ID, FragmentMenuItems.MORE_BY_ARTIST, Menu.NONE,
-                getString(R.string.context_menu_more_by_artist));
-
-        // Make the song a ringtone
-        menu.add(GROUP_ID, FragmentMenuItems.USE_AS_RINGTONE, Menu.NONE,
-                getString(R.string.context_menu_use_as_ringtone));
-
-        // Delete the song
-        menu.add(GROUP_ID, FragmentMenuItems.DELETE, Menu.NONE,
-                getString(R.string.context_menu_delete));
-    }
-
-    @Override
-    public boolean onContextItemSelected(final android.view.MenuItem item) {
-        if (item.getGroupId() == GROUP_ID) {
-            switch (item.getItemId()) {
-                case FragmentMenuItems.PLAY_SELECTION:
-                    MusicUtils.playAll(getActivity(), new long[] {
-                        mSelectedId
-                    }, 0, false);
-                    return true;
-                case FragmentMenuItems.PLAY_NEXT:
-                    MusicUtils.playNext(new long[] {
-                        mSelectedId
-                    });
-                    return true;
-                case FragmentMenuItems.ADD_TO_QUEUE:
-                    MusicUtils.addToQueue(getActivity(), new long[] {
-                        mSelectedId
-                    });
-                    return true;
-                case FragmentMenuItems.ADD_TO_FAVORITES:
-                    FavoritesStore.getInstance(getActivity()).addSongId(
-                            mSelectedId, mSongName, mAlbumName, mArtistName);
-                    return true;
-                case FragmentMenuItems.NEW_PLAYLIST:
-                    CreateNewPlaylist.getInstance(new long[] {
-                        mSelectedId
-                    }).show(getFragmentManager(), "CreatePlaylist");
-                    return true;
-                case FragmentMenuItems.PLAYLIST_SELECTED:
-                    final long mPlaylistId = item.getIntent().getLongExtra("playlist", 0);
-                    MusicUtils.addToPlaylist(getActivity(), new long[] {
-                        mSelectedId
-                    }, mPlaylistId);
-                    return true;
-                case FragmentMenuItems.MORE_BY_ARTIST:
-                    NavUtils.openArtistProfile(getActivity(), mArtistName);
-                    return true;
-                case FragmentMenuItems.USE_AS_RINGTONE:
-                    MusicUtils.setRingtone(getActivity(), mSelectedId);
-                    return true;
-                case FragmentMenuItems.DELETE:
-                    mShouldRefresh = true;
-                    DeleteDialog.newInstance(mSong.mSongName, new long[] {
-                        mSelectedId
-                    }, null).show(getFragmentManager(), "DeleteDialog");
-                    return true;
-                default:
-                    break;
-            }
-        }
-        return super.onContextItemSelected(item);
-    }
+public class SongFragment extends BasicSongFragment {
 
     /**
      * {@inheritDoc}
      */
-    @Override
-    public void onItemClick(final AdapterView<?> parent, final View view, final int position,
-            final long id) {
+    public void playAll(int position) {
         int internalPosition = mAdapter.getInternalPosition(position);
-        Cursor cursor = SongLoader.makeSongCursor(getActivity());
+        Cursor cursor = SongLoader.makeSongCursor(getActivity(), null);
         final long[] list = MusicUtils.getSongListForCursor(cursor);
         MusicUtils.playAll(getActivity(), list, internalPosition, false);
         cursor.close();
@@ -290,6 +51,9 @@ public class SongFragment extends Fragment implements LoaderCallbacks<SectionLis
      */
     @Override
     public Loader<SectionListContainer<Song>> onCreateLoader(final int id, final Bundle args) {
+        // show the loading progress bar
+        mLoadingEmptyContainer.showLoading();
+
         // get the context
         Context context = getActivity();
 
@@ -303,31 +67,10 @@ public class SongFragment extends Fragment implements LoaderCallbacks<SectionLis
         return new SectionCreator<Song>(context, songLoader, songComparison);
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void onLoadFinished(final Loader<SectionListContainer<Song>> loader, final SectionListContainer<Song> data) {
-        // Check for any errors
-        if (data.mListResults.isEmpty()) {
-            // Set the empty text
-            final TextView empty = (TextView)mRootView.findViewById(R.id.empty);
-            empty.setText(getString(R.string.empty_music));
-            mListView.setEmptyView(empty);
-            return;
-        }
 
-        // Set the data
-        mAdapter.setData(data);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
     @Override
-    public void onLoaderReset(final Loader<SectionListContainer<Song>> loader) {
-        // Clear the data in the adapter
-        mAdapter.unload();
+    public int getLoaderId() {
+        return PagerAdapter.MusicFragments.SONG.ordinal();
     }
 
     /**
@@ -344,7 +87,7 @@ public class SongFragment extends Fragment implements LoaderCallbacks<SectionLis
 
     /**
      * @return The position of an item in the list based on the name of the
-     *         currently playing song.
+     * currently playing song.
      */
     private int getItemPositionBySong() {
         final long trackId = MusicUtils.getCurrentAudioId();
@@ -362,32 +105,8 @@ public class SongFragment extends Fragment implements LoaderCallbacks<SectionLis
         return position;
     }
 
-    /**
-     * Restarts the loader.
-     */
-    public void refresh() {
-        // Wait a moment for the preference to change.
-        SystemClock.sleep(10);
-        getLoaderManager().restartLoader(LOADER, null, this);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void restartLoader() {
-        // Update the list when the user deletes any items
-        if (mShouldRefresh) {
-            getLoaderManager().restartLoader(LOADER, null, this);
-        }
-        mShouldRefresh = false;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
     @Override
-    public void onMetaChanged() {
-        // Nothing to do
+    public LoaderManager getFragmentLoaderManager() {
+        return getParentFragment().getLoaderManager();
     }
 }