Android Open Source - Resonos-Android-Framework Alt Android File Handle






From Project

Back to project page Resonos-Android-Framework.

License

The source code is released under:

Apache License

If you think the Android project Resonos-Android-Framework 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.resonos.apps.library.file;
//from  w w  w . j a va  2s  .c o m
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import android.content.res.AssetManager;

/**
 * @author mzechner
 * @author Nathan Sweet
 */
public class AltAndroidFileHandle extends AltFileHandle {
  // The asset manager, or null if this is not an internal file.
  transient AssetManager assets;

  public AltAndroidFileHandle(AssetManager assets, String fileName, AltFileType type) {
    super(fileName, type);
    this.assets = assets;
  }

  public AltAndroidFileHandle(AssetManager assets, File file, AltFileType type) {
    super(file, type);
    this.assets = assets;
  }

  public AltFileHandle child(String name) {
    if (file.getPath().length() == 0)
      return new AltAndroidFileHandle(assets, new File(name), type);
    return new AltAndroidFileHandle(assets, new File(file, name), type);
  }

  public AltFileHandle parent() {
    File parent = file.getParentFile();
    if (parent == null) {
      if (type == AltFileType.Absolute)
        parent = new File("/");
      else
        parent = new File("");
    }
    return new AltAndroidFileHandle(assets, parent, type);
  }

  public InputStream read() {
    if (type == AltFileType.Internal) {
      try {
        return assets.open(file.getPath());
      } catch (IOException ex) {
        throw new RuntimeException("Error reading file: " + file
            + " (" + type + ")", ex);
      }
    }
    return super.read();
  }

  public AltFileHandle[] list() {
    if (type == AltFileType.Internal) {
      try {
        String[] relativePaths = assets.list(file.getPath());
        AltFileHandle[] handles = new AltFileHandle[relativePaths.length];
        for (int i = 0, n = handles.length; i < n; i++)
          handles[i] = new AltAndroidFileHandle(assets, new File(file,
              relativePaths[i]), type);
        return handles;
      } catch (Exception ex) {
        throw new RuntimeException("Error listing children: " + file
            + " (" + type + ")", ex);
      }
    }
    return super.list();
  }
  
  public void setAssets(AssetManager assets) {
    this.assets = assets;
  }

  public AltFileHandle[] list(String suffix) {
    if (type == AltFileType.Internal) {
      try {
        String[] relativePaths = assets.list(file.getPath());
        AltFileHandle[] handles = new AltFileHandle[relativePaths.length];
        int count = 0;
        for (int i = 0, n = handles.length; i < n; i++) {
          String path = relativePaths[i];
          if (!path.endsWith(suffix))
            continue;
          handles[count] = new AltAndroidFileHandle(assets, new File(
              file, path), type);
          count++;
        }
        if (count < relativePaths.length) {
          AltFileHandle[] newHandles = new AltFileHandle[count];
          System.arraycopy(handles, 0, newHandles, 0, count);
          handles = newHandles;
        }
        return handles;
      } catch (Exception ex) {
        throw new RuntimeException("Error listing children: " + file
            + " (" + type + ")", ex);
      }
    }
    return super.list();
  }

  public boolean isDirectory() {
    if (type == AltFileType.Internal) {
      try {
        return assets.list(file.getPath()).length > 0;
      } catch (IOException ex) {
        return false;
      }
    }
    return super.isDirectory();
  }

  public boolean exists() {
    if (type == AltFileType.Internal) {
      String fileName = file.getPath();
      try {
        assets.open(fileName).close(); // Check if file exists.
        return true;
      } catch (Exception ex) {
        // This is SUPER slow!
        // try {
        // return assets.list(fileName).length > 0;
        // } catch (Exception ignored) {
        // }
        return false;
      }
    }
    return super.exists();
  }

  public long length() {
    if (type == AltFileType.Internal) {
      try {
        return assets.openFd(file.getPath()).getLength();
      } catch (IOException ignored) {
      }
    }
    return super.length();
  }

  public long lastModified() {
    return super.lastModified();
  }
}




Java Source Code List

com.resonos.apps.library.Action.java
com.resonos.apps.library.AlertFragment.java
com.resonos.apps.library.App.java
com.resonos.apps.library.BaseFragment.java
com.resonos.apps.library.FragmentBaseActivity.java
com.resonos.apps.library.file.AltAndroidFileHandle.java
com.resonos.apps.library.file.AltAndroidFiles.java
com.resonos.apps.library.file.AltFileHandle.java
com.resonos.apps.library.file.FileCache.java
com.resonos.apps.library.media.AudioVisualizer.java
com.resonos.apps.library.media.BitmapMemoryCache.java
com.resonos.apps.library.media.HueColorFilter.java
com.resonos.apps.library.media.ImageLoader.java
com.resonos.apps.library.media.MediaScannerNotifier.java
com.resonos.apps.library.model.Coord.java
com.resonos.apps.library.model.ImmutableCoord.java
com.resonos.apps.library.tabviewpager.CustomViewPager.java
com.resonos.apps.library.tabviewpager.PageIndicator.java
com.resonos.apps.library.tabviewpager.TabPageIndicator.java
com.resonos.apps.library.tabviewpager.TabViewPagerAdapter.java
com.resonos.apps.library.tabviewpager.TabViewPagerFragment.java
com.resonos.apps.library.tabviewpager.TitleProvider.java
com.resonos.apps.library.util.AppUtils.java
com.resonos.apps.library.util.ErrorReporter.java
com.resonos.apps.library.util.LifecycleTaskQueue.java
com.resonos.apps.library.util.M.java
com.resonos.apps.library.util.NetworkClient.java
com.resonos.apps.library.util.NetworkRequest.java
com.resonos.apps.library.util.ParameterList.java
com.resonos.apps.library.util.SensorReader.java
com.resonos.apps.library.util.TouchViewWorker.java
com.resonos.apps.library.util.ViewServer.java
com.resonos.apps.library.widget.DashboardLayout.java
com.resonos.apps.library.widget.FormBuilder.java
com.resonos.apps.library.widget.FormElement.java
com.resonos.apps.library.widget.ListFormBuilder.java
com.resonos.apps.library.widget.PopupWindows3D.java
com.resonos.apps.library.widget.QuickAction3D.java
com.resonos.apps.library.widget.RangeSeekBar.java
com.resonos.apps.library.widget.SeekBar.java
com.resonos.apps.library.widget.ToolBarButton.java
com.resonos.apps.library.widget.ToolBar.java