www.pudn.com > videoplayer.rar > FileList.java, change:2011-03-04,size:6474b


package com.amlogic.amplayer; 
 
import java.io.File; 
import java.util.ArrayList; 
import java.util.List; 
 
import java.io.FileReader; 
import java.io.BufferedReader; 
import java.io.IOException; 
import android.app.ListActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.util.Log; 
 
 
 
 
public class FileList extends ListActivity { 
    private static final String TAG = "FileList"; 
    private static final String start_path="/sdcard"; 
    private List<String> dir_ent = new ArrayList<String>(); 
    private File cur_dir = new File(start_path); 
 
    private static final String[] extensions = { "3gp", 
                                                 "divx", 
                                                 "h264", 
                                                 "avi", 
                                                 "m2ts", 
                                                 "mkv", 
                                                 "mov", 
                                                 "mp2", 
                                                 "mp4", 
                                                 "mpe", 
                                                 "mpg", 
                                                 "mpeg", 
                                                 "rm", 
                                                 "rmvb", 
                                                 "wm", 
                                                 "wmv", 
                                                 "ts", 
                                                 "tp", 
                                                 "dat", 
                                                 "vob", 
                                                 "flv", 
                                                 "vc1", 
                                                 "m4v", 
                                                 "f4v", 
                                                 "asf", 
                                                 "lst", 
                                                /* "" */}; 
     
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
       
      setContentView(R.layout.filelistlayout); 
      Button exitBtn = (Button) findViewById(R.id.exitBtn); 
      if (exitBtn != null) { 
          exitBtn.setOnClickListener(new View.OnClickListener() { 
            //@Override 
            public void onClick(View v) { 
            Log.d(TAG, "Exit"); 
			Intent intent = new Intent(); 
			setResult(RESULT_CANCELED, intent); 
            finish(); 
            } 
        }); 
      } 
 
      browse(new File(start_path)); 
    } 
	private void addFilesToListFromLst(File file) { 
        if (file != null) { 
            this.dir_ent.clear(); 
			this.dir_ent.add("."); 
			BufferedReader br=null; 
			try{ 
	            br=new BufferedReader(new FileReader(file),256); 
				String filename; 
				filename=br.readLine(); 
				while(filename!=null) 
				{ 
					if(filename.length()>1&& filename.charAt(0)!='\n' && filename.charAt(0)!='#') 
					{ 
						 
						String name =filename.trim(); 
						File mfile=new File(name); 
						if (mfile.isDirectory() || isListable(name)) 
	                    	this.dir_ent.add(name); 
					} 
					filename=br.readLine();  
				} 
	            setListAdapter(new ArrayAdapter<String>(this, R.layout.filelistitem, this.dir_ent)); 
 
			} catch (IOException e) { 
            	Log.wtf(TAG, "Error reading " + file.getName() , e); 
            	// Leave the maps existing but unpopulated 
	        } finally { 
	            try { if (br != null) br.close(); } catch (IOException e) {} 
	        } 
        } 
    } 
	 
    private void browse(final File path) { 
		Log.d(TAG, "browse:" +path.getPath()); 
        if (path.isDirectory()){ 
            this.cur_dir = path; 
            addFilesToList(path.listFiles()); 
        } 
        else { 
			if(path.getName().endsWith("lst")){ 
				addFilesToListFromLst(path); 
			} 
			else 
            	openFile(path); 
        } 
    } 
     
    private void addFilesToList(File[] files) { 
        if (files != null) { 
            this.dir_ent.clear(); 
            if (this.cur_dir.getParent() != null) { 
                this.dir_ent.add(".."); 
            } 
            for (File file : files){ 
                String name = file.getName(); 
                if (file.isDirectory() || isListable(name)) 
                    this.dir_ent.add(name); 
            } 
            setListAdapter(new ArrayAdapter<String>(this, R.layout.filelistitem, this.dir_ent)); 
        } 
    } 
 
 
	    
    private void openFile(File f) { 
        Intent intent = new Intent(); 
        Bundle b = new Bundle(); 
		Log.d(TAG, "openFile:" +f.getPath()); 
        b.putString("PATH", f.getPath());   
        intent.putExtras(b);   
        setResult(RESULT_OK, intent); 
        finish(); 
    } 
 
	private void openPath(String path) { 
        Intent intent = new Intent(); 
        Bundle b = new Bundle(); 
		Log.d(TAG, "openURL:" +path); 
        b.putString("PATH", path);   
        intent.putExtras(b);   
        setResult(RESULT_OK, intent); 
        finish(); 
    } 
 
 
    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
        String name = this.dir_ent.get((int) id); 
        if (name.equals("..")) { //".." 
            if (this.cur_dir.getParent() != null) 
                this.browse(this.cur_dir.getParentFile()); 
        } else { 
            File clickedFile = null; 
			if( name.toLowerCase().startsWith("http://",0) || 
				name.toLowerCase().startsWith("ftp://",0) || 
				name.toLowerCase().startsWith("shttp://",0)) 
			{ 
					openPath(name); 
			} 
			else 
			{ 
            		clickedFile = new File(this.cur_dir.getAbsolutePath(), name); 
			} 
            if (clickedFile != null) 
                 this.browse(clickedFile); 
        } 
    } 
     
       
    private boolean isListable(final String path) { 
        final String uPath = path.toLowerCase(); 
        for (String ext : extensions) { 
            if (uPath.endsWith(ext)) 
                return true; 
        } 
        return false; 
    } 
}