Android Open Source - my-wallpaper File Browser Activity






From Project

Back to project page my-wallpaper.

License

The source code is released under:

MIT License

If you think the Android project my-wallpaper 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.koonen.photostream;
//w  w w  .ja  va 2s .c  om
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
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.Toast;

public class FileBrowserActivity extends ListActivity {

  static final String EXTRA_FILE_PATH = "com.koonen.photostream.FilePath";
  public static final int REQUEST_SHOW_FILE_SYSTEM_PHOTO = 45;

  private enum DISPLAYMODE {
    ABSOLUTE, RELATIVE;
  }

  private final DISPLAYMODE displayMode = DISPLAYMODE.RELATIVE;
  private List<String> directoryEntries = new ArrayList<String>();
  private File currentDirectory = null;

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

    setDefaultCurrentPath();

    browseTo(currentDirectory);
  }

  private void setDefaultCurrentPath() {
    if (Environment.MEDIA_MOUNTED.equals(Environment
        .getExternalStorageState())) {
      currentDirectory = Environment.getExternalStorageDirectory();
    } else {
      Toast.makeText(FileBrowserActivity.this,
          R.string.can_not_access_to_sdcard, Toast.LENGTH_LONG)
          .show();
      currentDirectory = new File("/");
    }
  }

  /**
   * This function browses up one level according to the field:
   * currentDirectory
   */
  private void upOneLevel() {
    if (this.currentDirectory.getParent() != null)
      this.browseTo(this.currentDirectory.getParentFile());
  }

  private void browseTo(final File aDirectory) {
    if (aDirectory.isDirectory()) {
      this.currentDirectory = aDirectory;
      fill(aDirectory.listFiles());
    } else {
      clickToFile(aDirectory);
      // OnClickListener okButtonListener = new OnClickListener() {
      //
      // @Override
      // public void onClick(View v) {
      // // Lets start an intent to View the file, that was
      // // clicked...
      // clickToFile(aDirectory);
      // }
      // };
      // OnClickListener cancelButtonListener = new OnClickListener() {
      //
      // @Override
      // public void onClick(View v) {
      // }
      //
      // };

      // LinearLayout layout = new LinearLayout(this);
      // layout.setOrientation(LinearLayout.VERTICAL);
      //
      // LinearLayout fieldLayout = new LinearLayout(this);
      // fieldLayout.setOrientation(LinearLayout.VERTICAL);
      //
      // TextView view = new TextView(this);
      // view.setText("Do you want to open that file?\n");
      // view.setPadding(5, 0, 0, 0);
      // fieldLayout.addView(view);
      //
      // LinearLayout buttonLayout = new LinearLayout(this);
      // buttonLayout.setGravity(Gravity.RIGHT);
      // buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
      //
      // Button okButton = new Button(this);
      // okButton.setText("ok");
      // okButton.setOnClickListener(okButtonListener);
      // layout.addView(okButton);
      // Button cancelButton = new Button(this);
      // cancelButton.setText("cancel");
      //
      // layout.addView(fieldLayout);
      // layout.addView(buttonLayout);

      // new AlertDialog.Builder(FileBrowserActivity.this).setTitle(
      // "Question").setView(layout).create().show();
    }
  }

  private void clickToFile(File aFile) {
    Intent intent = new Intent();
    intent.putExtra(EXTRA_FILE_PATH, Uri.parse(
        "file://" + aFile.getAbsolutePath()).toString());
    setResult(ActivityConstants.SIMILAR_RESPONSE_ID, intent);
    finish();
  }

  private void fill(File[] files) {
    this.directoryEntries.clear();

    // Add the "." == "current directory"
    // and the ".." == 'Up one level'
    this.directoryEntries.add(getString(R.string.current_dir));
    if (this.currentDirectory.getParent() != null)
      this.directoryEntries.add(getString(R.string.up_one_level));

    switch (this.displayMode) {
    case ABSOLUTE:
      for (File file : files) {
        String fileName = file.getName();

        if (file.isDirectory()
            || checkEndsWithInStringArray(fileName, getResources()
                .getStringArray(R.array.fileEndingImage))) {
          this.directoryEntries.add(file.getPath());
        }
      }
      break;
    case RELATIVE: // On relative Mode, we have to add the current-path to
      // the beginning
      int currentPathStringLenght = this.currentDirectory
          .getAbsolutePath().length();
      for (File file : files) {
        String fileName = file.getName();

        if (file.isDirectory()
            || checkEndsWithInStringArray(fileName, getResources()
                .getStringArray(R.array.fileEndingImage))) {
          this.directoryEntries.add(file.getAbsolutePath().substring(
              currentPathStringLenght));
        }
      }
      break;
    }

    ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,
        R.layout.file_row, this.directoryEntries);

    this.setListAdapter(directoryList);
  }

  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    // int selectionRowID = (int) this.getSelectionRowID();
    // int selectionRowID = (int) this.getSelectedItemId();
    int selectionRowID = position;

    String selectedFileString = this.directoryEntries.get(selectionRowID);
    if (selectedFileString.equals(getString(R.string.current_dir))) {
      // Refresh
      this.browseTo(this.currentDirectory);
    } else if (selectedFileString.equals(getString(R.string.up_one_level))) {
      this.upOneLevel();
    } else {
      File clickedFile = null;
      switch (this.displayMode) {
      case RELATIVE:
        clickedFile = new File(this.currentDirectory.getAbsolutePath()
            + this.directoryEntries.get(selectionRowID));
        break;
      case ABSOLUTE:
        clickedFile = new File(this.directoryEntries
            .get(selectionRowID));
        break;
      }
      if (clickedFile != null)
        this.browseTo(clickedFile);
    }
  }

  static void show(Activity context) {
    Intent intent = new Intent(context, FileBrowserActivity.class);

    context.startActivityForResult(intent, REQUEST_SHOW_FILE_SYSTEM_PHOTO);
  }

  /**
   * 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;
  }
}




Java Source Code List

com.koonen.photostream.ActivityConstants.java
com.koonen.photostream.BootReceiver.java
com.koonen.photostream.CameraPreviewActivity.java
com.koonen.photostream.CategoryActivity.java
com.koonen.photostream.CategoryAdapter.java
com.koonen.photostream.CategoryEditActivity.java
com.koonen.photostream.CropWallpaperTask.java
com.koonen.photostream.Eula.java
com.koonen.photostream.FastBitmapDrawable.java
com.koonen.photostream.FileBrowserActivity.java
com.koonen.photostream.GridLayout.java
com.koonen.photostream.ImageUtilities.java
com.koonen.photostream.PhotostreamActivity.java
com.koonen.photostream.RotationService.java
com.koonen.photostream.ServiceConnector.java
com.koonen.photostream.SetWallpaperTask.java
com.koonen.photostream.UserTask.java
com.koonen.photostream.ViewPhotoActivity.java
com.koonen.photostream.WallPaperExecutor.java
com.koonen.photostream.api.FilePhoto.java
com.koonen.photostream.api.IPhotoService.java
com.koonen.photostream.api.Location.java
com.koonen.photostream.api.PhotoList.java
com.koonen.photostream.api.PhotoSize.java
com.koonen.photostream.api.Photo.java
com.koonen.photostream.api.ResponseHandler.java
com.koonen.photostream.api.ResponseParser.java
com.koonen.photostream.api.ServiceContext.java
com.koonen.photostream.api.ServiceException.java
com.koonen.photostream.api.ServiceManager.java
com.koonen.photostream.api.ServiceNetworkException.java
com.koonen.photostream.api.SourceType.java
com.koonen.photostream.api.Type.java
com.koonen.photostream.api.UserInfo.java
com.koonen.photostream.api.UserNotFoundException.java
com.koonen.photostream.api.User.java
com.koonen.photostream.api.flickr.Auth.java
com.koonen.photostream.api.flickr.FlickrConstants.java
com.koonen.photostream.api.flickr.FlickrService.java
com.koonen.photostream.api.flickr.Perms.java
com.koonen.photostream.dao.CategoryDAO.java
com.koonen.photostream.dao.CategoryList.java
com.koonen.photostream.dao.Category.java
com.koonen.photostream.dao.ImageDAO.java
com.koonen.photostream.dao.PhotoDAO.java
com.koonen.photostream.dao.PhotoUrlListProvider.java
com.koonen.photostream.dao.PhotoUrlList.java
com.koonen.photostream.dao.PhotoUrl.java
com.koonen.photostream.effects.EffectsApplier.java
com.koonen.photostream.effects.EffectsFactory.java
com.koonen.photostream.effects.Rotate3dAnimation.java
com.koonen.photostream.effects.TypeEffect.java
com.koonen.photostream.settings.BackgroundSource.java
com.koonen.photostream.settings.Network.java
com.koonen.photostream.settings.UserPreferences.java
com.koonen.photostream.settings.UserSettingsActivity.java
com.koonen.photostream.settings.WallpaperSettingMode.java
com.koonen.utils.ConfigurationReader.java
com.koonen.utils.DialogUtils.java
com.koonen.utils.Enumeration.java
com.koonen.utils.GroupUtils.java
com.koonen.utils.MailSender.java
com.koonen.utils.StatisticUtils.java
com.koonen.utils.StreamUtils.java