Java tutorial
/* * Copyright (C) 2017 Team Gateship-One * (Hendrik Borghorst & Frederik Luetkes) * * The AUTHORS.md file contains a detailed contributors list: * <https://github.com/gateship-one/malp/blob/master/AUTHORS.md> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package org.gateshipone.malp.application.fragments.serverfragments; import android.content.Context; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.v4.content.Loader; import android.support.v4.graphics.drawable.DrawableCompat; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.SearchView; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; import java.util.List; import org.gateshipone.malp.R; import org.gateshipone.malp.application.adapters.FileAdapter; import org.gateshipone.malp.application.callbacks.AddPathToPlaylist; import org.gateshipone.malp.application.callbacks.FABFragmentCallback; import org.gateshipone.malp.application.loaders.PlaylistTrackLoader; import org.gateshipone.malp.application.utils.ThemeUtils; import org.gateshipone.malp.mpdservice.handlers.serverhandler.MPDCommandHandler; import org.gateshipone.malp.mpdservice.handlers.serverhandler.MPDQueryHandler; import org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDTrack; import org.gateshipone.malp.mpdservice.mpdprotocol.mpdobjects.MPDFileEntry; public class PlaylistTracksFragment extends GenericMPDFragment<List<MPDFileEntry>> implements AdapterView.OnItemClickListener { public final static String TAG = PlaylistTracksFragment.class.getSimpleName(); public final static String EXTRA_PLAYLIST_NAME = "name"; /** * Adapter used by the ListView */ private FileAdapter mFileAdapter; /** * Main ListView of this fragment */ private ListView mListView; /** * Name of the playlist to load */ private String mPath; private FABFragmentCallback mFABCallback = null; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.listview_layout_refreshable, container, false); // Get the main ListView of this fragment mListView = (ListView) rootView.findViewById(R.id.main_listview); Bundle args = getArguments(); if (null != args) { mPath = args.getString(EXTRA_PLAYLIST_NAME); } // Create the needed adapter for the ListView mFileAdapter = new FileAdapter(getActivity(), false, false, true); // Combine the two to a happy couple mListView.setAdapter(mFileAdapter); mListView.setOnItemClickListener(this); registerForContextMenu(mListView); // get swipe layout mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refresh_layout); // set swipe colors mSwipeRefreshLayout.setColorSchemeColors(ThemeUtils.getThemeColor(getContext(), R.attr.colorAccent), ThemeUtils.getThemeColor(getContext(), R.attr.colorPrimary)); // set swipe refresh listener mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { refreshContent(); } }); setHasOptionsMenu(true); // Return the ready inflated and configured fragment view. return rootView; } /** * Starts the loader to make sure the data is up-to-date after resuming the fragment (from background) */ @Override public void onResume() { super.onResume(); if (null != mFABCallback) { mFABCallback.setupFAB(true, new FABOnClickListener()); mFABCallback.setupToolbar(mPath, false, false, false); } } /** * Called when the fragment is first attached to its context. */ @Override public void onAttach(Context context) { super.onAttach(context); // This makes sure that the container activity has implemented // the callback interface. If not, it throws an exception try { mFABCallback = (FABFragmentCallback) context; } catch (ClassCastException e) { mFABCallback = null; } } /** * Create the context menu. */ @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getActivity().getMenuInflater(); inflater.inflate(R.menu.context_menu_track, menu); // Enable the remove from list action menu.findItem(R.id.action_remove_from_list).setVisible(true); } /** * Hook called when an menu item in the context menu is selected. * * @param item The menu item that was selected. * @return True if the hook was consumed here. */ @Override public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); if (info == null) { return super.onContextItemSelected(item); } switch (item.getItemId()) { case R.id.action_song_enqueue: enqueueTrack(info.position); return true; case R.id.action_song_play: play(info.position); return true; case R.id.action_song_play_next: playNext(info.position); return true; case R.id.action_add_to_saved_playlist: // open dialog in order to save the current playlist as a playlist in the mediastore ChoosePlaylistDialog choosePlaylistDialog = new ChoosePlaylistDialog(); Bundle args = new Bundle(); args.putBoolean(ChoosePlaylistDialog.EXTRA_SHOW_NEW_ENTRY, true); choosePlaylistDialog.setCallback( new AddPathToPlaylist((MPDFileEntry) mFileAdapter.getItem(info.position), getActivity())); choosePlaylistDialog.setArguments(args); choosePlaylistDialog.show(((AppCompatActivity) getContext()).getSupportFragmentManager(), "ChoosePlaylistDialog"); return true; case R.id.action_remove_from_list: MPDQueryHandler.removeSongFromSavedPlaylist(mPath, info.position); refreshContent(); return true; default: return super.onContextItemSelected(item); } } /** * Initialize the options menu. * Be sure to call {@link #setHasOptionsMenu} before. * * @param menu The container for the custom options menu. * @param menuInflater The inflater to instantiate the layout. */ @Override public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) { // Inflate the menu; this adds items to the action bar if it is present. menuInflater.inflate(R.menu.fragment_playlist_tracks, menu); // get tint color int tintColor = ThemeUtils.getThemeColor(getContext(), R.attr.malp_color_text_accent); Drawable drawable = menu.findItem(R.id.action_add_playlist).getIcon(); drawable = DrawableCompat.wrap(drawable); DrawableCompat.setTint(drawable, tintColor); menu.findItem(R.id.action_add_playlist).setIcon(drawable); drawable = menu.findItem(R.id.action_search).getIcon(); drawable = DrawableCompat.wrap(drawable); DrawableCompat.setTint(drawable, tintColor); menu.findItem(R.id.action_search).setIcon(drawable); SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); searchView.setOnQueryTextListener(new SearchTextObserver()); super.onCreateOptionsMenu(menu, menuInflater); } /** * Hook called when an menu item in the options menu is selected. * * @param item The menu item that was selected. * @return True if the hook was consumed here. */ @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_add_playlist: MPDQueryHandler.loadPlaylist(mPath); return true; } return super.onOptionsItemSelected(item); } /** * Creates a new Loader that retrieves the list of playlists * * @param id * @param args * @return Newly created loader */ @Override public Loader<List<MPDFileEntry>> onCreateLoader(int id, Bundle args) { return new PlaylistTrackLoader(getActivity(), mPath); } /** * When the loader finished its loading of the data it is transferred to the adapter. * * @param loader Loader that finished its loading * @param data Data that was retrieved by the laoder */ @Override public void onLoadFinished(Loader<List<MPDFileEntry>> loader, List<MPDFileEntry> data) { super.onLoadFinished(loader, data); mFileAdapter.swapModel(data); } /** * Resets the loader and clears the model data set * * @param loader The loader that gets cleared. */ @Override public void onLoaderReset(Loader<List<MPDFileEntry>> loader) { // Clear the model data of the used adapter mFileAdapter.swapModel(null); } private void enqueueTrack(int index) { MPDTrack track = (MPDTrack) mFileAdapter.getItem(index); MPDQueryHandler.addPath(track.getPath()); } private void play(int index) { MPDTrack track = (MPDTrack) mFileAdapter.getItem(index); MPDQueryHandler.playSong(track.getPath()); } private void playNext(int index) { MPDTrack track = (MPDTrack) mFileAdapter.getItem(index); MPDQueryHandler.playSongNext(track.getPath()); } public void applyFilter(String name) { mFileAdapter.applyFilter(name); } public void removeFilter() { mFileAdapter.removeFilter(); } @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { // Open song details dialog SongDetailsDialog songDetailsDialog = new SongDetailsDialog(); Bundle args = new Bundle(); args.putParcelable(SongDetailsDialog.EXTRA_FILE, (MPDTrack) mFileAdapter.getItem(position)); songDetailsDialog.setArguments(args); songDetailsDialog.show(((AppCompatActivity) getContext()).getSupportFragmentManager(), "SongDetails"); } private class FABOnClickListener implements View.OnClickListener { @Override public void onClick(View v) { MPDCommandHandler.setRandom(false); MPDCommandHandler.setRepeat(false); MPDQueryHandler.playPlaylist(mPath); } } private class SearchTextObserver implements SearchView.OnQueryTextListener { @Override public boolean onQueryTextSubmit(String query) { if (!query.isEmpty()) { applyFilter(query); } else { removeFilter(); } return false; } @Override public boolean onQueryTextChange(String newText) { if (!newText.isEmpty()) { applyFilter(newText); } else { removeFilter(); } return true; } } }