Remove ClientConfig class (#7038)

This commit is contained in:
ByteHamster 2024-03-29 13:39:19 +01:00 committed by GitHub
parent 8f553f08f0
commit f9dd837362
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
20 changed files with 49 additions and 144 deletions

View File

@ -5,9 +5,7 @@ import android.os.StrictMode;
import com.google.android.material.color.DynamicColors;
import de.danoeh.antennapod.config.ApplicationCallbacksImpl;
import de.danoeh.antennapod.core.ApCoreEventBusIndex;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.ClientConfigurator;
import de.danoeh.antennapod.error.CrashReportWriter;
import de.danoeh.antennapod.error.RxJavaErrorHandlerSetup;
@ -17,18 +15,9 @@ import org.greenrobot.eventbus.EventBus;
/** Main application class. */
public class PodcastApp extends Application {
private static PodcastApp singleton;
public static PodcastApp getInstance() {
return singleton;
}
@Override
public void onCreate() {
super.onCreate();
ClientConfig.applicationCallbacks = new ApplicationCallbacksImpl();
Thread.setDefaultUncaughtExceptionHandler(new CrashReportWriter());
RxJavaErrorHandlerSetup.setupRxJavaErrorHandler();
@ -43,8 +32,6 @@ public class PodcastApp extends Application {
StrictMode.setVmPolicy(builder.build());
}
singleton = this;
ClientConfigurator.initialize(this);
PreferenceUpgrader.checkUpgrades(this);

View File

@ -1,15 +0,0 @@
package de.danoeh.antennapod.config;
import android.app.Application;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.core.ApplicationCallbacks;
public class ApplicationCallbacksImpl implements ApplicationCallbacks {
@Override
public Application getApplicationInstance() {
return PodcastApp.getInstance();
}
}

View File

@ -20,7 +20,7 @@ import de.danoeh.antennapod.storage.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.util.FeedUtil;
import de.danoeh.antennapod.core.service.playback.PlaybackServiceInterface;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.sync.SynchronizationSettings;
import de.danoeh.antennapod.storage.preferences.SynchronizationSettings;
import de.danoeh.antennapod.core.sync.queue.SynchronizationQueueSink;
import de.danoeh.antennapod.core.util.FeedItemUtil;
import de.danoeh.antennapod.core.util.IntentUtils;

View File

@ -1,14 +0,0 @@
package de.danoeh.antennapod.core;
import android.app.Application;
/**
* Callbacks related to the application in general
*/
public interface ApplicationCallbacks {
/**
* Returns a non-null instance of the application class
*/
Application getApplicationInstance();
}

View File

@ -1,9 +0,0 @@
package de.danoeh.antennapod.core;
/**
* Stores callbacks for core classes like Services, DB classes etc. and other configuration variables.
* Apps using the core module of AntennaPod should register implementations of all interfaces here.
*/
public class ClientConfig {
public static ApplicationCallbacks applicationCallbacks;
}

View File

@ -3,6 +3,7 @@ package de.danoeh.antennapod.core;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import de.danoeh.antennapod.storage.preferences.SynchronizationSettings;
import de.danoeh.antennapod.storage.preferences.SynchronizationCredentials;
import de.danoeh.antennapod.storage.preferences.PlaybackPreferences;
import de.danoeh.antennapod.storage.preferences.SleepTimerPreferences;
@ -38,6 +39,7 @@ public class ClientConfigurator {
PodDBAdapter.init(context);
UserPreferences.init(context);
SynchronizationCredentials.init(context);
SynchronizationSettings.init(context);
UsageStatistics.init(context);
PlaybackPreferences.init(context);
SslProviderInstaller.install(context);

View File

@ -1,13 +1,10 @@
package de.danoeh.antennapod.core.service.download;
import android.content.Context;
import android.net.wifi.WifiManager;
import androidx.annotation.NonNull;
import java.util.Date;
import java.util.concurrent.Callable;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.model.download.DownloadResult;
import de.danoeh.antennapod.model.download.DownloadRequest;
@ -39,20 +36,7 @@ public abstract class Downloader implements Callable<Downloader> {
protected abstract void download();
public final Downloader call() {
WifiManager wifiManager = (WifiManager)
ClientConfig.applicationCallbacks.getApplicationInstance().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiManager.WifiLock wifiLock = null;
if (wifiManager != null) {
wifiLock = wifiManager.createWifiLock(TAG);
wifiLock.acquire();
}
download();
if (wifiLock != null) {
wifiLock.release();
}
finished = true;
return this;
}

View File

@ -7,6 +7,7 @@ import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.util.Log;
import androidx.annotation.NonNull;
@ -160,12 +161,22 @@ public class EpisodeDownloadWorker extends Worker {
return Result.failure();
}
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiManager.WifiLock wifiLock = null;
if (wifiManager != null) {
wifiLock = wifiManager.createWifiLock(TAG);
wifiLock.acquire();
}
try {
downloader.call();
} catch (Exception e) {
DBWriter.addDownloadStatus(downloader.getResult());
sendErrorNotification(request.getTitle());
return Result.failure();
} finally {
if (wifiLock != null) {
wifiLock.release();
}
}
if (downloader.cancelled) {

View File

@ -29,6 +29,7 @@ import de.danoeh.antennapod.event.MessageEvent;
import de.danoeh.antennapod.model.feed.FeedItemFilter;
import de.danoeh.antennapod.model.feed.SortOrder;
import de.danoeh.antennapod.storage.preferences.SynchronizationCredentials;
import de.danoeh.antennapod.storage.preferences.SynchronizationSettings;
import de.danoeh.antennapod.ui.notifications.NotificationUtils;
import org.apache.commons.lang3.StringUtils;
import org.greenrobot.eventbus.EventBus;

View File

@ -3,7 +3,7 @@ package de.danoeh.antennapod.core.sync.queue;
import android.content.Context;
import de.danoeh.antennapod.core.sync.LockingAsyncExecutor;
import de.danoeh.antennapod.core.sync.SynchronizationSettings;
import de.danoeh.antennapod.storage.preferences.SynchronizationSettings;
import de.danoeh.antennapod.model.feed.FeedMedia;
import de.danoeh.antennapod.net.sync.model.EpisodeAction;

View File

@ -8,7 +8,7 @@ import org.json.JSONException;
import java.util.ArrayList;
import de.danoeh.antennapod.core.sync.SynchronizationSettings;
import de.danoeh.antennapod.storage.preferences.SynchronizationSettings;
import de.danoeh.antennapod.net.sync.model.EpisodeAction;
public class SynchronizationQueueStorage {

View File

@ -1,6 +1,5 @@
package de.danoeh.antennapod.core.feed;
import android.app.Application;
import android.content.Context;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
@ -16,6 +15,7 @@ import de.danoeh.antennapod.core.util.FastDocumentFile;
import de.danoeh.antennapod.model.feed.Feed;
import de.danoeh.antennapod.model.feed.FeedItem;
import de.danoeh.antennapod.storage.database.PodDBAdapter;
import de.danoeh.antennapod.storage.preferences.SynchronizationSettings;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -33,8 +33,6 @@ import java.util.Collections;
import java.util.List;
import java.util.Objects;
import de.danoeh.antennapod.core.ApplicationCallbacks;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.storage.preferences.UserPreferences;
import de.danoeh.antennapod.storage.database.DBReader;
import de.danoeh.antennapod.core.storage.DBWriter;
@ -46,8 +44,6 @@ import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;
/**
@ -74,10 +70,7 @@ public class LocalFeedUpdaterTest {
context = InstrumentationRegistry.getInstrumentation().getContext();
UserPreferences.init(context);
PlaybackPreferences.init(context);
Application app = (Application) context;
ClientConfig.applicationCallbacks = mock(ApplicationCallbacks.class);
when(ClientConfig.applicationCallbacks.getApplicationInstance()).thenReturn(app);
SynchronizationSettings.init(context);
DownloadServiceInterface.setImpl(new DownloadServiceInterfaceStub());
// Initialize database

View File

@ -1,6 +1,5 @@
package de.danoeh.antennapod.core.storage;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import androidx.preference.PreferenceManager;
@ -13,12 +12,11 @@ import java.util.List;
import androidx.test.platform.app.InstrumentationRegistry;
import de.danoeh.antennapod.core.ApplicationCallbacks;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.model.feed.Feed;
import de.danoeh.antennapod.model.feed.FeedItem;
import de.danoeh.antennapod.model.feed.FeedMedia;
import de.danoeh.antennapod.storage.preferences.PlaybackPreferences;
import de.danoeh.antennapod.storage.preferences.SynchronizationSettings;
import de.danoeh.antennapod.storage.preferences.UserPreferences;
import de.danoeh.antennapod.storage.database.PodDBAdapter;
@ -32,8 +30,6 @@ import static de.danoeh.antennapod.core.storage.DbTestUtils.saveFeedlist;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Test class for DBTasks.
@ -83,10 +79,7 @@ public class DbCleanupTests {
UserPreferences.init(context);
PlaybackPreferences.init(context);
Application app = (Application) context;
ClientConfig.applicationCallbacks = mock(ApplicationCallbacks.class);
when(ClientConfig.applicationCallbacks.getApplicationInstance()).thenReturn(app);
SynchronizationSettings.init(context);
}
@After

View File

@ -1,6 +1,5 @@
package de.danoeh.antennapod.core.storage;
import android.app.Application;
import android.content.Context;
import androidx.test.platform.app.InstrumentationRegistry;
@ -18,8 +17,6 @@ import java.util.Collections;
import java.util.Date;
import java.util.List;
import de.danoeh.antennapod.core.ApplicationCallbacks;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.model.feed.Feed;
import de.danoeh.antennapod.model.feed.FeedItem;
import de.danoeh.antennapod.model.feed.FeedMedia;
@ -33,8 +30,6 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Test class for {@link FeedDatabaseWriter}.
@ -49,10 +44,6 @@ public class DbTasksTest {
UserPreferences.init(context);
PlaybackPreferences.init(context);
Application app = (Application) context;
ClientConfig.applicationCallbacks = mock(ApplicationCallbacks.class);
when(ClientConfig.applicationCallbacks.getApplicationInstance()).thenReturn(app);
// create new database
PodDBAdapter.init(context);
PodDBAdapter.deleteDatabase();

View File

@ -1,6 +1,5 @@
package de.danoeh.antennapod.core.storage;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
@ -29,8 +28,6 @@ import java.util.Locale;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.core.ApplicationCallbacks;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.model.feed.Feed;
import de.danoeh.antennapod.model.feed.FeedItem;
import de.danoeh.antennapod.model.feed.FeedMedia;
@ -44,8 +41,6 @@ import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Test class for {@link DBWriter}.
@ -64,10 +59,6 @@ public class DbWriterTest {
context = InstrumentationRegistry.getInstrumentation().getTargetContext();
UserPreferences.init(context);
PlaybackPreferences.init(context);
Application app = (Application) context;
ClientConfig.applicationCallbacks = mock(ApplicationCallbacks.class);
when(ClientConfig.applicationCallbacks.getApplicationInstance()).thenReturn(app);
DownloadServiceInterface.setImpl(new DownloadServiceInterfaceStub());
// create new database

View File

@ -1,25 +1,28 @@
package de.danoeh.antennapod.core.sync;
package de.danoeh.antennapod.storage.preferences;
import android.content.Context;
import android.content.SharedPreferences;
import de.danoeh.antennapod.core.ClientConfig;
public class SynchronizationSettings {
public static final String LAST_SYNC_ATTEMPT_TIMESTAMP = "last_sync_attempt_timestamp";
private static final String NAME = "synchronization";
private static final String PREF_NAME = "synchronization";
private static final String SELECTED_SYNC_PROVIDER = "selected_sync_provider";
private static final String LAST_SYNC_ATTEMPT_SUCCESS = "last_sync_attempt_success";
private static final String LAST_EPISODE_ACTIONS_SYNC_TIMESTAMP = "last_episode_actions_sync_timestamp";
private static final String LAST_SUBSCRIPTION_SYNC_TIMESTAMP = "last_sync_timestamp";
private static SharedPreferences prefs;
public static void init(Context context) {
prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
public static boolean isProviderConnected() {
return getSelectedSyncProviderKey() != null;
}
public static void resetTimestamps() {
getSharedPreferences().edit()
prefs.edit()
.putLong(LAST_SUBSCRIPTION_SYNC_TIMESTAMP, 0)
.putLong(LAST_EPISODE_ACTIONS_SYNC_TIMESTAMP, 0)
.putLong(LAST_SYNC_ATTEMPT_TIMESTAMP, 0)
@ -27,57 +30,42 @@ public class SynchronizationSettings {
}
public static boolean isLastSyncSuccessful() {
return getSharedPreferences().getBoolean(LAST_SYNC_ATTEMPT_SUCCESS, false);
return prefs.getBoolean(LAST_SYNC_ATTEMPT_SUCCESS, false);
}
public static long getLastSyncAttempt() {
return getSharedPreferences().getLong(LAST_SYNC_ATTEMPT_TIMESTAMP, 0);
return prefs.getLong(LAST_SYNC_ATTEMPT_TIMESTAMP, 0);
}
public static void setSelectedSyncProvider(SynchronizationProviderViewData provider) {
getSharedPreferences()
.edit()
.putString(SELECTED_SYNC_PROVIDER, provider == null ? null : provider.getIdentifier())
.apply();
public static void setSelectedSyncProvider(String providerIdentifier) {
prefs.edit().putString(SELECTED_SYNC_PROVIDER, providerIdentifier).apply();
}
public static String getSelectedSyncProviderKey() {
return getSharedPreferences().getString(SELECTED_SYNC_PROVIDER, null);
return prefs.getString(SELECTED_SYNC_PROVIDER, null);
}
public static void updateLastSynchronizationAttempt() {
getSharedPreferences().edit()
.putLong(LAST_SYNC_ATTEMPT_TIMESTAMP, System.currentTimeMillis())
.apply();
prefs.edit().putLong(LAST_SYNC_ATTEMPT_TIMESTAMP, System.currentTimeMillis()).apply();
}
public static void setLastSynchronizationAttemptSuccess(boolean isSuccess) {
getSharedPreferences().edit()
.putBoolean(LAST_SYNC_ATTEMPT_SUCCESS, isSuccess)
.apply();
prefs.edit().putBoolean(LAST_SYNC_ATTEMPT_SUCCESS, isSuccess).apply();
}
public static long getLastSubscriptionSynchronizationTimestamp() {
return getSharedPreferences().getLong(LAST_SUBSCRIPTION_SYNC_TIMESTAMP, 0);
return prefs.getLong(LAST_SUBSCRIPTION_SYNC_TIMESTAMP, 0);
}
public static void setLastSubscriptionSynchronizationAttemptTimestamp(long newTimeStamp) {
getSharedPreferences().edit()
.putLong(LAST_SUBSCRIPTION_SYNC_TIMESTAMP, newTimeStamp).apply();
prefs.edit().putLong(LAST_SUBSCRIPTION_SYNC_TIMESTAMP, newTimeStamp).apply();
}
public static long getLastEpisodeActionSynchronizationTimestamp() {
return getSharedPreferences()
.getLong(LAST_EPISODE_ACTIONS_SYNC_TIMESTAMP, 0);
return prefs.getLong(LAST_EPISODE_ACTIONS_SYNC_TIMESTAMP, 0);
}
public static void setLastEpisodeActionSynchronizationAttemptTimestamp(long timestamp) {
getSharedPreferences().edit()
.putLong(LAST_EPISODE_ACTIONS_SYNC_TIMESTAMP, timestamp).apply();
}
private static SharedPreferences getSharedPreferences() {
return ClientConfig.applicationCallbacks.getApplicationInstance()
.getSharedPreferences(NAME, Context.MODE_PRIVATE);
prefs.edit().putLong(LAST_EPISODE_ACTIONS_SYNC_TIMESTAMP, timestamp).apply();
}
}

View File

@ -3,7 +3,7 @@ package de.danoeh.antennapod.ui.preferences.screen;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;
import de.danoeh.antennapod.core.sync.SynchronizationSettings;
import de.danoeh.antennapod.storage.preferences.SynchronizationSettings;
import de.danoeh.antennapod.ui.preferences.R;
public class NotificationPreferencesFragment extends PreferenceFragmentCompat {

View File

@ -23,7 +23,7 @@ import de.danoeh.antennapod.net.common.AntennapodHttpClient;
import de.danoeh.antennapod.core.sync.SyncService;
import de.danoeh.antennapod.storage.preferences.SynchronizationCredentials;
import de.danoeh.antennapod.core.sync.SynchronizationProviderViewData;
import de.danoeh.antennapod.core.sync.SynchronizationSettings;
import de.danoeh.antennapod.storage.preferences.SynchronizationSettings;
import de.danoeh.antennapod.core.util.FileNameGenerator;
import de.danoeh.antennapod.net.sync.gpoddernet.GpodnetService;
import de.danoeh.antennapod.net.sync.gpoddernet.model.GpodnetDevice;
@ -257,7 +257,8 @@ public class GpodderAuthenticationFragment extends DialogFragment {
if (selectedDevice == null) {
throw new IllegalStateException("Device must not be null here");
} else {
SynchronizationSettings.setSelectedSyncProvider(SynchronizationProviderViewData.GPODDER_NET);
SynchronizationSettings.setSelectedSyncProvider(
SynchronizationProviderViewData.GPODDER_NET.getIdentifier());
SynchronizationCredentials.setUsername(username);
SynchronizationCredentials.setPassword(password);
SynchronizationCredentials.setDeviceId(selectedDevice.getId());

View File

@ -13,7 +13,7 @@ import de.danoeh.antennapod.net.common.AntennapodHttpClient;
import de.danoeh.antennapod.core.sync.SyncService;
import de.danoeh.antennapod.storage.preferences.SynchronizationCredentials;
import de.danoeh.antennapod.core.sync.SynchronizationProviderViewData;
import de.danoeh.antennapod.core.sync.SynchronizationSettings;
import de.danoeh.antennapod.storage.preferences.SynchronizationSettings;
import de.danoeh.antennapod.net.sync.nextcloud.NextcloudLoginFlow;
import de.danoeh.antennapod.ui.preferences.R;
import de.danoeh.antennapod.ui.preferences.databinding.NextcloudAuthDialogBinding;
@ -88,7 +88,8 @@ public class NextcloudAuthenticationFragment extends DialogFragment
@Override
public void onNextcloudAuthenticated(String server, String username, String password) {
SynchronizationSettings.setSelectedSyncProvider(SynchronizationProviderViewData.NEXTCLOUD_GPODDER);
SynchronizationSettings.setSelectedSyncProvider(
SynchronizationProviderViewData.NEXTCLOUD_GPODDER.getIdentifier());
SynchronizationCredentials.clear();
SynchronizationQueueSink.clearQueue(getContext());
SynchronizationCredentials.setPassword(password);

View File

@ -31,7 +31,7 @@ import de.danoeh.antennapod.event.SyncServiceEvent;
import de.danoeh.antennapod.storage.preferences.SynchronizationCredentials;
import de.danoeh.antennapod.core.sync.SyncService;
import de.danoeh.antennapod.core.sync.SynchronizationProviderViewData;
import de.danoeh.antennapod.core.sync.SynchronizationSettings;
import de.danoeh.antennapod.storage.preferences.SynchronizationSettings;
public class SynchronizationPreferencesFragment extends PreferenceFragmentCompat {
private static final String PREFERENCE_SYNCHRONIZATION_DESCRIPTION = "preference_synchronization_description";