Clover Coverage Report - Subsonic-Android Coverage Report
Coverage timestamp: ven dic 19 2014 17:57:13 EST
../../../../../img/srcFileCovDistChart7.png 55% of files have more coverage
39   132   19   4,88
18   86   0,49   8
8     2,38  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  VisualizerView       Line # 40 39 19 63,1% 0.63076925
 
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 2011 (C) Sindre Mehus
18    */
19    package net.sourceforge.subsonic.androidapp.view;
20   
21    import android.content.Context;
22    import android.graphics.Canvas;
23    import android.graphics.Color;
24    import android.graphics.Paint;
25    import android.media.audiofx.Visualizer;
26    import android.util.AttributeSet;
27    import android.view.View;
28    import net.sourceforge.subsonic.androidapp.audiofx.VisualizerController;
29    import net.sourceforge.subsonic.androidapp.domain.PlayerState;
30    import net.sourceforge.subsonic.androidapp.service.DownloadService;
31    import net.sourceforge.subsonic.androidapp.service.DownloadServiceImpl;
32   
33    /**
34    * A simple class that draws waveform data received from a
35    * {@link Visualizer.OnDataCaptureListener#onWaveFormDataCapture}
36    *
37    * @author Sindre Mehus
38    * @version $Id$
39    */
 
40    public class VisualizerView extends View {
41   
42    private static final int PREFERRED_CAPTURE_RATE_MILLIHERTZ = 20000;
43   
44    private final Paint paint = new Paint();
45   
46    private byte[] data;
47    private float[] points;
48    private boolean active;
49   
 
50  6 toggle public VisualizerView(Context context) {
51  6 super(context);
52   
53  6 paint.setStrokeWidth(2f);
54  6 paint.setAntiAlias(true);
55  6 paint.setColor(Color.rgb(0, 153, 204));
56    }
57   
 
58  27 toggle public boolean isActive() {
59  27 return active;
60    }
61   
 
62  28 toggle public void setActive(boolean active) {
63  28 this.active = active;
64  28 Visualizer visualizer = getVizualiser();
65  28 if (visualizer == null) {
66  0 return;
67    }
68   
69  28 int captureRate = Math.min(PREFERRED_CAPTURE_RATE_MILLIHERTZ, Visualizer.getMaxCaptureRate());
70  28 if (active) {
71  2 visualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
 
72  25 toggle @Override
73    public void onWaveFormDataCapture(Visualizer visualizer, byte[] waveform, int samplingRate) {
74  25 updateVisualizer(waveform);
75    }
76   
 
77  0 toggle @Override
78    public void onFftDataCapture(Visualizer visualizer, byte[] fft, int samplingRate) {
79    }
80    }, captureRate, true, false);
81    } else {
82  26 visualizer.setDataCaptureListener(null, captureRate, false, false);
83    }
84   
85  28 visualizer.setEnabled(active);
86  28 invalidate();
87    }
88   
 
89  28 toggle private Visualizer getVizualiser() {
90  28 DownloadService downloadService = DownloadServiceImpl.getInstance();
91  28 VisualizerController visualizerController = downloadService == null ? null : downloadService.getVisualizerController();
92  28 return visualizerController == null ? null : visualizerController.getVisualizer();
93    }
94   
 
95  25 toggle private void updateVisualizer(byte[] waveform) {
96  25 this.data = waveform;
97  25 invalidate();
98    }
99   
 
100  46 toggle @Override
101    protected void onDraw(Canvas canvas) {
102  46 super.onDraw(canvas);
103   
104  46 if (!active) {
105  19 return;
106    }
107  27 DownloadService downloadService = DownloadServiceImpl.getInstance();
108  27 if (downloadService != null && downloadService.getPlayerState() != PlayerState.STARTED) {
109  27 return;
110    }
111   
112  0 if (data == null) {
113  0 return;
114    }
115   
116  0 if (points == null || points.length < data.length * 4) {
117  0 points = new float[data.length * 4];
118    }
119   
120  0 int w = getWidth();
121  0 int h = getHeight();
122   
123  0 for (int i = 0; i < data.length - 1; i++) {
124  0 points[i * 4] = w * i / (data.length - 1);
125  0 points[i * 4 + 1] = h / 2 + ((byte) (data[i] + 128)) * (h / 2) / 128;
126  0 points[i * 4 + 2] = w * (i + 1) / (data.length - 1);
127  0 points[i * 4 + 3] = h / 2 + ((byte) (data[i + 1] + 128)) * (h / 2) / 128;
128    }
129   
130  0 canvas.drawLines(points, paint);
131    }
132    }