Added Android code
[wl-app.git] / Android / folioreader / src / main / java / com / folioreader / util / FileUtil.java
1 package com.folioreader.util;
2
3 import android.content.Context;
4 import android.content.res.AssetManager;
5 import android.content.res.Resources;
6 import android.os.Environment;
7 import android.util.Log;
8
9 import com.folioreader.Constants;
10 import com.folioreader.ui.folio.activity.FolioActivity;
11
12 import java.io.File;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.OutputStream;
17
18 /**
19  * Created by Mahavir on 12/15/16.
20  */
21
22 public class FileUtil {
23     private static final String TAG = FileUtil.class.getSimpleName();
24     private static final String FOLIO_READER_ROOT = "/folioreader/";
25
26     public static String saveEpubFileAndLoadLazyBook(final Context context, FolioActivity.EpubSourceType epubSourceType, String epubFilePath, int epubRawId, String epubFileName) {
27         String filePath;
28         InputStream epubInputStream;
29         boolean isFolderAvailable;
30         try {
31             isFolderAvailable = isFolderAvailable(epubFileName);
32             filePath = getFolioEpubFilePath(epubSourceType, epubFilePath, epubFileName);
33
34             if (!isFolderAvailable) {
35                 if (epubSourceType.equals(FolioActivity.EpubSourceType.RAW)) {
36                     epubInputStream = context.getResources().openRawResource(epubRawId);
37                     saveTempEpubFile(filePath, epubFileName, epubInputStream);
38                 } else if (epubSourceType.equals(FolioActivity.EpubSourceType.ASSETS)) {
39                     AssetManager assetManager = context.getAssets();
40                     epubFilePath = epubFilePath.replaceAll(Constants.ASSET, "");
41                     epubInputStream = assetManager.open(epubFilePath);
42                     saveTempEpubFile(filePath, epubFileName, epubInputStream);
43                 } else {
44                     filePath = epubFilePath;
45                 }
46             }
47             return filePath;
48         } catch (IOException e) {
49             Log.d(TAG, e.getMessage());
50         }
51
52         return null;
53     }
54
55     public static String getFolioEpubFolderPath(String epubFileName) {
56         return Environment.getExternalStorageDirectory().getAbsolutePath()
57                 + "/" + FOLIO_READER_ROOT + "/" + epubFileName;
58     }
59
60     public static String getFolioEpubFilePath(FolioActivity.EpubSourceType sourceType, String epubFilePath, String epubFileName) {
61         if (FolioActivity.EpubSourceType.SD_CARD.equals(sourceType)) {
62             return epubFilePath;
63         } else {
64             return getFolioEpubFolderPath(epubFileName) + "/" + epubFileName + ".epub";
65         }
66     }
67
68     private static boolean isFolderAvailable(String epubFileName) {
69         File file = new File(getFolioEpubFolderPath(epubFileName));
70         return file.isDirectory();
71     }
72
73     public static String getEpubFilename(Context context, FolioActivity.EpubSourceType epubSourceType,
74                                          String epubFilePath, int epubRawId) {
75         String epubFileName;
76         if (epubSourceType.equals(FolioActivity.EpubSourceType.RAW)) {
77             Resources res = context.getResources();
78             epubFileName = res.getResourceEntryName(epubRawId);
79         } else {
80             String[] temp = epubFilePath.split("/");
81             epubFileName = temp[temp.length - 1];
82             int fileMaxIndex = epubFileName.length();
83             epubFileName = epubFileName.substring(0, fileMaxIndex - 5);
84         }
85
86         return epubFileName;
87     }
88
89     public static Boolean saveTempEpubFile(String filePath, String fileName, InputStream inputStream) {
90         OutputStream outputStream = null;
91         File file = new File(filePath);
92         try {
93             if (!file.exists()) {
94                 File folder = new File(getFolioEpubFolderPath(fileName));
95                 folder.mkdirs();
96
97                 outputStream = new FileOutputStream(file);
98                 int read = 0;
99                 byte[] bytes = new byte[inputStream.available()];
100
101                 while ((read = inputStream.read(bytes)) != -1) {
102                     outputStream.write(bytes, 0, read);
103                 }
104             } else {
105                 return true;
106             }
107             inputStream.close();
108             outputStream.close();
109         } catch (IOException e) {
110             Log.d(TAG, e.getMessage());
111         }
112         return false;
113     }
114 }