From 1b752a6725e43c7614fcdf08ea601c0d3a3aee7a Mon Sep 17 00:00:00 2001 From: Nathaniel Brandes Date: Mon, 6 Dec 2021 20:33:58 -0800 Subject: [PATCH] Add caching to adapter lookup If we request an adapter with the same settings multiple times, we will now return the same instance. --- .../core/app/settings/ServerSetting.java | 6 +- .../transdroid/core/gui/DetailsActivity.java | 5 +- .../transdroid/core/gui/TorrentsActivity.java | 9 +-- .../core/gui/rss/RssFeedsActivity.java | 5 +- .../core/service/ControlService.java | 3 +- .../core/service/ServerCheckerJobRunner.java | 5 +- .../core/widget/ListWidgetConfigActivity.java | 4 +- .../core/widget/ListWidgetViewsService.java | 3 +- .../org/transdroid/daemon/DaemonFactory.java | 42 ++++++++++++++ .../org/transdroid/daemon/DaemonSettings.java | 58 +++++++++++++++++++ 10 files changed, 112 insertions(+), 28 deletions(-) create mode 100644 app/src/main/java/org/transdroid/daemon/DaemonFactory.java diff --git a/app/src/main/java/org/transdroid/core/app/settings/ServerSetting.java b/app/src/main/java/org/transdroid/core/app/settings/ServerSetting.java index d359755a..e5a5e9f5 100644 --- a/app/src/main/java/org/transdroid/core/app/settings/ServerSetting.java +++ b/app/src/main/java/org/transdroid/core/app/settings/ServerSetting.java @@ -18,10 +18,10 @@ package org.transdroid.core.app.settings; import android.content.Context; import android.text.TextUtils; - import org.transdroid.core.gui.lists.SimpleListItem; import org.transdroid.core.gui.log.Log_; import org.transdroid.daemon.Daemon; +import org.transdroid.daemon.DaemonFactory; import org.transdroid.daemon.DaemonSettings; import org.transdroid.daemon.IDaemonAdapter; import org.transdroid.daemon.OS; @@ -308,8 +308,8 @@ public class ServerSetting implements SimpleListItem { * @param context A context to access the logger * @return An IDaemonAdapter instance of the specific torrent client daemon type */ - public IDaemonAdapter createServerAdapter(String connectedToNetwork, Context context) { - return type.createAdapter(convertToDaemonSettings(connectedToNetwork, context)); + public IDaemonAdapter getServerAdapter(String connectedToNetwork, Context context) { + return DaemonFactory.getServerAdapter(convertToDaemonSettings(connectedToNetwork, context)); } /** diff --git a/app/src/main/java/org/transdroid/core/gui/DetailsActivity.java b/app/src/main/java/org/transdroid/core/gui/DetailsActivity.java index 41fdd85f..5e6c95fd 100644 --- a/app/src/main/java/org/transdroid/core/gui/DetailsActivity.java +++ b/app/src/main/java/org/transdroid/core/gui/DetailsActivity.java @@ -20,13 +20,10 @@ import android.annotation.TargetApi; import android.content.Intent; import android.os.Build; import android.os.Bundle; - import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; - import com.nispok.snackbar.Snackbar; import com.nispok.snackbar.SnackbarManager; - import org.androidannotations.annotations.AfterViews; import org.androidannotations.annotations.Background; import org.androidannotations.annotations.Bean; @@ -136,7 +133,7 @@ public class DetailsActivity extends AppCompatActivity implements TorrentTasksEx // Connect to the last used server ServerSetting lastUsed = applicationSettings.getLastUsedServer(); fragmentDetails.setCurrentServerSettings(lastUsed); - currentConnection = lastUsed.createServerAdapter(connectivityHelper.getConnectedNetworkName(), this); + currentConnection = lastUsed.getServerAdapter(connectivityHelper.getConnectedNetworkName(), this); // Show details and load fine stats and torrent files fragmentDetails.updateTorrent(torrent); diff --git a/app/src/main/java/org/transdroid/core/gui/TorrentsActivity.java b/app/src/main/java/org/transdroid/core/gui/TorrentsActivity.java index ec40eeb5..9002f96f 100644 --- a/app/src/main/java/org/transdroid/core/gui/TorrentsActivity.java +++ b/app/src/main/java/org/transdroid/core/gui/TorrentsActivity.java @@ -31,7 +31,6 @@ import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; - import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AppCompatActivity; @@ -39,13 +38,11 @@ import androidx.appcompat.widget.ActionMenuView; import androidx.appcompat.widget.SearchView; import androidx.appcompat.widget.Toolbar; import androidx.drawerlayout.widget.DrawerLayout; - import com.getbase.floatingactionbutton.FloatingActionButton; import com.getbase.floatingactionbutton.FloatingActionsMenu; import com.nispok.snackbar.Snackbar; import com.nispok.snackbar.SnackbarManager; import com.nispok.snackbar.enums.SnackbarType; - import org.androidannotations.annotations.AfterViews; import org.androidannotations.annotations.Background; import org.androidannotations.annotations.Bean; @@ -337,7 +334,7 @@ public class TorrentsActivity extends AppCompatActivity implements TorrentTasksE } else { // Resume after instead of fully loading the torrents list; create connection and set action bar title ServerSetting lastUsed = applicationSettings.getLastUsedServer(); - currentConnection = lastUsed.createServerAdapter(connectivityHelper.getConnectedNetworkName(), this); + currentConnection = lastUsed.getServerAdapter(connectivityHelper.getConnectedNetworkName(), this); serverSelectionView.updateCurrentServer(currentConnection); serverSelectionView.updateCurrentFilter(currentFilter); } @@ -380,7 +377,7 @@ public class TorrentsActivity extends AppCompatActivity implements TorrentTasksE if (currentConnection == null) { filterSelected(lastUsed, true); } else { - currentConnection = lastUsed.createServerAdapter(connectivityHelper.getConnectedNetworkName(), this); + currentConnection = lastUsed.getServerAdapter(connectivityHelper.getConnectedNetworkName(), this); } // Start auto refresh @@ -564,7 +561,7 @@ public class TorrentsActivity extends AppCompatActivity implements TorrentTasksE } // Update connection to the newly selected server and refresh - currentConnection = server.createServerAdapter(connectivityHelper.getConnectedNetworkName(), this); + currentConnection = server.getServerAdapter(connectivityHelper.getConnectedNetworkName(), this); applicationSettings.setLastUsedServer(server); serverSelectionView.updateCurrentServer(currentConnection); if (forceNewConnection) { diff --git a/app/src/main/java/org/transdroid/core/gui/rss/RssFeedsActivity.java b/app/src/main/java/org/transdroid/core/gui/rss/RssFeedsActivity.java index 11af9ad5..c05d999c 100644 --- a/app/src/main/java/org/transdroid/core/gui/rss/RssFeedsActivity.java +++ b/app/src/main/java/org/transdroid/core/gui/rss/RssFeedsActivity.java @@ -25,19 +25,16 @@ import android.os.Parcel; import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; - import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.viewpager.widget.PagerAdapter; import androidx.viewpager.widget.ViewPager; - import com.google.android.material.tabs.TabLayout; import com.nispok.snackbar.Snackbar; import com.nispok.snackbar.SnackbarManager; import com.nispok.snackbar.enums.SnackbarType; - import org.androidannotations.annotations.AfterViews; import org.androidannotations.annotations.Background; import org.androidannotations.annotations.Bean; @@ -269,7 +266,7 @@ public class RssFeedsActivity extends AppCompatActivity { protected IDaemonAdapter getCurrentConnection() { ServerSetting lastUsed = applicationSettings.getLastUsedServer(); - return lastUsed.createServerAdapter(connectivityHelper.getConnectedNetworkName(), this); + return lastUsed.getServerAdapter(connectivityHelper.getConnectedNetworkName(), this); } // @Background diff --git a/app/src/main/java/org/transdroid/core/service/ControlService.java b/app/src/main/java/org/transdroid/core/service/ControlService.java index e9ac264d..03e7915c 100644 --- a/app/src/main/java/org/transdroid/core/service/ControlService.java +++ b/app/src/main/java/org/transdroid/core/service/ControlService.java @@ -3,7 +3,6 @@ package org.transdroid.core.service; import android.app.IntentService; import android.appwidget.AppWidgetManager; import android.content.Intent; - import org.androidannotations.annotations.Bean; import org.androidannotations.annotations.EService; import org.transdroid.core.app.settings.ApplicationSettings; @@ -100,7 +99,7 @@ public class ControlService extends IntentService { } // See which action should be performed on the server - IDaemonAdapter adapter = server.createServerAdapter(connectivityHelper.getConnectedNetworkName(), this); + IDaemonAdapter adapter = server.getServerAdapter(connectivityHelper.getConnectedNetworkName(), this); DaemonTask task = null; switch (intent.getAction()) { case INTENT_RESUMEALL: diff --git a/app/src/main/java/org/transdroid/core/service/ServerCheckerJobRunner.java b/app/src/main/java/org/transdroid/core/service/ServerCheckerJobRunner.java index 42b9a384..3e7b3b14 100644 --- a/app/src/main/java/org/transdroid/core/service/ServerCheckerJobRunner.java +++ b/app/src/main/java/org/transdroid/core/service/ServerCheckerJobRunner.java @@ -21,11 +21,8 @@ import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.text.TextUtils; - import androidx.core.app.NotificationCompat; - import com.evernote.android.job.Job; - import org.androidannotations.annotations.Bean; import org.androidannotations.annotations.EBean; import org.androidannotations.annotations.RootContext; @@ -85,7 +82,7 @@ public class ServerCheckerJobRunner { JSONArray lastStats = applicationSettings.getServerLastStats(server); // Synchronously retrieve torrents listing - IDaemonAdapter adapter = server.createServerAdapter(connectivityHelper.getConnectedNetworkName(), context); + IDaemonAdapter adapter = server.getServerAdapter(connectivityHelper.getConnectedNetworkName(), context); DaemonTaskResult result = RetrieveTask.create(adapter).execute(log); if (!(result instanceof RetrieveTaskSuccessResult)) { // Cannot retrieve torrents at this time diff --git a/app/src/main/java/org/transdroid/core/widget/ListWidgetConfigActivity.java b/app/src/main/java/org/transdroid/core/widget/ListWidgetConfigActivity.java index 995cbe70..7ca10ef1 100644 --- a/app/src/main/java/org/transdroid/core/widget/ListWidgetConfigActivity.java +++ b/app/src/main/java/org/transdroid/core/widget/ListWidgetConfigActivity.java @@ -30,10 +30,8 @@ import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.ListView; import android.widget.Spinner; import android.widget.TextView; - import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; - import org.androidannotations.annotations.AfterViews; import org.androidannotations.annotations.Background; import org.androidannotations.annotations.Bean; @@ -209,7 +207,7 @@ public class ListWidgetConfigActivity extends AppCompatActivity { // Create a connection object and retrieve the live torrents IDaemonAdapter connection = - ((ServerSetting) serverSpinner.getSelectedItem()).createServerAdapter(connectivityHelper.getConnectedNetworkName(), this); + ((ServerSetting) serverSpinner.getSelectedItem()).getServerAdapter(connectivityHelper.getConnectedNetworkName(), this); DaemonTaskResult result = RetrieveTask.create(connection).execute(log); if (result instanceof RetrieveTaskSuccessResult) { // Success; show the active torrents in the widget preview diff --git a/app/src/main/java/org/transdroid/core/widget/ListWidgetViewsService.java b/app/src/main/java/org/transdroid/core/widget/ListWidgetViewsService.java index 0530eabc..96c6c6db 100644 --- a/app/src/main/java/org/transdroid/core/widget/ListWidgetViewsService.java +++ b/app/src/main/java/org/transdroid/core/widget/ListWidgetViewsService.java @@ -24,7 +24,6 @@ import android.os.Build; import android.view.View; import android.widget.RemoteViews; import android.widget.RemoteViewsService; - import org.androidannotations.annotations.EService; import org.transdroid.R; import org.transdroid.core.app.settings.ApplicationSettings; @@ -109,7 +108,7 @@ class WidgetViewsFactory implements RemoteViewsService.RemoteViewsFactory { // Load the torrents; synchronously IDaemonAdapter connection = - server.createServerAdapter(ConnectivityHelper_.getInstance_(context).getConnectedNetworkName(), + server.getServerAdapter(ConnectivityHelper_.getInstance_(context).getConnectedNetworkName(), context); DaemonTaskResult result = RetrieveTask.create(connection).execute(log); if (!(result instanceof RetrieveTaskSuccessResult)) { diff --git a/app/src/main/java/org/transdroid/daemon/DaemonFactory.java b/app/src/main/java/org/transdroid/daemon/DaemonFactory.java new file mode 100644 index 00000000..b86b04c2 --- /dev/null +++ b/app/src/main/java/org/transdroid/daemon/DaemonFactory.java @@ -0,0 +1,42 @@ +/* + * This file is part of Transdroid + * + * Transdroid is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Transdroid is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Transdroid. If not, see . + * + */ +package org.transdroid.daemon; + +import java.util.HashMap; +import java.util.Map; + +public class DaemonFactory { + + private static final Map adapterMap = new HashMap<>(); + private static final Map settingsMap = new HashMap<>(); + + public static synchronized IDaemonAdapter getServerAdapter(DaemonSettings daemonSettings) { + + String idString = daemonSettings.getIdString(); + IDaemonAdapter daemonAdapter = adapterMap.get(idString); + + //If there is no adapter or the settings have changed, generate a new instance + if(daemonAdapter == null || !daemonSettings.equals(settingsMap.get(idString))) { + daemonAdapter = daemonSettings.getType().createAdapter(daemonSettings); + adapterMap.put(idString, daemonAdapter); + settingsMap.put(idString, daemonSettings); + } + + return daemonAdapter; + } +} diff --git a/app/src/main/java/org/transdroid/daemon/DaemonSettings.java b/app/src/main/java/org/transdroid/daemon/DaemonSettings.java index 7b7f226f..56619455 100644 --- a/app/src/main/java/org/transdroid/daemon/DaemonSettings.java +++ b/app/src/main/java/org/transdroid/daemon/DaemonSettings.java @@ -189,6 +189,64 @@ public final class DaemonSettings { return isAutoGenerated; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + DaemonSettings that = (DaemonSettings) o; + + if (port != that.port) return false; + if (ssl != that.ssl) return false; + if (sslTrustAll != that.sslTrustAll) return false; + if (useAuthentication != that.useAuthentication) return false; + if (timeout != that.timeout) return false; + if (alarmOnFinishedDownload != that.alarmOnFinishedDownload) return false; + if (alarmOnNewTorrent != that.alarmOnNewTorrent) return false; + if (isAutoGenerated != that.isAutoGenerated) return false; + if (name != null ? !name.equals(that.name) : that.name != null) return false; + if (type != that.type) return false; + if (address != null ? !address.equals(that.address) : that.address != null) return false; + if (sslTrustKey != null ? !sslTrustKey.equals(that.sslTrustKey) : that.sslTrustKey != null) return false; + if (folder != null ? !folder.equals(that.folder) : that.folder != null) return false; + if (username != null ? !username.equals(that.username) : that.username != null) return false; + if (password != null ? !password.equals(that.password) : that.password != null) return false; + if (extraPass != null ? !extraPass.equals(that.extraPass) : that.extraPass != null) return false; + if (authToken != null ? !authToken.equals(that.authToken) : that.authToken != null) return false; + if (os != that.os) return false; + if (downloadDir != null ? !downloadDir.equals(that.downloadDir) : that.downloadDir != null) return false; + if (ftpUrl != null ? !ftpUrl.equals(that.ftpUrl) : that.ftpUrl != null) return false; + if (ftpPassword != null ? !ftpPassword.equals(that.ftpPassword) : that.ftpPassword != null) return false; + return idString != null ? idString.equals(that.idString) : that.idString == null; + } + + @Override + public int hashCode() { + int result = name != null ? name.hashCode() : 0; + result = 31 * result + (type != null ? type.hashCode() : 0); + result = 31 * result + (address != null ? address.hashCode() : 0); + result = 31 * result + port; + result = 31 * result + (ssl ? 1 : 0); + result = 31 * result + (sslTrustAll ? 1 : 0); + result = 31 * result + (sslTrustKey != null ? sslTrustKey.hashCode() : 0); + result = 31 * result + (folder != null ? folder.hashCode() : 0); + result = 31 * result + (useAuthentication ? 1 : 0); + result = 31 * result + (username != null ? username.hashCode() : 0); + result = 31 * result + (password != null ? password.hashCode() : 0); + result = 31 * result + (extraPass != null ? extraPass.hashCode() : 0); + result = 31 * result + (authToken != null ? authToken.hashCode() : 0); + result = 31 * result + (os != null ? os.hashCode() : 0); + result = 31 * result + (downloadDir != null ? downloadDir.hashCode() : 0); + result = 31 * result + (ftpUrl != null ? ftpUrl.hashCode() : 0); + result = 31 * result + (ftpPassword != null ? ftpPassword.hashCode() : 0); + result = 31 * result + timeout; + result = 31 * result + (alarmOnFinishedDownload ? 1 : 0); + result = 31 * result + (alarmOnNewTorrent ? 1 : 0); + result = 31 * result + (idString != null ? idString.hashCode() : 0); + result = 31 * result + (isAutoGenerated ? 1 : 0); + return result; + } + /** * Builds a text that can be used by a human reader to identify this daemon settings *