
it.tidalwave.mobile.android.io.AndroidFileSystem 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: AndroidFileSystem.java,v af5e94efb8ca 2010/06/26 18:14:20 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: $
*
**********************************************************************************************************************/
@ServiceProvider(service=FileSystem.class)
public class AndroidFileSystem 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
{
final String state = getExternalStorageState();
if (!MEDIA_MOUNTED.equals(state))
{
throw new IOException("External storage is not ready: " + state);
}
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 statFs. getAvailableBlocks() * statFs.getBlockSize();
}
@Nonnull
protected String getExternalStorageState()
{
return Environment.getExternalStorageState();
}
@Nonnull
protected File getExternalStorageDirectory()
{
return Environment.getExternalStorageDirectory();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy