aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ch/blinkenlights/android/vanilla/LibraryActivity.java
blob: 442bd28aa11dd9fa9a926179b359900f76265d8e (plain) (blame)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
/*
 * Copyright (C) 2012 Christopher Eby <kreed@kreed.org>
 * Copyright (C) 2015-2020 Adrian Ulrich <adrian@blinkenlights.ch>
 *
 * 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.
 */

package ch.blinkenlights.android.vanilla;

import ch.blinkenlights.android.medialibrary.MediaLibrary;
import ch.blinkenlights.android.vanilla.ui.FancyMenu;
import ch.blinkenlights.android.vanilla.ui.FancyMenuItem;
import ch.blinkenlights.android.vanilla.ui.ArrowedText;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.iosched.tabs.VanillaTabLayout;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.SearchView;
import android.widget.Toast;

import androidx.viewpager.widget.ViewPager;

import java.io.File;

import junit.framework.Assert;

/**
 * The library activity where songs to play can be selected from the library.
 */
public class LibraryActivity
	extends SlidingPlaybackActivity
	implements DialogInterface.OnClickListener
			 , DialogInterface.OnDismissListener
			 , SearchView.OnQueryTextListener
	                 , FancyMenu.Callback
{


	/**
	 * NOTE: ACTION_* must be in sync with default_action_entries
	 */

	/**
	 * Action for row click: play the row.
	 */
	public static final int ACTION_PLAY = 0;
	/**
	 * Action for row click: enqueue the row.
	 */
	public static final int ACTION_ENQUEUE = 1;
	/**
	 * Action for row click: perform the last used action.
	 */
	public static final int ACTION_LAST_USED = 2;
	/**
	 * Action for row click: play all the songs in the adapter, starting with
	 * the current row.
	 */
	public static final int ACTION_PLAY_ALL = 3;
	/**
	 * Action for row click: enqueue all the songs in the adapter, starting with
	 * the current row.
	 */
	public static final int ACTION_ENQUEUE_ALL = 4;
	/**
	 * Action for row click: do nothing.
	 */
	public static final int ACTION_DO_NOTHING = 5;
	/**
	 * Action for row click: expand the row.
	 */
	public static final int ACTION_EXPAND = 6;
	/**
	 * Action for row click: play if paused or enqueue if playing.
	 */
	public static final int ACTION_PLAY_OR_ENQUEUE = 7;
	/**
	 * Action for row click: queue selection as next item.
	 */
	public static final int ACTION_ENQUEUE_AS_NEXT = 8;
	/**
	 * Action for row click: expand or play all.
	 */
	public static final int ACTION_EXPAND_OR_PLAY_ALL = 9;
	/**
	 * The SongTimeline add song modes corresponding to each relevant action.
	 */
	private static final int[] modeForAction =
		{ SongTimeline.MODE_PLAY, SongTimeline.MODE_ENQUEUE, -1,
		  SongTimeline.MODE_PLAY_ID_FIRST, SongTimeline.MODE_ENQUEUE_ID_FIRST,
		  -1, -1, -1, SongTimeline.MODE_ENQUEUE_AS_NEXT };

	public ViewPager mViewPager;

	private BottomBarControls mBottomBarControls;

	private HorizontalScrollView mLimiterScroller;
	private ViewGroup mLimiterViews;
	private VanillaTabLayout mVanillaTabLayout;
	/**
	 * The action to execute when a row is tapped.
	 */
	private int mDefaultAction;
	/**
	 * Whether or not to jump to songs if the are in the queue
	 */
	private boolean mJumpToEnqueuedOnPlay;
	/**
	 * The last used action from the menu. Used with ACTION_LAST_USED.
	 */
	private int mLastAction = ACTION_PLAY;
	/**
	 * The pager adapter that manages each media ListView.
	 */
	public LibraryPagerAdapter mPagerAdapter;
	/**
	 * The adapter for the currently visible list.
	 */
	private LibraryAdapter mCurrentAdapter;


	@Override
	public void onCreate(Bundle state)
	{
		ThemeHelper.setTheme(this, R.style.Library);
		super.onCreate(state);

		if (state == null) {
			checkForLaunch(getIntent());
		}

		setContentView(R.layout.library_content);

		mLimiterScroller = (HorizontalScrollView)findViewById(R.id.limiter_scroller);
		mLimiterViews = (ViewGroup)findViewById(R.id.limiter_layout);

		LibraryPagerAdapter pagerAdapter = new LibraryPagerAdapter(this, mLooper);
		mPagerAdapter = pagerAdapter;

		ViewPager pager = (ViewPager)findViewById(R.id.pager);
		pager.setAdapter(pagerAdapter);
		mViewPager = pager;

		SharedPreferences settings = SharedPrefHelper.getSettings(this);

		mBottomBarControls = (BottomBarControls)findViewById(R.id.bottombar_controls);
		mBottomBarControls.setOnClickListener(this);
		mBottomBarControls.setOnQueryTextListener(this);
		mBottomBarControls.enableOptionsMenu(this);

		if(PermissionRequestActivity.havePermissions(this) == false) {
			PermissionRequestActivity.showWarning(this, getIntent());
		}

		mVanillaTabLayout = (VanillaTabLayout)findViewById(R.id.sliding_tabs);
		mVanillaTabLayout.setOnPageChangeListener(pagerAdapter);

		loadTabOrder();
		int page = settings.getInt(PrefKeys.LIBRARY_PAGE, PrefDefaults.LIBRARY_PAGE);
		if (page != 0) {
			pager.setCurrentItem(page);
		}

		loadLimiterIntent(getIntent());
		bindControlButtons();

		if (state != null && state.getBoolean("launch_search")) {
			mBottomBarControls.showSearch(true);
		}
	}

	@Override
	public void onStart()
	{
		super.onStart();

		loadPreferences();
		loadTabOrder();
		updateHeaders();
	}

	/**
	 * Load settings and cache them.
	 */
	private void loadPreferences() {
		SharedPreferences settings = SharedPrefHelper.getSettings(this);
		mDefaultAction = Integer.parseInt(settings.getString(PrefKeys.DEFAULT_ACTION_INT, PrefDefaults.DEFAULT_ACTION_INT));
		mJumpToEnqueuedOnPlay = settings.getBoolean(PrefKeys.JUMP_TO_ENQUEUED_ON_PLAY, PrefDefaults.JUMP_TO_ENQUEUED_ON_PLAY);
	}

	/**
	 * Load the tab order and update the tab bars if needed.
	 */
	private void loadTabOrder()
	{
		if (mPagerAdapter.loadTabOrder()) {
			// Reinitializes all tabs
			mVanillaTabLayout.setViewPager(mViewPager);
		}
	}

	/**
	 * If this intent looks like a launch from icon/widget/etc, perform
	 * launch actions.
	 */
	private void checkForLaunch(Intent intent)
	{
		SharedPreferences settings = SharedPrefHelper.getSettings(this);
		if (settings.getBoolean(PrefKeys.PLAYBACK_ON_STARTUP, PrefDefaults.PLAYBACK_ON_STARTUP) && Intent.ACTION_MAIN.equals(intent.getAction())) {
			startActivity(new Intent(this, FullPlaybackActivity.class));
		}
	}

	/**
	 * If the given intent has type data, set a limiter built from that
	 * data.
	 */
	private void loadLimiterIntent(Intent intent)
	{
		Limiter limiter = null;

		int type = intent.getIntExtra("type", -1);
		long id = intent.getLongExtra("id", -1);
		String folder = intent.getStringExtra("folder");

		if (type == MediaUtils.TYPE_FILE && folder != null) {
			limiter = FileSystemAdapter.buildLimiter(new File(folder));
		} else if (type != -1 && id != -1) {
			MediaAdapter adapter = new MediaAdapter(this, type, null, null);
			adapter.commitQuery(adapter.query());
			limiter = adapter.buildLimiter(id);
		}

		if (limiter != null) {
			int tab = mPagerAdapter.setLimiter(limiter);
			if (tab == -1 || tab == mViewPager.getCurrentItem())
				updateLimiterViews();
			else
				mViewPager.setCurrentItem(tab);
		}
	}

	@Override
	public void onNewIntent(Intent intent)
	{
		if (intent == null)
			return;

		checkForLaunch(intent);
		loadLimiterIntent(intent);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event)
	{
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			Limiter limiter = mPagerAdapter.getCurrentLimiter();

			if (mSlidingView.isShrinkable()) {
				mSlidingView.hideSlide();
				break;
			}

			if (mBottomBarControls.showSearch(false)) {
				break;
			}

			if (limiter != null) {
				int pos = -1;
				switch (limiter.type) {
				case MediaUtils.TYPE_ALBUM:
					setLimiter(MediaUtils.TYPE_ARTIST, limiter.data.toString());
					pos = mPagerAdapter.getMediaTypePosition(limiter.type);
					break;
				case MediaUtils.TYPE_ARTIST:
				case MediaUtils.TYPE_ALBARTIST:
				case MediaUtils.TYPE_COMPOSER:
				case MediaUtils.TYPE_GENRE:
					mPagerAdapter.clearLimiter(limiter.type);
					pos = mPagerAdapter.getMediaTypePosition(limiter.type);
					break;
				case MediaUtils.TYPE_FILE:
					File parentFile = ((File)limiter.data).getParentFile();
					mPagerAdapter.setLimiter(FileSystemAdapter.buildLimiter(parentFile));
					break;
				}
				if (pos == -1) {
					updateLimiterViews();
				} else {
					mViewPager.setCurrentItem(pos);
				}
			} else {
				finish();
			}
			break;
		case KeyEvent.KEYCODE_MENU:
			// We intercept these to avoid showing the activity-default menu
			mBottomBarControls.openMenu();
			break;
		case KeyEvent.KEYCODE_SEARCH:
			mBottomBarControls.showSearch(true);
			break;
		default:
			return false;
		}

		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL)
			// On ICS, EditText reports backspace events as unhandled despite
			// actually handling them. To workaround, just assume the event was
			// handled if we get here.
			return true;

		if (super.onKeyDown(keyCode, event))
			return true;

		return false;
	}

	/**
	 * Update the first row of the lists with the appropriate action (play all
	 * or enqueue all).
	 */
	private void updateHeaders()
	{
		int action = mDefaultAction;
		if (action == ACTION_LAST_USED)
			action = mLastAction;
		boolean isEnqueue = action == ACTION_ENQUEUE || action == ACTION_ENQUEUE_ALL;
		String text = getString(isEnqueue ? R.string.enqueue_all : R.string.play_all);
		mPagerAdapter.setHeaderText(text);
	}

	/**
	 * Adds songs matching the data from the given intent to the song timeline.
	 *
	 * @param intent An intent created with
	 * {@link LibraryAdapter#createData(View)}.
	 * @param action One of LibraryActivity.ACTION_*
	 */
	private void pickSongs(Intent intent, final int action)
	{
		int effectiveAction = action; // mutable copy
		long id = intent.getLongExtra("id", LibraryAdapter.INVALID_ID);
		int type = mCurrentAdapter.getMediaType();

		// special handling if we pick one song to be played that is already in queue
		boolean songPicked = (id >= 0 && type == MediaUtils.TYPE_SONG); // not invalid, not play all
		if (songPicked && effectiveAction == ACTION_PLAY && mJumpToEnqueuedOnPlay) {
			int songPosInQueue = PlaybackService.get(this).getQueuePositionForSongId(id);
			if (songPosInQueue > -1) {
				// we picked for play one song that is already present in the queue, just jump to it
				PlaybackService.get(this).jumpToQueuePosition(songPosInQueue);
				Toast.makeText(this, R.string.jumping_to_song, Toast.LENGTH_SHORT).show();
				return;
			}
		}

		boolean all = false;
		if (action == ACTION_PLAY_ALL || action == ACTION_ENQUEUE_ALL) {
			boolean notPlayAllAdapter =
				(id == LibraryAdapter.HEADER_ID)
				|| !(type <= MediaUtils.TYPE_SONG || type == MediaUtils.TYPE_FILE);
			if (effectiveAction == ACTION_ENQUEUE_ALL && notPlayAllAdapter) {
				effectiveAction = ACTION_ENQUEUE;
			} else if (effectiveAction == ACTION_PLAY_ALL && notPlayAllAdapter) {
				effectiveAction = ACTION_PLAY;
			} else {
				all = true;
			}
		}

		if (id == LibraryAdapter.HEADER_ID)
			all = true; // page header was clicked -> force all mode

		QueryTask query = buildQueryFromIntent(intent, false, (all ? mCurrentAdapter : null));
		query.mode = modeForAction[effectiveAction];
		PlaybackService.get(this).addSongs(query);

		if (mDefaultAction == ACTION_LAST_USED && mLastAction != action) {
			mLastAction = action;
			updateHeaders();
		}
	}

	/**
	 * "Expand" the view represented by the given intent by setting the limiter
	 * from the view and switching to the appropriate tab.
	 *
	 * @param intent An intent created with
	 * {@link LibraryAdapter#createData(View)}.
	 */
	private void expand(Intent intent)
	{
		mBottomBarControls.showSearch(false);
		int type = intent.getIntExtra("type", MediaUtils.TYPE_INVALID);
		long id = intent.getLongExtra("id", LibraryAdapter.INVALID_ID);
		int tab = mPagerAdapter.setLimiter(mPagerAdapter.mAdapters[type].buildLimiter(id));
		if (tab == -1 || tab == mViewPager.getCurrentItem())
			updateLimiterViews();
		else
			mViewPager.setCurrentItem(tab);
	}

	/**
	 * Open the playback activity and close any activities above it in the
	 * stack.
	 */
	public void openPlaybackActivity()
	{
		if (mSlidingView.isShrinkable())
			mSlidingView.hideSlideDelayed();
		startActivity(new Intent(this, FullPlaybackActivity.class));
	}

	/**
	 * Called by LibraryAdapters when a row has been clicked.
	 *
	 * @param rowData The data for the row that was clicked.
	 */
	public void onItemClicked(Intent rowData)
	{
		int action = mDefaultAction;
		if (action == ACTION_LAST_USED)
			action = mLastAction;

		boolean tryExpand = action == ACTION_EXPAND || action == ACTION_EXPAND_OR_PLAY_ALL;
		if (tryExpand && rowData.getBooleanExtra(LibraryAdapter.DATA_EXPANDABLE, false)) {
			onItemExpanded(rowData);
		} else if (action != ACTION_DO_NOTHING) {
			if (action == ACTION_EXPAND) {
				// default to playing when trying to expand something that can't
				// be expanded
				action = ACTION_PLAY;
			} else if (action == ACTION_EXPAND_OR_PLAY_ALL) {
				action = ACTION_PLAY_ALL;
			} else if (action == ACTION_PLAY_OR_ENQUEUE) {
				action = (mState & PlaybackService.FLAG_PLAYING) == 0 ? ACTION_PLAY : ACTION_ENQUEUE;
			}
			pickSongs(rowData, action);
		}
	}

	/**
	 * Called by LibraryAdapters when a row's expand arrow has been clicked.
	 *
	 * @param rowData The data for the row that was clicked.
	 */
	public void onItemExpanded(Intent rowData)
	{
		int type = rowData.getIntExtra(LibraryAdapter.DATA_TYPE, MediaUtils.TYPE_INVALID);
		if (type == MediaUtils.TYPE_PLAYLIST)
			editPlaylist(rowData);
		else
			expand(rowData);
	}

	/**
	 * Create or recreate the limiter breadcrumbs.
	 */
	public void updateLimiterViews()
	{
		mLimiterViews.removeAllViews();

		Limiter limiterData = mPagerAdapter.getCurrentLimiter();
		if (limiterData != null) {
			final int arrowWidth = 8;
			String[] limiter = limiterData.names;
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			for (int i = 0; i != limiter.length; ++i) {
				final boolean last = i+1 == limiter.length;
				final boolean first = i == 0;

				String txt = limiter[i];
				ArrowedText view = new ArrowedText(this);
				int[] colors = {0xFF606060, 0xFF707070};

				if (i%2 == 0) {
					int tmp = colors[0];
					colors[0] = colors[1];
					colors[1] = tmp;
				}

				if (last) {
					colors[1] = 0xFF404040;
				}

				int leftPadding = 14;
				if (first) {
					leftPadding = 6;
					colors[0] = colors[1];
				}

				view.setSingleLine();
				view.setEllipsize(TextUtils.TruncateAt.MARQUEE);
				view.setText(txt);
				view.setTextColor(Color.WHITE);
				view.setLayoutParams(params);
				view.setPaddingDIP(leftPadding, 2, 6, 2);
				view.setArrowWidthDIP(arrowWidth);
				view.setTag(i);
				view.setColors(colors[0], colors[1]);
				view.setOnClickListener(this);
				mLimiterViews.addView(view);

				if (last) {
					final int ap = 10;
					ArrowedText end = new ArrowedText(this);
					end.setOnClickListener(this);
					end.setText("×");
					end.setTextColor(0xFFB0B0B0);
					end.setLayoutParams(params);
					end.setPaddingDIP(0, 2, 0, 2);
					end.setArrowWidthDIP(arrowWidth);
					end.setArrowPaddingDIP(ap);
					end.setMinWidthDIP(arrowWidth+ap);
					end.setTag(i);
					end.setColors(colors[1], 0);
					mLimiterViews.addView(end);
				}
			}
			mLimiterScroller.setVisibility(View.VISIBLE);
		} else {
			mLimiterScroller.setVisibility(View.GONE);
		}
	}

	/**
	 * Updates mCover with the new bitmap, running in the UI thread
	 *
	 * @param cover the cover to set, will use a fallback drawable if null
	 */
	private void updateCover(final Bitmap cover) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mBottomBarControls.setCover(cover);
			}
		});
	}

	@Override
	public void onClick(View view)
	{
		if (view == mBottomBarControls) {
			openPlaybackActivity();
		} else if (view.getTag() != null) {
			// a limiter view was clicked
			int i = (Integer)view.getTag();

			Limiter limiter = mPagerAdapter.getCurrentLimiter();
			int type = limiter.type;
			if (i == 1 && type == MediaUtils.TYPE_ALBUM) {
				setLimiter(MediaUtils.TYPE_ARTIST, limiter.data.toString());
			} else if (i > 0) {
				Assert.assertEquals(MediaUtils.TYPE_FILE, limiter.type);
				File file = (File)limiter.data;
				int diff = limiter.names.length - i;
				while (--diff != -1) {
					file = file.getParentFile();
				}
				mPagerAdapter.setLimiter(FileSystemAdapter.buildLimiter(file));
			} else {
				mPagerAdapter.clearLimiter(type);
			}
			updateLimiterViews();
		} else {
			super.onClick(view);
		}
	}

	/**
	 * Set a new limiter of the given type built from the first
	 * MediaLibrary.VIEW_SONGS_ALBUMS_ARTISTS row that matches the selection.
	 *
	 * @param limiterType The type of limiter to create. Must be either
	 * MediaUtils.TYPE_ARTIST or MediaUtils.TYPE_ALBUM.
	 * @param selection Selection to pass to the query.
	 */
	private void setLimiter(int limiterType, String selection)
	{
		String[] projection = new String[] { MediaLibrary.ContributorColumns.ARTIST_ID, MediaLibrary.SongColumns.ALBUM_ID, MediaLibrary.ContributorColumns.ARTIST, MediaLibrary.AlbumColumns.ALBUM };
		QueryTask query = new QueryTask(MediaLibrary.VIEW_SONGS_ALBUMS_ARTISTS, projection, selection, null, null);
		Cursor cursor = query.runQuery(getApplicationContext());

		if (cursor != null) {
			if (cursor.moveToNext()) {
				String[] fields;
				String data;
				switch (limiterType) {
				case MediaUtils.TYPE_ARTIST:
					fields = new String[] { cursor.getString(2) };
					data = String.format(MediaLibrary.ContributorColumns.ARTIST_ID+"=%d", cursor.getLong(0));
					break;
				case MediaUtils.TYPE_ALBUM:
					fields = new String[] { cursor.getString(2), cursor.getString(3) };
					data = String.format(MediaLibrary.SongColumns.ALBUM_ID+"=%d", cursor.getLong(1));
					break;
				default:
					throw new IllegalArgumentException("setLimiter() does not support limiter type " + limiterType);
				}
				mPagerAdapter.setLimiter(new Limiter(limiterType, fields, data));
			}
			cursor.close();
		}
	}

	private static final int CTX_MENU_NOOP = -1;
	private static final int CTX_MENU_PLAY = 0;
	private static final int CTX_MENU_ENQUEUE = 1;
	private static final int CTX_MENU_EXPAND = 2;
	private static final int CTX_MENU_ENQUEUE_AS_NEXT = 3;
	private static final int CTX_MENU_DELETE = 4;
	private static final int CTX_MENU_RENAME_PLAYLIST = 5;
	private static final int CTX_MENU_PLAY_ALL = 6;
	private static final int CTX_MENU_ENQUEUE_ALL = 7;
	private static final int CTX_MENU_MORE_FROM_ALBUM = 8;
	private static final int CTX_MENU_MORE_FROM_ARTIST = 9;
	private static final int CTX_MENU_OPEN_EXTERNAL = 10;
	private static final int CTX_MENU_PLUGINS = 11;
	private static final int CTX_MENU_SHOW_DETAILS = 12;
	private static final int CTX_MENU_ADD_TO_HOMESCREEN = 13;
	private static final int CTX_MENU_ADD_TO_PLAYLIST = 14;

	/**
	 * Creates a context menu for an adapter row.
	 *
	 * @param rowData Data for the adapter row.
	 * @param view the view which was clicked.
	 * @param x x-coords of event
	 * @param y y-coords of event
	 */
	public boolean onCreateFancyMenu(Intent rowData, View view, float x, float y) {
		FancyMenu fm = new FancyMenu(this, this);
		// Add to playlist is always available.
		fm.addSpacer(20);
		fm.add(CTX_MENU_ADD_TO_PLAYLIST, 20, R.drawable.menu_add_to_playlist, R.string.add_to_playlist).setIntent(rowData);

		if (rowData.getLongExtra(LibraryAdapter.DATA_ID, LibraryAdapter.INVALID_ID) == LibraryAdapter.HEADER_ID) {
			fm.setHeaderTitle(getString(R.string.all_songs));
			fm.add(CTX_MENU_PLAY_ALL, 10, R.drawable.menu_play_all, R.string.play_all).setIntent(rowData);
			fm.add(CTX_MENU_ENQUEUE_ALL, 10, R.drawable.menu_enqueue, R.string.enqueue_all).setIntent(rowData);
		} else {
			int type = rowData.getIntExtra(LibraryAdapter.DATA_TYPE, MediaUtils.TYPE_INVALID);

			fm.setHeaderTitle(rowData.getStringExtra(LibraryAdapter.DATA_TITLE));

			if (type != MediaUtils.TYPE_FILE)
				fm.add(CTX_MENU_ADD_TO_HOMESCREEN, 20, R.drawable.menu_add_to_homescreen, R.string.add_to_homescreen).setIntent(rowData);

			if (FileUtils.canDispatchIntent(rowData))
				fm.add(CTX_MENU_OPEN_EXTERNAL, 10, R.drawable.menu_launch, R.string.open).setIntent(rowData);

			fm.add(CTX_MENU_PLAY, 0, R.drawable.menu_play, R.string.play).setIntent(rowData);
			if (type <= MediaUtils.TYPE_SONG || type == MediaUtils.TYPE_FILE)
				fm.add(CTX_MENU_PLAY_ALL, 1, R.drawable.menu_play_all, R.string.play_all).setIntent(rowData);

			fm.add(CTX_MENU_ENQUEUE_AS_NEXT, 1, R.drawable.menu_enqueue_as_next, R.string.enqueue_as_next).setIntent(rowData);
			fm.add(CTX_MENU_ENQUEUE, 1, R.drawable.menu_enqueue, R.string.enqueue).setIntent(rowData);

			if (type == MediaUtils.TYPE_PLAYLIST) {
				fm.add(CTX_MENU_RENAME_PLAYLIST, 0, R.drawable.menu_edit, R.string.rename).setIntent(rowData);
			} else if (rowData.getBooleanExtra(LibraryAdapter.DATA_EXPANDABLE, false)) {
				fm.add(CTX_MENU_EXPAND, 2, R.drawable.menu_expand, R.string.expand).setIntent(rowData);
			}

			if (type == MediaUtils.TYPE_SONG || type == MediaUtils.TYPE_ALBUM) {
				fm.addSpacer(30);
				fm.add(CTX_MENU_MORE_FROM_ARTIST, 30, R.drawable.menu_artist, R.string.more_from_artist).setIntent(rowData);

				if (type == MediaUtils.TYPE_SONG) {
					fm.add(CTX_MENU_MORE_FROM_ALBUM, 30, R.drawable.menu_album, R.string.more_from_album).setIntent(rowData);
					fm.add(CTX_MENU_SHOW_DETAILS, 91, R.drawable.menu_details, R.string.details).setIntent(rowData);
					if (PluginUtils.checkPlugins(this)) {
						// not part of submenu: just last item in normal menu.
						fm.add(CTX_MENU_PLUGINS, 99, R.drawable.menu_plugins, R.string.plugins).setIntent(rowData);
					}
				}
			}
			fm.addSpacer(90);
			fm.add(CTX_MENU_DELETE, 91, R.drawable.menu_delete, R.string.delete).setIntent(rowData);
		}
		fm.show(view, x, y);
		return true;
	}

	/**
	 * Open the playlist editor for the playlist with the given id.
	 */
	private void editPlaylist(Intent rowData)
	{
		Intent launch = new Intent(this, PlaylistActivity.class);
		launch.putExtra("playlist", rowData.getLongExtra(LibraryAdapter.DATA_ID, LibraryAdapter.INVALID_ID));
		launch.putExtra("title", rowData.getStringExtra(LibraryAdapter.DATA_TITLE));
		startActivity(launch);
	}

	@Override
	public boolean onFancyItemSelected(FancyMenuItem item) {
		final Intent intent = item.getIntent();
		switch (item.getItemId()) {
		case CTX_MENU_NOOP:
			break;
		case CTX_MENU_EXPAND:
			expand(intent);
			if (mDefaultAction == ACTION_LAST_USED && mLastAction != ACTION_EXPAND) {
				mLastAction = ACTION_EXPAND;
				updateHeaders();
			}
			break;
		case CTX_MENU_ENQUEUE:
			pickSongs(intent, ACTION_ENQUEUE);
			break;
		case CTX_MENU_PLAY:
			pickSongs(intent, ACTION_PLAY);
			break;
		case CTX_MENU_PLAY_ALL:
			pickSongs(intent, ACTION_PLAY_ALL);
			break;
		case CTX_MENU_ENQUEUE_ALL:
			pickSongs(intent, ACTION_ENQUEUE_ALL);
			break;
		case CTX_MENU_ENQUEUE_AS_NEXT:
			pickSongs(intent, ACTION_ENQUEUE_AS_NEXT);
			break;
		case CTX_MENU_RENAME_PLAYLIST: {
			final String playlistName = intent.getStringExtra("title");
			final long playlistId = intent.getLongExtra("id", -1);
			PlaylistInputDialog dialog = PlaylistInputDialog.newInstance(new PlaylistInputDialog.Callback() {
				@Override
				public void onSuccess(String input) {
					PlaylistTask playlistTask = new PlaylistTask(playlistId, input);
					mHandler.sendMessage(mHandler.obtainMessage(MSG_RENAME_PLAYLIST, playlistTask));
				}
			}, playlistName, R.string.rename);
			dialog.show(getFragmentManager(), "RenamePlaylistInputDialog");
			break;
		}
		case CTX_MENU_DELETE:
			String delete_message = getString(R.string.delete_item, intent.getStringExtra("title"));
			AlertDialog.Builder dialog = new AlertDialog.Builder(this);
			dialog.setTitle(R.string.delete);
			dialog
				.setMessage(delete_message)
				.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						mHandler.sendMessage(mHandler.obtainMessage(MSG_DELETE, intent));
					}
				})
				.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
					}
				});
				dialog.create().show();
			break;
		case CTX_MENU_OPEN_EXTERNAL: {
			FileUtils.dispatchIntent(this, intent);
			break;
		}
		case CTX_MENU_SHOW_DETAILS: {
			long songId = intent.getLongExtra(LibraryAdapter.DATA_ID, -1);
			TrackDetailsDialog.show(getFragmentManager(), songId);
			break;
		}
		case CTX_MENU_PLUGINS: {
			showPluginMenu(intent);
			break;
		}
		case CTX_MENU_MORE_FROM_ARTIST: {
			String selection;
			if (intent.getIntExtra(LibraryAdapter.DATA_TYPE, -1) == MediaUtils.TYPE_ALBUM) {
				selection = "album_id=";
			} else {
				selection = "_id=";
			}
			selection += intent.getLongExtra(LibraryAdapter.DATA_ID, LibraryAdapter.INVALID_ID);
			setLimiter(MediaUtils.TYPE_ARTIST, selection);
			updateLimiterViews();
			break;
		}
		case CTX_MENU_MORE_FROM_ALBUM:
			setLimiter(MediaUtils.TYPE_ALBUM, "_id=" + intent.getLongExtra(LibraryAdapter.DATA_ID, LibraryAdapter.INVALID_ID));
			updateLimiterViews();
			break;
		case CTX_MENU_ADD_TO_PLAYLIST: {
			long id = intent.getLongExtra("id", LibraryAdapter.INVALID_ID);
			long songId = intent.getLongExtra(LibraryAdapter.DATA_ID, -1);
			Song song = null;
			if(songId!=-1) {
				song = new Song(songId);
			}
			PlaylistDialog plDialog = PlaylistDialog.newInstance(this, intent, (id == LibraryAdapter.HEADER_ID ? mCurrentAdapter : null), song);
			plDialog.show(getFragmentManager(), "PlaylistDialog");
			break;
		}
		case CTX_MENU_ADD_TO_HOMESCREEN: {
			int type = intent.getIntExtra(LibraryAdapter.DATA_TYPE, MediaUtils.TYPE_INVALID);
			long id = intent.getLongExtra(LibraryAdapter.DATA_ID, LibraryAdapter.INVALID_ID);
			String label = intent.getStringExtra(LibraryAdapter.DATA_TITLE);
			SystemUtils.installLauncherShortcut(this, label, type, id);
			break;
		}
		default:
			return super.onContextItemSelected(item);
		}
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		super.onCreateOptionsMenu(menu);
		menu.add(0, MENU_PLAYBACK, 0, R.string.playback_view);
		menu.add(0, MENU_SEARCH, 0, R.string.search).setIcon(R.drawable.ic_menu_search).setVisible(false);
		menu.add(0, MENU_GO_HOME, 30, R.string.go_home);
		menu.add(0, MENU_SORT, 30, R.string.sort_by).setIcon(R.drawable.ic_menu_sort_alphabetically);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu)
	{
		LibraryAdapter adapter = mCurrentAdapter;
		menu.findItem(MENU_GO_HOME).setVisible(
				adapter != null &&
				adapter.getMediaType() == MediaUtils.TYPE_FILE &&
				(!mSlidingView.isShrinkable() || !mSlidingView.isFullyExpanded()));
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId()) {
		case MENU_SEARCH:
			mBottomBarControls.showSearch(true);
			break;
		case MENU_PLAYBACK:
			openPlaybackActivity();
			break;
		case MENU_GO_HOME:
			mPagerAdapter.setLimiter(FileSystemAdapter.buildHomeLimiter(getApplicationContext()));
			updateLimiterViews();
			break;
		case MENU_SORT: {
			SortableAdapter adapter = (SortableAdapter)mCurrentAdapter;
			LinearLayout list = (LinearLayout)getLayoutInflater().inflate(R.layout.sort_dialog, null);
			CheckBox reverseSort = (CheckBox)list.findViewById(R.id.reverse_sort);

			int[] itemIds = adapter.getSortEntries();
			String[] items = new String[itemIds.length];
			Resources res = getResources();
			for (int i = 0; i < itemIds.length; i++) {
				items[i] = res.getString(itemIds[i]);
			}

			int mode = adapter.getSortModeIndex();
			reverseSort.setChecked(adapter.isSortDescending());

			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(R.string.sort_by);
			builder.setSingleChoiceItems(items, mode, this);
			builder.setPositiveButton(R.string.done, null);

			AlertDialog dialog = builder.create();
			dialog.getListView().addFooterView(list);
			dialog.setOnDismissListener(this);
			dialog.show();
			break;
		}
		default:
			return super.onOptionsItemSelected(item);
		}
		return true;
	}

	/**
	 * Callback of mSearchView while user types in text
	 */
	@Override
	public boolean onQueryTextChange (String newText) {
		mPagerAdapter.setFilter(newText);
		return true;
	}

	/**
	 * Callback of mSearchViews submit action
	 */
	@Override
	public boolean onQueryTextSubmit (String query) {
		mPagerAdapter.setFilter(query);
		return true;
	}

	/**
	 * Save the current page, passed in arg1, to SharedPreferences.
	 */
	private static final int MSG_SAVE_PAGE = 40;
	/**
	 * Updates mCover using a background thread
	 */
	private static final int MSG_UPDATE_COVER = 41;

	@Override
	public boolean handleMessage(Message message)
	{
		switch (message.what) {
		case MSG_SAVE_PAGE: {
			SharedPreferences.Editor editor = SharedPrefHelper.getSettings(this).edit();
			editor.putInt("library_page", message.arg1);
			editor.apply();
			super.adjustSpines();
			break;
		}
		case MSG_UPDATE_COVER: {
			Bitmap cover = null;
			Song song = (Song)message.obj;
			if (song != null) {
				cover = song.getSmallCover(this);
			}
			// Dispatch view update to UI thread
			updateCover(cover);
			break;
		}
		default:
			return super.handleMessage(message);
		}

		return true;
	}

	@Override
	public void onMediaChange()
	{
		if (mPagerAdapter != null)
			mPagerAdapter.invalidateData();
	}

	@Override
	protected void onStateChange(int state, int toggled)
	{
		super.onStateChange(state, toggled);

		if ((state & PlaybackService.FLAG_EMPTY_QUEUE) != 0)
			mBottomBarControls.setSong(null);
	}

	@Override
	protected void onSongChange(Song song)
	{
		super.onSongChange(song);

		mBottomBarControls.setSong(song);
		if (song != null) {
			mHandler.sendMessage(mHandler.obtainMessage(MSG_UPDATE_COVER, song));
			mPagerAdapter.onSongChange(song);
		}
	}

	@Override
	public void onClick(DialogInterface dialog, int which)
	{
		dialog.dismiss();
	}

	@Override
	public void onDismiss(DialogInterface dialog)
	{
		ListView list = ((AlertDialog)dialog).getListView();
		int which = list.getCheckedItemPosition();

		CheckBox reverseSort = (CheckBox)list.findViewById(R.id.reverse_sort);
		if (reverseSort.isChecked()) {
			which = ~which;
		}

		mPagerAdapter.setSortMode(which);
	}

	/**
	 * Called when a new page becomes visible.
	 *
	 * @param position The position of the new page.
	 * @param adapter The new visible adapter.
	 */
	public void onPageChanged(int position, LibraryAdapter adapter)
	{
		mCurrentAdapter = adapter;
		updateLimiterViews();
		if (adapter != null && (adapter.getLimiter() == null || adapter.getMediaType() == MediaUtils.TYPE_FILE)) {
			// Save current page so it is opened on next startup. Don't save if
			// the page was expanded to, as the expanded page isn't the starting
			// point. This limitation does not affect the files tab as the limiter
			// (the files almost always have a limiter)
			Handler handler = mHandler;
			handler.sendMessage(mHandler.obtainMessage(MSG_SAVE_PAGE, position, 0));
		}
	}
}