it.tidalwave.mobile.android.io.AndroidExternalFileSystem Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* blueBill Mobile - open source birdwatching
* ==========================================
*
* Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
* http://bluebill.tidalwave.it/mobile/
*
***********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
***********************************************************************************************************************
*
* $Id: AndroidExternalFileSystem.java,v 79a497f64ec9 2010/08/18 23:41:56 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.mobile.android.io;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.openide.util.lookup.ServiceProvider;
import org.openide.util.Lookup;
import it.tidalwave.mobile.io.FileSystem;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import static android.os.Environment.*;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id: $
*
**********************************************************************************************************************/
public class AndroidExternalFileSystem implements FileSystem
{
@CheckForNull
private StatFs statFs;
@Nonnull
private final Context context = Lookup.getDefault().lookup(Context.class);
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public OutputStream openFileOutput (final @Nonnull String path)
throws IOException
{
return new FileOutputStream(getFile(path));
// return context.openFileOutput(name, Context.MODE_WORLD_READABLE);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public InputStream openFileInput (final @Nonnull String path)
throws IOException
{
return new FileInputStream(getFile(path));
// return context.openFileInput(name);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public File getFile (final @Nonnull String path)
throws IOException
{
if (!isWritable())
{
throw new IOException("External storage is not ready: " + getExternalStorageState());
}
final File file = new File(getExternalStorageDirectory(), context.getPackageName() +"/" + path);
file.getParentFile().mkdirs();
return file;
// final File f = new File(name);
// final File dir = context.getDir(f.getParent(), Context.MODE_WORLD_READABLE);
// return new File(dir, f.getName());
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnegative
public synchronized long getFreeSpace()
{
if (statFs == null)
{
statFs = new StatFs(getExternalStorageDirectory().getAbsolutePath());
}
return (long)statFs.getAvailableBlocks() * (long)statFs.getBlockSize();
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public boolean isWritable()
{
final String state = getExternalStorageState();
return MEDIA_MOUNTED.equals(state);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
protected String getExternalStorageState()
{
return Environment.getExternalStorageState();
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
protected File getExternalStorageDirectory()
{
return Environment.getExternalStorageDirectory();
}
}