1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
package net.sourceforge.subsonic.androidapp.util; |
20 |
|
|
21 |
|
import java.util.ArrayList; |
22 |
|
import java.util.List; |
23 |
|
import java.util.concurrent.Executors; |
24 |
|
import java.util.concurrent.ScheduledExecutorService; |
25 |
|
import java.util.concurrent.TimeUnit; |
26 |
|
|
27 |
|
import android.content.Context; |
28 |
|
import android.util.Log; |
29 |
|
import net.sourceforge.subsonic.androidapp.domain.MusicDirectory; |
30 |
|
import net.sourceforge.subsonic.androidapp.service.MusicService; |
31 |
|
import net.sourceforge.subsonic.androidapp.service.MusicServiceFactory; |
32 |
|
|
33 |
|
|
34 |
|
@author |
35 |
|
@version |
36 |
|
|
|
|
| 92,5% |
Uncovered Elements: 3 (40) |
Complexity: 13 |
Complexity Density: 0,46 |
|
37 |
|
public class ShufflePlayBuffer { |
38 |
|
|
39 |
|
private static final String TAG = ShufflePlayBuffer.class.getSimpleName(); |
40 |
|
private static final int CAPACITY = 50; |
41 |
|
private static final int REFILL_THRESHOLD = 40; |
42 |
|
|
43 |
|
private final ScheduledExecutorService executorService; |
44 |
|
private final List<MusicDirectory.Entry> buffer = new ArrayList<MusicDirectory.Entry>(); |
45 |
|
private Context context; |
46 |
|
private int currentServer; |
47 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
|
48 |
1
|
public ShufflePlayBuffer(Context context) {... |
49 |
1
|
this.context = context; |
50 |
1
|
executorService = Executors.newSingleThreadScheduledExecutor(); |
51 |
1
|
Runnable runnable = new Runnable() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
52 |
46
|
@Override... |
53 |
|
public void run() { |
54 |
46
|
refill(); |
55 |
|
} |
56 |
|
}; |
57 |
1
|
executorService.scheduleWithFixedDelay(runnable, 1, 10, TimeUnit.SECONDS); |
58 |
|
} |
59 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0,43 |
|
60 |
7
|
public List<MusicDirectory.Entry> get(int size) {... |
61 |
7
|
clearBufferIfnecessary(); |
62 |
|
|
63 |
7
|
List<MusicDirectory.Entry> result = new ArrayList<MusicDirectory.Entry>(size); |
64 |
7
|
synchronized (buffer) { |
65 |
73
|
while (!buffer.isEmpty() && result.size() < size) { |
66 |
66
|
result.add(buffer.remove(buffer.size() - 1)); |
67 |
|
} |
68 |
|
} |
69 |
7
|
Log.i(TAG, "Taking " + result.size() + " songs from shuffle play buffer. " + buffer.size() + " remaining."); |
70 |
7
|
return result; |
71 |
|
} |
72 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
73 |
0
|
public void shutdown() {... |
74 |
0
|
executorService.shutdown(); |
75 |
|
} |
76 |
|
|
|
|
| 92,3% |
Uncovered Elements: 1 (13) |
Complexity: 5 |
Complexity Density: 0,45 |
|
77 |
46
|
private void refill() {... |
78 |
|
|
79 |
|
|
80 |
46
|
clearBufferIfnecessary(); |
81 |
|
|
82 |
46
|
if (buffer.size() > REFILL_THRESHOLD || (!Util.isNetworkConnected(context) && !Util.isOffline(context))) { |
83 |
39
|
return; |
84 |
|
} |
85 |
|
|
86 |
7
|
try { |
87 |
7
|
MusicService service = MusicServiceFactory.getMusicService(context); |
88 |
7
|
int n = CAPACITY - buffer.size(); |
89 |
7
|
MusicDirectory songs = service.getRandomSongs(n, context, null); |
90 |
|
|
91 |
7
|
synchronized (buffer) { |
92 |
7
|
buffer.addAll(songs.getChildren()); |
93 |
7
|
Log.i(TAG, "Refilled shuffle play buffer with " + songs.getChildren().size() + " songs."); |
94 |
|
} |
95 |
|
} catch (Exception x) { |
96 |
0
|
Log.w(TAG, "Failed to refill shuffle play buffer.", x); |
97 |
|
} |
98 |
|
} |
99 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0,5 |
|
100 |
53
|
private void clearBufferIfnecessary() {... |
101 |
53
|
synchronized (buffer) { |
102 |
53
|
if (currentServer != Util.getActiveServer(context)) { |
103 |
3
|
currentServer = Util.getActiveServer(context); |
104 |
3
|
buffer.clear(); |
105 |
|
} |
106 |
|
} |
107 |
|
} |
108 |
|
|
109 |
|
} |