2015-01-23 17:26:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 David Schulte
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2015-02-06 20:59:23 +00:00
|
|
|
package de.arcus.playmusicexporter2.fragments;
|
2015-01-23 17:26:34 +00:00
|
|
|
|
|
|
|
import android.os.Bundle;
|
2015-05-14 09:42:57 +00:00
|
|
|
import android.os.Environment;
|
2015-08-10 15:57:01 +00:00
|
|
|
import android.support.design.widget.FloatingActionButton;
|
2015-05-14 09:42:57 +00:00
|
|
|
import android.support.v4.app.Fragment;
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.view.LayoutInflater;
|
2015-01-23 17:26:34 +00:00
|
|
|
import android.view.View;
|
2015-05-14 09:42:57 +00:00
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.ImageView;
|
2015-01-23 17:26:34 +00:00
|
|
|
import android.widget.ListView;
|
2015-05-14 09:42:57 +00:00
|
|
|
import android.widget.TextView;
|
2015-08-10 19:52:33 +00:00
|
|
|
import android.widget.Toast;
|
2015-01-23 17:26:34 +00:00
|
|
|
|
2015-08-10 15:57:01 +00:00
|
|
|
import de.arcus.framework.logger.Logger;
|
2015-05-14 09:42:57 +00:00
|
|
|
import de.arcus.playmusicexporter2.R;
|
|
|
|
import de.arcus.playmusicexporter2.activities.MusicContainerListActivity;
|
|
|
|
import de.arcus.playmusicexporter2.activities.MusicTrackListActivity;
|
2015-01-26 23:04:51 +00:00
|
|
|
import de.arcus.playmusicexporter2.adapter.MusicTrackListAdapter;
|
2015-05-14 09:42:57 +00:00
|
|
|
import de.arcus.playmusicexporter2.items.SelectedTrack;
|
|
|
|
import de.arcus.playmusicexporter2.items.SelectedTrackList;
|
|
|
|
import de.arcus.playmusicexporter2.utils.ArtworkViewLoader;
|
|
|
|
import de.arcus.playmusicexporter2.utils.MusicPathBuilder;
|
|
|
|
import de.arcus.playmusiclib.PlayMusicManager;
|
|
|
|
import de.arcus.playmusiclib.items.MusicTrack;
|
2015-01-26 23:04:51 +00:00
|
|
|
import de.arcus.playmusiclib.items.MusicTrackList;
|
2015-01-23 17:26:34 +00:00
|
|
|
|
2015-05-14 09:42:57 +00:00
|
|
|
|
2015-01-23 17:26:34 +00:00
|
|
|
/**
|
2015-05-14 09:42:57 +00:00
|
|
|
* A fragment representing a single Track detail screen.
|
|
|
|
* This fragment is either contained in a {@link MusicContainerListActivity}
|
|
|
|
* in two-pane mode (on tablets) or a {@link MusicTrackListActivity}
|
|
|
|
* on handsets.
|
2015-01-23 17:26:34 +00:00
|
|
|
*/
|
2015-05-14 09:42:57 +00:00
|
|
|
public class MusicTrackListFragment extends Fragment {
|
2015-01-23 17:26:34 +00:00
|
|
|
/**
|
2015-05-14 09:42:57 +00:00
|
|
|
* The fragment argument representing the item ID that this fragment
|
|
|
|
* represents.
|
2015-01-23 17:26:34 +00:00
|
|
|
*/
|
2015-05-14 09:42:57 +00:00
|
|
|
public static final String ARG_MUSIC_TRACK_LIST_ID = "music_track_list_id";
|
|
|
|
public static final String ARG_MUSIC_TRACK_LIST_TYPE = "music_track_list_type";
|
2015-01-23 17:26:34 +00:00
|
|
|
|
|
|
|
/**
|
2015-05-14 09:42:57 +00:00
|
|
|
* The track list
|
2015-01-23 17:26:34 +00:00
|
|
|
*/
|
2015-05-14 09:42:57 +00:00
|
|
|
private MusicTrackList mMusicTrackList;
|
2015-01-23 17:26:34 +00:00
|
|
|
|
|
|
|
/**
|
2015-05-14 09:42:57 +00:00
|
|
|
* The list view
|
2015-01-23 17:26:34 +00:00
|
|
|
*/
|
2015-05-14 09:42:57 +00:00
|
|
|
private ListView mListView;
|
2015-01-26 23:04:51 +00:00
|
|
|
|
2015-08-10 15:57:01 +00:00
|
|
|
private FloatingActionButton mFloatingButtonExport;
|
|
|
|
|
2015-08-09 19:38:14 +00:00
|
|
|
private MusicTrackListAdapter mMusicTrackAdapter;
|
|
|
|
|
2015-01-23 17:26:34 +00:00
|
|
|
/**
|
|
|
|
* Mandatory empty constructor for the fragment manager to instantiate the
|
|
|
|
* fragment (e.g. upon screen orientation changes).
|
|
|
|
*/
|
2015-02-06 20:59:23 +00:00
|
|
|
public MusicTrackListFragment() {
|
2015-01-23 17:26:34 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 18:17:42 +00:00
|
|
|
/**
|
|
|
|
* Update the list view
|
|
|
|
*/
|
|
|
|
public void updateListView() {
|
2015-05-14 09:42:57 +00:00
|
|
|
if (mListView != null)
|
|
|
|
mListView.invalidateViews();
|
2015-08-10 15:57:01 +00:00
|
|
|
|
|
|
|
updateFloatingButton();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the floating button
|
|
|
|
*/
|
|
|
|
public void updateFloatingButton() {
|
|
|
|
if (SelectedTrackList.getInstance().getSelectedItems().size() > 0) {
|
|
|
|
mFloatingButtonExport.show();
|
|
|
|
} else {
|
|
|
|
mFloatingButtonExport.hide();
|
|
|
|
}
|
2015-05-03 18:17:42 +00:00
|
|
|
}
|
|
|
|
|
2015-08-09 19:38:14 +00:00
|
|
|
/**
|
|
|
|
* Select all items
|
|
|
|
*/
|
|
|
|
public void selectAll() {
|
|
|
|
// Select all tracks
|
|
|
|
for(int i = 0; i < mMusicTrackAdapter.getCount(); i++ ) {
|
|
|
|
MusicTrack musicTrack = mMusicTrackAdapter.getItem(i);
|
|
|
|
|
|
|
|
selectTrack(musicTrack, null, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateListView();
|
|
|
|
}
|
|
|
|
|
2015-05-14 09:42:57 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2015-01-26 23:04:51 +00:00
|
|
|
|
2015-05-14 09:42:57 +00:00
|
|
|
if (getArguments().containsKey(ARG_MUSIC_TRACK_LIST_ID)
|
|
|
|
&& getArguments().containsKey(ARG_MUSIC_TRACK_LIST_TYPE)) {
|
|
|
|
|
|
|
|
// Loads the track list
|
|
|
|
long id = getArguments().getLong(ARG_MUSIC_TRACK_LIST_ID);
|
|
|
|
String type = getArguments().getString(ARG_MUSIC_TRACK_LIST_TYPE);
|
|
|
|
|
|
|
|
PlayMusicManager playMusicManager = PlayMusicManager.getInstance();
|
|
|
|
|
|
|
|
if (playMusicManager != null) {
|
|
|
|
mMusicTrackList = MusicTrackList.deserialize(playMusicManager, id, type);
|
|
|
|
}
|
2015-01-26 23:04:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 17:26:34 +00:00
|
|
|
@Override
|
2015-05-14 09:42:57 +00:00
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
|
Bundle savedInstanceState) {
|
|
|
|
View rootView = inflater.inflate(R.layout.fragment_track_detail, container, false);
|
2015-01-23 17:26:34 +00:00
|
|
|
|
2015-05-14 09:42:57 +00:00
|
|
|
// Show the dummy content as text in a TextView.
|
|
|
|
if (mMusicTrackList != null) {
|
|
|
|
mListView = (ListView)rootView.findViewById(R.id.list_music_track);
|
2015-08-09 19:38:14 +00:00
|
|
|
mMusicTrackAdapter = new MusicTrackListAdapter(getActivity());
|
2015-01-30 18:01:47 +00:00
|
|
|
|
2015-08-09 19:38:14 +00:00
|
|
|
mMusicTrackAdapter.setShowArtworks(mMusicTrackList.getShowArtworkInTrack());
|
2015-01-30 18:01:47 +00:00
|
|
|
|
2015-05-14 09:42:57 +00:00
|
|
|
View headerView = inflater.inflate(R.layout.header_music_track_list, mListView, false);
|
|
|
|
headerView.setEnabled(false);
|
2015-01-23 17:26:34 +00:00
|
|
|
|
2015-05-14 09:42:57 +00:00
|
|
|
TextView textView;
|
|
|
|
ImageView imageView;
|
2015-01-23 17:26:34 +00:00
|
|
|
|
2015-05-14 09:42:57 +00:00
|
|
|
// Sets the artwork image
|
|
|
|
imageView = (ImageView)headerView.findViewById(R.id.image_music_track_artwork);
|
2015-01-23 17:26:34 +00:00
|
|
|
|
2015-05-14 09:42:57 +00:00
|
|
|
// Loads the artwork
|
2015-05-14 09:58:37 +00:00
|
|
|
ArtworkViewLoader.loadImage(mMusicTrackList, imageView, R.drawable.cd_case);
|
2015-01-23 17:26:34 +00:00
|
|
|
|
2015-05-14 09:42:57 +00:00
|
|
|
// Sets the title
|
|
|
|
textView = (TextView)headerView.findViewById(R.id.text_music_track_list_title);
|
|
|
|
textView.setText(mMusicTrackList.getTitle());
|
2015-01-23 17:26:34 +00:00
|
|
|
|
2015-05-14 09:42:57 +00:00
|
|
|
// Sets the description
|
|
|
|
textView = (TextView)headerView.findViewById(R.id.text_music_track_list_description);
|
|
|
|
textView.setText(mMusicTrackList.getDescription());
|
2015-01-23 17:26:34 +00:00
|
|
|
|
2015-05-14 09:42:57 +00:00
|
|
|
mListView.addHeaderView(headerView);
|
2015-01-23 17:26:34 +00:00
|
|
|
|
2015-08-09 19:38:14 +00:00
|
|
|
mMusicTrackAdapter.setList(mMusicTrackList.getMusicTrackList());
|
2015-01-23 17:26:34 +00:00
|
|
|
|
2015-08-09 19:38:14 +00:00
|
|
|
mListView.setAdapter(mMusicTrackAdapter);
|
2015-01-23 17:26:34 +00:00
|
|
|
|
2015-05-14 09:42:57 +00:00
|
|
|
// Click on one list item
|
|
|
|
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
|
|
|
// The header is not clicked
|
|
|
|
if (position > 0) {
|
|
|
|
// We need to subtract the header view
|
|
|
|
position -= 1;
|
|
|
|
|
|
|
|
// Gets the selected track
|
2015-08-09 19:38:14 +00:00
|
|
|
MusicTrack musicTrack = mMusicTrackAdapter.getItem(position);
|
2015-01-23 17:26:34 +00:00
|
|
|
|
2015-08-09 19:38:14 +00:00
|
|
|
// Toggle the track
|
|
|
|
selectTrack(musicTrack, view, true);
|
2015-05-14 09:42:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2015-08-10 15:57:01 +00:00
|
|
|
|
|
|
|
// The floating action button
|
|
|
|
mFloatingButtonExport = (FloatingActionButton)rootView.findViewById(R.id.floating_button_export);
|
|
|
|
mFloatingButtonExport.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
// Export all selected tracks
|
|
|
|
for(SelectedTrack selectedTrack : SelectedTrackList.getInstance().getSelectedItems()) {
|
|
|
|
selectedTrack.export(getActivity());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the selection
|
|
|
|
SelectedTrackList.getInstance().clear(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
updateFloatingButton();
|
2015-01-23 17:26:34 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 09:42:57 +00:00
|
|
|
return rootView;
|
2015-01-23 17:26:34 +00:00
|
|
|
}
|
2015-08-09 19:38:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Select a track
|
|
|
|
* @param musicTrack The track
|
|
|
|
* @param view The view
|
|
|
|
* @param toggle If you want to toggle the selection set this to true
|
|
|
|
*/
|
|
|
|
private void selectTrack(MusicTrack musicTrack, View view, boolean toggle) {
|
|
|
|
// Track is available
|
|
|
|
if (musicTrack.isOfflineAvailable()) {
|
|
|
|
|
|
|
|
// Default structure
|
|
|
|
String pathStructure = "{album-artist}/{album}/{disc=CD $}/{no=$$.} {title}.mp3";
|
|
|
|
|
|
|
|
// Track is exported from a group (playlist or artist)
|
|
|
|
if (!TextUtils.isEmpty(musicTrack.getContainerName()))
|
|
|
|
{
|
|
|
|
pathStructure = "{group}/{group-no=$$.} {title}.mp3";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the path
|
|
|
|
String path = MusicPathBuilder.Build(musicTrack, pathStructure);
|
|
|
|
|
|
|
|
// Path to the public music folder
|
|
|
|
path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/" + path;
|
|
|
|
|
2015-08-10 15:57:01 +00:00
|
|
|
// Prevent the closing
|
|
|
|
SelectedTrackList.getInstance().setDoNotCloseActionMode(true);
|
|
|
|
|
2015-08-09 19:38:14 +00:00
|
|
|
if (toggle) {
|
|
|
|
// Toggles the music track
|
|
|
|
SelectedTrackList.getInstance().toggle(new SelectedTrack(musicTrack.getId(), path), view);
|
|
|
|
} else {
|
|
|
|
SelectedTrackList.getInstance().setSelected(new SelectedTrack(musicTrack.getId(), path), true, view);
|
|
|
|
}
|
2015-08-10 15:57:01 +00:00
|
|
|
|
|
|
|
SelectedTrackList.getInstance().setDoNotCloseActionMode(false);
|
2015-08-10 19:52:33 +00:00
|
|
|
} else {
|
|
|
|
// Show an info message for offline tracks
|
|
|
|
Toast toast = Toast.makeText(getActivity(), R.string.toast_error_track_not_offline, Toast.LENGTH_LONG);
|
|
|
|
toast.show();
|
2015-08-09 19:38:14 +00:00
|
|
|
}
|
2015-08-10 15:57:01 +00:00
|
|
|
|
|
|
|
updateFloatingButton();
|
2015-08-09 19:38:14 +00:00
|
|
|
}
|
2015-01-23 17:26:34 +00:00
|
|
|
}
|