aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/github/daneren2005/dsub/util/FileUtil.java
blob: e581173b4d5e0b938cc4ed3dcea36947c24926f6 (plain)
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
/*
 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 <http://www.gnu.org/licenses/>.

 Copyright 2009 (C) Sindre Mehus
 */
package github.daneren2005.dsub.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Iterator;
import java.util.List;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import github.daneren2005.dsub.domain.Artist;
import github.daneren2005.dsub.domain.Genre;
import github.daneren2005.dsub.domain.Indexes;
import github.daneren2005.dsub.domain.Playlist;
import github.daneren2005.dsub.domain.PodcastChannel;
import github.daneren2005.dsub.domain.MusicDirectory;
import github.daneren2005.dsub.domain.MusicFolder;
import github.daneren2005.dsub.domain.PodcastEpisode;
import github.daneren2005.dsub.service.MediaStoreService;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

/**
 * @author Sindre Mehus
 */
public class FileUtil {

    private static final String TAG = FileUtil.class.getSimpleName();
    private static final String[] FILE_SYSTEM_UNSAFE = {"/", "\\", "..", ":", "\"", "?", "*", "<", ">", "|"};
    private static final String[] FILE_SYSTEM_UNSAFE_DIR = {"\\", "..", ":", "\"", "?", "*", "<", ">", "|"};
    private static final List<String> MUSIC_FILE_EXTENSIONS = Arrays.asList("mp3", "ogg", "aac", "flac", "m4a", "wav", "wma", "opus", "oga");
	private static final List<String> VIDEO_FILE_EXTENSIONS = Arrays.asList("flv", "mp4", "m4v", "wmv", "avi", "mov", "mpg", "mkv", "3gp", "webm");
	private static final List<String> PLAYLIST_FILE_EXTENSIONS = Arrays.asList("m3u");
	private static final int MAX_FILENAME_LENGTH = 254 - ".complete.mp3".length();
    private static File DEFAULT_MUSIC_DIR;
	private static final Kryo kryo = new Kryo();
	private static HashMap<String, MusicDirectory.Entry> entryLookup;

	static {
		kryo.register(MusicDirectory.Entry.class);
		kryo.register(Indexes.class);
		kryo.register(Artist.class);
		kryo.register(MusicFolder.class);
		kryo.register(PodcastChannel.class);
		kryo.register(Playlist.class);
		kryo.register(Genre.class);
	}
	
	public static File getAnySong(Context context) {
		File dir = getMusicDirectory(context);
		return getAnySong(context, dir);
	}
	private static File getAnySong(Context context, File dir) {
		for(File file: dir.listFiles()) {
			if(file.isDirectory()) {
				return getAnySong(context, file);
			}
			
			String extension = getExtension(file.getName());
			if(MUSIC_FILE_EXTENSIONS.contains(extension)) {
				return file;
			}
		}
		
		return null;
	}
	
	public static File getEntryFile(Context context, MusicDirectory.Entry entry) {
		if(entry.isDirectory()) {
			return getAlbumDirectory(context, entry);
		} else {
			return getSongFile(context, entry);
		}
	}

    public static File getSongFile(Context context, MusicDirectory.Entry song) {
        File dir = getAlbumDirectory(context, song);

        StringBuilder fileName = new StringBuilder();
        Integer track = song.getTrack();
        if (track != null) {
            if (track < 10) {
                fileName.append("0");
            }
            fileName.append(track).append("-");
        }

        fileName.append(fileSystemSafe(song.getTitle()));
		if(fileName.length() >= MAX_FILENAME_LENGTH) {
			fileName.setLength(MAX_FILENAME_LENGTH);
		}

		fileName.append(".");
		if(song.isVideo()) {
			String videoPlayerType = Util.getVideoPlayerType(context);
			if("hls".equals(videoPlayerType)) {
				// HLS should be able to transcode to mp4 automatically
				fileName.append("mp4");
			} else if("raw".equals(videoPlayerType)) {
				// Download the original video without any transcoding
				fileName.append(song.getSuffix());
			}
		} else {
			if (song.getTranscodedSuffix() != null) {
				fileName.append(song.getTranscodedSuffix());
			} else {
				fileName.append(song.getSuffix());
			}
		}

        return new File(dir, fileName.toString());
    }

