Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2010, 2011, 2012, 2013 mapsforge.org
 * Copyright  2014 Ludwig M Brinckmann
 * Copyright  2014 devemux86
 *
 * This program is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

import android.annotation.TargetApi;

import android.os.StatFs;

public class Main {
    /**
     * @param cacheDirectoryName where the file system tile cache will be located
     * @param firstLevelSize     size of the first level cache, no point cache being smaller
     * @param tileSize           tile size
     * @return recommended number of files in FileSystemTileCache
     */
    public static int estimateSizeOfFileSystemCache(String cacheDirectoryName, int firstLevelSize, int tileSize) {
        // assumption on size of files in cache, on the large side as not to eat
        // up all free space, real average probably 50K compressed
        final int tileCacheFileSize = 4 * tileSize * tileSize;
        final int maxCacheFiles = 2000; // arbitrary, probably too high

        // result cannot be bigger than maxCacheFiles
        int result = (int) Math.min(maxCacheFiles, getAvailableCacheSlots(cacheDirectoryName, tileCacheFileSize));

        if (firstLevelSize > result) {
            // no point having a file system cache that does not even hold the memory cache
            result = 0;
        }
        return result;
    }

    /**
     * Get the number of tiles that can be stored on the file system.
     *
     * @param directory where the cache will reside
     * @param fileSize  average size of tile to be cached
     * @return number of tiles that can be stored without running out of space
     */
    @SuppressWarnings("deprecation")
    @TargetApi(18)
    public static long getAvailableCacheSlots(String directory, int fileSize) {
        StatFs statfs = new StatFs(directory);
        if (android.os.Build.VERSION.SDK_INT >= 18) {
            return statfs.getAvailableBytes() / fileSize;
        }
        // problem is overflow with devices with large storage, so order is important here
        // additionally avoid division by zero in devices with a large block size
        int blocksPerFile = Math.max(fileSize / statfs.getBlockSize(), 1);
        return statfs.getAvailableBlocks() / blocksPerFile;
    }
}