1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-28 04:02:19 -05:00

Provide for tracing length of WakeLock activity.

Improve thread safety using volatiles.
This commit is contained in:
Daniel Applebaum 2010-05-22 21:58:35 +00:00
parent 5e3e204a4c
commit a8339dfe9d

View File

@ -47,10 +47,12 @@ public class TracingPowerManager
} }
public class TracingWakeLock public class TracingWakeLock
{ {
WakeLock wakeLock = null; final WakeLock wakeLock;
int id; final int id;
String tag; final String tag;
TimerTask timerTask; volatile TimerTask timerTask;
volatile Long startTime = null;
volatile Long timeout = null;
public TracingWakeLock(int flags, String ntag) public TracingWakeLock(int flags, String ntag)
{ {
tag = ntag; tag = ntag;
@ -73,7 +75,11 @@ public class TracingPowerManager
+ " for " + timeout + " ms"); + " for " + timeout + " ms");
} }
raiseNotification(); raiseNotification();
if (startTime == null)
{
startTime = System.currentTimeMillis();
}
this.timeout = timeout;
} }
public void acquire() public void acquire()
{ {
@ -84,9 +90,14 @@ public class TracingPowerManager
raiseNotification(); raiseNotification();
if (K9.DEBUG) if (K9.DEBUG)
{ {
Log.v(K9.LOG_TAG, "Acquired TracingWakeLock for tag " + tag + " and id " + id Log.w(K9.LOG_TAG, "Acquired TracingWakeLock for tag " + tag + " and id " + id
+ " with no timeout"); + " with no timeout. K-9 Mail should not do this");
} }
if (startTime == null)
{
startTime = System.currentTimeMillis();
}
timeout = null;
} }
public void setReferenceCounted(boolean counted) public void setReferenceCounted(boolean counted)
{ {
@ -97,15 +108,28 @@ public class TracingPowerManager
} }
public void release() public void release()
{ {
if (K9.DEBUG) if (startTime != null)
{ {
Log.v(K9.LOG_TAG, "Releasing TracingWakeLock for tag " + tag + " and id " + id ); Long endTime = System.currentTimeMillis();
if (K9.DEBUG)
{
Log.v(K9.LOG_TAG, "Releasing TracingWakeLock for tag " + tag + " and id " + id + " after "
+ (endTime - startTime) + " ms, timeout = " + timeout + " ms");
}
}
else
{
if (K9.DEBUG)
{
Log.v(K9.LOG_TAG, "Releasing TracingWakeLock for tag " + tag + " and id " + id + ", timeout = " + timeout + " ms");
}
} }
cancelNotification(); cancelNotification();
synchronized(wakeLock) synchronized(wakeLock)
{ {
wakeLock.release(); wakeLock.release();
} }
startTime = null;
} }
private void cancelNotification() private void cancelNotification()
{ {
@ -136,7 +160,17 @@ public class TracingPowerManager
@Override @Override
public void run() public void run()
{ {
Log.i(K9.LOG_TAG, "TracingWakeLock for tag " + tag + " and id " + id + " still active"); if (startTime != null)
{
Long endTime = System.currentTimeMillis();
Log.i(K9.LOG_TAG, "TracingWakeLock for tag " + tag + " and id " + id + " has been active for "
+ (endTime - startTime) + " ms, timeout = " + timeout + " ms");
}
else
{
Log.i(K9.LOG_TAG, "TracingWakeLock for tag " + tag + " and id " + id + " still active, timeout = " + timeout + " ms");
}
} }
}; };