	public static File getPlaylistFile(Context context, String server, String name) {
		File playlistDir = getPlaylistDirectory(context, server);
		return new File(playlistDir, fileSystemSafe(name) + ".m3u");
	}
	public static void writePlaylistFile(Context context, File file, MusicDirectory playlist) throws IOException {
		FileWriter fw = new FileWriter(file);
		BufferedWriter bw = new BufferedWriter(fw);
		try {
			fw.write("#EXTM3U\n");
			for (MusicDirectory.Entry e : playlist.getChildren()) {
				String filePath = FileUtil.getSongFile(context, e).getAbsolutePath();
				if(! new File(filePath).exists()){
					String ext = FileUtil.getExtension(filePath);
					String base = FileUtil.getBaseName(filePath);
					filePath = base + ".complete." + ext;
				}
				fw.write(filePath + "\n");
			}
		} catch(Exception e) {
			Log.w(TAG, "Failed to save playlist: " + playlist.getName());
		} finally {
			bw.close();
			fw.close();
		}
	}
	public static File getPlaylistDirectory(Context context) {
		File playlistDir = new File(getSubsonicDirectory(context), "playlists");
		ensureDirectoryExistsAndIsReadWritable(playlistDir);
		return playlistDir;
	}
	public static File getPlaylistDirectory(Context context, String server) {
		File playlistDir = new File(getPlaylistDirectory(context), server);
		ensureDirectoryExistsAndIsReadWritable(playlistDir);
		return playlistDir;
	}

	public static File getAlbumArtFile(Context context, PodcastChannel channel) {
		MusicDirectory.Entry entry = new MusicDirectory.Entry();
		entry.setId(channel.getId());
		entry.setTitle(channel.getName());
		return getAlbumArtFile(context, entry);
	}
    public static File getAlbumArtFile(Context context, MusicDirectory.Entry entry) {
		if(entry.getId().indexOf(ImageLoader.PLAYLIST_PREFIX) != -1) {
			File dir = getAlbumArtDirectory(context);
			return  new File(dir, Util.md5Hex(ImageLoader.PLAYLIST_PREFIX + entry.getTitle()) + ".jpeg");
		} else if(entry.getId().indexOf(ImageLoader.PODCAST_PREFIX) != -1) {
			File dir = getAlbumArtDirectory(context);
			return  new File(dir, Util.md5Hex(ImageLoader.PODCAST_PREFIX + entry.getTitle()) + ".jpeg");
		} else {
			File albumDir = getAlbumDirectory(context, entry);
			File artFile;
			File albumFile = getAlbumArtFile(albumDir);
			File hexFile = getHexAlbumArtFile(context, albumDir);
			if (albumDir.exists()) {
				if (hexFile.exists()) {
					hexFile.renameTo(albumFile);
				}
				artFile = albumFile;
			} else {
				artFile = hexFile;
			}
			return artFile;
		}
    }

    public static File getAlbumArtFile(File albumDir) {
        return new File(albumDir, Constants.ALBUM_ART_FILE);
    }
	public static File getHexAlbumArtFile(Context context, File albumDir) {
		return new File(getAlbumArtDirectory(context), Util.md5Hex(albumDir.getPath()) + ".jpeg");
	}

    public static Bitmap getAlbumArtBitmap(Context context, MusicDirectory.Entry entry, int size) {
        File albumArtFile = getAlbumArtFile(context, entry);
        if (albumArtFile.exists()) {
			final BitmapFactory.Options opt = new BitmapFactory.Options();
			opt.inJustDecodeBounds = true;
			BitmapFactory.decodeFile(albumArtFile.getPath(), opt);
			opt.inPurgeable = true;
			opt.inSampleSize = Util.calculateInSampleSize(opt, size, Util.getScaledHeight(opt.outHeight, opt.outWidth, size));
			opt.inJustDecodeBounds = false;

			Bitmap bitmap = BitmapFactory.decodeFile(albumArtFile.getPath(), opt);
			return bitmap == null ? null : getScaledBitmap(bitmap, size);
        }
        return null;
    }

	public static File getAvatarDirectory(Context context) {
		File avatarDir = new File(getSubsonicDirectory(context), "avatars");
		ensureDirectoryExistsAndIsReadWritable(avatarDir);
		ensureDirectoryExistsAndIsReadWritable(new File(avatarDir, ".nomedia"));
		return avatarDir;
	}

