aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/github/daneren2005/dsub/service/DownloadFile.java
blob: 30e069829d93040e94ca5b6378904ce7832576e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*
 This file is part of Subsonic.

 Subsonic is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Subsonic is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Subsonic.  If not, see <http://www.gnu.org/licenses/>.

 Copyright 2009 (C) Sindre Mehus
 */
package github.daneren2005.dsub.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.PowerManager;
import android.util.Log;

import github.daneren2005.dsub.domain.InternetRadioStation;
import github.daneren2005.dsub.domain.MusicDirectory;
import github.daneren2005.dsub.util.Constants;
import github.daneren2005.dsub.util.SilentBackgroundTask;
import github.daneren2005.dsub.util.FileUtil;
import github.daneren2005.dsub.util.Util;
import github.daneren2005.dsub.util.CacheCleaner;
import github.daneren2005.serverproxy.BufferFile;

public class DownloadFile implements BufferFile {
    private static final String TAG = DownloadFile.class.getSimpleName();
    private static final int MAX_FAILURES = 5;
    private final Context context;
    private final MusicDirectory.Entry song;
    private final File partialFile;
    private final File completeFile;
    private final File saveFile;

    private final MediaStoreService mediaStoreService;
    private DownloadTask downloadTask;
    private boolean save;
	private boolean failedDownload = false;
    private int failed = 0;
    private int bitRate;
	private boolean isPlaying = false;
	private boolean saveWhenDone = false;
	private boolean completeWhenDone = false;
	private Long contentLength = null;
	private long currentSpeed = 0;
	private boolean rateLimit = false;

    public DownloadFile(Context context, MusicDirectory.Entry song, boolean save) {
        this.context = context;
        this.song = song;
        this.save = save;
        saveFile = FileUtil.getSongFile(context, song);
        bitRate = getActualBitrate();
        partialFile = new File(saveFile.getParent(), FileUtil.getBaseName(saveFile.getName()) +
                ".partial." + FileUtil.getExtension(saveFile.getName()));
        completeFile = new File(saveFile.getParent(), FileUtil.getBaseName(saveFile.getName()) +
                ".complete." + FileUtil.getExtension(saveFile.getName()));
        mediaStoreService = new MediaStoreService(context);
    }

    public MusicDirectory.Entry getSong() {
        return song;
    }
	public boolean isSong() {
		return song.isSong();
	}

	public Context getContext() {
		return context;
	}

    /**
     * Returns the effective bit rate.
     */
    public int getBitRate() {
		if(!partialFile.exists()) {
			bitRate = getActualBitrate();
		}
        if (bitRate > 0) {
            return bitRate;
        }
        return song.getBitRate() == null ? 160 : song.getBitRate();
    }
	private int getActualBitrate() {
		int br = song.isVideo() ? Util.getMaxVideoBitrate(context) : Util.getMaxBitrate(context);
		if(br == 0 && song.getTranscodedSuffix() != null && "mp3".equals(song.getTranscodedSuffix().toLowerCase())) {
			if(song.getBitRate() != null) {
				br = Math.min(320, song.getBitRate());
			} else {
				br = 320;
			}
		} else if(song.getSuffix() != null && (song.getTranscodedSuffix() == null || song.getSuffix().equals(song.getTranscodedSuffix()))) {
			// If just downsampling, don't try to upsample (ie: 128 kpbs -> 192 kpbs)
			if(song.getBitRate() != null && (br == 0 || br > song.getBitRate())) {
				br = song.getBitRate();
			}
		}

		return br;
	}
	
	public Long getContentLength() {
		return contentLength;
	}

	public long getCurrentSize() {
		if(partialFile.exists()) {
			return partialFile.length();
		} else {
			File file = getCompleteFile();
			if(file.exists()) {
				return file.length();
			} else {
				return 0L;
			}
		}
	}

	@Override
	public long getEstimatedSize() {
		if(contentLength != null) {
			return contentLength;
		}

		File file = getCompleteFile();
		if(file.exists()) {
			return file.length();
		} else if(song.getDuration() == null) {
			return 0;
		} else {
			int br = (getBitRate() * 1000) / 8;
			int duration = song.getDuration();
			return br * duration;
		}
	}

	public long getBytesPerSecond() {
		return currentSpeed;
	}

    public synchronized void download() {
    	rateLimit = false;
        preDownload();
        downloadTask.execute();
    }
    public synchronized void downloadNow(MusicService musicService) {
    	rateLimit = true;
    	preDownload();
		downloadTask.setMusicService(musicService);
		try {
			downloadTask.doInBackground();
		} catch(InterruptedException e) {
			// This should never be reached
		}
    }
    private void preDownload() {
    	FileUtil.createDirectoryForParent(saveFile);
        failedDownload = false;
		if(!partialFile.exists()) {
			bitRate = getActualBitrate();
		}
		downloadTask = new DownloadTask(context);
    }

    public synchronized void cancelDownload() {
        if (downloadTask != null) {
            downloadTask.cancel();
        }
    }

	@Override
	public File getFile() {
		if (saveFile.exists()) {
			return saveFile;
		} else if (completeFile.exists()) {
			return completeFile;
		} else {
			return partialFile;
		}
	}

    public File getCompleteFile() {
        if (saveFile.exists()) {
            return saveFile;
        }

        if (completeFile.exists()) {
            return completeFile;
        }

        return saveFile;
    }
	public File getSaveFile() {
		return saveFile;
	}

    public File getPartialFile() {
        return partialFile;
    }

    public boolean isSaved() {
        return saveFile.exists();
    }

    public synchronized boolean isCompleteFileAvailable() {
        return saveFile.exists() || completeFile.exists();
    }

	@Override
    public synchronized boolean isWorkDone() {
        return saveFile.exists() || (completeFile.exists() && !save) || saveWhenDone || completeWhenDone;
    }

	@Override
	public void onStart() {
		setPlaying(true);
	}

	@Override
	public void onStop() {
		setPlaying(false);
	}

	@Override
	public synchronized void onResume() {
		if(!isWorkDone() && !isFailedMax() && !isDownloading() && !isDownloadCancelled()) {
			download();
		}
	}

	public synchronized boolean isDownloading() {
        return downloadTask != null && downloadTask.isRunning();
    }

    public synchronized boolean isDownloadCancelled() {
        return downloadTask != null && downloadTask.isCancelled();
    }

    public boolean shouldSave() {
        return save;
    }

    public boolean isFailed() {
        return failedDownload;
    }
    public boolean isFailedMax() {
    	return failed > MAX_FAILURES;
    }

    public void delete() {
        cancelDownload();
        
        // Remove from mediaStore BEFORE deleting file since it calls getCompleteFile
		deleteFromStore();
		
		// Delete all possible versions of the file
		File parent = partialFile.getParentFile();
        Util.delete(partialFile);
        Util.delete(completeFile);
        Util.delete(saveFile);
		FileUtil.deleteEmptyDir(parent);
    }

    public void unpin() {
        if (saveFile.exists()) {
        	// Delete old store entry before renaming to pinned file
            saveFile.renameTo(completeFile);
			renameInStore(saveFile, completeFile);
        }
    }

    public boolean cleanup() {
        boolean ok = true;
        if (completeFile.exists() || saveFile.exists()) {
            ok = Util.delete(partialFile);
        }
        if (saveFile.exists()) {
            ok &= Util.delete(completeFile);
        }
        return ok;
    }

    // In support of LRU caching.
    public void updateModificationDate() {
        updateModificationDate(saveFile);
        updateModificationDate(partialFile);
        updateModificationDate(completeFile);
    }

    private void updateModificationDate(File file) {
        if (file.exists()) {
            boolean ok = file.setLastModified(System.currentTimeMillis());
            if (!ok) {
                Log.w(TAG, "Failed to set last-modified date on " + file);
            }
        }
    }
	
	public void setPlaying(boolean isPlaying) {
		try {
			if(saveWhenDone && !isPlaying) {
				Util.renameFile(completeFile, saveFile);
				renameInStore(completeFile, saveFile);
				saveWhenDone = false;
			} else if(completeWhenDone && !isPlaying) {
				if(save) {
					Util.renameFile(partialFile, saveFile);
                    saveToStore();
				} else {
					Util.renameFile(partialFile, completeFile);
					saveToStore();
				}
				completeWhenDone = false;
			}
		} catch(IOException ex) {
			Log.w(TAG, "Failed to rename file " + completeFile + " to " + saveFile, ex);
		}
		
		this.isPlaying = isPlaying;
	}
	public void renamePartial() {
		try {
			Util.renameFile(partialFile, completeFile);
			saveToStore();
		} catch(IOException ex) {
			Log.w(TAG, "Failed to rename file " + partialFile + " to " + completeFile, ex);
		}
	}
	public boolean getPlaying() {
		return isPlaying;
	}
	
