/*
This file is part of Subsonic.
Subsonic 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.
Subsonic 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 Subsonic. If not, see .
Copyright 2009 (C) Sindre Mehus
*/
package github.daneren2005.dsub.fragments;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.StatFs;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import github.daneren2005.dsub.R;
import github.daneren2005.dsub.activity.SubsonicActivity;
import github.daneren2005.dsub.activity.SubsonicFragmentActivity;
import github.daneren2005.dsub.adapter.SectionAdapter;
import github.daneren2005.dsub.domain.Artist;
import github.daneren2005.dsub.domain.Bookmark;
import github.daneren2005.dsub.domain.Genre;
import github.daneren2005.dsub.domain.MusicDirectory;
import github.daneren2005.dsub.domain.Playlist;
import github.daneren2005.dsub.domain.PodcastEpisode;
import github.daneren2005.dsub.domain.ServerInfo;
import github.daneren2005.dsub.domain.Share;
import github.daneren2005.dsub.service.DownloadFile;
import github.daneren2005.dsub.service.DownloadService;
import github.daneren2005.dsub.service.MediaStoreService;
import github.daneren2005.dsub.service.MusicService;
import github.daneren2005.dsub.service.MusicServiceFactory;
import github.daneren2005.dsub.service.OfflineException;
import github.daneren2005.dsub.service.ServerTooOldException;
import github.daneren2005.dsub.util.Constants;
import github.daneren2005.dsub.util.FileUtil;
import github.daneren2005.dsub.util.ImageLoader;
import github.daneren2005.dsub.util.MenuUtil;
import github.daneren2005.dsub.util.ProgressListener;
import github.daneren2005.dsub.util.SilentBackgroundTask;
import github.daneren2005.dsub.util.LoadingTask;
import github.daneren2005.dsub.util.SongDBHandler;
import github.daneren2005.dsub.util.UpdateHelper;
import github.daneren2005.dsub.util.UserUtil;
import github.daneren2005.dsub.util.Util;
import github.daneren2005.dsub.view.AlbumView;
import github.daneren2005.dsub.view.ArtistEntryView;
import github.daneren2005.dsub.view.ArtistView;
import github.daneren2005.dsub.view.GridSpacingDecoration;
import github.daneren2005.dsub.view.PlaylistSongView;
import github.daneren2005.dsub.view.SongView;
import github.daneren2005.dsub.view.UpdateView;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import static github.daneren2005.dsub.domain.MusicDirectory.Entry;
public class SubsonicFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
private static final String TAG = SubsonicFragment.class.getSimpleName();
private static int TAG_INC = 10;
private int tag;
protected SubsonicActivity context;
protected CharSequence title = null;
protected CharSequence subtitle = null;
protected View rootView;
protected boolean primaryFragment = false;
protected boolean secondaryFragment = false;
protected boolean isOnlyVisible = true;
protected boolean alwaysFullscreen = false;
protected boolean alwaysStartFullscreen = false;
protected boolean invalidated = false;
protected static Random random = new Random();
protected GestureDetector gestureScanner;
protected Share share;
protected boolean artist = false;
protected boolean artistOverride = false;
protected SwipeRefreshLayout refreshLayout;
protected boolean firstRun;
protected MenuItem searchItem;
protected SearchView searchView;
public SubsonicFragment() {
super();
tag = TAG_INC++;
}
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
if(bundle != null) {
String name = bundle.getString(Constants.FRAGMENT_NAME);
if(name != null) {
title = name;
}
}
firstRun = true;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if(title != null) {
outState.putString(Constants.FRAGMENT_NAME, title.toString());
}
}
@Override
public void onResume() {
super.onResume();
if(firstRun) {
firstRun = false;
} else {
UpdateView.triggerUpdate();
}
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
context = (SubsonicActivity)activity;
}
public void setContext(SubsonicActivity context) {
this.context = context;
}
protected void onFinishSetupOptionsMenu(final Menu menu) {
searchItem = menu.findItem(R.id.menu_global_search);
if(searchItem != null) {
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
SearchManager searchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(context.getComponentName());
if(searchableInfo == null) {
Log.w(TAG, "Failed to get SearchableInfo");
} else {
searchView.setSearchableInfo(searchableInfo);
}
String currentQuery = getCurrentQuery();
if(currentQuery != null) {
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchView.setQuery(getCurrentQuery(), false);
}
});
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_global_shuffle:
onShuffleRequested();
return true;
case R.id.menu_exit:
exit();
return true;
case R.id.menu_refresh:
refresh();
return true;
case R.id.menu_play_now:
playNow(false, false);
return true;
case R.id.menu_play_last:
playNow(false, true);
return true;
case R.id.menu_play_next:
playNow(false, true, true);
return true;
case R.id.menu_shuffle:
playNow(true, false);
return true;
case R.id.menu_download:
downloadBackground(false);
clearSelected();
return true;
case R.id.menu_cache:
downloadBackground(true);
clearSelected();
return true;
case R.id.menu_delete:
delete();
clearSelected();
return true;
case R.id.menu_add_playlist:
List songs = getSelectedEntries();
addToPlaylist(songs);
clearSelected();
return true;
case R.id.menu_star:case R.id.menu_unstar:
toggleSelectedStarred();
return true;
}
return false;
}
public void onCreateContextMenuSupport(Menu menu, MenuInflater menuInflater, UpdateView updateView, Object selected) {
if(selected instanceof Entry) {
Entry entry = (Entry) selected;
if(entry instanceof PodcastEpisode) {
if(Util.isOffline(context)) {
if(entry.isVideo()) {
menuInflater.inflate(R.menu.select_video_context_offline, menu);
} else {
menuInflater.inflate(R.menu.select_podcast_episode_context_offline, menu);
}
}
else {
if(entry.isVideo()) {
menuInflater.inflate(R.menu.select_podcast_episode_video_context, menu);
} else {
menuInflater.inflate(R.menu.select_podcast_episode_context, menu);
}
if(entry.getBookmark() == null) {
menu.removeItem(R.id.bookmark_menu_delete);
}
if(UserUtil.canPodcast()) {
String status = ((PodcastEpisode)entry).getStatus();
if("completed".equals(status)) {
menu.removeItem(R.id.song_menu_server_download);
}
} else {
menu.removeItem(R.id.song_menu_server_download);
menu.removeItem(R.id.song_menu_server_delete);
}
}
}
else if (entry.isDirectory()) {
if(Util.isOffline(context)) {
menuInflater.inflate(R.menu.select_album_context_offline, menu);
}
else {
menuInflater.inflate(R.menu.select_album_context, menu);
if(Util.isTagBrowsing(context)) {
menu.removeItem(R.id.menu_rate);
}
}
} else if(!entry.isVideo()) {
if(Util.isOffline(context)) {
menuInflater.inflate(R.menu.select_song_context_offline, menu);
}
else {
menuInflater.inflate(R.menu.select_song_context, menu);
if(entry.getBookmark() == null) {
menu.removeItem(R.id.bookmark_menu_delete);
}
String songPressAction = Util.getSongPressAction(context);
if(!"next".equals(songPressAction) && !"last".equals(songPressAction)) {
menu.setGroupVisible(R.id.hide_play_now, false);
}
}
} else {
if(Util.isOffline(context)) {
menuInflater.inflate(R.menu.select_video_context_offline, menu);
}
else {
menuInflater.inflate(R.menu.select_video_context, menu);
}
}
MenuItem starMenu = menu.findItem(entry.isDirectory() ? R.id.album_menu_star : R.id.song_menu_star);
if(starMenu != null) {
starMenu.setTitle(entry.isStarred() ? R.string.common_unstar : R.string.common_star);
}
if(!isShowArtistEnabled() || (!Util.isTagBrowsing(context) && entry.getParent() == null) || (Util.isTagBrowsing(context) && entry.getArtistId() == null)) {
menu.setGroupVisible(R.id.hide_show_artist, false);
}
} else if(selected instanceof Artist) {
Artist artist = (Artist) selected;
if(Util.isOffline(context)) {
menuInflater.inflate(R.menu.select_artist_context_offline, menu);
} else {
menuInflater.inflate(R.menu.select_artist_context, menu);
menu.findItem(R.id.artist_menu_star).setTitle(artist.isStarred() ? R.string.common_unstar : R.string.common_star);
}
}
MenuUtil.hideMenuItems(context, menu, updateView);
}
protected void recreateContextMenu(Menu menu) {
List