	public static File getAvatarFile(Context context, String username) {
		return new File(getAvatarDirectory(context), Util.md5Hex(username) + ".jpeg");
	}

	public static Bitmap getAvatarBitmap(Context context, String username, int size) {
		File avatarFile = getAvatarFile(context, username);
		if (avatarFile.exists()) {
			final BitmapFactory.Options opt = new BitmapFactory.Options();
			opt.inJustDecodeBounds = true;
			BitmapFactory.decodeFile(avatarFile.getPath(), opt);
			opt.inPurgeable = true;
			opt.inSampleSize = Util.calculateInSampleSize(opt, size, Util.getScaledHeight(opt.outHeight, opt.outWidth, size));
			opt.inJustDecodeBounds = false;

			Bitmap bitmap = BitmapFactory.decodeFile(avatarFile.getPath(), opt);
			return bitmap == null ? null : getScaledBitmap(bitmap, size, false);
		}
		return null;
	}

	public static File getMiscDirectory(Context context) {
		File dir = new File(getSubsonicDirectory(context), "misc");
		ensureDirectoryExistsAndIsReadWritable(dir);
		ensureDirectoryExistsAndIsReadWritable(new File(dir, ".nomedia"));
		return dir;
	}

	public static File getMiscFile(Context context, String url) {
		return new File(getMiscDirectory(context), Util.md5Hex(url) + ".jpeg");
	}

	public static Bitmap getMiscBitmap(Context context, String url, int size) {
		File avatarFile = getMiscFile(context, url);
		if (avatarFile.exists()) {
			final BitmapFactory.Options opt = new BitmapFactory.Options();
			opt.inJustDecodeBounds = true;
			BitmapFactory.decodeFile(avatarFile.getPath(), opt);
			opt.inPurgeable = true;
			opt.inSampleSize = Util.calculateInSampleSize(opt, size, Util.getScaledHeight(opt.outHeight, opt.outWidth, size));
			opt.inJustDecodeBounds = false;

			Bitmap bitmap = BitmapFactory.decodeFile(avatarFile.getPath(), opt);
			return bitmap == null ? null : getScaledBitmap(bitmap, size, false);
		}
		return null;
	}

