Convert TracingPowerManager to Kotlin
This commit is contained in:
parent
27cc4d34ca
commit
3393ae16ed
1 changed files with 76 additions and 87 deletions
|
@ -1,122 +1,111 @@
|
|||
package com.fsck.k9.power;
|
||||
package com.fsck.k9.power
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.os.SystemClock
|
||||
import com.fsck.k9.mail.power.PowerManager
|
||||
import com.fsck.k9.mail.power.WakeLock
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
import timber.log.Timber
|
||||
import android.os.PowerManager as SystemPowerManager
|
||||
import android.os.PowerManager.WakeLock as SystemWakeLock
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
class TracingPowerManager private constructor(context: Context) : PowerManager {
|
||||
private val powerManager = context.getSystemService(Context.POWER_SERVICE) as SystemPowerManager
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.PowerManager;
|
||||
import android.os.PowerManager.WakeLock;
|
||||
import android.os.SystemClock;
|
||||
|
||||
import com.fsck.k9.mail.K9MailLib;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import timber.log.Timber;
|
||||
|
||||
|
||||
public class TracingPowerManager implements com.fsck.k9.mail.power.PowerManager {
|
||||
public static AtomicInteger wakeLockId = new AtomicInteger(0);
|
||||
PowerManager pm = null;
|
||||
private static TracingPowerManager tracingPowerManager;
|
||||
|
||||
public static synchronized TracingPowerManager getPowerManager(Context context) {
|
||||
Context appContext = context.getApplicationContext();
|
||||
if (tracingPowerManager == null) {
|
||||
if (K9MailLib.isDebug()) {
|
||||
Timber.v("Creating TracingPowerManager");
|
||||
}
|
||||
tracingPowerManager = new TracingPowerManager(appContext);
|
||||
}
|
||||
return tracingPowerManager;
|
||||
override fun newWakeLock(tag: String): WakeLock {
|
||||
return TracingWakeLock(SystemPowerManager.PARTIAL_WAKE_LOCK, tag)
|
||||
}
|
||||
|
||||
|
||||
private TracingPowerManager(Context context) {
|
||||
pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
||||
fun newWakeLock(flags: Int, tag: String?): TracingWakeLock {
|
||||
return TracingWakeLock(flags, tag)
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public com.fsck.k9.mail.power.WakeLock newWakeLock(@NotNull String tag) {
|
||||
return new TracingWakeLock(PowerManager.PARTIAL_WAKE_LOCK, tag);
|
||||
}
|
||||
inner class TracingWakeLock(flags: Int, val tag: String?) : WakeLock {
|
||||
private val wakeLock: SystemWakeLock = powerManager.newWakeLock(flags, tag)
|
||||
private val id = wakeLockId.getAndIncrement()
|
||||
|
||||
public TracingWakeLock newWakeLock(int flags, String tag) {
|
||||
return new TracingWakeLock(flags, tag);
|
||||
}
|
||||
@Volatile
|
||||
private var startTime: Long? = null
|
||||
|
||||
@Volatile
|
||||
private var timeout: Long? = null
|
||||
|
||||
public class TracingWakeLock implements com.fsck.k9.mail.power.WakeLock {
|
||||
final WakeLock wakeLock;
|
||||
final int id;
|
||||
final String tag;
|
||||
volatile Long startTime = null;
|
||||
volatile Long timeout = null;
|
||||
|
||||
public TracingWakeLock(int flags, String ntag) {
|
||||
tag = ntag;
|
||||
wakeLock = pm.newWakeLock(flags, tag);
|
||||
id = wakeLockId.getAndIncrement();
|
||||
if (K9MailLib.isDebug()) {
|
||||
Timber.v("TracingWakeLock for tag %s / id %d: Create", tag, id);
|
||||
}
|
||||
init {
|
||||
Timber.v("TracingWakeLock for tag %s / id %d: Create", tag, id)
|
||||
}
|
||||
|
||||
@Override
|
||||
public void acquire(long timeout) {
|
||||
synchronized (wakeLock) {
|
||||
wakeLock.acquire(timeout);
|
||||
}
|
||||
if (K9MailLib.isDebug()) {
|
||||
Timber.v("TracingWakeLock for tag %s / id %d for %d ms: acquired", tag, id, timeout);
|
||||
override fun acquire(timeout: Long) {
|
||||
synchronized(wakeLock) {
|
||||
wakeLock.acquire(timeout)
|
||||
}
|
||||
|
||||
Timber.v("TracingWakeLock for tag %s / id %d for %d ms: acquired", tag, id, timeout)
|
||||
|
||||
if (startTime == null) {
|
||||
startTime = SystemClock.elapsedRealtime();
|
||||
startTime = SystemClock.elapsedRealtime()
|
||||
}
|
||||
this.timeout = timeout;
|
||||
|
||||
this.timeout = timeout
|
||||
}
|
||||
|
||||
@Override
|
||||
public void acquire() {
|
||||
synchronized (wakeLock) {
|
||||
wakeLock.acquire();
|
||||
@SuppressLint("WakelockTimeout")
|
||||
override fun acquire() {
|
||||
synchronized(wakeLock) {
|
||||
wakeLock.acquire()
|
||||
}
|
||||
|
||||
if (K9MailLib.isDebug()) {
|
||||
Timber.w("TracingWakeLock for tag %s / id %d: acquired with no timeout. K-9 Mail should not do this",
|
||||
tag, id);
|
||||
}
|
||||
Timber.v("TracingWakeLock for tag %s / id %d: acquired with no timeout.", tag, id)
|
||||
|
||||
if (startTime == null) {
|
||||
startTime = SystemClock.elapsedRealtime();
|
||||
startTime = SystemClock.elapsedRealtime()
|
||||
}
|
||||
timeout = null;
|
||||
|
||||
timeout = null
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReferenceCounted(boolean counted) {
|
||||
synchronized (wakeLock) {
|
||||
wakeLock.setReferenceCounted(counted);
|
||||
override fun setReferenceCounted(counted: Boolean) {
|
||||
synchronized(wakeLock) {
|
||||
wakeLock.setReferenceCounted(counted)
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void release() {
|
||||
override fun release() {
|
||||
val startTime = this.startTime
|
||||
if (startTime != null) {
|
||||
Long endTime = SystemClock.elapsedRealtime();
|
||||
if (K9MailLib.isDebug()) {
|
||||
Timber.v("TracingWakeLock for tag %s / id %d: releasing after %d ms, timeout = %d ms",
|
||||
tag, id, endTime - startTime, timeout);
|
||||
}
|
||||
val endTime = SystemClock.elapsedRealtime()
|
||||
|
||||
Timber.v(
|
||||
"TracingWakeLock for tag %s / id %d: releasing after %d ms, timeout = %d ms",
|
||||
tag, id, endTime - startTime, timeout
|
||||
)
|
||||
} else {
|
||||
if (K9MailLib.isDebug()) {
|
||||
Timber.v("TracingWakeLock for tag %s / id %d, timeout = %d ms: releasing", tag, id, timeout);
|
||||
}
|
||||
Timber.v("TracingWakeLock for tag %s / id %d, timeout = %d ms: releasing", tag, id, timeout)
|
||||
}
|
||||
|
||||
synchronized (wakeLock) {
|
||||
wakeLock.release();
|
||||
synchronized(wakeLock) {
|
||||
wakeLock.release()
|
||||
}
|
||||
startTime = null;
|
||||
|
||||
this.startTime = null
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val wakeLockId = AtomicInteger(0)
|
||||
private var tracingPowerManager: TracingPowerManager? = null
|
||||
|
||||
@JvmStatic
|
||||
@Synchronized
|
||||
fun getPowerManager(context: Context): TracingPowerManager {
|
||||
return tracingPowerManager ?: createPowerManager(context.applicationContext).also {
|
||||
tracingPowerManager = it
|
||||
}
|
||||
}
|
||||
|
||||
private fun createPowerManager(appContext: Context): TracingPowerManager {
|
||||
Timber.v("Creating TracingPowerManager")
|
||||
return TracingPowerManager(appContext)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue