Android Open Source - hubroid Request Cache






From Project

Back to project page hubroid.

License

The source code is released under:

Copyright (c) 2011 Eddie Ringle. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistribution...

If you think the Android project hubroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (c) 2012 Eddie Ringle//from  w  w w.java 2 s  .c o m
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted
 * provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this list of conditions
 * and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
 * and the following disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package net.idlesoft.android.apps.github.utils;

import net.idlesoft.android.apps.github.ui.activities.BaseActivity;

import org.eclipse.egit.github.core.Repository;
import org.eclipse.egit.github.core.User;
import org.eclipse.egit.github.core.client.GsonUtils;
import org.eclipse.egit.github.core.service.RepositoryService;
import org.eclipse.egit.github.core.service.UserService;

import android.content.Context;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Date;

public class RequestCache {

    private final static String ROOT_DIR = "requests/";

    private final static String REPOSITORY_DIR = "repositories/";

    private final static String USER_DIR = "users/";

    /* TODO: The amount of time to keep cache should be a preference, really */
    private final static long KEEP_LENGTH = 24 * 60 * 60000;

    public static Repository getRepository(final BaseActivity context, final String owner,
            final String name, final boolean forceUpdate) {
        Repository repo = null;
        boolean shouldRefresh = false;
        final File dir = new File(context.getCacheDir(), ROOT_DIR + REPOSITORY_DIR);
        if (!dir.exists() || !dir.isDirectory()) {
            dir.mkdirs();
        }
        final File f = new File(dir, owner + "_" + name + ".json");
        if (!forceUpdate && f.exists()) {            /* Check if the cached JSON is really old */
            final Date d = new Date();
            final long elderCheck = d.getTime() - (KEEP_LENGTH);
            if (f.lastModified() < elderCheck) {
                shouldRefresh = true;
            } else {
                try {
                    final FileInputStream in = new FileInputStream(f);
                    repo = GsonUtils.fromJson(new BufferedReader(
                            new InputStreamReader(in)), Repository.class);
                    in.close();
                    return repo;
                } catch (Exception e) {
                    shouldRefresh = true;
                }
            }
        } else {
            shouldRefresh = true;
        }
        if (shouldRefresh || forceUpdate) {
            try {
                final RepositoryService rs = new RepositoryService(context.getGHClient());
                repo = rs.getRepository(owner, name);
                if (repo != null) {
                    putRepository(context, repo);
                }
            } catch (Exception e) {
                repo = null;
                e.printStackTrace();
            }
        }
        return repo;
    }

    public static Repository getRepository(final BaseActivity context, final String owner,
            final String name) {
        return getRepository(context, owner, name, false);
    }

