Last active
May 19, 2016 01:46
-
-
Save VassilisPallas/9c8d1d1e5ab12f236563c73f052f12f9 to your computer and use it in GitHub Desktop.
Creates a list with files from a selected path. These data will be used on a GridView. The adapter and the FileItem can change according the information that must be shown on the GridView
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 android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
import com.vspallas.npflocker.R; | |
import com.vspallas.npflocker.general.FileType; | |
import com.vspallas.npflocker.listeners.RecyclerItemClickListener; | |
import com.vspallas.npflocker.models.FileItem; | |
import java.io.File; | |
import java.io.FilenameFilter; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
/** | |
* Created by vassilis on 5/19/16. | |
*/ | |
public class FillFilesGridView implements RecyclerItemClickListener.OnItemClickListener { | |
Context context; | |
private ArrayList<FileItem> imageItems; | |
// Check if the first level of the directory structure is the one showing | |
private Boolean isTopParent = true; | |
private File rootPath; | |
private File selectedFile; | |
// Stores names of traversed directories | |
private ArrayList<String> dirNames = new ArrayList<>(); | |
private Bitmap bitmap; | |
private RecyclerView recyclerView; | |
private final String root; | |
private SomeAdapter adapter; | |
public FillFilesGridView(Context context, RecyclerView recyclerView) { | |
this.context = context; | |
this.recyclerView = recyclerView; | |
root = Actions.getInternalPath(context); | |
rootPath = new File(root); | |
} | |
public void loadFiles() { | |
rootPath.mkdirs(); | |
if (rootPath.exists()) { | |
FilenameFilter filter = new FilenameFilter() { | |
@Override | |
public boolean accept(File dir, String filename) { | |
File sel = new File(dir, filename); | |
// Filters based on whether the file is hidden or not | |
return (sel.isFile() || sel.isDirectory()); | |
} | |
}; | |
String[] fileList = rootPath.list(filter); | |
imageItems = new ArrayList<>(); | |
for (int i = 0; i < fileList.length; i++) { | |
File selected = new File(rootPath, fileList[i]); | |
if (selected.isDirectory()) { | |
bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_folder_white); | |
imageItems.add(new FileItem(bitmap, fileList[i], selected.length())); | |
} else { | |
extensionImage(fileList[i], selected); | |
if (!isTopParent) { | |
ArrayList temp = new ArrayList(); | |
bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_up); | |
temp.add(0, new FileItem(bitmap, "..", selected.length())); | |
for (int ii = 0; ii < imageItems.size(); ii++) { | |
temp.add(ii + 1, imageItems.get(ii)); | |
} | |
imageItems = temp; | |
} | |
} | |
} | |
Collections.sort(imageItems); | |
adapter = new SomeAdapter(imageItems); | |
recyclerView.setAdapter(adapter); | |
fillList(); | |
} else { | |
// inform user that the path does not exists | |
} | |
} | |
private void fillList() { | |
if (imageItems == null || imageItems.size() == 0) { | |
return; | |
} | |
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(context, recyclerView, this)); | |
} | |
private void backButtonPressed() { | |
if (!isTopParent) { | |
// present directory removed from list | |
String s = dirNames.remove(dirNames.size() - 1); | |
// rootPath modified to exclude present directory | |
rootPath = new File(rootPath.toString().substring(0, | |
rootPath.toString().lastIndexOf(s))); | |
imageItems = null; | |
if (dirNames.isEmpty() || rootPath.equals(root)) { | |
isTopParent = true; | |
} | |
loadFiles(); | |
} | |
} | |
@Override | |
public void onItemClick(View view, int position) { | |
String chosenFile = imageItems.get(position).getTitle(); | |
selectedFile = new File(rootPath + "/" + chosenFile); | |
if (selectedFile.isDirectory() && !chosenFile.equalsIgnoreCase("..")) { | |
isTopParent = false; | |
// Adds chosen directory to list | |
dirNames.add(chosenFile); | |
imageItems = null; | |
rootPath = new File(selectedFile + ""); | |
loadFiles(); | |
} // Checks if '..' was clicked | |
else if (chosenFile.equalsIgnoreCase("..")) { | |
backButtonPressed(); | |
} else { | |
// open file | |
} | |
} | |
@Override | |
public void onItemLongClick(View view, int position) { | |
} | |
private void extensionImage(String file, File selected) { | |
FileType type = Actions.getAttachmentType(file); | |
switch (type) { | |
case TXT: | |
bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_file_white); | |
break; | |
case PDF: | |
bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_file_pdf); | |
break; | |
case WORD: | |
bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_file_word); | |
break; | |
case EXCEL: | |
bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_file_excel); | |
break; | |
case POWERPOINT: | |
bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_file_powerpoint); | |
break; | |
case AUDIO: | |
bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_file_audio); | |
break; | |
case VIDEO: | |
bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_file_video); | |
break; | |
case ZIP: | |
bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_zip); | |
break; | |
default: | |
bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_file_white); | |
break; | |
} | |
imageItems.add(new FileItem(bitmap, file, selected.length())); | |
} | |
} |
public static AttachmentType getAttachmentType(String file) {
String extension = file.substring(file.lastIndexOf('.') + 1).toLowerCase();
switch (extension.toLowerCase()) {
case "txt":
case "srt":
case "rtf":
case "nfo":
case "html":
case "css":
case "xml":
case "json":
return AttachmentType.TXT;
case "pdf":
return AttachmentType.PDF;
case "doc":
case "dot":
case "docx":
case "docm":
case "dotx":
case "dotm":
case "docb":
return AttachmentType.WORD;
case "xls":
case "xlt":
case "xlm":
case "xlsx":
case "xlsm":
case "xltx":
case "xltm":
case "xlsb":
case "xla":
case "xlam":
case "xll":
case "xlw":
return AttachmentType.EXCEL;
case "ppt":
case "pot":
case "pps":
case "pptx":
case "pptm":
case "potx":
case "potm":
case "ppam":
case "ppsx":
case "ppsm":
case "sldx":
case "sldm":
return AttachmentType.POWERPOINT;
case "wma":
case "wav":
case "mp3":
case "mid":
case "m4a":
return AttachmentType.AUDIO;
case "gif":
case "jpeg":
case "jpg":
case "jif":
case "jfif":
case "jp2":
case "jpx":
case "j2k":
case "j2c":
case "fpx":
case "png":
case "dng":
return AttachmentType.IMAGE;
case "mkv":
case "flv":
case "ogv":
case "ogg":
case "avi":
case "mov":
case "wmv":
case "mp4":
case "m4p":
case "m4v":
case "mpg":
case "mp2":
case "mpeg":
case "raw":
return AttachmentType.VIDEO;
case "rar":
case "zip":
case "tar":
return AttachmentType.ZIP;
default:
return AttachmentType.UNKwOWN;
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
listener for
RecycleView
is here. Images can be found here and here.