Android Open Source - AndroidRandomWallpaper Simple File Explorer






From Project

Back to project page AndroidRandomWallpaper.

License

The source code is released under:

GNU General Public License

If you think the Android project AndroidRandomWallpaper listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.elbauldelprogramador.randomwallpaper.util;
//from  w  ww .ja va  2 s  . com
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.elbauldelprogramador.randomwallpaper.R;

//http://android-er.blogspot.com.es/2010/01/implement-simple-file-explorer-in.html
public class SimpleFileExplorer extends ListActivity {

  private List<String> item = null;
  private List<String> path = null;
  private String root = null;
  private TextView myPath;
  private final String PARENT = "../";

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_simple_file_explorer);
    myPath = (TextView) findViewById(R.id.path);
    
    if (!checkStorageState()){
      setResult(RWGlobal.RESULT_NO_PATH);
      finish();
    } else {
      root = Environment.getExternalStorageDirectory().toString();
      getDir(root);
    }
  }

  private void getDir(String dirPath){

    myPath.setText("Location: " + dirPath);
    item = new ArrayList<String>();
    path = new ArrayList<String>();
    File f = new File(dirPath);
    FileFilter dotFilter = new FileFilter() {
      
      @Override
      public boolean accept(File pathname) {
        if (pathname.getName().startsWith(".")) 
          return false;
        return true;
      }
    };
    
    File[] files = f.listFiles(dotFilter);
    
    if (!dirPath.equals(root)) {
      item.add(root);
      path.add(root);
      item.add(PARENT);
      path.add(f.getParent());
    }

    if (files == null){
      setResult(RWGlobal.RESULT_NO_FILES);
      finish();
    } else {
      for (int i = 0; i < files.length; i++) {
  
        File file = files[i];
        path.add(file.getPath());
        if (file.isDirectory())
          item.add(file.getName() + "/");
        else
          item.add(file.getName());
      }

      Collections.sort(item.subList(2, item.size()));
      Collections.sort(path.subList(2, path.size()));
      ArrayAdapter<String> fileList = new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1, item);
      setListAdapter(fileList);
    }
  }

  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {

    final File file = new File(path.get(position));

    if (file.isDirectory()) {
      if (file.canRead())
        if ( item.get(position) == PARENT || item.get(position) == root )
          getDir(path.get(position));
        else
          cd_dir_or_pick(file, position);
      else {
        new AlertDialog.Builder(this)
            .setIcon(R.drawable.ic_launcher)
            .setTitle("[" + file.getName() + "] " + getString(R.string.sfe_cant_read_folder))
            .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog,
                      int which) {
                    // TODO Auto-generated method stub
                  }
        }).show();
      }
    }/* else {
      new AlertDialog.Builder(this)
          .setIcon(R.drawable.ic_launcher)
          .setTitle("[" + file.getName() + "]")
          .setPositiveButton("OK",
              new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog,
                    int which) {
                  // TODO Auto-generated method stub
                }
      }).show();
    }*/
  }
  /**
   * Returns true if the user wants enter the directory, false otherwise 
   */
  private void cd_dir_or_pick(final File file, final int position){
    new AlertDialog.Builder(this)
    .setIcon(R.drawable.ic_launcher)
    .setMessage(getString(R.string.sfe_select_action))
    .setPositiveButton(getString(R.string.sfe_select_folder), new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        if (RWGlobal.thereAreImages(file.getAbsolutePath().toString())){
          setResult(RESULT_OK, new Intent().setData(Uri.parse(file.getAbsolutePath().toString())));
          dialog.cancel();
          finish();
        } else 
          RWGlobal.toast(getApplicationContext(), getString(R.string.sfe_noImages));
      }
    })
    .setNegativeButton(getString(R.string.sfe_enter_folder), new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        //setResult(RESULT_CANCELED, null);
        dialog.cancel();
        getDir(path.get(position));
      }
    })
    .show();
  }
  
  private boolean checkStorageState(){
    boolean mExternalStorageAvailable = false;
    //boolean mExternalStorageWriteable = false;
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
        mExternalStorageAvailable /*= mExternalStorageWriteable*/ = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can only read the media
        mExternalStorageAvailable = true;
        //mExternalStorageWriteable = false;
    } else {
        // Something else is wrong. It may be one of many other states, but all we need
        //  to know is we can neither read nor write
        mExternalStorageAvailable /*= mExternalStorageWriteable*/ = false;
    }
    
    return mExternalStorageAvailable;
  }
}




Java Source Code List

com.elbauldelprogramador.randomwallpaper.Service.java
com.elbauldelprogramador.randomwallpaper.activities.AboutTheApp.java
com.elbauldelprogramador.randomwallpaper.activities.MainActivity.java
com.elbauldelprogramador.randomwallpaper.reveivers.AlarmReceiver.java
com.elbauldelprogramador.randomwallpaper.reveivers.OnBootReceiver.java
com.elbauldelprogramador.randomwallpaper.util.RWGlobal.java
com.elbauldelprogramador.randomwallpaper.util.SimpleFileExplorer.java
com.elbauldelprogramador.randomwallpaper.util.StrictModeWrapper.java