All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sdklite.sphere.hybrid.HybridApplication Maven / Gradle / Ivy

package com.sdklite.sphere.hybrid;

import java.io.File;
import java.io.IOException;

import android.app.Application;
import android.net.http.HttpResponseCache;
import android.os.Build;
import android.os.StatFs;

import com.sdklite.sphere.logging.Logger;
import com.sdklite.sphere.logging.LoggerFactory;

/**
 * The base class of hybrid application
 *
 * @author johnsonlee
 * @since 1.0.0
 */
public class HybridApplication extends Application {

    private static final Logger logger = LoggerFactory.getLogger("HybridApplication");

    private Object cache;

    @Override
    public void onCreate() {
        super.onCreate();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            final File dir = new File(getCacheDir(), "http");
            if (!dir.exists()) {
                dir.mkdirs();
            }

            try {
                final StatFs stat = new StatFs(dir.getAbsolutePath());
                final long blocks = stat.getBlockCount();
                final long blockSize = stat.getBlockSize();
                this.cache = HttpResponseCache.install(dir, blocks * blockSize);
                logger.debug("Install HTTP response cache at %s", dir.getAbsolutePath());
            } catch (final Exception e) {
                logger.error("Install HTTP response cache error", e);
            }
        }
    }

    @Override
    public void onTerminate() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            if (null != this.cache) {
                try {
                    ((HttpResponseCache) this.cache).close();
                } catch (final IOException e) {
                    logger.error("Close HTTP response cache error", e);
                }
            }
        }

        super.onTerminate();
    }

}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy