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  import sk.baka.ambient.commons.MiscUtils;
29  
30  /***
31   * Notifies periodically the {@link BackgroundOpExecutor} about its progress.
32   * 
33   * @author Martin Vysny
34   */
35  public class NotifyingInputStream extends CountingInputStream {
36  	private final int length;
37  	private final String caption;
38  
39  	/***
40  	 * @param in
41  	 *            input stream
42  	 * @param length
43  	 *            the length of the content.
44  	 * @param parts
45  	 *            reports the progress {@code parts} times.
46  	 * @param caption
47  	 *            the caption to set.
48  	 */
49  	public NotifyingInputStream(final InputStream in, final int length,
50  			final int parts, final String caption) {
51  		super(in, length / parts);
52  		this.length = length;
53  		this.caption = caption;
54  		executor = AmbientApplication.getInstance().getBackgroundTasks();
55  	}
56  
57  	/***
58  	 * Creates an input stream from given URL. If the url fails to supply
59  	 * content length then a simple buffered input stream is returned.
60  	 * 
61  	 * @param url
62  	 *            the URL to open.
63  	 * @param parts
64  	 *            reports the progress {@code parts} times.
65  	 * @param caption
66  	 *            the caption to set.
67  	 * @return the stream instance.
68  	 * @throws IOException
69  	 */
70  	public static InputStream fromURL(final URL url, final int parts,
71  			final String caption) throws IOException {
72  		final URLConnection conn = url.openConnection();
73  		final int length = conn.getContentLength();
74  		final InputStream source = new BufferedInputStream(conn
75  				.getInputStream());
76  		return length > 0 ? new NotifyingInputStream(source, length, parts,
77  				caption) : source;
78  	}
79  
80  	private final BackgroundOpExecutor executor;
81  
82  	@Override
83  	protected void countEvent(long currentPosition) {
84  		MiscUtils.sysout("CurrentPosition:" + currentPosition + " Length:"
85  				+ length);
86  		executor.backgroundTask(0, caption, (int) currentPosition, length);
87  	}
88  }