1 package com.moiseum.wolnelektury.connection.downloads;
3 import android.app.IntentService;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.support.annotation.Nullable;
7 import android.util.Log;
9 import com.moiseum.wolnelektury.base.WLApplication;
10 import com.moiseum.wolnelektury.connection.ErrorHandler;
11 import com.moiseum.wolnelektury.connection.RestClient;
12 import com.moiseum.wolnelektury.connection.services.BooksService;
14 import org.greenrobot.eventbus.EventBus;
15 import org.parceler.Parcels;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.List;
21 import okhttp3.ResponseBody;
22 import retrofit2.Call;
23 import retrofit2.Response;
26 * Created by piotrostrowski on 07.05.2017.
29 public class FileDownloadIntentService extends IntentService {
31 private static final String TAG = FileDownloadIntentService.class.getSimpleName();
32 public static final String FILE_URL_KEY = "FileUrlKey";
33 public static final String FILES_URLS_KEY = "FilesUrlsKey";
35 public static void downloadFile(Context context, String fileUrl) {
36 Intent downloadIntent = new Intent(context, FileDownloadIntentService.class);
37 downloadIntent.putExtra(FILE_URL_KEY, fileUrl);
38 context.startService(downloadIntent);
41 public static void downloadFiles(Context context, ArrayList<String> filesUrls) {
42 Intent downloadIntent = new Intent(context, FileDownloadIntentService.class);
43 downloadIntent.putExtra(FILES_URLS_KEY, Parcels.wrap(filesUrls));
44 context.startService(downloadIntent);
47 public FileDownloadIntentService() {
52 public void onCreate() {
58 protected void onHandleIntent(@Nullable Intent intent) {
63 if (intent.hasExtra(FILE_URL_KEY)) {
64 String fileUrl = intent.getStringExtra(FILE_URL_KEY);
65 checkCacheAndDownload(fileUrl);
66 } else if (intent.hasExtra(FILES_URLS_KEY)) {
67 ArrayList<String> filesUrls = Parcels.unwrap(intent.getParcelableExtra(FILES_URLS_KEY));
68 for (String fileUrl : filesUrls) {
69 if (!checkCacheAndDownload(fileUrl)) {
76 private boolean checkCacheAndDownload(String fileUrl) {
77 if (FileCacheUtils.getCachedFileForUrl(fileUrl) != null) {
78 Log.v(TAG, fileUrl + " is already in cache.");
79 EventBus.getDefault().post(new DownloadFileEvent(fileUrl, true));
82 return downloadFile(fileUrl);
85 private boolean downloadFile(String fileUrl) {
86 RestClient client = WLApplication.getInstance().getRestClient();
87 BooksService booksService = client.createService(BooksService.class);
89 Call<ResponseBody> call = booksService.downloadFileWithUrl(fileUrl);
90 Response<ResponseBody> response = call.execute();
91 if (response.isSuccessful()) {
92 boolean result = FileCacheUtils.writeResponseBodyToDiskCache(response.body(), fileUrl);
93 EventBus.getDefault().post(new DownloadFileEvent(fileUrl, result));
95 ErrorHandler<ResponseBody> errorHandler = new ErrorHandler<>(response);
96 errorHandler.handle();
97 //if nothing cause, throw exception
98 throw new UnsupportedOperationException("Unhandled exception");
100 } catch (IOException e) {
101 Log.e(TAG, "Failed to download audio file: " + fileUrl, e);
102 EventBus.getDefault().post(new DownloadFileEvent(fileUrl, false));
108 public static class DownloadFileEvent {
110 private String fileUrl;
111 private boolean success;
113 DownloadFileEvent(String fileUrl, boolean success) {
114 this.fileUrl = fileUrl;
115 this.success = success;
118 public String getFileUrl() {
122 public boolean isSuccess() {