Remove BootReceiver

This commit is contained in:
cketti 2020-04-06 04:52:28 +02:00
parent b93ad397c3
commit e0b1d77650
5 changed files with 1 additions and 222 deletions

View file

@ -9,7 +9,6 @@ import android.os.Handler
import android.os.Looper
import com.fsck.k9.job.K9JobManager
import com.fsck.k9.mail.internet.BinaryTempFileBody
import com.fsck.k9.service.BootReceiver
import com.fsck.k9.service.StorageGoneReceiver
import java.util.concurrent.SynchronousQueue
import timber.log.Timber
@ -19,8 +18,6 @@ object Core : EarlyInit {
private val appConfig: AppConfig by inject()
private val jobManager: K9JobManager by inject()
private val componentsToDisable = listOf(BootReceiver::class.java)
/**
* This needs to be called from [Application#onCreate][android.app.Application#onCreate] before calling through
* to the super class's `onCreate` implementation and before initializing the dependency injection library.
@ -62,8 +59,7 @@ object Core : EarlyInit {
private fun setServicesEnabled(context: Context, enabled: Boolean) {
val pm = context.packageManager
val classes = componentsToDisable + appConfig.componentsToDisable
for (clazz in classes) {
for (clazz in appConfig.componentsToDisable) {
val alreadyEnabled = pm.getComponentEnabledSetting(ComponentName(context, clazz)) ==
PackageManager.COMPONENT_ENABLED_STATE_ENABLED

View file

@ -1,99 +0,0 @@
package com.fsck.k9.service
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.net.Uri
import com.fsck.k9.EarlyInit
import com.fsck.k9.K9
import com.fsck.k9.helper.K9AlarmManager
import com.fsck.k9.inject
import com.fsck.k9.job.K9JobManager
import timber.log.Timber
class BootReceiver : CoreReceiver(), EarlyInit {
private val jobManager: K9JobManager by inject()
override fun receive(context: Context, intent: Intent, _tmpWakeLockId: Int?): Int? {
var tmpWakeLockId = _tmpWakeLockId
Timber.i("BootReceiver.onReceive %s", intent)
val action = intent.action
if (Intent.ACTION_BOOT_COMPLETED == action) {
// K9.setServicesEnabled(context, tmpWakeLockId);
// tmpWakeLockId = null;
} else if ("com.android.sync.SYNC_CONN_STATUS_CHANGED" == action) {
val bOps = K9.backgroundOps
if (bOps == K9.BACKGROUND_OPS.WHEN_CHECKED_AUTO_SYNC) {
jobManager.scheduleAllMailJobs()
}
} else if (FIRE_INTENT == action) {
val alarmedIntent = intent.getParcelableExtra<Intent>(ALARMED_INTENT)
val alarmedAction = alarmedIntent.action
Timber.i("BootReceiver Got alarm to fire alarmedIntent %s", alarmedAction)
alarmedIntent.putExtra(CoreReceiver.WAKE_LOCK_ID, tmpWakeLockId)
tmpWakeLockId = null
context.startService(alarmedIntent)
} else if (SCHEDULE_INTENT == action) {
val atTime = intent.getLongExtra(AT_TIME, -1)
val alarmedIntent = intent.getParcelableExtra<Intent>(ALARMED_INTENT)
Timber.i("BootReceiver Scheduling intent %s for %tc", alarmedIntent, atTime)
val pi = buildPendingIntent(context, intent)
val alarmMgr = K9AlarmManager.getAlarmManager(context)
alarmMgr.set(AlarmManager.RTC_WAKEUP, atTime, pi)
}
return tmpWakeLockId
}
private fun buildPendingIntent(context: Context, intent: Intent): PendingIntent {
val alarmedIntent = intent.getParcelableExtra<Intent>(ALARMED_INTENT)
val alarmedAction = alarmedIntent.action
val i = Intent(context, BootReceiver::class.java)
i.action = FIRE_INTENT
i.putExtra(ALARMED_INTENT, alarmedIntent)
val uri = Uri.parse("action://" + alarmedAction!!)
i.data = uri
return PendingIntent.getBroadcast(context, 0, i, 0)
}
companion object {
const val FIRE_INTENT = "com.fsck.k9.service.BroadcastReceiver.fireIntent"
const val SCHEDULE_INTENT = "com.fsck.k9.service.BroadcastReceiver.scheduleIntent"
const val ALARMED_INTENT = "com.fsck.k9.service.BroadcastReceiver.pendingIntent"
const val AT_TIME = "com.fsck.k9.service.BroadcastReceiver.atTime"
@JvmStatic
fun scheduleIntent(context: Context, atTime: Long, alarmedIntent: Intent) {
Timber.i("BootReceiver Got request to schedule alarmedIntent %s", alarmedIntent.action)
val i = Intent()
i.setClass(context, BootReceiver::class.java)
i.action = SCHEDULE_INTENT
i.putExtra(ALARMED_INTENT, alarmedIntent)
i.putExtra(AT_TIME, atTime)
context.sendBroadcast(i)
}
/**
* Cancel any scheduled alarm.
*
* @param context
*/
@JvmStatic
fun purgeSchedule(context: Context) {
val alarmService = K9AlarmManager.getAlarmManager(context)
alarmService.cancel(PendingIntent.getBroadcast(context, 0, object : Intent() {
override fun filterEquals(other: Intent): Boolean {
// we want to match all intents
return true
}
}, 0))
}
}
}

