Just a slight modification to the existing Generalized File Manager. This one is with a better UI (hopefully

The snaps should hopefully explain the functionality, if not please let me know.
The IconifiedText.java , IconifiedTextListAdapter.java and IconifiedTextView.java remains the same from the initial version of Generalized File Manager.
GeneralizedFileManager2.java
Using java Syntax Highlighting
- package com.cognizant.GFM2;
- 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.KeyEvent;
- import android.view.View;
- import android.widget.ListView;
- public class GeneralizedFileManager2 extends ListActivity {
- private List<String> directoryEntries = new ArrayList<String>();
- private List<IconifiedText> searchEntries = new ArrayList<IconifiedText>();
- private File currentDirectory = new File("/");
- private String folder_type;
- private static int checkWhichItem = 0;
- 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);
- setContentView(R.layout.main);
- Toolbar.setup(this);
- browseToRoot();
- this.setTitleColor(Color.TRANSPARENT);
- searchFile();
- }
- @Override
- public boolean onKeyUp(int keyCode, KeyEvent event) {
- switch (keyCode) {
- case KeyEvent.KEYCODE_DPAD_RIGHT:
- if (checkWhichItem > 4) {
- checkWhichItem = 0;
- }
- checkWhichItem++;
- searchFile();
- return true;
- case KeyEvent.KEYCODE_DPAD_LEFT:
- checkWhichItem--;
- if (checkWhichItem < 0) {
- checkWhichItem = 4;
- }
- searchFile();
- return true;
- }
- return false;
- }
- private void searchFile() {
- switch (checkWhichItem) {
- case 0:
- SearchForDirectory(getResources().getStringArray(
- R.array.fileEndingImage), "Image", checkWhichItem);
- break;
- case 1:
- SearchForDirectory(getResources().getStringArray(
- R.array.fileEndingAudio), "Audio", checkWhichItem);
- break;
- case 2:
- SearchForDirectory(getResources().getStringArray(
- R.array.fileEndingVideo), "Video", checkWhichItem);
- break;
- case 3:
- SearchForDirectory(getResources().getStringArray(
- R.array.fileEndingPackage), "Package", checkWhichItem);
- break;
- case 4:
- SearchForDirectory(getResources().getStringArray(
- R.array.fileEndingApp), "Android App", checkWhichItem);
- break;
- }
- }
- /**
- Calls the setNewFocus defined in Toolbar.java and
- sets the request to the appropriate icon.
- */
- private void setNewFocus(int focus_id) {
- Toolbar.setNewFocus(focus_id);
- }
- /**
- * 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" ,
- "OK", okButtonListener, "Cancel", cancelButtonListener,
- false, null);
- }
- }
- /*Filters the unwanted dirs and passes the
- * rest and searches for the file types*/
- 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);
- }
- }
- 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;
- }
- /**
- * This will get the list of all the files
- * depending on the file type
- */
- protected void SearchForDirectory(String[] searchText, String file_type,
- int file_type_no) {
- this.searchEntries.clear();
- Drawable currentIcon = null;
- folder_type = file_type;
- 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.fileEndingPackage))) {
- currentIcon = getResources().getDrawable(
- R.drawable.packed);
- } 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.android);
- } else if (checkEndsWithInStringArray(type, getResources()
- .getStringArray(R.array.fileEndingVideo))) {
- currentIcon = getResources().getDrawable(
- R.drawable.video);
- } else {
- currentIcon = getResources().getDrawable(
- R.drawable.packed);
- }
- 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;
- String no_match = "Sorry! No matches for " + folder_type;
- this.searchEntries.add(new IconifiedText(no_match,
- getCurrentIcon(file_type_no)));
- IconifiedTextListAdapter itlanomatch = new IconifiedTextListAdapter(
- this);
- itlanomatch.setListItems(this.searchEntries);
- this.setListAdapter(itlanomatch);
- }
- }
- /**
- This will set the focus of the icons
- whenever the LEFT or RIGHT Keypad are
- pressed.
- */
- setNewFocus(checkWhichItem);
- }
- /**
- * This will get the current icon depending on the
- * file type.
- */
- private Drawable getCurrentIcon(int file_type_no) {
- Drawable icon= null;
- switch (file_type_no) {
- case 0:
- icon= getResources().getDrawable(
- R.drawable.image);
- break;
- case 1:
- icon= getResources().getDrawable(
- R.drawable.audio);
- break;
- case 2:
- icon= getResources().getDrawable(
- R.drawable.video);
- break;
- case 3:
- icon= getResources().getDrawable(
- R.drawable.packed);
- break;
- case 4:
- icon= getResources().getDrawable(
- R.drawable.android);
- break;
- }
- return icon;
- }
- /**
- * 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.026 seconds, using GeSHi 1.0.8.4
Toolbar.java
Using java Syntax Highlighting
- /**
- *
- */
- package com.cognizant.GFM2;
- import java.util.Map;
- import android.app.Activity;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.View;
- import android.widget.ImageButton;
- import android.widget.TableLayout;
- import android.widget.TableRow;
- /*
- * referenced from Steven Osborn's Undroid project at: http://code.google.com/p/undroid/w/list
- * MANY THANKS!!!
- */
- public class Toolbar {
- public static final int PICTURE_ID = 100;
- public static final int AUDIO_ID = 101;
- public static final int VIDEO_ID = 102;
- public static final int APP_ID = 103;
- public static final int PACKAGE_ID = 104;
- //button references
- public static ImageButton pictureIcon;
- public static ImageButton audioIcon;
- public static ImageButton videoIcon;
- public static ImageButton appIcon;
- public static ImageButton packageIcon;
- public static Activity currentActivity;
- public static void setup(Activity a){
- currentActivity = a;
- //Images
- pictureIcon = (ImageButton) a.findViewById(PICTURE_ID);
- pictureIcon.setOnFocusChangeListener(toolbarFocusListener);
- // Audio
- audioIcon = (ImageButton) a.findViewById(AUDIO_ID);
- audioIcon.setOnFocusChangeListener(toolbarFocusListener);
- // Video
- videoIcon = (ImageButton) a.findViewById(VIDEO_ID);
- videoIcon.setOnFocusChangeListener(toolbarFocusListener);
- // Packages
- appIcon = (ImageButton) a.findViewById(APP_ID);
- appIcon.setOnFocusChangeListener(toolbarFocusListener);
- // AndroidApps
- packageIcon = (ImageButton) a.findViewById(PACKAGE_ID);
- packageIcon.setOnFocusChangeListener(toolbarFocusListener);
- }
- public static void setNewFocus(int focus_id){
- switch (focus_id) {
- case 0:
- pictureIcon.requestFocus();
- break;
- case 1:
- audioIcon.requestFocus();
- break;
- case 2:
- videoIcon.requestFocus();
- break;
- case 3:
- appIcon.requestFocus();
- break;
- case 4:
- packageIcon.requestFocus();
- break;
- }
- }
- private static ImageButton.OnFocusChangeListener toolbarFocusListener =
- new ImageButton.OnFocusChangeListener() {
- public void onFocusChanged(View arg0, boolean arg1) {
- if(arg1) {
- arg0.setBackground(R.drawable.background2);
- } else {
- arg0.setBackground(R.drawable.background);
- }
- }
- };
- public static class ToolBarView extends TableLayout {
- public ToolBarView(Context context, AttributeSet attrs, Map inflateParams) {
- super(context, attrs, inflateParams);
- // Create row container
- TableRow row = new TableRow(context);
- // Create buttons
- ImageButton pictureButton = new ImageButton(context);
- ImageButton audioButton = new ImageButton(context);
- ImageButton videoButton = new ImageButton(context);
- ImageButton appButton = new ImageButton(context);
- ImageButton packageButton = new ImageButton(context);
- pictureButton.setId(PICTURE_ID);
- audioButton.setId(AUDIO_ID);
- videoButton.setId(VIDEO_ID);
- appButton.setId(APP_ID);
- packageButton.setId(PACKAGE_ID);
- // Set button images
- pictureButton.setImageDrawable(getResources()
- .getDrawable(R.drawable.image));
- audioButton.setImageDrawable(getResources()
- .getDrawable(R.drawable.audio));
- videoButton.setImageDrawable(getResources()
- .getDrawable(R.drawable.video));
- appButton.setImageDrawable(getResources()
- .getDrawable(R.drawable.packed));
- packageButton.setImageDrawable(getResources()
- .getDrawable(R.drawable.android));
- // Set Padding l,t,r,b
- pictureButton.setPadding(0, 3, 0, 3);
- audioButton.setPadding(0, 3, 0, 3);
- videoButton.setPadding(0, 3, 0, 3);
- appButton.setPadding(0, 3, 0, 3);
- packageButton.setPadding(0, 3, 0, 3);
- // Set Background Image
- pictureButton.setBackground(getResources()
- .getDrawable(R.drawable.background));
- audioButton.setBackground(getResources()
- .getDrawable(R.drawable.background));
- videoButton.setBackground(getResources()
- .getDrawable(R.drawable.background));
- appButton.setBackground(getResources()
- .getDrawable(R.drawable.background));
- packageButton.setBackground(getResources()
- .getDrawable(R.drawable.background));
- // Add buttons to row
- row.addView(pictureButton, new TableRow.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
- row.addView(audioButton, new TableRow.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
- row.addView(videoButton, new TableRow.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
- row.addView(appButton, new TableRow.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
- row.addView(packageButton, new TableRow.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
- // Make columns expand to fill space
- setColumnStretchable(0, true);
- setColumnStretchable(1, true);
- setColumnStretchable(2, true);
- setColumnStretchable(3, true);
- setColumnStretchable(4, true);
- // Set row background color
- row.setBackground(getResources().getDrawable(R.drawable.background));
- // Add row to table
- addView(row, new TableLayout.LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
- }
- }
- }
Parsed in 0.020 seconds, using GeSHi 1.0.8.4
main.xml
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <view class="com.cognizant.GFM2.Toolbar$ToolBarView"
- id="@+id/toolbar"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- <ListView id="@android:id/list"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
All the other xml files remain the same.
The GFM2.0 makes use of the LEFT and RIGHT KEYPAD buttons to navigate to the icons.
As usual i have an issue

However on click of the Left or Right Keypad button, there is a considerable amount of delay. what i mean is that the focus first goes to the icon and after about a second only the file types are listed. Not sure if this is bcoz of the slowness of my PC(700MHZ)..
Please let me know of any improvements.
And now to the snaps!!
Thanx,
Nitin