Commit 32f02997 authored by Drahomír Karchňák's avatar Drahomír Karchňák

#656 Added platform dependant FileSystemManager to check available free space....

#656 Added platform dependant FileSystemManager to check available free space. Still needs some testing...
parent 1e6325d0
......@@ -26,12 +26,17 @@ import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import java.io.File;
import java.io.IOException;
import cz.nic.tablexia.Tablexia;
import cz.nic.tablexia.android.camera.AndroidCamera2QRCodeScanner;
import cz.nic.tablexia.debug.BuildConfig;
import cz.nic.tablexia.debug.R;
import cz.nic.tablexia.screen.loader.IConnectionManager;
import cz.nic.tablexia.android.camera.AndroidQRCodeScanner;
import cz.nic.tablexia.util.IFileSystemManager;
import cz.nic.tablexia.util.Log;
import cz.nic.tablexia.util.ui.QRCodeScanner;
public class AndroidLauncher extends AndroidApplication {
......@@ -65,6 +70,7 @@ public class AndroidLauncher extends AndroidApplication {
getResources().getConfiguration().locale,
SQL_CONNECTION_TYPE,
new AndroidConnectionManager(getContext()),
new AndroidFileSystemManager(),
qrCodeScanner,
getResources().getString(R.string.sentry_dsn),
HAS_SOFT_BACK_BUTTON,
......@@ -116,4 +122,27 @@ public class AndroidLauncher extends AndroidApplication {
}
}
}
public static class AndroidFileSystemManager implements IFileSystemManager {
@Override
public Long getSpaceAvailable(String path) {
File file = new File(path);
if(file.exists() && file.isDirectory()) {
return file.getUsableSpace();
}
else {
try {
file.createNewFile();
Long bytes = file.getUsableSpace();
file.delete();
return bytes;
} catch (IOException e) {
Log.err(getClass(), "Can't get space available for path: " + path, e);
}
}
return null;
}
}
}
......@@ -47,6 +47,7 @@ import cz.nic.tablexia.menu.MenuController;
import cz.nic.tablexia.model.DatabaseManager;
import cz.nic.tablexia.screen.AbstractTablexiaScreen;
import cz.nic.tablexia.screen.loader.IConnectionManager;
import cz.nic.tablexia.util.IFileSystemManager;
import cz.nic.tablexia.util.Log;
import cz.nic.tablexia.util.Utility;
import cz.nic.tablexia.util.ui.button.StandardTablexiaButton;
......@@ -90,6 +91,7 @@ public class Tablexia extends TablexiaApplication {
private static final boolean LOADER_ERROR_DIALOG_HIDE_ANIMATION = false;
private static IConnectionManager connectionManager;
private static IFileSystemManager fileSystemManager;
private static QRCodeScanner qrCodeScanner;
private final SQLConnectionType sqlConnectionType;
......@@ -124,9 +126,10 @@ public class Tablexia extends TablexiaApplication {
}
}
public Tablexia(String buildType, Locale systemLocale, SQLConnectionType sqlConnectionType, IConnectionManager connManager, QRCodeScanner qrCodeScanner, String sentryDSN, boolean hasSoftBackButton, boolean reset, String hwSerial) {
public Tablexia(String buildType, Locale systemLocale, SQLConnectionType sqlConnectionType, IConnectionManager connManager, IFileSystemManager fileSystemManager, QRCodeScanner qrCodeScanner, String sentryDSN, boolean hasSoftBackButton, boolean reset, String hwSerial) {
this.reset = reset;
this.connectionManager = validateConnectionManager(connManager);
this.fileSystemManager = validateFileSystemManager(fileSystemManager);
this.qrCodeScanner = qrCodeScanner;
this.sqlConnectionType = sqlConnectionType;
TablexiaSettings.init(buildType, systemLocale, hasSoftBackButton, hwSerial);
......@@ -460,6 +463,18 @@ public class Tablexia extends TablexiaApplication {
return connectionManager;
}
//////////////////////////// File System Manager
private static IFileSystemManager validateFileSystemManager(IFileSystemManager fileSystemManager) {
if(fileSystemManager == null)
throw new RuntimeException(Tablexia.class.getName() + ": File System Manager equals to " + fileSystemManager);
return fileSystemManager;
}
public static IFileSystemManager getFileSystemManager() {
return fileSystemManager;
}
//////////////////////////// QR CODE READER
public static boolean hasQRCodeScanner() {
return qrCodeScanner != null;
......
......@@ -145,14 +145,14 @@ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplic
* @return true if enough space is available, false otherwise.
*/
public boolean checkSpaceAvailable() {
boolean enoughSpaceAvailable = true;
File downloadDir = new File(TablexiaAbstractFileManager.getFileStoragePath(TablexiaAbstractFileManager.DownloadStorageType.EXTERNAL));
long usableSpace = downloadDir.getUsableSpace(); // if returns 0L we don't know for sure that there is not enough place
Log.debug(getClass(), "Usable space: " + usableSpace);
if (usableSpace != 0 && usableSpace < TABLEXIA_ASSETS_MINIMAL_SPACE_REQUIRED)
enoughSpaceAvailable = false;
return enoughSpaceAvailable;
Long result = Tablexia.getFileSystemManager().getSpaceAvailable(downloadDir.getAbsolutePath());
if(result == null) {
Log.err(getClass(), "FileSystemManager.getSpaceAvailable() returned null!");
return true; //Something went wrong. Try to download assets anyway...
}
return result > TABLEXIA_ASSETS_MINIMAL_SPACE_REQUIRED;
}
/**
......
package cz.nic.tablexia.util;
/**
* Created by bcx on 1/16/17.
*/
public interface IFileSystemManager {
/**
* Returns free space for path in bytes (null on failure)
*/
Long getSpaceAvailable(String path);
}
/*
* Copyright (C) 2016 CZ.NIC, z.s.p.o. (http://www.nic.cz/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.nic.tablexia.util.assetmanager;
import com.badlogic.gdx.files.FileHandle;
import java.util.HashMap;
import cz.nic.tablexia.loader.TablexiaAbstractFileManager;
import cz.nic.tablexia.util.Log;
/**
* Created by lhoracek on 4/3/15.
*/
public class DirectoryAsset extends HashMap<String, String> {
private final String basePath;
public DirectoryAsset(String path){
basePath = path;
loadFilesInDir(basePath);
}
private void loadFilesInDir(String path) {
for (FileHandle entry : getDirHandle(path).list()) {
loadFiles("", entry);
}
}
private void loadFiles(String path, FileHandle fh) {
if (fh.isDirectory()) {
Log.debug(getClass(), "Is directory: " + fh.name());
for (FileHandle entry : fh.list()) {
loadFiles(path + fh.name() + "/", entry);
}
} else {
add(path, fh);
}
}
private FileHandle getDirHandle(String path) {
FileHandle dirHandle;
dirHandle = TablexiaAbstractFileManager.getFileStoragePathFileHandle(TablexiaAbstractFileManager.AssetsStorageType.EXTERNAL, path);
return dirHandle;
}
public String add(String path, FileHandle fh) {
String fullPath = basePath + "/" + path + fh.name();
String key = path + fh.nameWithoutExtension();
Log.debug(getClass(), "Adding asset: " + key + " : " + fullPath);
return super.put(key, fullPath);
}
@Override
public String get(Object key) {
String value = super.get(key);
if(value == null){
Log.err(getClass(), "Asset not prepared: " + key);
throw new IllegalArgumentException("Asset not prepared: " + key);
}
return value;
}
}
......@@ -22,12 +22,16 @@ import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.jcabi.manifests.Manifests;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
import cz.nic.tablexia.Tablexia;
import cz.nic.tablexia.TablexiaBuildConfig;
import cz.nic.tablexia.TablexiaSettings;
import cz.nic.tablexia.screen.loader.IConnectionManager;
import cz.nic.tablexia.util.IFileSystemManager;
import cz.nic.tablexia.util.Log;
import cz.nic.tablexia.util.ui.QRCodeScanner;
public class DesktopLauncher {
......@@ -75,7 +79,7 @@ public class DesktopLauncher {
config.addIcon(DESKTOP_ICON_128, Files.FileType.Internal);
}
new LwjglApplication(new Tablexia(buildType, Locale.getDefault(), SQL_CONNECTION_TYPE, new DesktopConnectionManager(), null, sentryDSN, HAS_SOFT_BACK_BUTTON, true, null), config);
new LwjglApplication(new Tablexia(buildType, Locale.getDefault(), SQL_CONNECTION_TYPE, new DesktopConnectionManager(), new DesktopFileSystemManager(), null, sentryDSN, HAS_SOFT_BACK_BUTTON, true, null), config);
}
private static String loadAttributeFromManifest(String attributeName, String defaultValue) {
......@@ -97,4 +101,27 @@ public class DesktopLauncher {
return ConnectionType.Ethernet;
}
}
public static class DesktopFileSystemManager implements IFileSystemManager {
@Override
public Long getSpaceAvailable(String path) {
File file = new File(path);
if(file.exists() && file.isDirectory()) {
return file.getUsableSpace();
}
else {
try {
file.createNewFile();
Long bytes = file.getUsableSpace();
file.delete();
return bytes;
} catch (IOException e) {
Log.err(getClass(), "Can't get space available for path: " + path, e);
}
}
return null;
}
}
}
......@@ -22,7 +22,10 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.iosrobovm.IOSApplication;
import com.badlogic.gdx.backends.iosrobovm.IOSApplicationConfiguration;
import org.robovm.apple.foundation.NSErrorException;
import org.robovm.apple.foundation.NSException;
import org.robovm.apple.foundation.NSFileManager;
import org.robovm.apple.foundation.NSFileSystemAttributes;
import org.robovm.objc.block.VoidBlock1;
import org.robovm.apple.foundation.NSAutoreleasePool;
import org.robovm.apple.foundation.NSBundle;
......@@ -37,6 +40,8 @@ import org.robovm.apple.uikit.UIApplicationLaunchOptions;
import java.util.Locale;
import cz.nic.tablexia.screen.loader.IConnectionManager;
import cz.nic.tablexia.util.IFileSystemManager;
import cz.nic.tablexia.util.Log;
public class IOSLauncher extends IOSApplication.Delegate {
......@@ -68,6 +73,7 @@ public class IOSLauncher extends IOSApplication.Delegate {
Locale.getDefault(),
SQL_CONNECTION_TYPE,
new IOSConnectionManager(),
new IOSFileSystemManager(),
new IOSQRCodeScanner(),
sentryDSN,
HAS_SOFT_BACK_BUTTON,
......@@ -132,4 +138,18 @@ public class IOSLauncher extends IOSApplication.Delegate {
}
}
}
private static class IOSFileSystemManager implements IFileSystemManager {
@Override
public Long getSpaceAvailable(String path) {
try {
NSFileSystemAttributes fileSystemAttributes = NSFileManager.getDefaultManager().getAttributesOfFileSystemAtPath(path);
return fileSystemAttributes.getFreeSize();
} catch (NSErrorException e) {
Log.err(getClass(), "Can't get free space for path: " + path + "!", e);
}
return null;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment