Added Android code
[wl-app.git] / Android / folioreader / src / main / java / com / folioreader / Config.java
1 package com.folioreader;
2
3 import android.os.Parcel;
4 import android.os.Parcelable;
5
6 import org.json.JSONObject;
7
8 /**
9  * Created by mahavir on 4/12/16.
10  */
11 public class Config implements Parcelable {
12         public static final String INTENT_CONFIG = "config";
13         public static final String CONFIG_FONT = "font";
14         public static final String CONFIG_FONT_SIZE = "font_size";
15         public static final String CONFIG_MARGIN_SIZE = "margin_size";
16         public static final String CONFIG_INTERLINE_SIZE = "interline_size";
17         public static final String CONFIG_IS_NIGHTMODE = "is_night_mode";
18         public static final String CONFIG_THEMECOLOR = "theme_color";
19         public static final String CONFIG_ICON_COLOR = "icon_color";
20         public static final String CONFIG_TOOLBAR_COLOR = "toolbar_color";
21         public static final String CONFIG_IS_TTS = "is_tts";
22         public static final String INTENT_PORT = "port";
23         private int font;
24         private int fontSize;
25         private int marginSize;
26         private int interlineSize;
27         private boolean nightMode;
28         private int themeColor;
29         private int iconColor;
30         private int toolbarColor;
31         private boolean showTts;
32
33         //    public Config(int font, int fontSize, boolean nightMode, int themeColor, int iconColor, int toolbarColor, boolean showTts) {
34         //        this.font = font;
35         //        this.fontSize = fontSize;
36         //        this.nightMode = nightMode;
37         //        this.themeColor = themeColor;
38         //        this.iconColor = iconColor;
39         //        this.toolbarColor = toolbarColor;
40         //        this.showTts = showTts;
41         //    }
42
43         private Config(ConfigBuilder configBuilder) {
44                 font = configBuilder.mFont;
45                 fontSize = configBuilder.mFontSize;
46                 marginSize = configBuilder.mMarginSize;
47                 interlineSize = configBuilder.mInterlineSize;
48                 nightMode = configBuilder.mNightMode;
49                 themeColor = configBuilder.mThemeColor;
50                 iconColor = configBuilder.iconColor;
51                 toolbarColor = configBuilder.toolbarColor;
52                 showTts = configBuilder.mShowTts;
53         }
54
55         public Config(JSONObject jsonObject) {
56                 font = jsonObject.optInt(CONFIG_FONT);
57                 fontSize = jsonObject.optInt(CONFIG_FONT_SIZE);
58                 marginSize = jsonObject.optInt(CONFIG_MARGIN_SIZE);
59                 interlineSize = jsonObject.optInt(CONFIG_INTERLINE_SIZE);
60                 nightMode = jsonObject.optBoolean(CONFIG_IS_NIGHTMODE);
61                 themeColor = jsonObject.optInt(CONFIG_THEMECOLOR);
62                 iconColor = jsonObject.optInt(CONFIG_ICON_COLOR);
63                 toolbarColor = jsonObject.optInt(CONFIG_TOOLBAR_COLOR);
64                 showTts = jsonObject.optBoolean(CONFIG_IS_TTS);
65         }
66
67         private Config() {
68                 fontSize = 2;
69                 marginSize = 1;
70                 interlineSize = 1;
71                 font = 3;
72                 nightMode = false;
73                 themeColor = R.color.app_green;
74                 showTts = true;
75         }
76
77         private Config(Parcel in) {
78                 readFromParcel(in);
79         }
80
81
82         public int getFont() {
83                 return font;
84         }
85
86         public void setFont(int font) {
87                 this.font = font;
88         }
89
90         public int getFontSize() {
91                 return fontSize;
92         }
93
94         public void setFontSize(int fontSize) {
95                 this.fontSize = fontSize;
96         }
97
98         public int getMarginSize() {
99                 return marginSize;
100         }
101
102         public void setMarginSize(int marginSize) {
103                 this.marginSize = marginSize;
104         }
105
106         public int getInterlineSize() {
107                 return interlineSize;
108         }
109
110         public void setInterlineSize(int interlineSize) {
111                 this.interlineSize = interlineSize;
112         }
113
114         public boolean isNightMode() {
115                 return nightMode;
116         }
117
118         public void setNightMode(boolean nightMode) {
119                 this.nightMode = nightMode;
120         }
121
122
123         public int getThemeColor() {
124                 return themeColor;
125         }
126
127         public void setThemeColor(int themeColor) {
128                 this.themeColor = themeColor;
129         }
130
131         public boolean isShowTts() {
132                 return showTts;
133         }
134
135         public void setShowTts(boolean showTts) {
136                 this.showTts = showTts;
137         }
138
139         public int getIconColor() {
140                 return iconColor;
141         }
142
143         public int getToolbarColor() {
144                 return toolbarColor;
145         }
146
147         @Override
148
149         public boolean equals(Object o) {
150                 if (this == o) {
151                         return true;
152                 }
153                 if (!(o instanceof Config)) {
154                         return false;
155                 }
156
157                 Config config = (Config) o;
158
159                 return font == config.font && fontSize == config.fontSize && marginSize == config.marginSize && interlineSize == config.interlineSize && nightMode == config.nightMode &&
160                                 toolbarColor == config.toolbarColor && iconColor == config.iconColor;
161         }
162
163         @Override
164         public int hashCode() {
165                 int result = font;
166                 result = 31 * result + fontSize;
167                 result = 31 * result + marginSize;
168                 result = 31 * result + interlineSize;
169                 result = 31 * result + (nightMode ? 1 : 0);
170                 return result;
171         }
172
173         @Override
174         public String toString() {
175                 return "Config{"
176                                 + "font=" + font
177                                 + ", fontSize=" + fontSize
178                                 + ", marginSize=" + marginSize
179                                 + ", interlineSize=" + interlineSize
180                                 + ", nightMode=" + nightMode
181                                 + '}';
182         }
183
184         @Override
185         public int describeContents() {
186                 return 0;
187         }
188
189         @Override
190         public void writeToParcel(Parcel dest, int flags) {
191                 dest.writeInt(font);
192                 dest.writeInt(fontSize);
193                 dest.writeInt(marginSize);
194                 dest.writeInt(interlineSize);
195                 dest.writeInt(nightMode ? 1 : 0);
196                 dest.writeInt(themeColor);
197                 dest.writeInt(showTts ? 1 : 0);
198                 dest.writeInt(toolbarColor);
199                 dest.writeInt(iconColor);
200         }
201
202         private void readFromParcel(Parcel in) {
203                 font = in.readInt();
204                 fontSize = in.readInt();
205                 marginSize = in.readInt();
206                 interlineSize = in.readInt();
207                 nightMode = in.readInt() == 1;
208                 themeColor = in.readInt();
209                 showTts = in.readInt() == 1;
210                 toolbarColor = in.readInt();
211                 iconColor = in.readInt();
212         }
213
214         public static final Creator<Config> CREATOR = new Creator<Config>() {
215                 @Override
216                 public Config createFromParcel(Parcel in) {
217                         return new Config(in);
218                 }
219
220                 @Override
221                 public Config[] newArray(int size) {
222                         return new Config[size];
223                 }
224         };
225
226         public static class ConfigBuilder {
227                 private int mFont = 3;
228                 private int mFontSize = 2;
229                 private int mMarginSize = 1;
230                 private int mInterlineSize = 1;
231                 private boolean mNightMode = false;
232                 private int mThemeColor = R.color.settings_icons;
233                 private boolean mShowTts = true;
234                 private int iconColor = R.color.toolbar_icons;
235                 private int toolbarColor = R.color.toolbar_background;
236
237                 public ConfigBuilder font(int font) {
238                         mFont = font;
239                         return this;
240                 }
241
242                 public ConfigBuilder fontSize(int fontSize) {
243                         mFontSize = fontSize;
244                         return this;
245                 }
246
247                 public ConfigBuilder marginSize(int marginSize) {
248                         mMarginSize = marginSize;
249                         return this;
250                 }
251
252                 public ConfigBuilder interlineSize(int interlineSize) {
253                         mInterlineSize = interlineSize;
254                         return this;
255                 }
256
257                 public ConfigBuilder nightmode(boolean nightMode) {
258                         mNightMode = nightMode;
259                         return this;
260                 }
261
262                 public ConfigBuilder themeColor(int themeColor) {
263                         mThemeColor = themeColor;
264                         return this;
265                 }
266
267                 public ConfigBuilder setShowTts(boolean showTts) {
268                         mShowTts = showTts;
269                         return this;
270                 }
271
272
273                 public Config build() {
274                         return new Config(this);
275                 }
276         }
277 }
278
279