package com.example.fichier01;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.Toast;

public class MainActivity extends ListActivity   {

	private File currentDir;
	
	private ListView mListView;
    private List<String> fileNameList;
   // private FlAdapter mAdapter;
    private File file;
    
    private Context _Context = null;
    private ArrayList<String> _List = new ArrayList<String>();
    private ArrayList<String> _FolderList = new ArrayList<String>();
    private ArrayList<String> _FileList = new ArrayList<String>();
    private ArrayAdapter<String> _Adapter = null;
	
    String[] DayOfWeek = {"Sunday", "Monday", "Tuesday",
    		   "Wednesday", "Thursday", "Friday", "Saturday"
    		 };
    
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, DayOfWeek));
		
		File path;
    	
    	List values = new ArrayList();
    	
    	
    	File dir = new File("/sdcard/");
    	String[] list = dir.list();
    	
   
    	for (String file : list) 
    	{
           
              values.add(file);
           
          }
    	
    	
    	File file = new File("/sdcard/");
        File[] files = file.listFiles();
        
        for (int i = 0; i < files.length; i++) 
        {
          if (files[i].isDirectory()) {
            _FolderList.add("<" + files[i].getName() + ">");
          } else {
            _FileList.add(files[i].getName());
          }
        }
    	
    	
        //_List.clear();
       // _List.addAll(_FolderList);
       // _List.addAll(_FileList);

       // _Adapter = new ArrayAdapter<String>(_Context, android.R.layout.simple_list_item_1, _FileList);
      //  setListAdapter(_Adapter);
        
        
        
        
        List<String> myList = new ArrayList<String>();   

        String root_sd = Environment.getExternalStorageDirectory().toString();
        file = new File( root_sd  ) ;       
        File liste[] = file.listFiles();

        for( int i=0; i< liste.length; i++)
        {
        	if(liste[i].isFile())
        	{
        		myList.add( liste[i].getName() );
        	}	
        }

        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myList ));
    	
    	//ArrayAdapter adapter = new ArrayAdapter(this,  R.layout.simple_list_item_1 , android.R.id.text1, values);
    	//    setListAdapter(adapter);
		
		
		
		 //currentDir = new File("/sdcard/");
		
		//file = Environment.getExternalStorageDirectory();
      //  fileNameList = getFileListfromSDCard();
		
		 
		 //File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
		 //		
		 //String[] files = dir.list();
		 //File[] dirs = currentDir.listFiles();
		 //fill(currentDir);
	}

	
	
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
	    String filename = (String) getListAdapter().getItem(position);
	    Toast.makeText(this, filename , Toast.LENGTH_LONG).show();
	    Intent intent = new Intent(this, SecondActivity.class);
        intent.putExtra("selected-item", filename);
        startActivity(intent);  
	}
	
	
	
	private void fill(File f) 
	{
			File[] dirs = f.listFiles();
			//  this.setTitle("Current Dir: " + f.getName());
			List dir = new ArrayList();
			List fls = new ArrayList();
			
			try {
			
					for (File ff : dirs)
					{
						String name = ff.getName();
					}
					
			}
			
			catch (Exception e) 
			{
	
			}
	}
	
	
	 private List<String> getFileListfromSDCard() 
	 {
	        String state = Environment.getExternalStorageState();
	        List<String> flLst = new ArrayList<String>();
	        if (Environment.MEDIA_MOUNTED.equals(state) && file.isDirectory()) 
	        {

	        	File path;
	        	
	        	List values = new ArrayList();
	        	
	        	File dir = new File("/sdcard/");
	        	String[] list = dir.list();
	        	
	        	for (String file : list) 
	        	{
	               
	                  values.add(file);
	               
	              }
	        	
	        	
	        	
	        //	ArrayAdapter adapter = new ArrayAdapter(this,  R.layout.simple_list_item_1 , android.R.id.text1, values);
	        //	    setListAdapter(adapter);
	        	
	        	
	        	File[] fileArr = file.listFiles();
	            
	            List file_items = new ArrayList();
	            List folder_items = new ArrayList();
	            
	            List<String> listValues = new ArrayList<String>();
	            
	            
	            for (File file : fileArr) 
	            {
	                if (file.isFile()) 
	                {
	                	file_items.add(file);
	                } 
	                else if (file.isDirectory()) 
	                {
	                	folder_items.add(file);
	                }
	            }
	            
	        }
	        
	        
	//        ArrayAdapter adapter = new ArrayAdapter(this,  android.R.layout.simple_list_item_2, android.R.id.text1, values);
	//        setListAdapter(adapter);
	        
	       // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.activity_main,listValues);
	        
	        

	        return flLst;
	    }
	
}
