Android Open Source - keepmoving Timetable Controller






From Project

Back to project page keepmoving.

License

The source code is released under:

GNU General Public License

If you think the Android project keepmoving 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

package it.rainbowbreeze.keepmoving.logic;
//w  w  w.  ja va2s.c o m
/**
 * This file is part of KeepMoving. KeepMoving 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, version 2.
 * <p/>
 * 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.
 * <p/>
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 * <p/>
 * Copyright Alfredo Morresi
 * <p/>
 * Created by Alfredo Morresi on 17/07/14.
 */

import android.content.Context;
import android.content.Intent;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import it.rainbowbreeze.keepmoving.common.ILogManager;
import it.rainbowbreeze.keepmoving.common.Utils;

/**
 * Controller for the Timetable activity and fragment
 */
public class TimetableController {
    private static final String LOG_TAG = TimetableController.class.getSimpleName();

    private final ILogManager mLogManager;
    private final PositionManager mPositionManager;
    private final RouteManager mRouteManager;
    private final TimetableModel mModel;
    private final ScheduledExecutorService mScheduledExecutorService;
    private ScheduledFuture<?> mScheduleFuture;

    private Context mAppContext;

    public TimetableController(ILogManager logManager, PositionManager positionManager, RouteManager routeManager) {
        mLogManager = logManager;
        mPositionManager = positionManager;
        mRouteManager = routeManager;
        mModel = new TimetableModel();
        mScheduledExecutorService = Executors.newScheduledThreadPool(2);
    }

    public TimetableModel getModel() {
        return mModel;
    }

    public void startTimetableUpdate(Context appContext) {
        mLogManager.d(LOG_TAG, "Starting background data update");
        mAppContext = appContext;
        mScheduleFuture = mScheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            public void run() {
                checkAndUpdateTimetable();
            }
        //}, 0, 1, TimeUnit.MINUTES);
        }, 0, 30, TimeUnit.SECONDS);
    }

    public void stopTimetableUpdate() {
        mLogManager.d(LOG_TAG, "Stopping background data update");
        mAppContext = null;
        if (null != mScheduleFuture) {
            //mScheduledExecutorService.shutdown();
            mScheduleFuture.cancel(true);
            mScheduleFuture = null;
        }
    }


    private void checkAndUpdateTimetable() {
        mLogManager.d(LOG_TAG, "Checking for a refresh of available routes");

        if (isActivityDetached()) {
            mLogManager.d(LOG_TAG, "No activity attached, refresh suspended");
            return;
        }
        // Checks if the model has to be updated
        boolean updatedNeeded = mModel.hasNoRoutes()
                || mModel.hasInvalidLeavingTimestamp()
                || mModel.isOutdated(Utils.getCurrentTimestamp());

        if (!updatedNeeded) {
            // Only refresh views
            updateOnlyTimeProgress();
            return;
        }

        mLogManager.d(LOG_TAG, "A refresh of available routes is required");
        mModel.setRoutes(mRouteManager.getRoutesStartingAt(
                mPositionManager.getCurrentPosition(),
                mPositionManager.getWorkLocation(),
                Utils.getCurrentTimestamp(),
                Utils.getCurrentDay(),
                4));

        updateAllViews();
    }

    /**
     * Sends a message to refresh all the UI views because the underline data has changed
     */
    private void updateAllViews() {
        if (isActivityDetached()) return;
        mLogManager.d(LOG_TAG, "Sending message " + Utils.MESSAGE_UPDATE_ALL_VIEWS);
        Intent i = new Intent(Utils.MESSAGE_UPDATE_ALL_VIEWS);
        mAppContext.sendBroadcast(i);
    }

    /**
     * Sends a message to refresh only the UI views that displays a time progressive. Undeline data
     * has not changed,
     */
    private void updateOnlyTimeProgress() {
        if (isActivityDetached()) return;
        mLogManager.d(LOG_TAG, "Sending message " + Utils.MESSAGE_REFRESH_TIME_VIEWS);
        Intent i = new Intent(Utils.MESSAGE_REFRESH_TIME_VIEWS);
        mAppContext.sendBroadcast(i);
    }

    /**
     * Returns true if an activity is attached to this controller
     * @return
     */
    private boolean isActivityDetached() {
        return null == mAppContext;
    }
}




Java Source Code List

it.rainbowbreeze.keepmoving.ApplicationTest.java
it.rainbowbreeze.keepmoving.common.Config.java
it.rainbowbreeze.keepmoving.common.DaggerModule.java
it.rainbowbreeze.keepmoving.common.ILogManager.java
it.rainbowbreeze.keepmoving.common.LogManager.java
it.rainbowbreeze.keepmoving.common.MyApp.java
it.rainbowbreeze.keepmoving.common.Utils.java
it.rainbowbreeze.keepmoving.data.GeoPointDao.java
it.rainbowbreeze.keepmoving.data.GeoPointFixedRepository.java
it.rainbowbreeze.keepmoving.data.IGeoPointRepository.java
it.rainbowbreeze.keepmoving.domain.Coord.java
it.rainbowbreeze.keepmoving.domain.GeoPointTypes.java
it.rainbowbreeze.keepmoving.domain.GeoPoint.java
it.rainbowbreeze.keepmoving.domain.Route.java
it.rainbowbreeze.keepmoving.domain.Weekdays.java
it.rainbowbreeze.keepmoving.logic.PositionManager.java
it.rainbowbreeze.keepmoving.logic.RouteManager.java
it.rainbowbreeze.keepmoving.logic.TimetableController.java
it.rainbowbreeze.keepmoving.logic.TimetableModel.java
it.rainbowbreeze.keepmoving.ui.SpannableHelper.java
it.rainbowbreeze.keepmoving.ui.StepsArrayAdapter.java
it.rainbowbreeze.keepmoving.ui.TimetableActivity.java
it.rainbowbreeze.keepmoving.ui.TimetableActivity.java
it.rainbowbreeze.keepmoving.ui.TimetableFragment.java