|
1 | 1 | package xyz.gianlu.librespot; |
2 | 2 |
|
| 3 | +import org.jetbrains.annotations.Contract; |
3 | 4 | import org.jetbrains.annotations.NotNull; |
4 | 5 | import org.jetbrains.annotations.Nullable; |
5 | 6 | import xyz.gianlu.librespot.core.Session; |
@@ -39,6 +40,18 @@ private float getFloat(@NotNull String key, float fallback) { |
39 | 40 | } |
40 | 41 | } |
41 | 42 |
|
| 43 | + @Contract("_, _, !null -> !null") |
| 44 | + private <E extends Enum<E>> E getEnum(@NotNull Class<E> clazz, @NotNull String key, @Nullable E fallback) { |
| 45 | + String val = properties.getProperty(key, null); |
| 46 | + if (val == null) return fallback; |
| 47 | + |
| 48 | + try { |
| 49 | + return Enum.valueOf(clazz, val); |
| 50 | + } catch (RuntimeException ex) { |
| 51 | + return fallback; |
| 52 | + } |
| 53 | + } |
| 54 | + |
42 | 55 | @Override |
43 | 56 | public boolean cacheEnabled() { |
44 | 57 | return getBoolean("cache.enabled", defaults.cacheEnabled()); |
@@ -76,8 +89,27 @@ public float normalisationPregain() { |
76 | 89 |
|
77 | 90 | @Override |
78 | 91 | public @Nullable Session.DeviceType deviceType() { |
79 | | - String val = properties.getProperty("deviceType", null); |
80 | | - if (val == null) return null; |
81 | | - return Session.DeviceType.valueOf(val); |
| 92 | + return getEnum(Session.DeviceType.class, "deviceType", null); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public @Nullable String username() { |
| 97 | + return properties.getProperty("auth.username", null); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public @Nullable String password() { |
| 102 | + return properties.getProperty("auth.password", null); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public @Nullable String blob() { |
| 107 | + return properties.getProperty("auth.blob", null); |
| 108 | + } |
| 109 | + |
| 110 | + @NotNull |
| 111 | + @Override |
| 112 | + public Strategy strategy() { |
| 113 | + return getEnum(Strategy.class, "auth.strategy", defaults.strategy()); |
82 | 114 | } |
83 | 115 | } |
0 commit comments