View file

@ -1,82 +0,0 @@
package com.fsck.k9.service;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import timber.log.Timber;
import com.fsck.k9.K9;
import com.fsck.k9.power.TracingPowerManager;
import com.fsck.k9.power.TracingPowerManager.TracingWakeLock;
public class CoreReceiver extends BroadcastReceiver {
public static final String WAKE_LOCK_RELEASE = "com.fsck.k9.service.CoreReceiver.wakeLockRelease";
public static final String WAKE_LOCK_ID = "com.fsck.k9.service.CoreReceiver.wakeLockId";
private static ConcurrentHashMap<Integer, TracingWakeLock> wakeLocks = new ConcurrentHashMap<>();
private static AtomicInteger wakeLockSeq = new AtomicInteger(0);
private static Integer getWakeLock(Context context) {
TracingPowerManager pm = TracingPowerManager.getPowerManager(context);
TracingWakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "CoreReceiver getWakeLock");
wakeLock.setReferenceCounted(false);
wakeLock.acquire(K9.BOOT_RECEIVER_WAKE_LOCK_TIMEOUT);
Integer tmpWakeLockId = wakeLockSeq.getAndIncrement();
wakeLocks.put(tmpWakeLockId, wakeLock);
Timber.v("CoreReceiver Created wakeLock %d", tmpWakeLockId);
return tmpWakeLockId;
}
private static void releaseWakeLock(Integer wakeLockId) {
if (wakeLockId != null) {
TracingWakeLock wl = wakeLocks.remove(wakeLockId);
if (wl != null) {
Timber.v("CoreReceiver Releasing wakeLock %d", wakeLockId);
wl.release();
} else {
Timber.w("BootReceiver WakeLock %d doesn't exist", wakeLockId);
}
}
}
@Override
public void onReceive(Context context, Intent intent) {
Integer tmpWakeLockId = CoreReceiver.getWakeLock(context);
try {
Timber.i("CoreReceiver.onReceive %s", intent);
if (CoreReceiver.WAKE_LOCK_RELEASE.equals(intent.getAction())) {
Integer wakeLockId = intent.getIntExtra(WAKE_LOCK_ID, -1);
if (wakeLockId != -1) {
Timber.v("CoreReceiver Release wakeLock %d", wakeLockId);
CoreReceiver.releaseWakeLock(wakeLockId);
}
} else {
tmpWakeLockId = receive(context, intent, tmpWakeLockId);
}
} finally {
CoreReceiver.releaseWakeLock(tmpWakeLockId);
}
}
public Integer receive(Context context, Intent intent, Integer wakeLockId) {
return wakeLockId;
}
public static void releaseWakeLock(Context context, int wakeLockId) {
Timber.v("CoreReceiver Got request to release wakeLock %d", wakeLockId);
Intent i = new Intent();
i.setClass(context, CoreReceiver.class);
i.setAction(WAKE_LOCK_RELEASE);
i.putExtra(WAKE_LOCK_ID, wakeLockId);
context.sendBroadcast(i);
}
}

View file

@ -232,24 +232,6 @@
android:name="com.fsck.k9.ui.addaccount.AddAccountActivity"
android:label="@string/add_account_action" />
<receiver
android:name="com.fsck.k9.service.BootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter>
<action android:name="android.net.conn.BACKGROUND_DATA_SETTING_CHANGED"/>
</intent-filter>
<intent-filter>
<action android:name="com.android.sync.SYNC_CONN_STATUS_CHANGED"/>
</intent-filter>
</receiver>
<receiver
android:name="com.fsck.k9.service.CoreReceiver"
android:exported="false"/>
<receiver
android:name="com.fsck.k9.service.StorageReceiver"
android:enabled="true">

View file

@ -285,24 +285,6 @@
android:name=".ui.settings.account.AccountSettingsActivity"
android:label="@string/account_settings_title_fmt" />
<receiver
android:name=".service.BootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter>
<action android:name="android.net.conn.BACKGROUND_DATA_SETTING_CHANGED"/>
</intent-filter>
<intent-filter>
<action android:name="com.android.sync.SYNC_CONN_STATUS_CHANGED"/>
</intent-filter>
</receiver>
<receiver
android:name=".service.CoreReceiver"
android:exported="false"/>
<receiver
android:name=".service.StorageReceiver"
android:enabled="true">