    public static boolean putRepository(final BaseActivity context, final Repository repository) {
        final File dir = new File(context.getCacheDir(), ROOT_DIR + REPOSITORY_DIR);
        if (!dir.exists() || !dir.isDirectory()) {
            dir.mkdirs();
        }
        final File f = new File(dir, repository.getOwner().getLogin() + "_"
                + repository.getName() + ".json");
        if (f.exists()) {
            f.delete();
        }
        try {
            final FileOutputStream out = new FileOutputStream(f, false);
            final String outJson = GsonUtils.toJson(repository);
            final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(outJson);
            writer.flush();
            out.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static User getUser(final BaseActivity context, final String login,
            final boolean forceUpdate) {
        User user = null;
        boolean shouldRefresh = false;
        final File dir = new File(context.getCacheDir(), ROOT_DIR + USER_DIR);
        if (!dir.exists() || !dir.isDirectory()) {
            dir.mkdirs();
        }
        final File f = new File(dir, login + ".json");
        if (!forceUpdate && f.exists()) {            /* Check if the cached JSON is really old (>1 day) */
            final Date d = new Date();
            final long elderCheck = d.getTime() - (KEEP_LENGTH);
            if (f.lastModified() < elderCheck) {
                shouldRefresh = true;
            } else {
                try {
                    final FileInputStream in = new FileInputStream(f);
                    user = GsonUtils.fromJson(new BufferedReader(
                            new InputStreamReader(in)), User.class);
                    in.close();
                    return user;
                } catch (Exception e) {
                    shouldRefresh = true;
                }
            }
        } else {
            shouldRefresh = true;
        }
        if (shouldRefresh || forceUpdate) {
            try {
                final UserService us = new UserService(context.getGHClient());
                user = us.getUser(login);
                if (user != null) {
                    putUser(context, user);
                }
            } catch (Exception e) {
                user = null;
                e.printStackTrace();
            }
        }
        return user;
    }

    public static User getUser(final BaseActivity context, final String login) {
        return getUser(context, login, false);
    }

    public static boolean putUser(final BaseActivity context, final User user) {
        final File dir = new File(context.getCacheDir(), ROOT_DIR + USER_DIR);
        if (!dir.exists() || !dir.isDirectory()) {
            dir.mkdirs();
        }
        final File f = new File(dir, user.getLogin() + ".json");
        if (f.exists()) {
            f.delete();
        }
        try {
            final FileOutputStream out = new FileOutputStream(f, false);
            final String outJson = GsonUtils.toJson(user);
            final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(outJson);
            writer.flush();
            out.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    private static boolean deleteDirectory(File path) {
        if (path.exists()) {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                } else {
                    files[i].delete();
                }
            }
            return path.delete();
        } else {
            return false;
        }
    }

    public static boolean clearCache(final Context context) {
        final File root = new File(context.getCacheDir(), ROOT_DIR);
        return deleteDirectory(root);
    }

    public static boolean clearRepositoryCache(final Context context) {
        final File repositories = new File(context.getCacheDir(), REPOSITORY_DIR);
        return deleteDirectory(repositories);
    }

    public static boolean clearUserCache(final Context context) {
        final File users = new File(context.getCacheDir(), USER_DIR);
        return deleteDirectory(users);
    }
}




Java Source Code List

net.idlesoft.android.apps.github.GitHubClientProvider.java
net.idlesoft.android.apps.github.HubroidConstants.java
net.idlesoft.android.apps.github.authenticator.AccountAuthenticatorService.java
net.idlesoft.android.apps.github.authenticator.AuthConstants.java
net.idlesoft.android.apps.github.authenticator.GitHubAccountAuthenticator.java
net.idlesoft.android.apps.github.authenticator.OAuthUserProvider.java
net.idlesoft.android.apps.github.services.GitHubApiService.java
net.idlesoft.android.apps.github.ui.HubroidApplication.java
net.idlesoft.android.apps.github.ui.activities.BaseActivity.java
net.idlesoft.android.apps.github.ui.activities.BaseDashboardActivity.java
net.idlesoft.android.apps.github.ui.activities.GitHubIntentFilter.java
net.idlesoft.android.apps.github.ui.activities.RoboSherlockFragmentActivity.java
net.idlesoft.android.apps.github.ui.activities.app.AccountSelectActivity.java
net.idlesoft.android.apps.github.ui.activities.app.EventsActivity.java
net.idlesoft.android.apps.github.ui.activities.app.GitHubAuthenticatorActivity.java
net.idlesoft.android.apps.github.ui.activities.app.HomeActivity.java
net.idlesoft.android.apps.github.ui.activities.app.ProfileActivity.java
net.idlesoft.android.apps.github.ui.activities.app.RepositoriesActivity.java
net.idlesoft.android.apps.github.ui.adapters.BaseListAdapter.java
net.idlesoft.android.apps.github.ui.adapters.ContextListAdapter.java
net.idlesoft.android.apps.github.ui.adapters.DashboardListAdapter.java
net.idlesoft.android.apps.github.ui.adapters.EventListAdapter.java
net.idlesoft.android.apps.github.ui.adapters.HeaderFooterListAdapter.java
net.idlesoft.android.apps.github.ui.adapters.InfoListAdapter.java
net.idlesoft.android.apps.github.ui.adapters.RepositoryListAdapter.java
net.idlesoft.android.apps.github.ui.fragments.BaseFragment.java
net.idlesoft.android.apps.github.ui.fragments.BaseListFragment.java
net.idlesoft.android.apps.github.ui.fragments.PagedListFragment.java
net.idlesoft.android.apps.github.ui.fragments.app.AboutDialogFragment.java
net.idlesoft.android.apps.github.ui.fragments.app.EventListFragment.java
net.idlesoft.android.apps.github.ui.fragments.app.ProfileFragment.java
net.idlesoft.android.apps.github.ui.fragments.app.RepositoryListFragment.java
net.idlesoft.android.apps.github.ui.fragments.app.UserListFragment.java
net.idlesoft.android.apps.github.ui.widgets.FlowLayout.java
net.idlesoft.android.apps.github.ui.widgets.GravatarView.java
net.idlesoft.android.apps.github.ui.widgets.IdleList.java
net.idlesoft.android.apps.github.ui.widgets.ListViewPager.java
net.idlesoft.android.apps.github.ui.widgets.LoadableImageView.java
net.idlesoft.android.apps.github.ui.widgets.OcticonView.java
net.idlesoft.android.apps.github.ui.widgets.RefreshActionView.java
net.idlesoft.android.apps.github.utils.AsyncLoader.java
net.idlesoft.android.apps.github.utils.EventUtil.java
net.idlesoft.android.apps.github.utils.GravatarCache.java
net.idlesoft.android.apps.github.utils.RequestCache.java
net.idlesoft.android.apps.github.utils.StringUtils.java
net.idlesoft.android.apps.github.utils.TextWatcherAdapter.java
net.idlesoft.android.apps.github.utils.ToastUtil.java