Created
January 23, 2015 05:35
-
-
Save Antarix/7cc8dd1aeccb974c5e79 to your computer and use it in GitHub Desktop.
AudioFileFilter for android (For folder wise music)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileFilter; | |
import java.io.FilenameFilter; | |
import java.util.ArrayList; | |
import com.designfuture.framework.util.LogHelper; | |
public class AudioFileFilter implements FileFilter { | |
protected static final String TAG = "AudioFileFilter"; | |
/** | |
* allows Directories | |
*/ | |
private final boolean allowDirectories; | |
public AudioFileFilter( boolean allowDirectories) { | |
this.allowDirectories = allowDirectories; | |
} | |
public AudioFileFilter() { | |
this(true); | |
} | |
@Override | |
public boolean accept(File f) { | |
if ( f.isHidden() || !f.canRead() ) { | |
return false; | |
} | |
if ( f.isDirectory() ) { | |
return checkDirectory( f ); | |
} | |
return checkFileExtension( f ); | |
} | |
private boolean checkFileExtension( File f ) { | |
String ext = getFileExtension(f); | |
if ( ext == null) return false; | |
try { | |
if ( SupportedFileFormat.valueOf(ext.toUpperCase()) != null ) { | |
return true; | |
} | |
} catch(IllegalArgumentException e) { | |
//Not known enum value | |
return false; | |
} | |
return false; | |
} | |
private boolean checkDirectory( File dir ) { | |
if ( !allowDirectories ) { | |
return false; | |
} else { | |
final ArrayList<File> subDirs = new ArrayList<File>(); | |
int songNumb = dir.listFiles( new FileFilter() { | |
@Override | |
public boolean accept(File file) { | |
if ( file.isFile() ) { | |
if ( file.getName().equals( ".nomedia" ) ) | |
return false; | |
return checkFileExtension( file ); | |
} else if ( file.isDirectory() ){ | |
subDirs.add( file ); | |
return false; | |
} else | |
return false; | |
} | |
} ).length; | |
if ( songNumb > 0 ) { | |
LogHelper.i(TAG, "checkDirectory: dir " + dir.toString() + " return true con songNumb -> " + songNumb ); | |
return true; | |
} | |
for( File subDir: subDirs ) { | |
if ( checkDirectory( subDir ) ) { | |
LogHelper.i(TAG, "checkDirectory [for]: subDir " + subDir.toString() + " return true" ); | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
private boolean checkFileExtension( String fileName ) { | |
String ext = getFileExtension(fileName); | |
if ( ext == null) return false; | |
try { | |
if ( SupportedFileFormat.valueOf(ext.toUpperCase()) != null ) { | |
return true; | |
} | |
} catch(IllegalArgumentException e) { | |
//Not known enum value | |
return false; | |
} | |
return false; | |
} | |
public String getFileExtension( File f ) { | |
return getFileExtension( f.getName() ); | |
} | |
public String getFileExtension( String fileName ) { | |
int i = fileName.lastIndexOf('.'); | |
if (i > 0) { | |
return fileName.substring(i+1); | |
} else | |
return null; | |
} | |
/** | |
* Files formats currently supported by Library | |
*/ | |
public enum SupportedFileFormat | |
{ | |
_3GP("3gp"), | |
MP4("mp4"), | |
M4A("m4a"), | |
AAC("aac"), | |
TS("ts"), | |
FLAC("flac"), | |
MP3("mp3"), | |
MID("mid"), | |
XMF("xmf"), | |
MXMF("mxmf"), | |
RTTTL("rtttl"), | |
RTX("rtx"), | |
OTA("ota"), | |
IMY("imy"), | |
OGG("ogg"), | |
MKV("mkv"), | |
WAV("wav"); | |
private String filesuffix; | |
SupportedFileFormat( String filesuffix ) { | |
this.filesuffix = filesuffix; | |
} | |
public String getFilesuffix() { | |
return filesuffix; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great script thanx alot