View Javadoc

1   /***
2    *     Ambient - A music player for the Android platform
3    Copyright (C) 2007 Martin Vysny
4    
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9    
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14  
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package sk.baka.ambient;
20  
21  import java.io.BufferedInputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.net.URL;
25  import java.net.URLConnection;
26  
27  import sk.baka.ambient.commons.CountingInputStream;
28  
29  /***
30   * Notifies periodically the {@link BackgroundOpExecutor} about its progress.
31   * 
32   * @author Martin Vysny
33   */
34  public class NotifyingInputStream extends CountingInputStream {
35  	private final int length;
36  	private final String caption;
37  
38  	/***
39  	 * @param in
40  	 *            input stream
41  	 * @param length
42  	 *            the length of the content.
43  	 * @param parts
44  	 *            reports the progress {@code parts} times.
45  	 * @param caption
46  	 *            the caption to set.
47  	 */
48  	public NotifyingInputStream(final InputStream in, final int length,
49  			final int parts, final String caption) {
50  		super(in, length / parts);
51  		this.length = length;
52  		this.caption = caption;
53  		executor = AmbientApplication.getInstance().getBackgroundTasks();
54  	}
55  
56  	/***
57  	 * Creates an input stream from given URL. If the url fails to supply
58  	 * content length then a simple buffered input stream is returned.
59  	 * 
60  	 * @param url
61  	 *            the URL to open.
62  	 * @param parts
63  	 *            reports the progress {@code parts} times.
64  	 * @param caption
65  	 *            the caption to set.
66  	 * @return the stream instance.
67  	 * @throws IOException
68  	 */
69  	public static InputStream fromURL(final URL url, final int parts,
70  			final String caption) throws IOException {
71  		final URLConnection conn = url.openConnection();
72  		final int length = conn.getContentLength();
73  		final InputStream source = new BufferedInputStream(conn
74  				.getInputStream());
75  		return length > 0 ? new NotifyingInputStream(source, length, parts,
76  				caption) : source;
77  	}
78  
79  	private final BackgroundOpExecutor executor;
80  
81  	@Override
82  	protected void countEvent(long currentPosition) {
83  		executor.backgroundTask(0, caption, (int) currentPosition, length);
84  	}
85  }