1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
package github.daneren2005.dsub.fragments;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
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 android.widget.TextView;
import github.daneren2005.dsub.R;
import github.daneren2005.dsub.domain.Artist;
import github.daneren2005.dsub.domain.Indexes;
import github.daneren2005.dsub.domain.MusicFolder;
import github.daneren2005.dsub.service.MusicService;
import github.daneren2005.dsub.service.MusicServiceFactory;
import github.daneren2005.dsub.util.BackgroundTask;
import github.daneren2005.dsub.util.Constants;
import github.daneren2005.dsub.util.FileUtil;
import github.daneren2005.dsub.util.TabBackgroundTask;
import github.daneren2005.dsub.util.Util;
import github.daneren2005.dsub.view.ArtistAdapter;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class SelectArtistFragment extends SubsonicFragment implements AdapterView.OnItemClickListener {
private static final String TAG = SelectArtistFragment.class.getSimpleName();
private static final int MENU_GROUP_MUSIC_FOLDER = 10;
private ListView artistList;
private View folderButtonParent;
private View folderButton;
private TextView folderName;
private List<MusicFolder> musicFolders = null;
private List<Artist> artists;
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
if(bundle != null) {
artists = (List<Artist>) bundle.getSerializable(Constants.FRAGMENT_LIST);
musicFolders = (List<MusicFolder>) bundle.getSerializable(Constants.FRAGMENT_LIST2);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(Constants.FRAGMENT_LIST, (Serializable) artists);
outState.putSerializable(Constants.FRAGMENT_LIST2, (Serializable) musicFolders);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
rootView = inflater.inflate(R.layout.select_artist, container, false);
artistList = (ListView) rootView.findViewById(R.id.select_artist_list);
artistList.setOnItemClickListener(this);
folderButtonParent = inflater.inflate(R.layout.select_artist_header, artistList, false);
folderName = (TextView) folderButtonParent.findViewById(R.id.select_artist_folder_2);
artistList.addHeaderView(folderButtonParent);
folderButton = folderButtonParent.findViewById(R.id.select_artist_folder);
registerForContextMenu(artistList);
if(artists == null) {
if(!primaryFragment) {
invalidated = true;
} else {
refresh(false);
}
} else {
artistList.setAdapter(new ArtistAdapter(context, artists));
setMusicFolders();
}
return rootView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
menuInflater.inflate(R.menu.select_artist, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(super.onOptionsItemSelected(item)) {
return true;
}
return false;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
Object entry = artistList.getItemAtPosition(info.position);
if (entry instanceof Artist) {
onCreateContextMenu(menu, view, menuInfo, entry);
} else if (info.position == 0) {
String musicFolderId = Util.getSelectedMusicFolderId(context);
MenuItem menuItem = menu.add(MENU_GROUP_MUSIC_FOLDER, -1, 0, R.string.select_artist_all_folders);
if (musicFolderId == null) {
menuItem.setChecked(true);
}
if (musicFolders != null) {
for (int i = 0; i < musicFolders.size(); i++) {
MusicFolder musicFolder = musicFolders.get(i);
menuItem = menu.add(MENU_GROUP_MUSIC_FOLDER, i, i + 1, musicFolder.getName());
if (musicFolder.getId().equals(musicFolderId)) {
menuItem.setChecked(true);
}
}
}
menu.setGroupCheckable(MENU_GROUP_MUSIC_FOLDER, true, true);
}
}
@Override
public boolean onContextItemSelected(MenuItem menuItem) {
if(!primaryFragment) {
return false;
}
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuItem.getMenuInfo();
Artist artist = (Artist) artistList.getItemAtPosition(info.position);
if (artist != null) {
return onContextItemSelected(menuItem, artist);
} else if (info.position == 0) {
MusicFolder selectedFolder = menuItem.getItemId() == -1 ? null : musicFolders.get(menuItem.getItemId());
String musicFolderId = selectedFolder == null ? null : selectedFolder.getId();
String musicFolderName = selectedFolder == null ? context.getString(R.string.select_artist_all_folders)
: selectedFolder.getName();
Util.setSelectedMusicFolderId(context, musicFolderId);
folderName.setText(musicFolderName);
refresh();
}
return true;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (view == folderButtonParent) {
selectFolder();
} else {
Artist artist = (Artist) parent.getItemAtPosition(position);
SubsonicFragment fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ID, artist.getId());
args.putString(Constants.INTENT_EXTRA_NAME_NAME, artist.getName());
fragment.setArguments(args);
replaceFragment(fragment, R.id.select_artist_layout);
}
}
@Override
protected void refresh(boolean refresh) {
load(refresh);
}
private void load(final boolean refresh) {
setTitle(R.string.search_artists);
if (Util.isOffline(context)) {
folderButton.setVisibility(View.GONE);
} else {
folderButton.setVisibility(View.VISIBLE);
}
artistList.setVisibility(View.INVISIBLE);
BackgroundTask<Indexes> task = new TabBackgroundTask<Indexes>(this) {
@Override
protected Indexes doInBackground() throws Throwable {
MusicService musicService = MusicServiceFactory.getMusicService(context);
if (!Util.isOffline(context)) {
musicFolders = musicService.getMusicFolders(refresh, context, this);
}
String musicFolderId = Util.getSelectedMusicFolderId(context);
return musicService.getIndexes(musicFolderId, refresh, context, this);
}
@Override
protected void done(Indexes result) {
artists = new ArrayList<Artist>(result.getShortcuts().size() + result.getArtists().size());
artists.addAll(result.getShortcuts());
artists.addAll(result.getArtists());
artistList.setAdapter(new ArtistAdapter(context, artists));
setMusicFolders();
artistList.setVisibility(View.VISIBLE);
}
};
task.execute();
}
private void setMusicFolders() {
// Display selected music folder
if (musicFolders != null) {
String musicFolderId = Util.getSelectedMusicFolderId(context);
if (musicFolderId == null) {
folderName.setText(R.string.select_artist_all_folders);
} else {
for (MusicFolder musicFolder : musicFolders) {
if (musicFolder.getId().equals(musicFolderId)) {
folderName.setText(musicFolder.getName());
break;
}
}
}
}
}
private void selectFolder() {
folderButton.showContextMenu();
}
}
|