Generalized File Manager (File Categorizer)
What you learn: You will learn recursively search files and list them
Description:
I have modified the existing File Browser to a more generalized feel. I have taken some of the code from here and there
. I'm using the steve osborn's files' IconifiedText, IconifiedTextListAdapter and IconifiedTextView. There are however certain issued which i'm facing. I'll be posting the issues down. The screen shots should be self-explanatory regarding the App.Code: AllFiles.java
Using java Syntax Highlighting
- package com.cognizant.android.PhoneContentSearch;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.ListActivity;
- import android.app.NotificationManager;
- import android.content.Intent;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- public class AllFiles extends ListActivity {
- private List<String> allfiles = new ArrayList<String>();
- protected static String file_type = "FILE_TYPE";
- protected static String folder_type = "Folder_TYPE";
- protected static int search = 0;
- private int notification_id = 1;
- private boolean m_shownotification = true;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, getAllFiles());
- this.setTheme(android.R.style.Theme_Dark);
- this.setTitleColor(Color.TRANSPARENT);
- this.setListAdapter(directoryList);
- }
- private List<String> getAllFiles() {
- allfiles.clear();
- String[] fileList = getResources().getStringArray(R.array.allFileList);
- for (String filetype : fileList) {
- allfiles.add(filetype);
- }
- return allfiles;
- }
- /** Called when an item is selected* */
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- int selectionRowID = (int) this.getSelectionRowID();
- String[] param;
- String selectedFileString = this.allfiles.get(selectionRowID);
- start_stop_Notification();
- switch (selectionRowID) {
- case 0:
- param = getResources().getStringArray(R.array.fileEndingImage);
- break;
- case 1:
- param = getResources().getStringArray(R.array.fileEndingAudio);
- break;
- case 2:
- param = getResources().getStringArray(R.array.fileEndingVideo);
- break;
- case 3:
- param = getResources().getStringArray(R.array.fileEndingApp);
- break;
- case 4:
- param = getResources().getStringArray(R.array.fileEndingWebText);
- break;
- default:
- param = getResources().getStringArray(R.array.fileEndingWebText);
- }
- Bundle bundle = new Bundle();
- bundle.putStringArray(file_type, param);
- Intent i = new Intent(AllFiles.this, SearchAllFiles.class);
- i.putExtras(bundle);
- i.putExtra(folder_type, selectedFileString);
- startSubActivity(i, search);
- }
- protected void start_stop_Notification() {
- NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- if (m_shownotification) {
- nm.notifyWithText(notification_id,
- getText(R.string.notification_text),
- NotificationManager.LENGTH_LONG, null);
- }else{
- nm.cancel(notification_id);
- }
- }
- //@Override
- protected void onActivityResult(int requestCode, int resultCode,
- String data, Bundle extras) {
- super.onActivityResult(requestCode, resultCode, data, extras);
- // Here We identify the subActivity we started
- if(requestCode == search){
- m_shownotification=false;
- start_stop_Notification();
- }
- }
- }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4
Code: SearchAllFiles.java
Using java Syntax Highlighting
- package com.cognizant.android.PhoneContentSearch;
- import java.io.File;
- import java.net.URISyntaxException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Iterator;
- import java.util.List;
- import android.app.AlertDialog;
- import android.app.ListActivity;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.DialogInterface.OnClickListener;
- import android.graphics.Color;
- import android.graphics.drawable.Drawable;
- import android.net.ContentURI;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ListView;
- public class SearchAllFiles extends ListActivity {
- private List<String> directoryEntries = new ArrayList<String>();
- private List<IconifiedText> searchEntries = new ArrayList<IconifiedText>();
- private File currentDirectory = new File("/");
- private String[] search_text;
- private String folder_type;
- private boolean filter_dir = true;
- private boolean disable_onlistclick=false;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- browseToRoot();
- this.setTitleColor(Color.TRANSPARENT);
- this.setTheme(android.R.style.Theme_Dialog);
- Bundle bundle = getIntent().getExtras();
- if (bundle != null) {
- search_text = bundle.getStringArray(AllFiles.file_type);
- folder_type = bundle.getString(AllFiles.folder_type);
- if (search_text != null) {
- SearchForDirectory(search_text);
- }
- }
- this.setResult(AllFiles.search);
- this.finishSubActivity(AllFiles.search);
- }
- /**
- * This function browses to the root-directory of the file-system.
- */
- private void browseToRoot() {
- filterFiles(new File("/"));
- }
- private void browseTo(final File aDirectory) {
- if (!(aDirectory.isDirectory())) {
- OnClickListener okButtonListener = new OnClickListener() {
- // @Override
- public void onClick(DialogInterface arg0, int arg1) {
- try {
- // Lets start an intent to View the file, that was
- // clicked...
- Intent myIntent = new Intent(
- android.content.Intent.VIEW_ACTION,
- new ContentURI("file://"
- + aDirectory.getAbsolutePath()));
- startActivity(myIntent);
- } catch (URISyntaxException e) {
- e.printStackTrace();
- }
- }
- };
- OnClickListener cancelButtonListener = new OnClickListener() {
- // @Override
- public void onClick(DialogInterface arg0, int arg1) {
- // Do nothing
- }
- };
- AlertDialog.show(this, "Question",
- "Do you want to open this file?n" + aDirectory.getName(),
- "OK", okButtonListener, "Cancel", cancelButtonListener,
- false, null);
- }
- }
- //Filter the files.
- //forbidden_folders = { "/sys", "/dev", "/proc", "etc" };
- private void filterFiles(final File aDir) {
- File[] filesAndDirs = aDir.listFiles();
- List<File> filesDirs = Arrays.asList(filesAndDirs);
- List<File> updatedDir = new ArrayList<File>();
- if (filter_dir) {
- for (File file : filesDirs) {
- if (!((file.getPath().equals("/sys"))
- || (file.getPath().equals("/dev"))
- || (file.getPath().equals("/proc")) || (file.getPath()
- .equals("/etc")))) {
- updatedDir.add(file);
- }
- }
- }
- for (File file : updatedDir) {
- fillFiles(file);
- }
- }
- //Recursive loop to populate the files.
- //Not a fast method <img src="http://www.anddev.org/images/smilies/icon_cry.gif" alt=":cry:" title="Crying or Very sad" />
- private List<String> fillFiles(final File aDir) {
- this.currentDirectory = aDir;
- int currentDirectoryLength = this.currentDirectory.getAbsolutePath()
- .length();
- File[] filesAndDirs = aDir.listFiles();
- if (filesAndDirs != null) {
- List<File> filesDirs = Arrays.asList(filesAndDirs);
- for (File file : filesDirs) {
- this.directoryEntries.add(file.getAbsolutePath().substring(
- currentDirectoryLength+1));
- if (file.isDirectory()) {
- fillFiles(file);
- }
- }
- }
- Collections.sort(this.directoryEntries);
- return this.directoryEntries;
- }
- // Search for the file type
- protected void SearchForDirectory(String[] searchText) {
- this.searchEntries.clear();
- Drawable currentIcon = null;
- for (String filetype : searchText) {
- for (Iterator iterator = directoryEntries.iterator(); iterator
- .hasNext();) {
- String type = (String) iterator.next();
- if (type.contains(filetype)) {
- if (checkEndsWithInStringArray(type, getResources()
- .getStringArray(R.array.fileEndingImage))) {
- currentIcon = getResources().getDrawable(
- R.drawable.image);
- } else if (checkEndsWithInStringArray(type, getResources()
- .getStringArray(R.array.fileEndingWebText))) {
- currentIcon = getResources().getDrawable(
- R.drawable.webpage);
- } else if (checkEndsWithInStringArray(type, getResources()
- .getStringArray(R.array.fileEndingAudio))) {
- currentIcon = getResources().getDrawable(
- R.drawable.audio);
- } else if (checkEndsWithInStringArray(type, getResources()
- .getStringArray(R.array.fileEndingApp))) {
- currentIcon = getResources().getDrawable(
- R.drawable.icon1);
- } else if (checkEndsWithInStringArray(type, getResources()
- .getStringArray(R.array.fileEndingVideo))) {
- currentIcon = getResources().getDrawable(
- R.drawable.video);
- } else {
- currentIcon = getResources().getDrawable(
- R.drawable.text);
- }
- searchEntries.add(new IconifiedText(type, currentIcon));
- }
- }
- if (!(this.searchEntries.isEmpty())) {
- IconifiedTextListAdapter itla = new IconifiedTextListAdapter(
- this);
- itla.setListItems(this.searchEntries);
- this.setListAdapter(itla);
- } else {
- disable_onlistclick=true;
- //this.setDefaultKeyMode(DISABLE_DEFAULT_KEYS);
- currentIcon=getResources().getDrawable(R.drawable.sadsmiley);
- String no_match="Sorry! No matches for " + folder_type;
- this.searchEntries.add(new IconifiedText(no_match, currentIcon));
- IconifiedTextListAdapter itlanomatch = new IconifiedTextListAdapter(
- this);
- itlanomatch.setListItems(this.searchEntries);
- this.setListAdapter(itlanomatch);
- }
- }
- }
- /**
- * Checks whether checkItsEnd ends with one of the Strings from fileEndings
- */
- private boolean checkEndsWithInStringArray(String checkItsEnd,
- String[] fileEndings) {
- for (String aEnd : fileEndings) {
- if (checkItsEnd.endsWith(aEnd))
- return true;
- }
- return false;
- }
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- int selectionRowID = (int) this.getSelectionRowID();
- File clickedFile = new File(this.currentDirectory.getAbsolutePath()+this.directoryEntries.get(selectionRowID));
- if (clickedFile != null && !disable_onlistclick){
- this.browseTo(clickedFile);
- }
- }
- }
Parsed in 0.058 seconds, using GeSHi 1.0.8.4
code:allfiles.xml
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <array name="allFileList">
- <item>Pictures</item>
- <item>Sounds</item>
- <item>Videos</item>
- <item>Android Applications</item>
- <item>Saved Web Pages</item>
- <item>Others</item>
- </array>
- </resources>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
code:fileendings.xml
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <array name="fileEndingImage">
- <item>.png</item>
- <item>.gif</item>
- <item>.jpg</item>
- <item>.jpeg</item>
- <item>.bmp</item>
- </array>
- <array name="fileEndingAudio">
- <item>.mp3</item>
- <item>.wav</item>
- <item>.ogg</item>
- <item>.midi</item>
- </array>
- <array name="fileEndingPackage">
- <item>.jar</item>
- <item>.zip</item>
- <item>.rar</item>
- <item>.gz</item>
- </array>
- <array name="fileEndingWebText">
- <item>.htm</item>
- <item>.html</item>
- <item>.php</item>
- </array>
- <array name="fileEndingVideo">
- <item>.avi</item>
- <item>.flv</item>
- <item>.wav</item>
- </array>
- <array name="fileEndingApp">
- <item>.apk</item>
- </array>
- </resources>
Parsed in 0.007 seconds, using GeSHi 1.0.8.4
O.K. now i come to the issues that i'm facing.
1)As seen in the androidapp2.PNG im doing a sort of a wait message using the notification manager (not sure if i'm using it coreectly
2)Is there any way to display an image or icon in that status bar which displays the 'Please wait...' message??
3)How can i attach an image inline with the code
I also welcome any suggestions on improving the app.
Thanks,
Nitin



):


.
)

