Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / connection / downloads / FileDownloadIntentService.java
1 package com.moiseum.wolnelektury.connection.downloads;
2
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;
8
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;
13
14 import org.greenrobot.eventbus.EventBus;
15 import org.parceler.Parcels;
16
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import okhttp3.ResponseBody;
22 import retrofit2.Call;
23 import retrofit2.Response;
24
25 /**
26  * Created by piotrostrowski on 07.05.2017.
27  */
28
29 public class FileDownloadIntentService extends IntentService {
30
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";
34
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);
39         }
40
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);
45         }
46
47         public FileDownloadIntentService() {
48                 super(TAG);
49         }
50
51         @Override
52         public void onCreate() {
53                 super.onCreate();
54         }
55
56
57         @Override
58         protected void onHandleIntent(@Nullable Intent intent) {
59                 if (intent == null) {
60                         return;
61                 }
62
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)) {
70                                         break;
71                                 }
72                         }
73                 }
74         }
75
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));
80                         return true;
81                 }
82                 return downloadFile(fileUrl);
83         }
84
85         private boolean downloadFile(String fileUrl) {
86                 RestClient client = WLApplication.getInstance().getRestClient();
87                 BooksService booksService = client.createService(BooksService.class);
88                 try {
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));
94                         } else {
95                                 ErrorHandler<ResponseBody> errorHandler = new ErrorHandler<>(response);
96                                 errorHandler.handle();
97                                 //if nothing cause, throw exception
98                                 throw new UnsupportedOperationException("Unhandled exception");
99                         }
100                 } catch (IOException e) {
101                         Log.e(TAG, "Failed to download audio file: " + fileUrl, e);
102                         EventBus.getDefault().post(new DownloadFileEvent(fileUrl, false));
103                         return false;
104                 }
105                 return true;
106         }
107
108         public static class DownloadFileEvent {
109
110                 private String fileUrl;
111                 private boolean success;
112
113                 DownloadFileEvent(String fileUrl, boolean success) {
114                         this.fileUrl = fileUrl;
115                         this.success = success;
116                 }
117
118                 public String getFileUrl() {
119                         return fileUrl;
120                 }
121
122                 public boolean isSuccess() {
123                         return success;
124                 }
125         }
126 }