Back to project page openMusicTagger-Android.
The source code is released under:
GNU General Public License
If you think the Android project openMusicTagger-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package paul.arian.fileselector; /*from w w w. j ava 2 s . c o m*/ import java.io.File; import java.io.FileFilter; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import com.amethystAnt.openmusictagger.R; import com.commonsware.cwac.merge.MergeAdapter; public class FileSelectionActivity extends Activity { private static final String TAG = "FileSelection"; private static final String FILES_TO_UPLOAD = "upload"; private File mainPath = new File(Environment.getExternalStorageDirectory()+""); private ArrayList<File> resultFileList; private ListView directoryView; private ArrayList<File> directoryList = new ArrayList<File>(); private ArrayList<String> directoryNames = new ArrayList<String>(); //private ListView fileView; private ArrayList<File> fileList = new ArrayList<File>(); private ArrayList<String> fileNames = new ArrayList<String>(); Button ok, all, cancel, storage , New; private TextView path; Boolean Switch = false; Boolean switcher = false; String primary_sd; String secondary_sd; int index = 0; int top = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_file_selection); //getActionBar().setDisplayHomeAsUpEnabled(true); directoryView = (ListView)findViewById(R.id.directorySelectionList); ok = (Button)findViewById(R.id.ok); all = (Button)findViewById(R.id.all); cancel = (Button)findViewById(R.id.cancel); storage = (Button)findViewById(R.id.storage); New = (Button)findViewById(R.id.New); setPath((TextView)findViewById(R.id.folderpath)); loadLists(); New.setEnabled(false); ExtStorageSearch(); if(secondary_sd==null){ storage.setEnabled(false); } directoryView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { index = directoryView.getFirstVisiblePosition(); View v = directoryView.getChildAt(0); top = (v == null) ? 0 : v.getTop(); File lastPath = getMainPath(); try { if (position < getDirectoryList().size()) { setMainPath(getDirectoryList().get(position)); loadLists(); } }catch (Throwable e){ setMainPath(lastPath); loadLists(); } } }); ok.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ ok(); } }); cancel.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ finish(); } }); storage.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ try { if (!switcher) { setMainPath(new File(secondary_sd)); loadLists(); switcher = true; storage.setText(getString(R.string.Int)); } else { setMainPath(new File(primary_sd)); loadLists(); switcher = false; storage.setText(getString(R.string.ext)); } }catch (Throwable e){ } } }); all.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if(!Switch){ for (int i = getDirectoryList().size(); i < directoryView.getCount(); i++){ directoryView.setItemChecked(i, true); } all.setText(getString(R.string.none)); Switch = true; }else if(Switch){ for (int i = getDirectoryList().size(); i < directoryView.getCount(); i++) { directoryView.setItemChecked(i, false); } all.setText(getString(R.string.all)); Switch = false; } } }); } public void onBackPressed() { try { if(getMainPath().equals(Environment.getExternalStorageDirectory().getParentFile().getParentFile())){ finish(); }else{ File parent = getMainPath().getParentFile(); setMainPath(parent); loadLists(); directoryView.setSelectionFromTop(index, top); } }catch (Throwable e){ } } public void ok(){ Log.d(TAG, "Upload clicked, finishing activity"); resultFileList = new ArrayList<File>(); for(int i = 0 ; i < directoryView.getCount(); i++){ if(directoryView.isItemChecked(i)){ resultFileList.add(getFileList().get(i-getDirectoryList().size())); } } if(resultFileList.isEmpty()){ Log.d(TAG, "Nada seleccionado"); finish(); } Log.d(TAG, "Files: "+resultFileList.toString()); Intent result = this.getIntent(); result.putExtra(FILES_TO_UPLOAD, resultFileList); setResult(Activity.RESULT_OK, result); finish(); } protected void loadLists(){ FileFilter fileFilter = new FileFilter() { public boolean accept(File file) { return file.isFile(); } }; FileFilter directoryFilter = new FileFilter(){ public boolean accept(File file){ return file.isDirectory(); } }; //if(mainPath.exists() && mainPath.length()>0){ //Lista de directorios File[] tempDirectoryList = getMainPath().listFiles(directoryFilter); if (tempDirectoryList != null && tempDirectoryList.length > 1) { Arrays.sort(tempDirectoryList, new Comparator<File>() { @Override public int compare(File object1, File object2) { return object1.getName().compareTo(object2.getName()); } }); } setDirectoryList(new ArrayList<File>()); setDirectoryNames(new ArrayList<String>()); for(File file: tempDirectoryList){ getDirectoryList().add(file); getDirectoryNames().add(file.getName()); } @SuppressWarnings("unused") ArrayAdapter<String> directoryAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getDirectoryNames()); //Lista de ficheros File[] tempFileList = getMainPath().listFiles(fileFilter); if (tempFileList != null && tempFileList.length > 1) { Arrays.sort(tempFileList, new Comparator<File>() { @Override public int compare(File object1, File object2) { return object1.getName().compareTo(object2.getName()); } }); } setFileList(new ArrayList<File>()); setFileNames(new ArrayList<String>()); for(File file : tempFileList){ getFileList().add(file); getFileNames().add(file.getName()); } getPath().setText(getMainPath().toString()); iconload(); setTitle(getMainPath().getName()); //} } /**@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_file_selection, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); }**/ public void iconload(){ String[] foldernames = new String[getDirectoryNames().size()]; foldernames = getDirectoryNames().toArray(foldernames); String[] filenames = new String[getFileNames().size()]; filenames = getFileNames().toArray(filenames); CustomListSingleOnly adapter1 = new CustomListSingleOnly(FileSelectionActivity.this, getDirectoryNames().toArray(foldernames), getMainPath().getPath()); CustomList adapter2 = new CustomList(FileSelectionActivity.this, getFileNames().toArray(filenames), getMainPath().getPath()); MergeAdapter adap = new MergeAdapter(); adap.addAdapter(adapter1); adap.addAdapter(adapter2); directoryView.setAdapter(adap); } public void ExtStorageSearch(){ String[] extStorlocs = {"/storage/sdcard1","/storage/extsdcard","/storage/sdcard0/external_sdcard","/mnt/extsdcard", "/mnt/sdcard/external_sd","/mnt/external_sd","/mnt/media_rw/sdcard1","/removable/microsd","/mnt/emmc", "/storage/external_SD","/storage/ext_sd","/storage/removable/sdcard1","/data/sdext","/data/sdext2", "/data/sdext3","/data/sdext4","/storage/sdcard0"}; //First Attempt primary_sd = System.getenv("EXTERNAL_STORAGE"); secondary_sd = System.getenv("SECONDARY_STORAGE"); if(primary_sd == null) { primary_sd = Environment.getExternalStorageDirectory()+""; } if(secondary_sd == null) {//if fail, search among known list of extStorage Locations for(String string: extStorlocs){ if((new File(string)).exists() && (new File(string)).isDirectory() ){ secondary_sd = string; break; } } } } public ArrayList<File> getDirectoryList() { return directoryList; } public void setDirectoryList(ArrayList<File> directoryList) { this.directoryList = directoryList; } public ArrayList<String> getDirectoryNames() { return directoryNames; } public void setDirectoryNames(ArrayList<String> directoryNames) { this.directoryNames = directoryNames; } public File getMainPath() { return mainPath; } public void setMainPath(File mainPath) { this.mainPath = mainPath; } public ArrayList<File> getFileList() { return fileList; } public void setFileList(ArrayList<File> fileList) { this.fileList = fileList; } public ArrayList<String> getFileNames() { return fileNames; } public void setFileNames(ArrayList<String> fileNames) { this.fileNames = fileNames; } public TextView getPath() { return path; } public void setPath(TextView path) { this.path = path; } }