summaryrefslogtreecommitdiff
path: root/net/download
diff options
context:
space:
mode:
authorByteHamster <ByteHamster@users.noreply.github.com>2024-03-31 18:40:15 +0200
committerGitHub <noreply@github.com>2024-03-31 18:40:15 +0200
commitedb440a5a9a05e24c344a71b272b9238217e9c55 (patch)
tree13623ca7d0dac052ac35d693aac940d0727c87f9 /net/download
parent4e47691e70e85736c7eeb30ce02c73176e565a86 (diff)
downloadAntennaPod-edb440a5a9a05e24c344a71b272b9238217e9c55.zip
Restructure related UI classes together (#7044)
Diffstat (limited to 'net/download')
-rw-r--r--net/download/service-interface/src/test/java/de/danoeh/antennapod/net/download/serviceinterface/FilenameGeneratorTest.java98
-rw-r--r--net/download/service/src/main/AndroidManifest.xml18
-rw-r--r--net/download/service/src/main/java/de/danoeh/antennapod/net/download/service/ConnectivityActionReceiver.java34
-rw-r--r--net/download/service/src/main/java/de/danoeh/antennapod/net/download/service/PowerConnectionReceiver.java46
4 files changed, 196 insertions, 0 deletions
diff --git a/net/download/service-interface/src/test/java/de/danoeh/antennapod/net/download/serviceinterface/FilenameGeneratorTest.java b/net/download/service-interface/src/test/java/de/danoeh/antennapod/net/download/serviceinterface/FilenameGeneratorTest.java
new file mode 100644
index 000000000..521fc2d36
--- /dev/null
+++ b/net/download/service-interface/src/test/java/de/danoeh/antennapod/net/download/serviceinterface/FilenameGeneratorTest.java
@@ -0,0 +1,98 @@
+package de.danoeh.antennapod.net.download.serviceinterface;
+
+import androidx.test.platform.app.InstrumentationRegistry;
+import android.text.TextUtils;
+
+import java.io.File;
+
+import org.apache.commons.lang3.StringUtils;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(RobolectricTestRunner.class)
+public class FilenameGeneratorTest {
+
+ public FilenameGeneratorTest() {
+ super();
+ }
+
+ @Test
+ public void testGenerateFileName() throws Exception {
+ String result = FileNameGenerator.generateFileName("abc abc");
+ assertEquals(result, "abc abc");
+ createFiles(result);
+ }
+
+ @Test
+ public void testGenerateFileName1() throws Exception {
+ String result = FileNameGenerator.generateFileName("ab/c: <abc");
+ assertEquals(result, "abc abc");
+ createFiles(result);
+ }
+
+ @Test
+ public void testGenerateFileName2() throws Exception {
+ String result = FileNameGenerator.generateFileName("abc abc ");
+ assertEquals(result, "abc abc");
+ createFiles(result);
+ }
+
+ @Test
+ public void testFeedTitleContainsApostrophe() {
+ String result = FileNameGenerator.generateFileName("Feed's Title ...");
+ assertEquals("Feeds Title", result);
+ }
+
+ @Test
+ public void testFeedTitleContainsDash() {
+ String result = FileNameGenerator.generateFileName("Left - Right");
+ assertEquals("Left - Right", result);
+ }
+
+ @Test
+ public void testFeedTitleContainsAccents() {
+ String result = FileNameGenerator.generateFileName("Äàáâãå");
+ assertEquals("Aaaaaa", result);
+ }
+
+ @Test
+ public void testInvalidInput() {
+ String result = FileNameGenerator.generateFileName("???");
+ assertFalse(TextUtils.isEmpty(result));
+ }
+
+ @Test
+ public void testLongFilename() throws Exception {
+ String longName = StringUtils.repeat("x", 20 + FileNameGenerator.MAX_FILENAME_LENGTH);
+ String result = FileNameGenerator.generateFileName(longName);
+ assertTrue(result.length() <= FileNameGenerator.MAX_FILENAME_LENGTH);
+ createFiles(result);
+ }
+
+ @Test
+ public void testLongFilenameNotEquals() {
+ // Verify that the name is not just trimmed and different suffixes end up with the same name
+ String longName = StringUtils.repeat("x", 20 + FileNameGenerator.MAX_FILENAME_LENGTH);
+ String result1 = FileNameGenerator.generateFileName(longName + "a");
+ String result2 = FileNameGenerator.generateFileName(longName + "b");
+ assertNotEquals(result1, result2);
+ }
+
+ /**
+ * Tests if files can be created.
+ */
+ private void createFiles(String name) throws Exception {
+ File cache = InstrumentationRegistry.getInstrumentation().getTargetContext().getExternalCacheDir();
+ File testFile = new File(cache, name);
+ assertTrue(testFile.mkdir());
+ assertTrue(testFile.exists());
+ assertTrue(testFile.delete());
+ assertTrue(testFile.createNewFile());
+ }
+}
diff --git a/net/download/service/src/main/AndroidManifest.xml b/net/download/service/src/main/AndroidManifest.xml
index 240319a5e..89b6823e5 100644
--- a/net/download/service/src/main/AndroidManifest.xml
+++ b/net/download/service/src/main/AndroidManifest.xml
@@ -14,6 +14,24 @@
android:label="@string/feed_update_receiver_name"
android:exported="true"
tools:ignore="ExportedReceiver" /> <!-- allow feeds update to be triggered by external apps -->
+
+ <receiver
+ android:name=".ConnectivityActionReceiver"
+ android:exported="true">
+ <intent-filter>
+ <action android:name="android.net.conn.CONNECTIVITY_CHANGE" tools:ignore="BatteryLife"/>
+ </intent-filter>
+ </receiver>
+
+ <receiver
+ android:name=".PowerConnectionReceiver"
+ android:exported="true">
+ <intent-filter>
+ <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
+ <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
+ </intent-filter>
+ </receiver>
+
</application>
</manifest>
diff --git a/net/download/service/src/main/java/de/danoeh/antennapod/net/download/service/ConnectivityActionReceiver.java b/net/download/service/src/main/java/de/danoeh/antennapod/net/download/service/ConnectivityActionReceiver.java
new file mode 100644
index 000000000..3b733a482
--- /dev/null
+++ b/net/download/service/src/main/java/de/danoeh/antennapod/net/download/service/ConnectivityActionReceiver.java
@@ -0,0 +1,34 @@
+package de.danoeh.antennapod.net.download.service;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.net.ConnectivityManager;
+import android.text.TextUtils;
+import android.util.Log;
+
+import de.danoeh.antennapod.net.common.NetworkUtils;
+import de.danoeh.antennapod.net.download.serviceinterface.AutoDownloadManager;
+import de.danoeh.antennapod.net.download.serviceinterface.DownloadServiceInterface;
+
+public class ConnectivityActionReceiver extends BroadcastReceiver {
+ private static final String TAG = "ConnectivityActionRecvr";
+
+ @Override
+ public void onReceive(final Context context, Intent intent) {
+ if (TextUtils.equals(intent.getAction(), ConnectivityManager.CONNECTIVITY_ACTION)) {
+ Log.d(TAG, "Received intent");
+
+ if (NetworkUtils.isAutoDownloadAllowed()) {
+ Log.d(TAG, "auto-dl network available, starting auto-download");
+ AutoDownloadManager.getInstance().autodownloadUndownloadedItems(context);
+ } else { // if new network is Wi-Fi, finish ongoing downloads,
+ // otherwise cancel all downloads
+ if (NetworkUtils.isNetworkRestricted()) {
+ Log.i(TAG, "Device is no longer connected to Wi-Fi. Cancelling ongoing downloads");
+ DownloadServiceInterface.get().cancelAll(context);
+ }
+ }
+ }
+ }
+}
diff --git a/net/download/service/src/main/java/de/danoeh/antennapod/net/download/service/PowerConnectionReceiver.java b/net/download/service/src/main/java/de/danoeh/antennapod/net/download/service/PowerConnectionReceiver.java
new file mode 100644
index 000000000..12a3f82e0
--- /dev/null
+++ b/net/download/service/src/main/java/de/danoeh/antennapod/net/download/service/PowerConnectionReceiver.java
@@ -0,0 +1,46 @@
+package de.danoeh.antennapod.net.download.service;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+import de.danoeh.antennapod.net.download.serviceinterface.AutoDownloadManager;
+import de.danoeh.antennapod.storage.preferences.UserPreferences;
+import de.danoeh.antennapod.net.download.serviceinterface.DownloadServiceInterface;
+
+// modified from http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
+// and ConnectivityActionReceiver.java
+// Updated based on http://stackoverflow.com/questions/20833241/android-charge-intent-has-no-extra-data
+// Since the intent doesn't have the EXTRA_STATUS like the android.com article says it does
+// (though it used to)
+public class PowerConnectionReceiver extends BroadcastReceiver {
+ private static final String TAG = "PowerConnectionReceiver";
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ final String action = intent.getAction();
+
+ Log.d(TAG, "charging intent: " + action);
+
+ if (Intent.ACTION_POWER_CONNECTED.equals(action)) {
+ Log.d(TAG, "charging, starting auto-download");
+ // we're plugged in, this is a great time to auto-download if everything else is
+ // right. So, even if the user allows auto-dl on battery, let's still start
+ // downloading now. They shouldn't mind.
+ // autodownloadUndownloadedItems will make sure we're on the right wifi networks,
+ // etc... so we don't have to worry about it.
+ AutoDownloadManager.getInstance().autodownloadUndownloadedItems(context);
+ } else {
+ // if we're not supposed to be auto-downloading when we're not charging, stop it
+ if (!UserPreferences.isEnableAutodownloadOnBattery()) {
+ Log.d(TAG, "not charging anymore, canceling auto-download");
+ DownloadServiceInterface.get().cancelAll(context);
+ } else {
+ Log.d(TAG, "not charging anymore, but the user allows auto-download " +
+ "when on battery so we'll keep going");
+ }
+ }
+
+ }
+}