Android Open Source - MassRoute Service Task Queue From Project Back to project page MassRoute .
License The source code is released under:
Copyright (c) 2010 Todd Anderson http://www.custardbelly.com/blog
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (th...
If you think the Android project MassRoute 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 com.custardbelly.massdot.service;
/ * w w w . j a v a 2 s . c o m * /
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.widget.Toast;
import com.custardbelly.massdot.R;
public class ServiceTaskQueue implements IServiceTaskQueue, IQueueableTaskResponder
{
// Must wait at least 10 seconds between service calls as explained in MassDOT documentation.
final long minimumElapse = 10000;
long _lastTaskTime;
boolean _isRunning;
List<TaskToken> _tasks;
Handler _taskHandler;
private Context _context;
public ServiceTaskQueue( Context context )
{
_context = context;
_isRunning = false;
_tasks = new ArrayList<TaskToken>();
_taskHandler = new Handler();
_lastTaskTime = 0;
}
protected void runNextTask()
{
_isRunning = ( _tasks.size() > 0 );
if ( _isRunning )
{
// check timer.
final long currentTime = new Date().getTime();
final long elapse = ( _lastTaskTime == 0 ) ? currentTime : ( currentTime - _lastTaskTime );
if ( elapse < minimumElapse )
{
String alert = _context.getString( R.string.delayed_request_alert );
float seconds = ( ( elapse ) - 1000 ) / 1000;
Toast.makeText( _context, alert.replace( "{0}" , Float.toString( seconds ) ), Toast.LENGTH_LONG);
// start timer with elapse
Runnable t = new Runnable() {
public void run()
{
runNextTask();
}
};
_taskHandler.postAtTime( t, elapse );
}
else
{
// reset time, execute task.
_lastTaskTime = new Date().getTime();
TaskToken token = _tasks.remove(0);
AsyncTask<URL, Integer , Long> task = token.task;
task.execute( token.url );
}
}
}
public void handleQueueableTaskResult()
{
runNextTask();
}
public void handleQueueableTaskFault()
{
runNextTask();
}
public void push( AsyncTask<URL, Integer , Long> task, URL url, boolean autostart )
{
_tasks.add( new TaskToken( task, url ) );
if ( autostart && !_isRunning )
flush();
}
public void flush()
{
runNextTask();
}
public void empty()
{
while ( _tasks.size() > 0 )
_tasks.remove(0);
_tasks = new ArrayList<TaskToken>();
}
private class TaskToken
{
AsyncTask<URL, Integer , Long> task;
URL url;
public TaskToken( AsyncTask<URL, Integer , Long> task, URL url )
{
this.task = task;
this.url = url;
}
}
}
Java Source Code List com.custardbelly.massdot.MassRoute.java com.custardbelly.massdot.enumeration.ActivityResultType.java com.custardbelly.massdot.enumeration.ActivityViewType.java com.custardbelly.massdot.enumeration.IntentExtraType.java com.custardbelly.massdot.exception.MassRouteParserException.java com.custardbelly.massdot.model.MassRouteModel.java com.custardbelly.massdot.model.RouteConfig.java com.custardbelly.massdot.model.RouteDirection.java com.custardbelly.massdot.model.RouteSearchContentProvider.java com.custardbelly.massdot.model.RouteStop.java com.custardbelly.massdot.model.Route.java com.custardbelly.massdot.model.StopPrediction.java com.custardbelly.massdot.model.StoredStopRequest.java com.custardbelly.massdot.model.StoredStopRequests.java com.custardbelly.massdot.parser.IRouteConfigParser.java com.custardbelly.massdot.parser.IRouteParser.java com.custardbelly.massdot.parser.IStopPredictionsParser.java com.custardbelly.massdot.parser.RouteConfigParser.java com.custardbelly.massdot.parser.RouteParser.java com.custardbelly.massdot.parser.StopPredictionsParser.java com.custardbelly.massdot.parser.handler.MassRouteServiceParserHandler.java com.custardbelly.massdot.service.IMassRouteService.java com.custardbelly.massdot.service.IPreferenceService.java com.custardbelly.massdot.service.IQueueableTaskResponder.java com.custardbelly.massdot.service.IQueueableTask.java com.custardbelly.massdot.service.IServiceTaskQueue.java com.custardbelly.massdot.service.MassRouteService.java com.custardbelly.massdot.service.PreferenceService.java com.custardbelly.massdot.service.RouteConfigTask.java com.custardbelly.massdot.service.RouteListTask.java com.custardbelly.massdot.service.ServiceTaskQueue.java com.custardbelly.massdot.service.StopPredictionTask.java com.custardbelly.massdot.service.responder.IMassRouteServiceResponder.java com.custardbelly.massdot.service.responder.IRoutesConfigServiceResponder.java com.custardbelly.massdot.service.responder.IRoutesServiceResponder.java com.custardbelly.massdot.service.responder.IStopPredictionsServiceResponder.java com.custardbelly.massdot.view.MassRouteServiceView.java com.custardbelly.massdot.view.RouteDirectionView.java com.custardbelly.massdot.view.RouteListView.java com.custardbelly.massdot.view.RouteStopsView.java com.custardbelly.massdot.view.StopPredictionsView.java com.custardbelly.massdot.view.adapter.RouteAdapter.java com.custardbelly.massdot.view.adapter.RouteDirectionAdapter.java com.custardbelly.massdot.view.adapter.RouteStopsAdapter.java com.custardbelly.massdot.view.adapter.StopPredictionsAdapter.java com.custardbelly.massdot.view.adapter.StoredStopRequestsAdapter.java