	public static Bitmap getSampledBitmap(byte[] bytes, int size) {
		return getSampledBitmap(bytes, size, true);
	}
	public static Bitmap getSampledBitmap(byte[] bytes, int size, boolean allowUnscaled) {
		final BitmapFactory.Options opt = new BitmapFactory.Options();
		opt.inJustDecodeBounds = true;
		BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opt);
		opt.inPurgeable = true;
		opt.inSampleSize = Util.calculateInSampleSize(opt, size, Util.getScaledHeight(opt.outHeight, opt.outWidth, size));
		opt.inJustDecodeBounds = false;
		Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opt);
		if(bitmap == null) {
			return null;
		} else {
			return getScaledBitmap(bitmap, size, allowUnscaled);
		}
	}
	public static Bitmap getScaledBitmap(Bitmap bitmap, int size) {
		return getScaledBitmap(bitmap, size, true);
	}
	public static Bitmap getScaledBitmap(Bitmap bitmap, int size, boolean allowUnscaled) {
		// Don't waste time scaling if the difference is minor
		// Large album arts still need to be scaled since displayed as is on now playing!
		if(allowUnscaled && size < 400 && bitmap.getWidth() < (size * 1.1)) {
			return bitmap;
		} else {
			return Bitmap.createScaledBitmap(bitmap, size, Util.getScaledHeight(bitmap, size), true);
		}
	}

	public static File getAlbumArtDirectory(Context context) {
		File albumArtDir = new File(getSubsonicDirectory(context), "artwork");
		ensureDirectoryExistsAndIsReadWritable(albumArtDir);
		ensureDirectoryExistsAndIsReadWritable(new File(albumArtDir, ".nomedia"));
		return albumArtDir;
	}

	public static File getArtistDirectory(Context context, Artist artist) {
		File dir = new File(getMusicDirectory(context).getPath() + "/" + fileSystemSafe(artist.getName()));
		return dir;
	}
	public static File getArtistDirectory(Context context, MusicDirectory.Entry artist) {
		File dir = new File(getMusicDirectory(context).getPath() + "/" + fileSystemSafe(artist.getTitle()));
		return dir;
	}

    public static File getAlbumDirectory(Context context, MusicDirectory.Entry entry) {
        File dir = null;
        if (entry.getPath() != null) {
            File f = new File(fileSystemSafeDir(entry.getPath()));
			String folder = getMusicDirectory(context).getPath();
			if(entry.isDirectory()) {
				folder += "/" + f.getPath();
			} else if(f.getParent() != null) {
				folder += "/" + f.getParent();
			}
            dir = new File(folder);
        } else {
			MusicDirectory.Entry firstSong;
			if(!Util.isOffline(context)) {
				firstSong = lookupChild(context, entry, false);
				if(firstSong != null) {
					File songFile = FileUtil.getSongFile(context, firstSong);
					dir = songFile.getParentFile();
				}
			}

			if(dir == null) {
				String artist = fileSystemSafe(entry.getArtist());
				String album = fileSystemSafe(entry.getAlbum());
				if("unnamed".equals(album)) {
					album = fileSystemSafe(entry.getTitle());
				}
				dir = new File(getMusicDirectory(context).getPath() + "/" + artist + "/" + album);
			}
        }
        return dir;
    }

	public static MusicDirectory.Entry lookupChild(Context context, MusicDirectory.Entry entry, boolean allowDir) {
		// Initialize lookupMap if first time called
		String lookupName = Util.getCacheName(context, "entryLookup");
		if(entryLookup == null) {
			entryLookup = deserialize(context, lookupName, HashMap.class);
			
			// Create it if 
			if(entryLookup == null) {
				entryLookup = new HashMap<String, MusicDirectory.Entry>();
			}
		}
		
		// Check if this lookup has already been done before
		MusicDirectory.Entry child = entryLookup.get(entry.getId());
		if(child != null) {
			return child;
		}
		
		// Do a special lookup since 4.7+ doesn't match artist/album to entry.getPath
		String s = Util.getRestUrl(context, null, false) + entry.getId();
		String cacheName = (Util.isTagBrowsing(context) ? "album-" : "directory-") + s.hashCode() + ".ser";
		MusicDirectory entryDir = FileUtil.deserialize(context, cacheName, MusicDirectory.class);

		if(entryDir != null) {
			List<MusicDirectory.Entry> songs = entryDir.getChildren(allowDir, true);
			if(songs.size() > 0) {
				child = songs.get(0);
				entryLookup.put(entry.getId(), child);
				serialize(context, entryLookup, lookupName);
				return child;
			}
		}

		return null;
	}
	
	public static String getPodcastPath(Context context, PodcastEpisode episode) {
		return fileSystemSafe(episode.getArtist()) + "/" + fileSystemSafe(episode.getTitle());
	}
	public static File getPodcastFile(Context context, String server) {
		File dir = getPodcastDirectory(context);
		return new File(dir.getPath() + "/" +  fileSystemSafe(server));
	}
	public static File getPodcastDirectory(Context context) {
		File dir = new File(context.getCacheDir(), "podcasts");
		ensureDirectoryExistsAndIsReadWritable(dir);
		return dir;
	}
	public static File getPodcastDirectory(Context context, PodcastChannel channel) {
		File dir = new File(getMusicDirectory(context).getPath() + "/" + fileSystemSafe(channel.getName()));
		return dir;
	}
	public static File getPodcastDirectory(Context context, String channel) {
		File dir = new File(getMusicDirectory(context).getPath() + "/" + fileSystemSafe(channel));
		return dir;
	}

    public static void createDirectoryForParent(File file) {
        File dir = file.getParentFile();
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                Log.e(TAG, "Failed to create directory " + dir);
            }
        }
    }

    private static File createDirectory(Context context, String name) {
        File dir = new File(getSubsonicDirectory(context), name);
        if (!dir.exists() && !dir.mkdirs()) {
            Log.e(TAG, "Failed to create " + name);
        }
        return dir;
    }

    public static File getSubsonicDirectory(Context context) {
        return context.getExternalFilesDir(null);
    }

    public static File getDefaultMusicDirectory(Context context) {
		if(DEFAULT_MUSIC_DIR == null) {
			File[] dirs;
			if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
				dirs = context.getExternalMediaDirs();
			} else {
				dirs = ContextCompat.getExternalFilesDirs(context, null);
			}

			DEFAULT_MUSIC_DIR = new File(getBestDir(dirs), "music");

			if (!DEFAULT_MUSIC_DIR.exists() && !DEFAULT_MUSIC_DIR.mkdirs()) {
				Log.e(TAG, "Failed to create default dir " + DEFAULT_MUSIC_DIR);

				// Some devices seem to have screwed up the new media directory API.  Go figure.  Try again with standard locations
				if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
					dirs = ContextCompat.getExternalFilesDirs(context, null);

					DEFAULT_MUSIC_DIR = new File(getBestDir(dirs), "music");
					if (!DEFAULT_MUSIC_DIR.exists() && !DEFAULT_MUSIC_DIR.mkdirs()) {
						Log.e(TAG, "Failed to create default dir " + DEFAULT_MUSIC_DIR);
					} else {
						Log.w(TAG, "Stupid OEM's messed up media dir API added in 5.0");
					}
				}
			}
		}

        return DEFAULT_MUSIC_DIR;
    }
	private static File getBestDir(File[] dirs) {
		// Past 5.0 we can query directly for SD Card
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			for(int i = 0; i < dirs.length; i++) {
				try {
					if (dirs[i] != null && Environment.isExternalStorageRemovable(dirs[i])) {
						return dirs[i];
					}
				} catch (Exception e) {
					Log.e(TAG, "Failed to check if is external", e);
				}
			}
		}

		// Before 5.0, we have to guess.  Most of the time the SD card is last
		for(int i = dirs.length - 1; i >= 0; i--) {
			if(dirs[i] != null) {
				return dirs[i];
			}
		}

		// Should be impossible to be reached
		return dirs[0];
	}

    public static File getMusicDirectory(Context context) {
        String path = Util.getPreferences(context).getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, getDefaultMusicDirectory(context).getPath());
        File dir = new File(path);
        return ensureDirectoryExistsAndIsReadWritable(dir) ? dir : getDefaultMusicDirectory(context);
    }
	public static boolean deleteMusicDirectory(Context context) {
		File musicDirectory = FileUtil.getMusicDirectory(context);
		MediaStoreService mediaStore = new MediaStoreService(context);
		return recursiveDelete(musicDirectory, mediaStore);
	}
	public static void deleteSerializedCache(Context context) {
		for(File file: context.getCacheDir().listFiles()) {
			if(file.getName().indexOf(".ser") != -1) {
				file.delete();
			}
		}
	}
	public static boolean deleteArtworkCache(Context context) {
		File artDirectory = FileUtil.getAlbumArtDirectory(context);
		return recursiveDelete(artDirectory);
	}
	public static boolean deleteAvatarCache(Context context) {
		File artDirectory = FileUtil.getAvatarDirectory(context);
		return recursiveDelete(artDirectory);
	}

	public static boolean recursiveDelete(File dir) {
		return recursiveDelete(dir, null);
	}
	public static boolean recursiveDelete(File dir, MediaStoreService mediaStore) {
		if (dir != null && dir.exists()) {
			File[] list = dir.listFiles();
			if(list != null) {
				for(File file: list) {
					if(file.isDirectory()) {
						if(!recursiveDelete(file, mediaStore)) {
							return false;
						}
					} else if(file.exists()) {
						if(!file.delete()) {
							return false;
						} else if(mediaStore != null) {
							mediaStore.deleteFromMediaStore(file);
						}
					}
				}
			}
			return dir.delete();
		}
		return false;
	}

	public static void deleteEmptyDir(File dir) {
		try {
			File[] children = dir.listFiles();
			if(children == null) {
				return;
			}

			// No songs left in the folder
			if (children.length == 1 && children[0].getPath().equals(FileUtil.getAlbumArtFile(dir).getPath())) {
				Util.delete(children[0]);
				children = dir.listFiles();
			}

			// Delete empty directory
			if (children.length == 0) {
				Util.delete(dir);
			}
		} catch(Exception e) {
			Log.w(TAG, "Error while trying to delete empty dir", e);
		}
	}

	public static void unpinSong(Context context, File saveFile) {
		// Don't try to unpin a song which isn't actually pinned
		if(saveFile.getName().contains(".complete")) {
			return;
		}

		// Unpin file, rename to .complete
		File completeFile = new File(saveFile.getParent(), FileUtil.getBaseName(saveFile.getName()) +
				".complete." + FileUtil.getExtension(saveFile.getName()));

		if(!saveFile.renameTo(completeFile)) {
			Log.w(TAG, "Failed to upin " + saveFile + " to " + completeFile);
		} else {
			try {
				new MediaStoreService(context).renameInMediaStore(completeFile, saveFile);
			} catch(Exception e) {
				Log.w(TAG, "Failed to write to media store");
			}
		}
	}

    public static boolean ensureDirectoryExistsAndIsReadWritable(File dir) {
        if (dir == null) {
            return false;
        }

        if (dir.exists()) {
            if (!dir.isDirectory()) {
                Log.w(TAG, dir + " exists but is not a directory.");
                return false;
            }
        } else {
            if (dir.mkdirs()) {
                Log.i(TAG, "Created directory " + dir);
            } else {
                Log.w(TAG, "Failed to create directory " + dir);
                return false;
            }
        }

        if (!dir.canRead()) {
            Log.w(TAG, "No read permission for directory " + dir);
            return false;
        }

        if (!dir.canWrite()) {
            Log.w(TAG, "No write permission for directory " + dir);
            return false;
        }
        return true;
    }
	public static boolean verifyCanWrite(File dir) {
		if(ensureDirectoryExistsAndIsReadWritable(dir)) {
			try {
				File tmp = new File(dir, "checkWrite");
				tmp.createNewFile();
				if(tmp.exists()) {
					if(tmp.delete()) {
						return true;
					} else {
						Log.w(TAG, "Failed to delete temp file, retrying");
						
						// This should never be reached since this is a file DSub created!
						Thread.sleep(100L);
						tmp = new File(dir, "checkWrite");
						if(tmp.delete()) {
							return true;
						} else {
							Log.w(TAG, "Failed retry to delete temp file");
							return false;
						}
					}
				} else {
					Log.w(TAG, "Temp file does not actually exist");
					return false;
				}
			} catch(Exception e) {
				Log.w(TAG, "Failed to create tmp file", e);
				return false;
			}
		} else {
			return false;
		}
	}

    /**
    * Makes a given filename safe by replacing special characters like slashes ("/" and "\")
    * with dashes ("-").
    *
    * @param filename The filename in question.
    * @return The filename with special characters replaced by hyphens.
    */
    private static String fileSystemSafe(String filename) {
        if (filename == null || filename.trim().length() == 0) {
            return "unnamed";
        }

        for (String s : FILE_SYSTEM_UNSAFE) {
            filename = filename.replace(s, "-");
        }
        return filename;
    }

    /**
     * Makes a given filename safe by replacing special characters like colons (":")
     * with dashes ("-").
     *
     * @param path The path of the directory in question.
     * @return The the directory name with special characters replaced by hyphens.
     */
    private static String fileSystemSafeDir(String path) {
        if (path == null || path.trim().length() == 0) {
            return "";
        }

        for (String s : FILE_SYSTEM_UNSAFE_DIR) {
            path = path.replace(s, "-");
        }
        return path;
    }

    /**
     * Similar to {@link File#listFiles()}, but returns a sorted set.
     * Never returns {@code null}, instead a warning is logged, and an empty set is returned.
     */
    public static SortedSet<File> listFiles(File dir) {
        File[] files = dir.listFiles();
        if (files == null) {
            Log.w(TAG, "Failed to list children for " + dir.getPath());
            return new TreeSet<File>();
        }

        return new TreeSet<File>(Arrays.asList(files));
    }

    public static SortedSet<File> listMediaFiles(File dir) {
        SortedSet<File> files = listFiles(dir);
        Iterator<File> iterator = files.iterator();
        while (iterator.hasNext()) {
            File file = iterator.next();
            if (!file.isDirectory() && !isMediaFile(file)) {
                iterator.remove();
            }
        }
        return files;
    }

    private static boolean isMediaFile(File file) {
        String extension = getExtension(file.getName());
        return MUSIC_FILE_EXTENSIONS.contains(extension) || VIDEO_FILE_EXTENSIONS.contains(extension);
    }
	
	public static boolean isMusicFile(File file) {
		String extension = getExtension(file.getName());
        return MUSIC_FILE_EXTENSIONS.contains(extension);
	}
	public static boolean isVideoFile(File file) {
		String extension = getExtension(file.getName());
        return VIDEO_FILE_EXTENSIONS.contains(extension);
	}
	
	public static boolean isPlaylistFile(File file) {
		String extension = getExtension(file.getName());
		return PLAYLIST_FILE_EXTENSIONS.contains(extension);
	}

    /**
     * Returns the extension (the substring after the last dot) of the given file. The dot
     * is not included in the returned extension.
     *
     * @param name The filename in question.
     * @return The extension, or an empty string if no extension is found.
     */
    public static String getExtension(String name) {
        int index = name.lastIndexOf('.');
        return index == -1 ? "" : name.substring(index + 1).toLowerCase();
    }

    /**
     * Returns the base name (the substring before the last dot) of the given file. The dot
     * is not included in the returned basename.
     *
     * @param name The filename in question.
     * @return The base name, or an empty string if no basename is found.
     */
    public static String getBaseName(String name) {
        int index = name.lastIndexOf('.');
        return index == -1 ? name : name.substring(0, index);
    }
	
	public static Long[] getUsedSize(Context context, File file) {
		long number = 0L;
		long permanent = 0L;
		long size = 0L;
		
		if(file.isFile()) {
			if(isMediaFile(file)) {
				if(file.getAbsolutePath().indexOf(".complete") == -1) {
					permanent++;
				}
				return new Long[] {1L, permanent, file.length()};
			} else {
				return new Long[] {0L, 0L, 0L};
			}
		} else {
			for (File child : FileUtil.listFiles(file)) {
				Long[] pair = getUsedSize(context, child);
				number += pair[0];
				permanent += pair[1];
				size += pair[2];
			}
			return new Long[] {number, permanent, size};
		}
	}

    public static <T extends Serializable> boolean serialize(Context context, T obj, String fileName) {
		Output out = null;
		try {
			RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "rw");
			out = new Output(new FileOutputStream(file.getFD()));
			synchronized (kryo) {
				kryo.writeObject(out, obj);
			}
			return true;
		} catch (Throwable x) {
			Log.w(TAG, "Failed to serialize object to " + fileName);
			return false;
		} finally {
			Util.close(out);
		}
    }

	public static <T extends Serializable> T deserialize(Context context, String fileName, Class<T> tClass) {
		return deserialize(context, fileName, tClass, 0);
	}

    public static <T extends Serializable> T deserialize(Context context, String fileName, Class<T> tClass, int hoursOld) {
		Input in = null;
		try {
			File file = new File(context.getCacheDir(), fileName);
			if(!file.exists()) {
				return null;
			}

			if(hoursOld != 0) {
				Date fileDate = new Date(file.lastModified());
				// Convert into hours
				long age = (new Date().getTime() - fileDate.getTime()) / 1000 / 3600;
				if(age > hoursOld) {
					return null;
				}
			}

			RandomAccessFile randomFile = new RandomAccessFile(file, "r");

			in = new Input(new FileInputStream(randomFile.getFD()));
			synchronized (kryo) {
				T result = kryo.readObject(in, tClass);
				return result;
			}
		} catch(FileNotFoundException e) {
			// Different error message
			Log.w(TAG, "No serialization for object from " + fileName);
			return null;
		} catch (Throwable x) {
			Log.w(TAG, "Failed to deserialize object from " + fileName);
			return null;
		} finally {
			Util.close(in);
		}
    }

	public static <T extends Serializable> boolean serializeCompressed(Context context, T obj, String fileName) {
		Output out = null;
		try {
			RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "rw");
			out = new Output(new DeflaterOutputStream(new FileOutputStream(file.getFD())));
			synchronized (kryo) {
				kryo.writeObject(out, obj);
			}
			return true;
		} catch (Throwable x) {
			Log.w(TAG, "Failed to serialize compressed object to " + fileName);
			return false;
		} finally {
			Util.close(out);
		}
	}

	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	public static <T extends Serializable> T deserializeCompressed(Context context, String fileName, Class<T> tClass) {
		Input in = null;
		try {
			RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "r");

			in = new Input(new InflaterInputStream(new FileInputStream(file.getFD())));
			synchronized (kryo) {
				T result = kryo.readObject(in, tClass);
				return result;
			}
		} catch(FileNotFoundException e) {
			// Different error message
			Log.w(TAG, "No serialization compressed for object from " + fileName);
			return null;
		} catch (Throwable x) {
			Log.w(TAG, "Failed to deserialize compressed object from " + fileName);
			return null;
		} finally {
			Util.close(in);
		}
	}
}