andbook!.pdf - Learning Android Get an anddev.org - Android-Shirt Back to index
anddev.org Header Logo
FAQ Search Top rated articles Browse Feeds anddev.org - Authors Contact Details Register Log in

Generalized File Manager 2 - GFM2.0


 
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials
Author Message
Nitinkcv
Developer
Developer


Joined: 29 Nov 2007
Posts: 29

PostPosted: Sat Dec 29, 2007 11:49 am    Post subject: Generalized File Manager 2 - GFM2.0 Reply with quote

Hi all,

Just a slight modification to the existing Generalized File Manager. This one is with a better UI (hopefully Razz ) which makes use of the existing Toolbar functionality.

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
Java:

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);
          }

     }
}


Toolbar.java

Java:

/**
 *
 */

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));
         }


     }
}



main.xml
XML:

<?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>


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 Smile
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



GFM2.0(pic2).JPG
 Description:
On loading the App..
 Filesize:  10.59 KB
 Viewed:  7930 Time(s)

GFM2.0(pic2).JPG



GFM2.0(pic1).JPG
 Description:
On clicking the LEFT KEYPAD..
 Filesize:  17.48 KB
 Viewed:  7932 Time(s)

GFM2.0(pic1).JPG


Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2660
Location: College Park, MD

PostPosted: Thu Jan 03, 2008 11:59 am    Post subject: Reply with quote

Hello Nitinkcv,

Delays are never good for user-experience. (Haven't run your updated project yet.) Perhaps add a Source ProgressBar and/or make the entries appear as they are found during the search and not just in the very end of the search.

Regards,
plusminus

_________________
Download my apps Idea
Please remember, that this board is give & take Smile


| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
vijays
Freshman
Freshman


Joined: 23 Mar 2008
Posts: 4
Location: india

PostPosted: Sun Mar 23, 2008 11:14 am    Post subject: get errors in FileManager2 code Reply with quote

error :ContentURI cant br resolved

code:
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();
}

2)error:R.array cannot be resolved

code:
private void searchFile() {
switch (checkWhichItem) {

case 0:
SearchForDirectory(getResources().getStringArray(
R.array1.fileEndingImage), "Image", checkWhichItem);

break;

case 1:
SearchForDirectory(getResources().getStringArray(
R.array1.fileEndingAudio), "Audio", checkWhichItem);

break;

case 2:
SearchForDirectory(getResources().getStringArray(
R.array1.fileEndingVideo), "Video", checkWhichItem);

break;

case 3:
SearchForDirectory(getResources().getStringArray(
R.array1.fileEndingPackage), "Package", checkWhichItem);

break;

case 4:
SearchForDirectory(getResources().getStringArray(
R.array1.fileEndingApp), "Android App", checkWhichItem);

break;

}
}

3)error:multiple makers at this line..for this(IconifiedTextListAdapter itla = new IconifiedTextListAdapter)

code:if (!(this.searchEntries.isEmpty())) {

IconifiedTextListAdapter itla = new IconifiedTextListAdapter(
this);
itla.setListItems(this.searchEntries);
this.setListAdapter(itla);
} else {


4)error:int selectionRowID = (int) this.getSelectionRowID();

getSelectionRowID() METHOD GOT PROBLEM



plz help mi to solve this problem..
thanks in advanced
Back to top
View user's profile Send private message
vijays
Freshman
Freshman


Joined: 23 Mar 2008
Posts: 4
Location: india

PostPosted: Sun Mar 23, 2008 11:17 am    Post subject: sry forgot file name Reply with quote

errors in:
GeneralizedFileManager2.java
Back to top
View user's profile Send private message
Rajkumar_A
Freshman
Freshman


Joined: 22 Dec 2008
Posts: 2
Location: Bangalore

PostPosted: Mon Dec 22, 2008 6:26 am    Post subject: Re: sry forgot file name Reply with quote

vijays wrote:
errors in:
GeneralizedFileManager2.java


I am new in android application development. So please guide me.

I have some questions.

1. How to monitor device using our application?. Means before any event occur into the device we have to Monitor them. For example Suppose I got one email, call or even I opened the browser. I have to collect all its information.

2. Can I control all the native(Default) application from my application ?.

Is it possible to do this ?

please reply ..

Smile
[mod]Dude, you asked this in a new thread, why ask it here too? Your question has nothing to do with this thread.

_________________
I have good knowledge about Blackberry, J2ME, T-Mobile and now working on Android
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger
Display posts from previous:   
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


© 2007, Android Development Community
All rights reserved.
Powered by phpBB.