Create ControllerExtension to allow accessing MessagingController internals

This commit is contained in:
cketti 2018-08-17 18:22:53 +02:00
parent ea07d248df
commit c8de3e6a18
5 changed files with 47 additions and 3 deletions

View file

@ -0,0 +1,13 @@
package com.fsck.k9.controller
import com.fsck.k9.backend.BackendManager
interface ControllerExtension {
fun init(controller: MessagingController, backendManager: BackendManager, controllerInternals: ControllerInternals)
interface ControllerInternals {
fun put(description: String, listener: MessagingListener?, runnable: Runnable)
fun putBackground(description: String, listener: MessagingListener?, runnable: Runnable)
}
}

View file

@ -3,6 +3,6 @@ package com.fsck.k9.controller
import org.koin.dsl.module.applicationContext
val controllerModule = applicationContext {
bean { MessagingController(get(), get(), get(), get(), get(), get()) }
bean { MessagingController(get(), get(), get(), get(), get(), get(), get("controllerExtensions")) }
bean { DefaultAccountStatsCollector(get()) as AccountStatsCollector }
}

View file

@ -37,6 +37,7 @@ import com.fsck.k9.Account.DeletePolicy;
import com.fsck.k9.Account.Expunge;
import com.fsck.k9.AccountStats;
import com.fsck.k9.CoreResourceProvider;
import com.fsck.k9.controller.ControllerExtension.ControllerInternals;
import com.fsck.k9.core.BuildConfig;
import com.fsck.k9.DI;
import com.fsck.k9.K9;
@ -83,6 +84,7 @@ import com.fsck.k9.search.LocalSearch;
import com.fsck.k9.search.SearchAccount;
import com.fsck.k9.search.SearchSpecification;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import timber.log.Timber;
import static com.fsck.k9.K9.MAX_SEND_ATTEMPTS;
@ -137,7 +139,7 @@ public class MessagingController {
MessagingController(Context context, NotificationController notificationController, Contacts contacts,
AccountStatsCollector accountStatsCollector, CoreResourceProvider resourceProvider,
BackendManager backendManager) {
BackendManager backendManager, List<ControllerExtension> controllerExtensions) {
this.context = context;
this.notificationController = notificationController;
this.contacts = contacts;
@ -154,6 +156,32 @@ public class MessagingController {
controllerThread.setName("MessagingController");
controllerThread.start();
addListener(memorizingMessagingListener);
initializeControllerExtensions(controllerExtensions);
}
private void initializeControllerExtensions(List<ControllerExtension> controllerExtensions) {
if (controllerExtensions.isEmpty()) {
return;
}
ControllerInternals internals = new ControllerInternals() {
@Override
public void put(@NotNull String description, @Nullable MessagingListener listener,
@NotNull Runnable runnable) {
MessagingController.this.put(description, listener, runnable);
}
@Override
public void putBackground(@NotNull String description, @Nullable MessagingListener listener,
@NotNull Runnable runnable) {
MessagingController.this.putBackground(description, listener, runnable);
}
};
for (ControllerExtension extension : controllerExtensions) {
extension.init(this, backendManager, internals);
}
}
@VisibleForTesting

View file

@ -138,7 +138,8 @@ public class MessagingControllerTest extends K9RobolectricTest {
appContext = RuntimeEnvironment.application;
controller = new MessagingController(appContext, notificationController, contacts,
accountStatsCollector, mock(CoreResourceProvider.class), backendManager);
accountStatsCollector, mock(CoreResourceProvider.class), backendManager,
Collections.<ControllerExtension>emptyList());
configureBackendManager();
configureAccount();

View file

@ -1,6 +1,7 @@
package com.fsck.k9
import com.fsck.k9.backends.backendsModule
import com.fsck.k9.controller.ControllerExtension
import com.fsck.k9.external.BroadcastSenderListener
import com.fsck.k9.external.externalModule
import com.fsck.k9.notification.notificationModule
@ -20,6 +21,7 @@ private val mainAppModule = applicationContext {
get<BroadcastSenderListener>()
))
}
bean("controllerExtensions") { emptyList<ControllerExtension>() }
}
val appModules = listOf(