summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorH. Lehmann <ByteHamster@users.noreply.github.com>2020-05-06 17:46:40 +0200
committerGitHub <noreply@github.com>2020-05-06 17:46:40 +0200
commit320ab280625c383505f5b24656b2d39e9a323664 (patch)
treead6d7117b3dba8b4f7dd473bc9c201d0af45f7ee /core/src
parent0a8e1ac48407ad436e6297a5caba66f9f8321a12 (diff)
parent8be147c603d92c9520adeaa2b9e9f8ce6b76fa40 (diff)
downloadAntennaPod-320ab280625c383505f5b24656b2d39e9a323664.zip
Merge pull request #4121 from ByteHamster/on-demand-configuration
Added on-demand configuration for stream vs download
Diffstat (limited to 'core/src')
-rw-r--r--core/src/free/java/de/danoeh/antennapod/core/ClientConfig.java2
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/preferences/UsageStatistics.java73
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java6
-rw-r--r--core/src/main/res/layout/popup_bubble_view.xml40
-rw-r--r--core/src/main/res/values/strings.xml5
-rw-r--r--core/src/play/java/de/danoeh/antennapod/core/ClientConfig.java2
6 files changed, 127 insertions, 1 deletions
diff --git a/core/src/free/java/de/danoeh/antennapod/core/ClientConfig.java b/core/src/free/java/de/danoeh/antennapod/core/ClientConfig.java
index eea7d0ace..3c1eda242 100644
--- a/core/src/free/java/de/danoeh/antennapod/core/ClientConfig.java
+++ b/core/src/free/java/de/danoeh/antennapod/core/ClientConfig.java
@@ -4,6 +4,7 @@ import android.content.Context;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.preferences.SleepTimerPreferences;
+import de.danoeh.antennapod.core.preferences.UsageStatistics;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.download.AntennapodHttpClient;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
@@ -42,6 +43,7 @@ public class ClientConfig {
}
PodDBAdapter.init(context);
UserPreferences.init(context);
+ UsageStatistics.init(context);
PlaybackPreferences.init(context);
NetworkUtils.init(context);
AntennapodHttpClient.setCacheDirectory(new File(context.getCacheDir(), "okhttp"));
diff --git a/core/src/main/java/de/danoeh/antennapod/core/preferences/UsageStatistics.java b/core/src/main/java/de/danoeh/antennapod/core/preferences/UsageStatistics.java
new file mode 100644
index 000000000..a5b00b08c
--- /dev/null
+++ b/core/src/main/java/de/danoeh/antennapod/core/preferences/UsageStatistics.java
@@ -0,0 +1,73 @@
+package de.danoeh.antennapod.core.preferences;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import androidx.annotation.NonNull;
+
+import java.util.Calendar;
+
+/**
+ * Collects statistics about the app usage. The statistics are used to allow on-demand configuration:
+ * "Looks like you stream a lot. Do you want to toggle the 'Prefer streaming' setting?".
+ * The data is only stored locally on the device. It is NOT used for analytics/tracking.
+ * A private instance of this class must first be instantiated via
+ * init() or otherwise every public method will throw an Exception
+ * when called.
+ */
+public class UsageStatistics {
+ private UsageStatistics() {
+
+ }
+
+ private static final String PREF_DB_NAME = "UsageStatistics";
+ private static final float MOVING_AVERAGE_WEIGHT = 0.8f;
+ private static final float MOVING_AVERAGE_BIAS_THRESHOLD = 0.1f;
+ private static final long ASK_AGAIN_LATER_DELAY = 1000 * 3600 * 24 * 10; // 10 days
+ private static final String SUFFIX_HIDDEN_UNTIL = "_hiddenUntil";
+ private static SharedPreferences prefs;
+
+ public static final StatsAction ACTION_STREAM = new StatsAction("downloadVsStream", 0);
+ public static final StatsAction ACTION_DOWNLOAD = new StatsAction("downloadVsStream", 1);
+
+ /**
+ * Sets up the UsageStatistics class.
+ *
+ * @throws IllegalArgumentException if context is null
+ */
+ public static void init(@NonNull Context context) {
+ prefs = context.getSharedPreferences(PREF_DB_NAME, Context.MODE_PRIVATE);
+ }
+
+ public static void logAction(StatsAction action) {
+ int numExecutions = prefs.getInt(action.type + action.value, 0);
+ float movingAverage = prefs.getFloat(action.type, 0.5f);
+ prefs.edit()
+ .putInt(action.type + action.value, numExecutions + 1)
+ .putFloat(action.type, MOVING_AVERAGE_WEIGHT * movingAverage
+ + (1 - MOVING_AVERAGE_WEIGHT) * action.value)
+ .apply();
+ }
+
+ public static boolean hasSignificantBiasTo(StatsAction action) {
+ final float movingAverage = prefs.getFloat(action.type, 0.5f);
+ final long askAfter = prefs.getLong(action.type + SUFFIX_HIDDEN_UNTIL, 0);
+ return Math.abs(action.value - movingAverage) < MOVING_AVERAGE_BIAS_THRESHOLD
+ && Calendar.getInstance().getTimeInMillis() > askAfter;
+ }
+
+ public static void askAgainLater(StatsAction action) {
+ prefs.edit().putLong(action.type + SUFFIX_HIDDEN_UNTIL,
+ Calendar.getInstance().getTimeInMillis() + ASK_AGAIN_LATER_DELAY)
+ .apply();
+ }
+
+ public static final class StatsAction {
+ public final String type;
+ public final int value;
+
+ public StatsAction(String type, int value) {
+ this.type = type;
+ this.value = value;
+ }
+ }
+}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java
index a42350b4a..876251563 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java
@@ -1000,10 +1000,14 @@ public class UserPreferences {
return prefs.getBoolean(PREF_TIME_RESPECTS_SPEED, false);
}
- public static boolean streamOverDownload() {
+ public static boolean isStreamOverDownload() {
return prefs.getBoolean(PREF_STREAM_OVER_DOWNLOAD, false);
}
+ public static void setStreamOverDownload(boolean stream) {
+ prefs.edit().putBoolean(PREF_STREAM_OVER_DOWNLOAD, stream).apply();
+ }
+
/**
* Returns if the queue is in keep sorted mode.
*
diff --git a/core/src/main/res/layout/popup_bubble_view.xml b/core/src/main/res/layout/popup_bubble_view.xml
new file mode 100644
index 000000000..6b2e16f99
--- /dev/null
+++ b/core/src/main/res/layout/popup_bubble_view.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="vertical"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:padding="16dp">
+
+ <TextView
+ android:layout_width="match_parent"
+ android:textColor="?attr/colorOnSecondary"
+ android:layout_height="wrap_content"
+ android:lines="3"
+ android:id="@+id/balloon_message"/>
+
+ <LinearLayout
+ android:orientation="horizontal"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:layout_marginTop="8dp"
+ android:gravity="end">
+
+ <Button
+ style="@style/Widget.MaterialComponents.Button.TextButton"
+ android:textColor="?attr/colorOnSecondary"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/no"
+ android:id="@+id/balloon_button_negative"/>
+
+ <Button
+ style="@style/Widget.MaterialComponents.Button.TextButton"
+ android:textColor="?attr/colorOnSecondary"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/yes"
+ android:id="@+id/balloon_button_positive"/>
+
+ </LinearLayout>
+</LinearLayout>
diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml
index d11fa3a92..1c2db3a5b 100644
--- a/core/src/main/res/values/strings.xml
+++ b/core/src/main/res/values/strings.xml
@@ -812,4 +812,9 @@
<string name="widget_settings">Widget settings</string>
<string name="widget_create_button">Create widget</string>
<string name="widget_opacity">Opacity</string>
+
+ <!-- On-Demand configuration -->
+ <string name="on_demand_config_setting_changed">Setting updated successfully.</string>
+ <string name="on_demand_config_stream_text">Looks like you stream a lot. Do you want episode lists to show stream buttons?</string>
+ <string name="on_demand_config_download_text">Looks like you download a lot. Do you want episode lists to show download buttons?</string>
</resources>
diff --git a/core/src/play/java/de/danoeh/antennapod/core/ClientConfig.java b/core/src/play/java/de/danoeh/antennapod/core/ClientConfig.java
index ba30c0ef6..60fd5f4ee 100644
--- a/core/src/play/java/de/danoeh/antennapod/core/ClientConfig.java
+++ b/core/src/play/java/de/danoeh/antennapod/core/ClientConfig.java
@@ -10,6 +10,7 @@ import com.google.android.gms.security.ProviderInstaller;
import de.danoeh.antennapod.core.cast.CastManager;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.preferences.SleepTimerPreferences;
+import de.danoeh.antennapod.core.preferences.UsageStatistics;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.download.AntennapodHttpClient;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
@@ -51,6 +52,7 @@ public class ClientConfig {
}
PodDBAdapter.init(context);
UserPreferences.init(context);
+ UsageStatistics.init(context);
PlaybackPreferences.init(context);
NetworkUtils.init(context);
// Don't initialize Cast-related logic unless it is enabled, to avoid the unnecessary