	private void deleteFromStore() {
		try {
			mediaStoreService.deleteFromMediaStore(this);
		} catch(Exception e) {
			Log.w(TAG, "Failed to remove from store", e);
		}
	}
	private void saveToStore() {
		if(!Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_HIDE_MEDIA, false)) {
			try {
				mediaStoreService.saveInMediaStore(this);
			} catch(Exception e) {
				Log.w(TAG, "Failed to save in media store", e);
			}
		}
	}
	private void renameInStore(File start, File end) {
		try {
			mediaStoreService.renameInMediaStore(start, end);
		} catch(Exception e) {
			Log.w(TAG, "Failed to rename in store", e);
		}
	}

	public boolean isStream() {
		return song != null && song instanceof InternetRadioStation;
	}
	public String getStream() {
		if(song != null && song instanceof InternetRadioStation) {
			InternetRadioStation station = (InternetRadioStation) song;
			return station.getStreamUrl();
		} else {
			return null;
		}
	}

    @Override
    public String toString() {
        return "DownloadFile (" + song + ")";
    }

	// Don't do this.  Causes infinite loop if two instances of same song
	/*@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (o == null || getClass() != o.getClass()) {
			return false;
		}

		DownloadFile downloadFile = (DownloadFile) o;
		return Util.equals(this.getSong(), downloadFile.getSong());
	}*/

    private class DownloadTask extends SilentBackgroundTask<Void> {
		private MusicService musicService;

		public DownloadTask(Context context) {
			super(context);
		}

        @Override
        public Void doInBackground() throws InterruptedException {
            InputStream in = null;
            FileOutputStream out = null;
            PowerManager.WakeLock wakeLock = null;
			WifiManager.WifiLock wifiLock = null;
            try {

                if (Util.isScreenLitOnDownload(context)) {
                    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
                    wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, toString());
                    wakeLock.acquire();
                }
				
				wifiLock = Util.createWifiLock(context, toString());
				wifiLock.acquire();

                if (saveFile.exists()) {
                    Log.i(TAG, saveFile + " already exists. Skipping.");
                    checkDownloads();
                    return null;
                }
                if (completeFile.exists()) {
                    if (save) {
						if(isPlaying) {
							saveWhenDone = true;
						} else {
							Util.renameFile(completeFile, saveFile);
							renameInStore(completeFile, saveFile);
						}
                    } else {
                        Log.i(TAG, completeFile + " already exists. Skipping.");
                    }
                    checkDownloads();
                    return null;
                }

				if(musicService == null) {
                	musicService = MusicServiceFactory.getMusicService(context);
				}

				// Some devices seem to throw error on partial file which doesn't exist
				boolean compare;
				try {
					compare = (bitRate == 0) || (song.getDuration() == 0) || (partialFile.length() == 0) || (bitRate * song.getDuration() * 1000 / 8) > partialFile.length();
				} catch(Exception e) {
					compare = true;
				}
				if(compare) {
					// Attempt partial HTTP GET, appending to the file if it exists.
					HttpURLConnection connection = musicService.getDownloadInputStream(context, song, partialFile.length(), bitRate, DownloadTask.this);
					long contentLength = connection.getContentLength();
					if(contentLength > 0) {
						Log.i(TAG, "Content Length: " + contentLength);
						DownloadFile.this.contentLength = contentLength;
					}

					in = connection.getInputStream();
					boolean partial = connection.getResponseCode() == HttpURLConnection.HTTP_PARTIAL;
					if (partial) {
						Log.i(TAG, "Executed partial HTTP GET, skipping " + partialFile.length() + " bytes");
					}

					out = new FileOutputStream(partialFile, partial);
					long n = copy(in, out);
					Log.i(TAG, "Downloaded " + n + " bytes to " + partialFile);
					out.flush();
					out.close();

					if (isCancelled()) {
						throw new Exception("Download of '" + song + "' was cancelled");
					} else if(partialFile.length() == 0) {
						throw new Exception("Download of '" + song + "' failed.  File is 0 bytes long.");
					}

					downloadAndSaveCoverArt(musicService);
				}

				if(isPlaying) {
					completeWhenDone = true;
				} else {
					if(save) {
						Util.renameFile(partialFile, saveFile);
					} else {
						Util.renameFile(partialFile, completeFile);
					}
					DownloadFile.this.saveToStore();
				}

            } catch(InterruptedException x) {
				throw x;
			} catch(FileNotFoundException x) {
				Util.delete(completeFile);
				Util.delete(saveFile);
				if(!isCancelled()) {
					failed = MAX_FAILURES + 1;
					failedDownload = true;
					Log.w(TAG, "Failed to download '" + song + "'.", x);
				}
			} catch(IOException x) {
				Util.delete(completeFile);
				Util.delete(saveFile);
				if(!isCancelled()) {
					failedDownload = true;
					Log.w(TAG, "Failed to download '" + song + "'.", x);
				}
			} catch (Exception x) {
                Util.delete(completeFile);
                Util.delete(saveFile);
                if (!isCancelled()) {
                	failed++;
                    failedDownload = true;
                    Log.w(TAG, "Failed to download '" + song + "'.", x);
                }
            } finally {
                Util.close(in);
                Util.close(out);
                if (wakeLock != null) {
                    wakeLock.release();
                    Log.i(TAG, "Released wake lock " + wakeLock);
                }
				if (wifiLock != null) {
					wifiLock.release();
				}
			}
			
			// Only run these if not interrupted, ie: cancelled
			DownloadService downloadService = DownloadService.getInstance();
			if(downloadService != null && !isCancelled()) {
				new CacheCleaner(context, downloadService).cleanSpace();
				checkDownloads();
			}

			return null;
        }
        
        private void checkDownloads() {
        	DownloadService downloadService = DownloadService.getInstance();
        	if(downloadService != null) {
        		downloadService.checkDownloads();
        	}
        }

        @Override
        public String toString() {
            return "DownloadTask (" + song + ")";
        }

		public void setMusicService(MusicService musicService) {
			this.musicService = musicService;
		}

        private void downloadAndSaveCoverArt(MusicService musicService) throws Exception {
            try {
                if (song.getCoverArt() != null) {
					// Check if album art already exists, don't want to needlessly load into memory
					File albumArtFile = FileUtil.getAlbumArtFile(context, song);
					if(!albumArtFile.exists()) {
						musicService.getCoverArt(context, song, 0, null, null);
					}
                }
            } catch (Exception x) {
                Log.e(TAG, "Failed to get cover art.", x);
            }
        }

        private long copy(final InputStream in, OutputStream out) throws IOException, InterruptedException {

            // Start a thread that will close the input stream if the task is
            // cancelled, thus causing the copy() method to return.
            new Thread("DownloadFile_copy") {
                @Override
                public void run() {
                    while (true) {
                        Util.sleepQuietly(3000L);
                        if (isCancelled()) {
                            Util.close(in);
                            return;
                        }
                        if (!isRunning()) {
                            return;
                        }
                    }
                }
            }.start();

            byte[] buffer = new byte[1024 * 16];
            long count = 0;
            int n;
            long lastLog = System.currentTimeMillis();
			long lastCount = 0;

			boolean activeLimit = rateLimit;
            while (!isCancelled() && (n = in.read(buffer)) != -1) {
                out.write(buffer, 0, n);
                count += n;
				lastCount += n;

                long now = System.currentTimeMillis();
                if (now - lastLog > 3000L) {  // Only every so often.
                    Log.i(TAG, "Downloaded " + Util.formatBytes(count) + " of " + song);
					currentSpeed = lastCount / ((now - lastLog) / 1000L);
                    lastLog = now;
					lastCount = 0;
					
					// Re-establish every few seconds whether screen is on or not
					if(rateLimit) {
						PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
						if(pm.isScreenOn()) {
							activeLimit = true;
						} else {
							activeLimit = false;
						}
					}
                }
                
                // If screen is on and rateLimit is true, stop downloading from exhausting bandwidth
                if(activeLimit) {
                	Thread.sleep(10L);
                }
            }
            return count;
        }
    }
}