Clover Coverage Report - Subsonic-Android Coverage Report
Coverage timestamp: ven dic 19 2014 17:57:13 EST
../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
28   109   13   4,67
6   70   0,46   6
6     2,17  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  ShufflePlayBuffer       Line # 37 28 13 92,5% 0.925
 
No Tests
 
1    /*
2    This file is part of Subsonic.
3   
4    Subsonic is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8   
9    Subsonic is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12    GNU General Public License for more details.
13   
14    You should have received a copy of the GNU General Public License
15    along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
16   
17    Copyright 2009 (C) Sindre Mehus
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 Sindre Mehus
35    * @version $Id$
36    */
 
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   
 
48  1 toggle public ShufflePlayBuffer(Context context) {
49  1 this.context = context;
50  1 executorService = Executors.newSingleThreadScheduledExecutor();
51  1 Runnable runnable = new Runnable() {
 
52  46 toggle @Override
53    public void run() {
54  46 refill();
55    }
56    };
57  1 executorService.scheduleWithFixedDelay(runnable, 1, 10, TimeUnit.SECONDS);
58    }
59   
 
60  7 toggle 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   
 
73  0 toggle public void shutdown() {
74  0 executorService.shutdown();
75    }
76   
 
77  46 toggle private void refill() {
78   
79    // Check if active server has changed.
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  53 toggle 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    }