2009-12-14 21:50:53 -05:00
package com.fsck.k9.service ;
2009-11-22 12:01:04 -05:00
2009-12-20 20:48:15 -05:00
import java.util.concurrent.ConcurrentHashMap ;
2010-01-12 09:09:30 -05:00
import java.util.concurrent.ExecutorService ;
import java.util.concurrent.Executors ;
2009-12-20 20:48:15 -05:00
import java.util.concurrent.atomic.AtomicInteger ;
2009-11-22 12:01:04 -05:00
import android.app.Service ;
import android.content.Context ;
import android.content.Intent ;
import android.os.IBinder ;
import android.os.PowerManager ;
import android.util.Log ;
2009-12-14 21:50:53 -05:00
import com.fsck.k9.K9 ;
2010-05-16 20:30:32 -04:00
import com.fsck.k9.helper.power.TracingPowerManager ;
import com.fsck.k9.helper.power.TracingPowerManager.TracingWakeLock ;
2009-11-22 12:01:04 -05:00
public abstract class CoreService extends Service
{
2009-11-24 19:40:29 -05:00
2009-12-20 20:48:15 -05:00
public static String WAKE_LOCK_ID = " com.fsck.k9.service.CoreService.wakeLockId " ;
2010-05-16 20:30:32 -04:00
private static ConcurrentHashMap < Integer , TracingWakeLock > wakeLocks = new ConcurrentHashMap < Integer , TracingWakeLock > ( ) ;
2009-12-20 20:48:15 -05:00
private static AtomicInteger wakeLockSeq = new AtomicInteger ( 0 ) ;
2010-04-19 23:22:43 -04:00
private ExecutorService threadPool = null ;
private final String className = getClass ( ) . getName ( ) ;
2010-04-29 00:59:14 -04:00
2010-04-19 23:22:43 -04:00
@Override
public void onCreate ( )
{
if ( K9 . DEBUG )
Log . i ( K9 . LOG_TAG , " CoreService: " + className + " .onCreate() " ) ;
threadPool = Executors . newFixedThreadPool ( 1 ) ; // Must be single threaded
super . onCreate ( ) ;
2010-04-29 00:59:14 -04:00
2010-04-19 23:22:43 -04:00
}
2010-04-29 00:59:14 -04:00
2009-11-22 12:01:04 -05:00
protected static void addWakeLockId ( Intent i , Integer wakeLockId )
{
if ( wakeLockId ! = null )
{
i . putExtra ( BootReceiver . WAKE_LOCK_ID , wakeLockId ) ;
}
}
2009-11-24 19:40:29 -05:00
2009-12-20 20:48:15 -05:00
protected static void addWakeLock ( Context context , Intent i )
{
2010-05-16 20:30:32 -04:00
TracingPowerManager pm = TracingPowerManager . getPowerManager ( context ) ;
TracingWakeLock wakeLock = pm . newWakeLock ( PowerManager . PARTIAL_WAKE_LOCK , " CoreService addWakeLock " ) ;
2009-12-20 20:48:15 -05:00
wakeLock . setReferenceCounted ( false ) ;
wakeLock . acquire ( K9 . MAIL_SERVICE_WAKE_LOCK_TIMEOUT ) ;
2010-01-02 20:50:51 -05:00
2009-12-20 20:48:15 -05:00
Integer tmpWakeLockId = wakeLockSeq . getAndIncrement ( ) ;
wakeLocks . put ( tmpWakeLockId , wakeLock ) ;
2010-01-02 20:50:51 -05:00
2009-12-20 20:48:15 -05:00
i . putExtra ( WAKE_LOCK_ID , tmpWakeLockId ) ;
}
2010-04-29 00:59:14 -04:00
2010-01-02 20:50:51 -05:00
2009-11-22 12:01:04 -05:00
@Override
2009-11-24 19:40:29 -05:00
public void onStart ( Intent intent , int startId )
{
2010-05-16 20:30:32 -04:00
TracingPowerManager pm = TracingPowerManager . getPowerManager ( this ) ;
TracingWakeLock wakeLock = pm . newWakeLock ( PowerManager . PARTIAL_WAKE_LOCK , " CoreService onStart " ) ;
2009-11-22 12:01:04 -05:00
wakeLock . setReferenceCounted ( false ) ;
2009-12-14 21:50:53 -05:00
wakeLock . acquire ( K9 . MAIL_SERVICE_WAKE_LOCK_TIMEOUT ) ;
2009-11-24 19:40:29 -05:00
2010-01-02 20:50:32 -05:00
if ( K9 . DEBUG )
2010-04-19 23:22:43 -04:00
Log . i ( K9 . LOG_TAG , " CoreService: " + className + " .onStart( " + intent + " , " + startId ) ;
2009-11-24 19:40:29 -05:00
2009-11-22 12:01:04 -05:00
int wakeLockId = intent . getIntExtra ( BootReceiver . WAKE_LOCK_ID , - 1 ) ;
if ( wakeLockId ! = - 1 )
{
BootReceiver . releaseWakeLock ( this , wakeLockId ) ;
}
2009-12-20 20:48:15 -05:00
Integer coreWakeLockId = intent . getIntExtra ( WAKE_LOCK_ID , - 1 ) ;
if ( coreWakeLockId ! = null & & coreWakeLockId ! = - 1 )
{
if ( K9 . DEBUG )
Log . d ( K9 . LOG_TAG , " Got core wake lock id " + coreWakeLockId ) ;
2010-05-16 20:30:32 -04:00
TracingWakeLock coreWakeLock = wakeLocks . remove ( coreWakeLockId ) ;
2009-12-20 20:48:15 -05:00
if ( coreWakeLock ! = null )
{
if ( K9 . DEBUG )
Log . d ( K9 . LOG_TAG , " Found core wake lock with id " + coreWakeLockId + " , releasing " ) ;
coreWakeLock . release ( ) ;
}
}
2009-11-24 19:40:29 -05:00
2009-11-22 12:01:04 -05:00
try
{
super . onStart ( intent , startId ) ;
startService ( intent , startId ) ;
}
finally
{
if ( wakeLock ! = null )
{
wakeLock . release ( ) ;
}
}
2009-11-24 19:40:29 -05:00
2009-11-22 12:01:04 -05:00
}
2009-11-24 19:40:29 -05:00
2010-01-12 09:09:30 -05:00
public void execute ( Context context , final Runnable runner , int wakeLockTime , final Integer startId )
{
2010-04-29 00:59:14 -04:00
2010-05-16 20:30:32 -04:00
TracingPowerManager pm = TracingPowerManager . getPowerManager ( context ) ;
final TracingWakeLock wakeLock = pm . newWakeLock ( PowerManager . PARTIAL_WAKE_LOCK , " CoreService execute " ) ;
2010-01-12 09:09:30 -05:00
wakeLock . setReferenceCounted ( false ) ;
wakeLock . acquire ( wakeLockTime ) ;
2010-04-29 00:59:14 -04:00
2010-01-12 09:09:30 -05:00
Runnable myRunner = new Runnable ( )
{
public void run ( )
{
try
{
if ( K9 . DEBUG )
2010-04-19 23:22:43 -04:00
Log . d ( K9 . LOG_TAG , " CoreService ( " + className + " ) running Runnable " + runner . hashCode ( ) + " with startId " + startId ) ;
2010-01-12 09:09:30 -05:00
runner . run ( ) ;
}
finally
{
if ( K9 . DEBUG )
2010-04-19 23:22:43 -04:00
Log . d ( K9 . LOG_TAG , " CoreService ( " + className + " ) completed Runnable " + runner . hashCode ( ) + " with startId " + startId ) ;
2010-01-12 09:09:30 -05:00
wakeLock . release ( ) ;
if ( startId ! = null )
{
stopSelf ( startId ) ;
}
}
}
} ;
2010-04-19 23:22:43 -04:00
if ( threadPool = = null )
{
Log . e ( K9 . LOG_TAG , " CoreService.execute ( " + className + " ) called with no threadPool available; running Runnable " + runner . hashCode ( ) + " in calling thread " , new Throwable ( ) ) ;
2010-04-29 00:59:14 -04:00
synchronized ( this )
2010-04-19 23:22:43 -04:00
{
myRunner . run ( ) ;
}
}
else
{
if ( K9 . DEBUG )
Log . d ( K9 . LOG_TAG , " CoreService ( " + className + " ) queueing Runnable " + runner . hashCode ( ) + " with startId " + startId ) ;
threadPool . execute ( myRunner ) ;
}
2010-01-12 09:09:30 -05:00
}
2009-11-24 19:40:29 -05:00
2009-11-22 12:01:04 -05:00
public abstract void startService ( Intent intent , int startId ) ;
2009-11-24 19:40:29 -05:00
2009-11-22 12:01:04 -05:00
@Override
public IBinder onBind ( Intent arg0 )
{
// TODO Auto-generated method stub
return null ;
}
2009-11-24 19:40:29 -05:00
2009-11-22 12:01:04 -05:00
@Override
2009-11-24 19:40:29 -05:00
public void onDestroy ( )
{
2010-01-02 20:50:32 -05:00
if ( K9 . DEBUG )
2010-04-19 23:22:43 -04:00
Log . i ( K9 . LOG_TAG , " CoreService: " + className + " .onDestroy() " ) ;
threadPool . shutdown ( ) ;
2009-11-22 12:01:04 -05:00
super . onDestroy ( ) ;
2009-11-24 19:40:29 -05:00
// MessagingController.getInstance(getApplication()).removeListener(mListener);
2009-11-22 12:01:04 -05:00
}
}