summaryrefslogtreecommitdiff
path: root/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java
blob: 6ef91a22a950ec48d3faed20ae785f9fd036cdb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
package de.danoeh.antennapod.core.preferences;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.json.JSONArray;
import org.json.JSONException;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.receiver.FeedUpdateReceiver;

/**
 * Provides access to preferences set by the user in the settings screen. A
 * private instance of this class must first be instantiated via
 * createInstance() or otherwise every public method will throw an Exception
 * when called.
 */
public class UserPreferences implements
        SharedPreferences.OnSharedPreferenceChangeListener {

    public static final String IMPORT_DIR = "import/";

    private static final String TAG = "UserPreferences";

    // User Infercasce
    public static final String PREF_THEME = "prefTheme";
    public static final String PREF_HIDDEN_DRAWER_ITEMS = "prefHiddenDrawerItems";
    public static final String PREF_EXPANDED_NOTIFICATION = "prefExpandNotify";
    public static final String PREF_PERSISTENT_NOTIFICATION = "prefPersistNotify";
    public static final String PREF_SHOW_DOWNLOAD_REPORT = "prefShowDownloadReport";

    // Queue
    public static final String PREF_QUEUE_ADD_TO_FRONT = "prefQueueAddToFront";

    // Playback
    public static final String PREF_PAUSE_ON_HEADSET_DISCONNECT = "prefPauseOnHeadsetDisconnect";
    public static final String PREF_UNPAUSE_ON_HEADSET_RECONNECT = "prefUnpauseOnHeadsetReconnect";
    public static final String PREF_FOLLOW_QUEUE = "prefFollowQueue";
    public static final String PREF_AUTO_DELETE = "prefAutoDelete";
    public static final String PREF_SMART_MARK_AS_PLAYED_SECS = "prefSmartMarkAsPlayedSecs";
    private static final String PREF_PLAYBACK_SPEED_ARRAY = "prefPlaybackSpeedArray";
    public static final String PREF_PAUSE_PLAYBACK_FOR_FOCUS_LOSS = "prefPauseForFocusLoss";
    public static final String PREF_RESUME_AFTER_CALL = "prefResumeAfterCall";

    // Network
    public static final String PREF_UPDATE_INTERVAL = "prefAutoUpdateIntervall";
    public static final String PREF_MOBILE_UPDATE = "prefMobileUpdate";
    public static final String PREF_PARALLEL_DOWNLOADS = "prefParallelDownloads";
    public static final String PREF_EPISODE_CACHE_SIZE = "prefEpisodeCacheSize";
    public static final String PREF_ENABLE_AUTODL = "prefEnableAutoDl";
    public static final String PREF_ENABLE_AUTODL_ON_BATTERY = "prefEnableAutoDownloadOnBattery";
    public static final String PREF_ENABLE_AUTODL_WIFI_FILTER = "prefEnableAutoDownloadWifiFilter";
    public static final String PREF_AUTODL_SELECTED_NETWORKS = "prefAutodownloadSelectedNetworks";

    // Services
    public static final String PREF_AUTO_FLATTR = "pref_auto_flattr";
    public static final String PREF_AUTO_FLATTR_PLAYED_DURATION_THRESHOLD = "prefAutoFlattrPlayedDurationThreshold";

    // Other
    public static final String PREF_DATA_FOLDER = "prefDataFolder";

    // Mediaplayer
    public static final String PREF_PLAYBACK_SPEED = "prefPlaybackSpeed";
    private static final String PREF_FAST_FORWARD_SECS = "prefFastForwardSecs";
    private static final String PREF_REWIND_SECS = "prefRewindSecs";
    public static final String PREF_QUEUE_LOCKED = "prefQueueLocked";

    // TODO: Make this value configurable
    private static final float PREF_AUTO_FLATTR_PLAYED_DURATION_THRESHOLD_DEFAULT = 0.8f;

    private static int EPISODE_CACHE_SIZE_UNLIMITED = -1;

    private static UserPreferences instance;
    private final Context context;

    // User Interface
    private int theme;
    private List<String> hiddenDrawerItems;
    private int notifyPriority;
    private boolean persistNotify;
    private boolean showDownloadReport;

    // Queue
    private boolean enqueueAtFront;

    // Playback
    private boolean pauseOnHeadsetDisconnect;
    private boolean unpauseOnHeadsetReconnect;
    private boolean followQueue;
    private boolean autoDelete;
    private int smartMarkAsPlayedSecs;
    private String[] playbackSpeedArray;
    private boolean pauseForFocusLoss;
    private boolean resumeAfterCall;

    // Network
    private long updateInterval;
    private boolean allowMobileUpdate;
    private int parallelDownloads;
    private int episodeCacheSize;
    private boolean enableAutodownload;
    private boolean enableAutodownloadOnBattery;
    private boolean enableAutodownloadWifiFilter;
    private String[] autodownloadSelectedNetworks;

    // Services
    private boolean autoFlattr;
    private float autoFlattrPlayedDurationThreshold;

    // Settings somewhere in the GUI
    private String playbackSpeed;
    private int fastForwardSecs;
    private int rewindSecs;
    private boolean queueLocked;


    private UserPreferences(Context context) {
        this.context = context;
        loadPreferences();
    }

    /**
     * Sets up the UserPreferences class.
     *
     * @throws IllegalArgumentException if context is null
     */
    public static void createInstance(Context context) {
        Log.d(TAG, "Creating new instance of UserPreferences");
        Validate.notNull(context);

        instance = new UserPreferences(context);

        createImportDirectory();
        createNoMediaFile();
        PreferenceManager.getDefaultSharedPreferences(context)
                .registerOnSharedPreferenceChangeListener(instance);

    }

    private void loadPreferences() {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);

        // User Interface
        theme = readThemeValue(sp.getString(PREF_THEME, "0"));
        if (sp.getBoolean(PREF_EXPANDED_NOTIFICATION, false)) {
            notifyPriority = NotificationCompat.PRIORITY_MAX;
        } else {
            notifyPriority = NotificationCompat.PRIORITY_DEFAULT;
        }
        hiddenDrawerItems = Arrays.asList(StringUtils.split(sp.getString(PREF_HIDDEN_DRAWER_ITEMS, ""), ','));
        persistNotify = sp.getBoolean(PREF_PERSISTENT_NOTIFICATION, false);
        showDownloadReport = sp.getBoolean(PREF_SHOW_DOWNLOAD_REPORT, true);

        // Queue
        enqueueAtFront = sp.getBoolean(PREF_QUEUE_ADD_TO_FRONT, false);

        // Playback
        pauseOnHeadsetDisconnect = sp.getBoolean(PREF_PAUSE_ON_HEADSET_DISCONNECT, true);
        unpauseOnHeadsetReconnect = sp.getBoolean(PREF_UNPAUSE_ON_HEADSET_RECONNECT, true);
        followQueue = sp.getBoolean(PREF_FOLLOW_QUEUE, false);
        autoDelete = sp.getBoolean(PREF_AUTO_DELETE, false);
        smartMarkAsPlayedSecs = Integer.valueOf(sp.getString(PREF_SMART_MARK_AS_PLAYED_SECS, "30"));
        playbackSpeedArray = readPlaybackSpeedArray(sp.getString(
                PREF_PLAYBACK_SPEED_ARRAY, null));
        pauseForFocusLoss = sp.getBoolean(PREF_PAUSE_PLAYBACK_FOR_FOCUS_LOSS, false);

        // Network
        updateInterval = readUpdateInterval(sp.getString(PREF_UPDATE_INTERVAL, "0"));
        allowMobileUpdate = sp.getBoolean(PREF_MOBILE_UPDATE, false);
        parallelDownloads = Integer.valueOf(sp.getString(PREF_PARALLEL_DOWNLOADS, "6"));
        EPISODE_CACHE_SIZE_UNLIMITED = context.getResources().getInteger(
                R.integer.episode_cache_size_unlimited);
        episodeCacheSize = readEpisodeCacheSizeInternal(sp.getString(PREF_EPISODE_CACHE_SIZE, "20"));
        enableAutodownload = sp.getBoolean(PREF_ENABLE_AUTODL, false);
        enableAutodownloadOnBattery = sp.getBoolean(PREF_ENABLE_AUTODL_ON_BATTERY, true);
        enableAutodownloadWifiFilter = sp.getBoolean(PREF_ENABLE_AUTODL_WIFI_FILTER, false);
        autodownloadSelectedNetworks = StringUtils.split(
                sp.getString(PREF_AUTODL_SELECTED_NETWORKS, ""), ',');

        // Services
        autoFlattr = sp.getBoolean(PREF_AUTO_FLATTR, false);
        autoFlattrPlayedDurationThreshold = sp.getFloat(PREF_AUTO_FLATTR_PLAYED_DURATION_THRESHOLD,
                PREF_AUTO_FLATTR_PLAYED_DURATION_THRESHOLD_DEFAULT);

        // MediaPlayer
        playbackSpeed = sp.getString(PREF_PLAYBACK_SPEED, "1.0");
        fastForwardSecs = sp.getInt(PREF_FAST_FORWARD_SECS, 30);
        rewindSecs = sp.getInt(PREF_REWIND_SECS, 30);
        queueLocked = sp.getBoolean(PREF_QUEUE_LOCKED, false);
    }

    private int readThemeValue(String valueFromPrefs) {
        switch (Integer.parseInt(valueFromPrefs)) {
            case 0:
                return R.style.Theme_AntennaPod_Light;
            case 1:
                return R.style.Theme_AntennaPod_Dark;
            default:
                return R.style.Theme_AntennaPod_Light;
        }
    }

    private long readUpdateInterval(String valueFromPrefs) {
        int hours = Integer.parseInt(valueFromPrefs);
        return TimeUnit.HOURS.toMillis(hours);
    }

    private int readEpisodeCacheSizeInternal(String valueFromPrefs) {
        if (valueFromPrefs.equals(context
                .getString(R.string.pref_episode_cache_unlimited))) {
            return EPISODE_CACHE_SIZE_UNLIMITED;
        } else {
            return Integer.valueOf(valueFromPrefs);
        }
    }

    private String[] readPlaybackSpeedArray(String valueFromPrefs) {
        String[] selectedSpeeds = null;
        // If this preference hasn't been set yet, return the default options
        if (valueFromPrefs == null) {
            String[] allSpeeds = context.getResources().getStringArray(
                    R.array.playback_speed_values);
            List<String> speedList = new LinkedList<String>();
            for (String speedStr : allSpeeds) {
                float speed = Float.parseFloat(speedStr);
                if (speed < 2.0001 && speed * 10 % 1 == 0) {
                    speedList.add(speedStr);
                }
            }
            selectedSpeeds = speedList.toArray(new String[speedList.size()]);
        } else {
            try {
                JSONArray jsonArray = new JSONArray(valueFromPrefs);
                selectedSpeeds = new String[jsonArray.length()];
                for (int i = 0; i < jsonArray.length(); i++) {
                    selectedSpeeds[i] = jsonArray.getString(i);
                }
            } catch (JSONException e) {
                Log.e(TAG, "Got JSON error when trying to get speeds from JSONArray");
                e.printStackTrace();
            }
        }
        return selectedSpeeds;
    }

    private static void instanceAvailable() {
        if (instance == null) {
            throw new IllegalStateException("UserPreferences was used before being set up");
        }
    }

    /**
     * Returns theme as R.style value
     *
     * @return R.style.Theme_AntennaPod_Light or R.style.Theme_AntennaPod_Dark
     */
    public static int getTheme() {
        instanceAvailable();
        return instance.theme;
    }

    public static int getNoTitleTheme() {
        int theme = getTheme();
        if (theme == R.style.Theme_AntennaPod_Dark) {
            return R.style.Theme_AntennaPod_Dark_NoTitle;
        } else {
            return R.style.Theme_AntennaPod_Light_NoTitle;
        }
    }

    public static List<String> getHiddenDrawerItems() {
        instanceAvailable();
        return new ArrayList<String>(instance.hiddenDrawerItems);
    }

    /**
     * Returns notification priority.
     *
     * @return NotificationCompat.PRIORITY_MAX or NotificationCompat.PRIORITY_DEFAULT
     */
    public static int getNotifyPriority() {
        instanceAvailable();
        return instance.notifyPriority;
    }

    /**
     * Returns true if notifications are persistent
     *
     * @return {@code true} if notifications are persistent, {@code false}  otherwise
     */
    public static boolean isPersistNotify() {
        instanceAvailable();
        return instance.persistNotify;
    }

    /**
     * Returns true if download reports are shown
     *
     * @return {@code true} if download reports are shown, {@code false}  otherwise
     */
    public static boolean showDownloadReport() {
        instanceAvailable();
        return instance.showDownloadReport;
    }

    /**
     * Returns {@code true} if new queue elements are added to the front
     *
     * @return {@code true} if new queue elements are added to the front; {@code false}  otherwise
     */
    public static boolean enqueueAtFront() {
        instanceAvailable();
        return instance.enqueueAtFront;
    }

    public static boolean isPauseOnHeadsetDisconnect() {
        instanceAvailable();
        return instance.pauseOnHeadsetDisconnect;
    }

    public static boolean isUnpauseOnHeadsetReconnect() {
        instanceAvailable();
        return instance.unpauseOnHeadsetReconnect;
    }


    public static boolean isFollowQueue() {
        instanceAvailable();
        return instance.followQueue;
    }

    public static boolean isAutoDelete() {
        instanceAvailable();
        return instance.autoDelete;
    }

    public static int getSmartMarkAsPlayedSecs() {
        instanceAvailable();
        return instance.smartMarkAsPlayedSecs;
    }

    public static boolean isAutoFlattr() {
        instanceAvailable();
        return instance.autoFlattr;
    }

    public static String getPlaybackSpeed() {
        instanceAvailable();
        return instance.playbackSpeed;
    }

    public static String[] getPlaybackSpeedArray() {
        instanceAvailable();
        return instance.playbackSpeedArray;
    }

    public static boolean shouldPauseForFocusLoss() {
        instanceAvailable();
        return instance.pauseForFocusLoss;
    }

    public static long getUpdateInterval() {
        instanceAvailable();
        return instance.updateInterval;
    }

    public static boolean isAllowMobileUpdate() {
        instanceAvailable();
        return instance.allowMobileUpdate;
    }

    public static int getParallelDownloads() {
        instanceAvailable();
        return instance.parallelDownloads;
    }

    public static int getEpisodeCacheSizeUnlimited() {
        return EPISODE_CACHE_SIZE_UNLIMITED;
    }

    /**
     * Returns the capacity of the episode cache. This method will return the
     * negative integer EPISODE_CACHE_SIZE_UNLIMITED if the cache size is set to
     * 'unlimited'.
     */
    public static int getEpisodeCacheSize() {
        instanceAvailable();
        return instance.episodeCacheSize;
    }

    public static boolean isEnableAutodownload() {
        instanceAvailable();
        return instance.enableAutodownload;
    }

    public static boolean isEnableAutodownloadOnBattery() {
        instanceAvailable();
        return instance.enableAutodownloadOnBattery;
    }

    public static boolean isEnableAutodownloadWifiFilter() {
        instanceAvailable();
        return instance.enableAutodownloadWifiFilter;
    }

    public static int getFastFowardSecs() {
        instanceAvailable();
        return instance.fastForwardSecs;
    }

    public static int getRewindSecs() {
        instanceAvailable();
        return instance.rewindSecs;
    }


    /**
     * Returns the time after which an episode should be auto-flattr'd in percent of the episode's
     * duration.
     */
    public static float getAutoFlattrPlayedDurationThreshold() {
        instanceAvailable();
        return instance.autoFlattrPlayedDurationThreshold;
    }

    public static String[] getAutodownloadSelectedNetworks() {
        instanceAvailable();
        return instance.autodownloadSelectedNetworks;
    }

    public static boolean shouldResumeAfterCall() {
        instanceAvailable();
        return instance.resumeAfterCall;
    }

    public static boolean isQueueLocked() {
        instanceAvailable();
        return instance.queueLocked;
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
        Log.d(TAG, "Registered change of user preferences. Key: " + key);
        switch(key) {
            // User Interface
            case PREF_THEME:
                theme = readThemeValue(sp.getString(PREF_THEME, ""));
                break;
            case PREF_HIDDEN_DRAWER_ITEMS:
                hiddenDrawerItems = Arrays.asList(StringUtils.split(sp.getString(PREF_HIDDEN_DRAWER_ITEMS, ""), ','));
                break;
            case PREF_EXPANDED_NOTIFICATION:
                if (sp.getBoolean(PREF_EXPANDED_NOTIFICATION, false)) {
                    notifyPriority = NotificationCompat.PRIORITY_MAX;
                } else {
                    notifyPriority = NotificationCompat.PRIORITY_DEFAULT;
                }
                break;
            case PREF_PERSISTENT_NOTIFICATION:
                persistNotify = sp.getBoolean(PREF_PERSISTENT_NOTIFICATION, false);
                break;
            case PREF_SHOW_DOWNLOAD_REPORT:
                showDownloadReport = sp.getBoolean(PREF_SHOW_DOWNLOAD_REPORT, true);
                break;
            // Queue
            case PREF_QUEUE_ADD_TO_FRONT:
                enqueueAtFront = sp.getBoolean(PREF_QUEUE_ADD_TO_FRONT, false);
                break;
            // Playback
            case PREF_PAUSE_ON_HEADSET_DISCONNECT:
                pauseOnHeadsetDisconnect = sp.getBoolean(PREF_PAUSE_ON_HEADSET_DISCONNECT, true);
                break;
            case PREF_UNPAUSE_ON_HEADSET_RECONNECT:
                unpauseOnHeadsetReconnect = sp.getBoolean(PREF_UNPAUSE_ON_HEADSET_RECONNECT, true);
                break;
            case PREF_FOLLOW_QUEUE:
                followQueue = sp.getBoolean(PREF_FOLLOW_QUEUE, false);
                break;
            case PREF_AUTO_DELETE:
                autoDelete = sp.getBoolean(PREF_AUTO_DELETE, false);
                break;
            case PREF_SMART_MARK_AS_PLAYED_SECS:
                smartMarkAsPlayedSecs = Integer.valueOf(sp.getString(PREF_SMART_MARK_AS_PLAYED_SECS, "30"));
                break;
            case PREF_PLAYBACK_SPEED_ARRAY:
                playbackSpeedArray = readPlaybackSpeedArray(sp.getString(PREF_PLAYBACK_SPEED_ARRAY, null));
                break;
            case PREF_PAUSE_PLAYBACK_FOR_FOCUS_LOSS:
                pauseForFocusLoss = sp.getBoolean(PREF_PAUSE_PLAYBACK_FOR_FOCUS_LOSS, false);
                break;
            case PREF_RESUME_AFTER_CALL:
                resumeAfterCall = sp.getBoolean(PREF_RESUME_AFTER_CALL, true);
                break;
            // Network
            case PREF_UPDATE_INTERVAL:
                updateInterval = readUpdateInterval(sp.getString(PREF_UPDATE_INTERVAL, "0"));
                ClientConfig.applicationCallbacks.setUpdateInterval(updateInterval);
                break;
            case PREF_MOBILE_UPDATE:
                allowMobileUpdate = sp.getBoolean(PREF_MOBILE_UPDATE, false);
                break;
            case PREF_PARALLEL_DOWNLOADS:
                parallelDownloads = Integer.valueOf(sp.getString(PREF_PARALLEL_DOWNLOADS, "6"));
                break;
            case PREF_EPISODE_CACHE_SIZE:
                episodeCacheSize = readEpisodeCacheSizeInternal(sp.getString(PREF_EPISODE_CACHE_SIZE, "20"));
                break;
            case PREF_ENABLE_AUTODL:
                enableAutodownload = sp.getBoolean(PREF_ENABLE_AUTODL, false);
                break;
            case PREF_ENABLE_AUTODL_ON_BATTERY:
                enableAutodownloadOnBattery = sp.getBoolean(PREF_ENABLE_AUTODL_ON_BATTERY, true);
                break;
            case PREF_ENABLE_AUTODL_WIFI_FILTER:
                enableAutodownloadWifiFilter = sp.getBoolean(PREF_ENABLE_AUTODL_WIFI_FILTER, false);
                break;
            case PREF_AUTODL_SELECTED_NETWORKS:
                autodownloadSelectedNetworks = StringUtils.split(
                        sp.getString(PREF_AUTODL_SELECTED_NETWORKS, ""), ',');
                break;
            // Services
            case PREF_AUTO_FLATTR:
                autoFlattr = sp.getBoolean(PREF_AUTO_FLATTR, false);
                break;
            case PREF_AUTO_FLATTR_PLAYED_DURATION_THRESHOLD:
                autoFlattrPlayedDurationThreshold = sp.getFloat(PREF_AUTO_FLATTR_PLAYED_DURATION_THRESHOLD,
                        PREF_AUTO_FLATTR_PLAYED_DURATION_THRESHOLD_DEFAULT);
                break;
            // Mediaplayer
            case PREF_PLAYBACK_SPEED:
                playbackSpeed = sp.getString(PREF_PLAYBACK_SPEED, "1.0");
                break;
            case PREF_FAST_FORWARD_SECS:
                fastForwardSecs = sp.getInt(PREF_FAST_FORWARD_SECS, 30);
                break;
            case PREF_REWIND_SECS:
                rewindSecs = sp.getInt(PREF_REWIND_SECS, 30);
                break;
            case PREF_QUEUE_LOCKED:
                queueLocked = sp.getBoolean(PREF_QUEUE_LOCKED, false);
                break;
            default:
                Log.w(TAG, "Unhandled key: " + key);
        }
    }

    public static void setPrefFastForwardSecs(int secs) {
        Log.d(TAG, "setPrefFastForwardSecs(" + secs +")");
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(instance.context).edit();
        editor.putInt(PREF_FAST_FORWARD_SECS, secs);
        editor.commit();
    }

    public static void setPrefRewindSecs(int secs) {
        Log.d(TAG, "setPrefRewindSecs(" + secs +")");
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(instance.context).edit();
        editor.putInt(PREF_REWIND_SECS, secs);
        editor.commit();
    }

    public static void setPlaybackSpeed(String speed) {
        PreferenceManager.getDefaultSharedPreferences(instance.context).edit()
                .putString(PREF_PLAYBACK_SPEED, speed).apply();
    }

    public static void setPlaybackSpeedArray(String[] speeds) {
        JSONArray jsonArray = new JSONArray();
        for (String speed : speeds) {
            jsonArray.put(speed);
        }
        PreferenceManager.getDefaultSharedPreferences(instance.context).edit()
                .putString(PREF_PLAYBACK_SPEED_ARRAY, jsonArray.toString())
                .apply();
    }

    public static void setAutodownloadSelectedNetworks(Context context,
                                                       String[] value) {
        SharedPreferences.Editor editor = PreferenceManager
                .getDefaultSharedPreferences(context.getApplicationContext())
                .edit();
        editor.putString(PREF_AUTODL_SELECTED_NETWORKS,
                StringUtils.join(value, ','));
        editor.commit();
    }

    /**
     * Sets the update interval value. Should only be used for testing purposes!
     */
    public static void setUpdateInterval(Context context, long newValue) {
        instanceAvailable();
        SharedPreferences.Editor editor = PreferenceManager
                .getDefaultSharedPreferences(context.getApplicationContext())
                .edit();
        editor.putString(PREF_UPDATE_INTERVAL,
                String.valueOf(newValue));
        editor.commit();
        instance.updateInterval = newValue;
    }

    /**
     * Change the auto-flattr settings
     *
     * @param context For accessing the shared preferences
     * @param enabled Whether automatic flattring should be enabled at all
     * @param autoFlattrThreshold The percentage of playback time after which an episode should be
     *                            flattrd. Must be a value between 0 and 1 (inclusive)
     * */
    public static void setAutoFlattrSettings(Context context, boolean enabled, float autoFlattrThreshold) {
        instanceAvailable();
        Validate.inclusiveBetween(0.0, 1.0, autoFlattrThreshold);
        PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext())
                .edit()
                .putBoolean(PREF_AUTO_FLATTR, enabled)
                .putFloat(PREF_AUTO_FLATTR_PLAYED_DURATION_THRESHOLD, autoFlattrThreshold)
                .commit();
        instance.autoFlattr = enabled;
        instance.autoFlattrPlayedDurationThreshold = autoFlattrThreshold;
    }

    public static void setHiddenDrawerItems(Context context, List<String> items) {
        instanceAvailable();
        instance.hiddenDrawerItems = items;
        String str = StringUtils.join(items, ',');
        PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext())
                .edit()
                .putString(PREF_HIDDEN_DRAWER_ITEMS, str)
                .commit();
    }

    public static void setQueueLocked(boolean locked) {
        instanceAvailable();
        instance.queueLocked = locked;
        PreferenceManager.getDefaultSharedPreferences(instance.context)
                .edit()
                .putBoolean(PREF_QUEUE_LOCKED, locked)
                .commit();
    }


    /**
     * Return the folder where the app stores all of its data. This method will
     * return the standard data folder if none has been set by the user.
     *
     * @param type The name of the folder inside the data folder. May be null
     *             when accessing the root of the data folder.
     * @return The data folder that has been requested or null if the folder
     * could not be created.
     */
    public static File getDataFolder(Context context, String type) {
        instanceAvailable();
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context.getApplicationContext());
        String strDir = prefs.getString(PREF_DATA_FOLDER, null);
        if (strDir == null) {
            Log.d(TAG, "Using default data folder");
            return context.getExternalFilesDir(type);
        } else {
            File dataDir = new File(strDir);
            if (!dataDir.exists()) {
                if (!dataDir.mkdir()) {
                    Log.w(TAG, "Could not create data folder");
                    return null;
                }
            }

            if (type == null) {
                return dataDir;
            } else {
                // handle path separators
                String[] dirs = type.split("/");
                for (int i = 0; i < dirs.length; i++) {
                    if (dirs.length > 0) {
                        if (i < dirs.length - 1) {
                            dataDir = getDataFolder(context, dirs[i]);
                            if (dataDir == null) {
                                return null;
                            }
                        }
                        type = dirs[i];
                    }
                }
                File typeDir = new File(dataDir, type);
                if (!typeDir.exists()) {
                    if (dataDir.canWrite()) {
                        if (!typeDir.mkdir()) {
                            Log.e(TAG, "Could not create data folder named " + type);
                            return null;
                        }
                    }
                }
                return typeDir;
            }
        }
    }

    public static void setDataFolder(String dir) {
        Log.d(TAG, "Result from DirectoryChooser: " + dir);
        instanceAvailable();
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(instance.context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(PREF_DATA_FOLDER, dir);
        editor.commit();
        createImportDirectory();
    }

    /**
     * Create a .nomedia file to prevent scanning by the media scanner.
     */
    private static void createNoMediaFile() {
        File f = new File(instance.context.getExternalFilesDir(null),
                ".nomedia");
        if (!f.exists()) {
            try {
                f.createNewFile();
            } catch (IOException e) {
                Log.e(TAG, "Could not create .nomedia file");
                e.printStackTrace();
            }
            Log.d(TAG, ".nomedia file created");
        }
    }

    /**
     * Creates the import directory if it doesn't exist and if storage is
     * available
     */
    private static void createImportDirectory() {
        File importDir = getDataFolder(instance.context,
                IMPORT_DIR);
        if (importDir != null) {
            if (importDir.exists()) {
                Log.d(TAG, "Import directory already exists");
            } else {
                Log.d(TAG, "Creating import directory");
                importDir.mkdir();
            }
        } else {
            Log.d(TAG, "Could not access external storage.");
        }
    }

    /**
     * Updates alarm registered with the AlarmManager service or deactivates it.
     */
    public static void restartUpdateAlarm(long triggerAtMillis, long intervalMillis) {
        instanceAvailable();
        Log.d(TAG, "Restarting update alarm.");
        AlarmManager alarmManager = (AlarmManager) instance.context
                .getSystemService(Context.ALARM_SERVICE);
        PendingIntent updateIntent = PendingIntent.getBroadcast(
                instance.context, 0, new Intent(ClientConfig.applicationCallbacks.getApplicationInstance(), FeedUpdateReceiver.class), 0);
        alarmManager.cancel(updateIntent);
        if (intervalMillis != 0) {
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtMillis, intervalMillis,
                    updateIntent);
            Log.d(TAG, "Changed alarm to new interval");
        } else {
            Log.d(TAG, "Automatic update was deactivated");
        }
    }


    /**
     * Reads episode cache size as it is saved in the episode_cache_size_values array.
     */
    public static int readEpisodeCacheSize(String valueFromPrefs) {
        instanceAvailable();
        return instance.readEpisodeCacheSizeInternal(valueFromPrefs);
    }
}