Added Android code
[wl-app.git] / Android / folioreader / src / main / java / com / folioreader / util / AppUtil.java
1 package com.folioreader.util;
2
3 import android.content.Context;
4 import android.support.v4.content.ContextCompat;
5 import android.util.Log;
6
7 import com.folioreader.Config;
8 import com.folioreader.Constants;
9
10 import org.json.JSONArray;
11 import org.json.JSONException;
12 import org.json.JSONObject;
13
14 import java.net.URLConnection;
15 import java.text.SimpleDateFormat;
16 import java.util.Date;
17 import java.util.HashMap;
18 import java.util.Iterator;
19 import java.util.Locale;
20 import java.util.Map;
21
22 import static com.folioreader.Constants.BOOK_STATE;
23 import static com.folioreader.Constants.BOOK_TITLE;
24 import static com.folioreader.Constants.VIEWPAGER_COUNT;
25 import static com.folioreader.Constants.VIEWPAGER_POSITION;
26 import static com.folioreader.Constants.WEBVIEW_SCROLL_POSITION;
27 import static com.folioreader.util.SharedPreferenceUtil.getSharedPreferencesString;
28
29 /**
30  * Created by mahavir on 5/7/16.
31  */
32 public class AppUtil {
33
34     private static final String SMIL_ELEMENTS = "smil_elements";
35     private static final String TAG = AppUtil.class.getSimpleName();
36     private static final String FOLIO_READER_ROOT = "folioreader";
37
38     private enum FileType {
39         OPS,
40         OEBPS,
41         NONE
42     }
43
44     public static Map<String, String> toMap(String jsonString) {
45         Map<String, String> map = new HashMap<String, String>();
46         try {
47             JSONArray jsonArray = new JSONArray(jsonString);
48             JSONObject jObject = jsonArray.getJSONObject(0);
49             Iterator<String> keysItr = jObject.keys();
50         while(keysItr.hasNext()) {
51             String key = keysItr.next();
52             Object value = null;
53             value = jObject.get(key);
54
55             if(value instanceof JSONObject) {
56                 value = toMap(value.toString());
57             }
58             map.put(key, value.toString());
59         }
60         } catch (JSONException e) {
61             Log.e(TAG, "toMap failed", e);
62         }
63         return map;
64     }
65
66     public static String charsetNameForURLConnection(URLConnection connection) {
67         // see https://stackoverflow.com/a/3934280/1027646
68         String contentType = connection.getContentType();
69         String[] values = contentType.split(";");
70         String charset = null;
71
72         for (String value : values) {
73             value = value.trim();
74
75             if (value.toLowerCase().startsWith("charset=")) {
76                 charset = value.substring("charset=".length());
77                 break;
78             }
79         }
80
81         if (charset == null || charset.isEmpty()) {
82             charset = "UTF-8"; //Assumption
83         }
84
85         return charset;
86     }
87
88     public static String formatDate(Date hightlightDate) {
89         SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DATE_FORMAT, Locale.getDefault());
90         return simpleDateFormat.format(hightlightDate);
91     }
92
93     public static void saveBookState(Context context, String bookTitle, int folioPageViewPagerPosition, int folioPagesViewPagerCount, int
94             webViewScrollPosition) {
95         SharedPreferenceUtil.removeSharedPreferencesKey(context, bookTitle + BOOK_STATE);
96         JSONObject obj = new JSONObject();
97         try {
98             obj.put(BOOK_TITLE, bookTitle);
99             obj.put(WEBVIEW_SCROLL_POSITION, webViewScrollPosition);
100             obj.put(VIEWPAGER_POSITION, folioPageViewPagerPosition);
101             obj.put(VIEWPAGER_COUNT, folioPagesViewPagerCount);
102             SharedPreferenceUtil.
103                     putSharedPreferencesString(
104                             context, bookTitle + BOOK_STATE, obj.toString());
105         } catch (JSONException e) {
106             Log.e(TAG, e.getMessage());
107         }
108     }
109
110     public static void removeBookState(Context context, String bookTitle) {
111         SharedPreferenceUtil.removeSharedPreferencesKey(context, bookTitle + BOOK_STATE);
112     }
113
114     public static boolean checkPreviousBookStateExist(Context context, String bookName) {
115         String json
116                 = getSharedPreferencesString(
117                 context, bookName + BOOK_STATE,
118                 null);
119         if (json != null) {
120             try {
121                 JSONObject jsonObject = new JSONObject(json);
122                 String bookTitle = jsonObject.getString(BOOK_TITLE);
123                 if (bookTitle.equals(bookName))
124                     return true;
125             } catch (JSONException e) {
126                 Log.e(TAG, e.getMessage());
127                 return false;
128             }
129         }
130         return false;
131     }
132
133     public static int getPreviousBookStatePosition(Context context, String bookName) {
134         String json
135                 = getSharedPreferencesString(context,
136                 bookName + BOOK_STATE,
137                 null);
138         if (json != null) {
139             try {
140                 JSONObject jsonObject = new JSONObject(json);
141                 return jsonObject.getInt(VIEWPAGER_POSITION);
142             } catch (JSONException e) {
143                 Log.e(TAG, e.getMessage());
144                 return 0;
145             }
146         }
147         return 0;
148     }
149
150     public static int getPreviousBookStateCount(Context context, String bookName) {
151         String json
152                 = getSharedPreferencesString(context,
153                 bookName + BOOK_STATE,
154                 null);
155         if (json != null) {
156             try {
157                 JSONObject jsonObject = new JSONObject(json);
158                 return jsonObject.getInt(VIEWPAGER_COUNT);
159             } catch (JSONException e) {
160                 Log.e(TAG, e.getMessage());
161                 return 0;
162             }
163         }
164         return 0;
165     }
166
167     public static int getPreviousBookStateWebViewPosition(Context context, String bookTitle) {
168         String json = getSharedPreferencesString(context, bookTitle + BOOK_STATE, null);
169         if (json != null) {
170             try {
171                 JSONObject jsonObject = new JSONObject(json);
172                 return jsonObject.getInt(WEBVIEW_SCROLL_POSITION);
173             } catch (JSONException e) {
174                 Log.e(TAG, e.getMessage());
175                 return 0;
176             }
177         }
178         return 0;
179     }
180
181
182     public static void saveConfig(Context context, Config config) {
183         JSONObject obj = new JSONObject();
184         try {
185             obj.put(Config.CONFIG_FONT, config.getFont());
186             obj.put(Config.CONFIG_FONT_SIZE, config.getFontSize());
187             obj.put(Config.CONFIG_MARGIN_SIZE, config.getMarginSize());
188             obj.put(Config.CONFIG_INTERLINE_SIZE, config.getInterlineSize());
189             obj.put(Config.CONFIG_IS_NIGHTMODE, config.isNightMode());
190             obj.put(Config.CONFIG_THEMECOLOR, config.getThemeColor());
191             obj.put(Config.CONFIG_IS_TTS,config.isShowTts());
192             obj.put(Config.CONFIG_ICON_COLOR,config.getIconColor());
193             obj.put(Config.CONFIG_TOOLBAR_COLOR,config.getToolbarColor());
194             SharedPreferenceUtil.
195                     putSharedPreferencesString(
196                             context, Config.INTENT_CONFIG, obj.toString());
197         } catch (JSONException e) {
198             Log.e(TAG, e.getMessage(), e);
199         }
200     }
201
202     public static Config getSavedConfig(Context context) {
203         String json = getSharedPreferencesString(context, Config.INTENT_CONFIG, null);
204         if (json != null) {
205             try {
206                 JSONObject jsonObject = new JSONObject(json);
207                 return new Config(jsonObject);
208             } catch (JSONException e) {
209                 Log.e(TAG, e.getMessage());
210                 return null;
211             }
212         }
213         return null;
214     }
215 }
216
217
218
219
220
221
222