Android Open Source - EnergyWastingApp Ext Storage File Writer






From Project

Back to project page EnergyWastingApp.

License

The source code is released under:

Copyright ? 2013-2014 Pekka Ekman <pekka.ekman@aalto.fi> 2013 Babujee Jerome Robin <robin.babujeejerome@aalto.fi> Permission is hereby granted, free of charge, to any person obtaining a ...

If you think the Android project EnergyWastingApp 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 fi.aalto.pekman.energywastingapp.components;
//from  www .j a  va 2 s.co m
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Vector;

import android.os.Environment;
import android.util.Log;

public class ExtStorageFileWriter extends Component {

  @Override
  public String getName() { return "Write file (all external storage)"; }

  @Override
  public boolean isSupported() {
    String state = Environment.getExternalStorageState();
    return Environment.MEDIA_MOUNTED.equals(state);
  }

  private Vector<SpecifiedDirectoryFileWriter> writers =
      new Vector<ExtStorageFileWriter.SpecifiedDirectoryFileWriter>();

  /** File writer class for each external storage directory */
  private static class SpecifiedDirectoryFileWriter extends AbstractFileWriter {
    private File directory;

    public SpecifiedDirectoryFileWriter(File directory) {
      this.directory = directory;
    }

    @Override
    public String getName() { return "(used internally)"; }

    @Override
    protected File getPath() {
      return directory;
    }
  }

  @Override
  public void start() {
    Map<String, File> dirs = getAllStorageLocations();
    
    StringBuilder s = new StringBuilder("Found " + dirs.size() + " external storage locations:");
    for (Map.Entry<String, File> entry : dirs.entrySet()) {
      s.append("\n    " + entry.getKey() + ": " + entry.getValue());
    }
    Log.d("ExtStorageFileWriter", s.toString());
    
    for (File dir : dirs.values()) {
      SpecifiedDirectoryFileWriter writer = new SpecifiedDirectoryFileWriter(dir);
      writers.add(writer);
      Log.d("ExtStorageFileWriter", "Starting writer for " + dir);
      writer.start();
    }
  }

  @Override
  public void stop() {
    for (SpecifiedDirectoryFileWriter writer : writers) {
      Log.d("ExtStorageFileWriter", "Stopping writer for " + writer.directory);
      writer.stop();
    }
    writers.clear();
  }

  
  private static final String SD_CARD = "sdCard";
  private static final String EXTERNAL_SD_CARD = "externalSdCard";

  /**
   * Finds external storage locations.
   *
   * This code is copied from
   * <a href="http://stackoverflow.com/a/15612964">this stackoverflow.com message</a>.
   *
   * @return A map of all storage locations available
   */
  private static Map<String, File> getAllStorageLocations() {
    Map<String, File> map = new HashMap<String, File>(10);

    List<String> mMounts = new ArrayList<String>(10);
    List<String> mVold = new ArrayList<String>(10);
    mMounts.add("/mnt/sdcard");
    mVold.add("/mnt/sdcard");

    try {
      File mountFile = new File("/proc/mounts");
      if(mountFile.exists()){
        Scanner scanner = new Scanner(mountFile);
        try {
          while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.startsWith("/dev/block/vold/")) {
              String[] lineElements = line.split(" ");
              String element = lineElements[1];
  
              // don't add the default mount path
              // it's already in the list.
              if (!element.equals("/mnt/sdcard"))
                mMounts.add(element);
            }
          }
        } finally {
          scanner.close();
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      File voldFile = new File("/system/etc/vold.fstab");
      if(voldFile.exists()){
        Scanner scanner = new Scanner(voldFile);
        try {
          while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.startsWith("dev_mount")) {
              String[] lineElements = line.split(" ");
              String element = lineElements[2];
  
              if (element.contains(":"))
                element = element.substring(0, element.indexOf(":"));
              if (!element.equals("/mnt/sdcard"))
                mVold.add(element);
            }
          }
        } finally {
          scanner.close();
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }


    for (int i = 0; i < mMounts.size(); i++) {
      String mount = mMounts.get(i);
      if (!mVold.contains(mount))
        mMounts.remove(i--);
    }
    mVold.clear();

    List<String> mountHash = new ArrayList<String>(10);

    for(String mount : mMounts){
      File root = new File(mount);
      if (root.exists() && root.isDirectory() && root.canWrite()) {
        File[] list = root.listFiles();
        String hash = "[";
        if(list!=null){
          for(File f : list){
            hash += f.getName().hashCode()+":"+f.length()+", ";
          }
        }
        hash += "]";
        if(!mountHash.contains(hash)){
          String key = SD_CARD + "_" + map.size();
          if (map.size() == 0) {
            key = SD_CARD;
          } else if (map.size() == 1) {
            key = EXTERNAL_SD_CARD;
          }
          mountHash.add(hash);
          map.put(key, root);
        }
      }
    }

    mMounts.clear();

    if(map.isEmpty()){
      map.put(SD_CARD, Environment.getExternalStorageDirectory());
    }
    return map;
  }

}




Java Source Code List

fi.aalto.pekman.energywastingapp.MainActivity.java
fi.aalto.pekman.energywastingapp.components.AbstractCamera.java
fi.aalto.pekman.energywastingapp.components.AbstractFileWriter.java
fi.aalto.pekman.energywastingapp.components.AppDirFileWriter.java
fi.aalto.pekman.energywastingapp.components.BlueToothBurn.java
fi.aalto.pekman.energywastingapp.components.CPUBurn.java
fi.aalto.pekman.energywastingapp.components.Component.java
fi.aalto.pekman.energywastingapp.components.Display.java
fi.aalto.pekman.energywastingapp.components.ExtStorageFileWriter.java
fi.aalto.pekman.energywastingapp.components.GPSCoordSearch.java
fi.aalto.pekman.energywastingapp.components.RecordAudio.java
fi.aalto.pekman.energywastingapp.components.StillCamera.java
fi.aalto.pekman.energywastingapp.components.TonePlay.java
fi.aalto.pekman.energywastingapp.components.Vibration.java
fi.aalto.pekman.energywastingapp.components.VideoCamera.java
fi.aalto.pekman.energywastingapp.components.WiFiDataTransfer.java