/* Swisscom Safe Connect Copyright (C) 2014 Swisscom 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 com.swisscom.safeconnect.loader; import android.content.Context; import android.support.v4.content.AsyncTaskLoader; import com.swisscom.safeconnect.backend.BackendConnector; import java.util.Timer; import java.util.TimerTask; /** * Created by cianci on 6/16/14. */ public abstract class PipePeriodicLoader<T> extends AsyncTaskLoader<T> { protected T mData; protected BackendConnector backendConnector = new BackendConnector(getContext()); protected Timer mTimer; protected int timerInterval; public PipePeriodicLoader(Context context) { super(context); } /** * Called when there is new data to deliver to the client. The * super class will take care of delivering it; the implementation * here just adds a little more logic. */ @Override public void deliverResult(T result) { if (isStarted()) { // If the Loader is currently started, we can immediately // deliver its results. super.deliverResult(result); } } /** * Handles a request to start the Loader. */ @Override protected void onStartLoading() { // Start watching for changes in the app data. mTimer = new Timer(); mTimer.schedule(new TimerTask() { @Override public void run() { onContentChanged(); } }, timerInterval, timerInterval); //we want to display new statistics, when the user reloads the window forceLoad(); } /** * Handles a request to stop the Loader. */ @Override protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); if (mTimer != null) mTimer.cancel(); } /** * Handles a request to completely reset the Loader. */ @Override protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); if (mData != null) { mData = null; } // Stop monitoring for changes. if (mTimer != null) { mTimer.cancel(); mTimer = null; } } }