Added Android code
[wl-app.git] / Android / folioreader / src / main / java / com / folioreader / ui / base / HtmlUtil.java
1 package com.folioreader.ui.base;
2
3 import android.content.Context;
4
5 import com.folioreader.Config;
6 import com.folioreader.Constants;
7 import com.folioreader.R;
8
9 /**
10  * @author gautam chibde on 14/6/17.
11  */
12
13 public final class HtmlUtil {
14
15     /**
16      * Function modifies input html string by adding extra css,js and font information.
17      *
18      * @param context     Activity Context
19      * @param htmlContent input html raw data
20      * @return modified raw html string
21      */
22     public static String getHtmlContent(Context context, String htmlContent, Config config) {
23         String cssPath =
24                 String.format(context.getString(R.string.css_tag), "file:///android_asset/css/Style.css");
25
26
27         String jsPath = String.format(context.getString(R.string.script_tag),
28                         "file:///android_asset/js/jsface.min.js");
29         jsPath =
30                 jsPath + String.format(context.getString(R.string.script_tag),
31                         "file:///android_asset/js/jquery-3.1.1.min.js");
32
33         jsPath =
34                 jsPath + String.format(context.getString(R.string.script_tag),
35                         "file:///android_asset/js/rangy-core.js");
36         jsPath =
37                 jsPath + String.format(context.getString(R.string.script_tag),
38                         "file:///android_asset/js/rangy-highlighter.js");
39         jsPath =
40                 jsPath + String.format(context.getString(R.string.script_tag),
41                         "file:///android_asset/js/rangy-classapplier.js");
42         jsPath =
43                 jsPath + String.format(context.getString(R.string.script_tag),
44                         "file:///android_asset/js/rangy-serializer.js");
45         jsPath =
46                 jsPath + String.format(context.getString(R.string.script_tag),
47                         "file:///android_asset/js/rangy-serializer.js");
48         jsPath =
49                 jsPath + String.format(context.getString(R.string.script_tag),
50                         "file:///android_asset/js/Bridge.js");
51
52         jsPath =
53                 jsPath + String.format(context.getString(R.string.script_tag),
54                         "file:///android_asset/android.selection.js");
55         jsPath =
56                 jsPath + String.format(context.getString(R.string.script_tag_method_call),
57                         "setMediaOverlayStyleColors('#C0ED72','#C0ED72')");
58
59         String toInject = "\n" + cssPath + "\n" + jsPath + "\n</head>";
60         htmlContent = htmlContent.replace("</head>", toInject);
61
62         String classes = "";
63         switch (config.getFont()) {
64             case Constants.FONT_EBGARAMOND:
65                 classes = "garamond";
66                 break;
67             case Constants.FONT_LATO:
68                 classes = "lato";
69                 break;
70             case Constants.FONT_LORA:
71                 classes = "lora";
72                 break;
73             case Constants.FONT_RALEWAY:
74                 classes = "raleway";
75                 break;
76             default:
77                 break;
78         }
79
80         if (config.isNightMode()) {
81             classes += " nightMode";
82         }
83
84         switch (config.getFontSize()) {
85             case 0:
86                 classes += " textSizeOne";
87                 break;
88             case 1:
89                 classes += " textSizeTwo";
90                 break;
91             case 2:
92                 classes += " textSizeThree";
93                 break;
94             case 3:
95                 classes += " textSizeFour";
96                 break;
97             case 4:
98                 classes += " textSizeFive";
99                 break;
100             default:
101                 break;
102         }
103
104         switch (config.getMarginSize()) {
105             case 0:
106                 classes += " marginSizeOne";
107                 break;
108             case 1:
109                 classes += " marginSizeTwo";
110                 break;
111             case 2:
112                 classes += " marginSizeThree";
113                 break;
114             case 3:
115                 classes += " marginSizeFour";
116                 break;
117             case 4:
118                 classes += " marginSizeFive";
119                 break;
120             default:
121                 break;
122         }
123
124         switch (config.getInterlineSize()) {
125             case 0:
126                 classes += " interlineSizeOne";
127                 break;
128             case 1:
129                 classes += " interlineSizeTwo";
130                 break;
131             case 2:
132                 classes += " interlineSizeThree";
133                 break;
134             case 3:
135                 classes += " interlineSizeFour";
136                 break;
137             case 4:
138                 classes += " interlineSizeFive";
139                 break;
140             default:
141                 break;
142         }
143
144         htmlContent = htmlContent.replace("<html ", "<html class=\"" + classes + "\" ");
145         return htmlContent;
146     }
147 }