1 package com.folioreader.util;
3 import android.content.Context;
4 import android.support.v4.content.ContextCompat;
5 import android.util.Log;
7 import com.folioreader.Config;
8 import com.folioreader.Constants;
10 import org.json.JSONArray;
11 import org.json.JSONException;
12 import org.json.JSONObject;
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;
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;
30 * Created by mahavir on 5/7/16.
32 public class AppUtil {
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";
38 private enum FileType {
44 public static Map<String, String> toMap(String jsonString) {
45 Map<String, String> map = new HashMap<String, String>();
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();
53 value = jObject.get(key);
55 if(value instanceof JSONObject) {
56 value = toMap(value.toString());
58 map.put(key, value.toString());
60 } catch (JSONException e) {
61 Log.e(TAG, "toMap failed", e);
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;
72 for (String value : values) {
75 if (value.toLowerCase().startsWith("charset=")) {
76 charset = value.substring("charset=".length());
81 if (charset == null || charset.isEmpty()) {
82 charset = "UTF-8"; //Assumption
88 public static String formatDate(Date hightlightDate) {
89 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DATE_FORMAT, Locale.getDefault());
90 return simpleDateFormat.format(hightlightDate);
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();
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());
110 public static void removeBookState(Context context, String bookTitle) {
111 SharedPreferenceUtil.removeSharedPreferencesKey(context, bookTitle + BOOK_STATE);
114 public static boolean checkPreviousBookStateExist(Context context, String bookName) {
116 = getSharedPreferencesString(
117 context, bookName + BOOK_STATE,
121 JSONObject jsonObject = new JSONObject(json);
122 String bookTitle = jsonObject.getString(BOOK_TITLE);
123 if (bookTitle.equals(bookName))
125 } catch (JSONException e) {
126 Log.e(TAG, e.getMessage());
133 public static int getPreviousBookStatePosition(Context context, String bookName) {
135 = getSharedPreferencesString(context,
136 bookName + BOOK_STATE,
140 JSONObject jsonObject = new JSONObject(json);
141 return jsonObject.getInt(VIEWPAGER_POSITION);
142 } catch (JSONException e) {
143 Log.e(TAG, e.getMessage());
150 public static int getPreviousBookStateCount(Context context, String bookName) {
152 = getSharedPreferencesString(context,
153 bookName + BOOK_STATE,
157 JSONObject jsonObject = new JSONObject(json);
158 return jsonObject.getInt(VIEWPAGER_COUNT);
159 } catch (JSONException e) {
160 Log.e(TAG, e.getMessage());
167 public static int getPreviousBookStateWebViewPosition(Context context, String bookTitle) {
168 String json = getSharedPreferencesString(context, bookTitle + BOOK_STATE, null);
171 JSONObject jsonObject = new JSONObject(json);
172 return jsonObject.getInt(WEBVIEW_SCROLL_POSITION);
173 } catch (JSONException e) {
174 Log.e(TAG, e.getMessage());
182 public static void saveConfig(Context context, Config config) {
183 JSONObject obj = new JSONObject();
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);
202 public static Config getSavedConfig(Context context) {
203 String json = getSharedPreferencesString(context, Config.INTENT_CONFIG, null);
206 JSONObject jsonObject = new JSONObject(json);
207 return new Config(jsonObject);
208 } catch (JSONException e) {
209 Log.e(TAG, e.getMessage());