Added Android code
[wl-app.git] / Android / folioreader / src / main / java / com / folioreader / util / HighlightUtil.java
1 package com.folioreader.util;
2
3 import android.content.Context;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.support.v4.content.LocalBroadcastManager;
7 import android.util.Log;
8
9 import com.folioreader.model.HighLight;
10 import com.folioreader.model.HighlightImpl;
11 import com.folioreader.model.sqlite.HighLightTable;
12
13 import org.json.JSONException;
14 import org.json.JSONObject;
15
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.Calendar;
19 import java.util.List;
20
21 /**
22  * Created by priyank on 5/12/16.
23  */
24 public class HighlightUtil {
25
26     private static final String TAG = "HighlightUtil";
27
28     public static String createHighlightRangy(Context context,
29                                               String content,
30                                               String bookTitle,
31                                               String pageId,
32                                               int pageNo,
33                                               String oldRangy) {
34         try {
35             JSONObject jObject = new JSONObject(content);
36
37             String rangy = jObject.getString("rangy");
38             String textContent = jObject.getString("content");
39             String color = jObject.getString("color");
40
41             String rangyHighlightElement = getRangyString(rangy, oldRangy);
42
43             HighlightImpl highlightImpl = new HighlightImpl();
44             highlightImpl.setContent(textContent);
45             highlightImpl.setType(color);
46             highlightImpl.setPageNumber(pageNo);
47             highlightImpl.setBookId(bookTitle);
48             highlightImpl.setPageId(pageId);
49             highlightImpl.setRangy(rangyHighlightElement);
50             highlightImpl.setDate(Calendar.getInstance().getTime());
51             // save highlight to database
52             long id = HighLightTable.insertHighlight(highlightImpl);
53             if (id != -1) {
54                 highlightImpl.setId((int) id);
55                 sendHighlightBroadcastEvent(context, highlightImpl, HighLight.HighLightAction.NEW);
56             }
57             return rangy;
58         } catch (JSONException e) {
59             Log.e(TAG, "createHighlightRangy failed", e);
60         }
61         return "";
62     }
63
64     /**
65      * function extracts rangy element corresponding to latest highlight.
66      *
67      * @param rangy    new rangy string generated after adding new highlight.
68      * @param oldRangy rangy string before new highlight.
69      * @return rangy element corresponding to latest element.
70      */
71     private static String getRangyString(String rangy, String oldRangy) {
72         List<String> rangyList = getRangyArray(rangy);
73         for (String firs : getRangyArray(oldRangy)) {
74             if (rangyList.contains(firs)) {
75                 rangyList.remove(firs);
76             }
77         }
78         if (rangyList.size() >= 1) {
79             return rangyList.get(0);
80         } else {
81             return "";
82         }
83     }
84
85     /**
86      * function converts Rangy text into each individual element
87      * splitting with '|'.
88      *
89      * @param rangy rangy test with format: type:textContent|start$end$id$class$containerId
90      * @return ArrayList of each rangy element corresponding to each highlight
91      */
92     private static List<String> getRangyArray(String rangy) {
93         List<String> rangyElementList = new ArrayList<>();
94         rangyElementList.addAll(Arrays.asList(rangy.split("\\|")));
95         if (rangyElementList.contains("type:textContent")) {
96             rangyElementList.remove("type:textContent");
97         } else if (rangyElementList.contains("")) {
98             return new ArrayList<>();
99         }
100         return rangyElementList;
101     }
102
103     public static String generateRangyString(String pageId) {
104         List<String> rangyList = HighLightTable.getHighlightsForPageId(pageId);
105         StringBuilder builder = new StringBuilder();
106         if (!rangyList.isEmpty()) {
107             builder.append("type:textContent");
108             for (String rangy : rangyList) {
109                 builder.append('|');
110                 builder.append(rangy);
111             }
112         }
113         return builder.toString();
114     }
115
116     public static void sendHighlightBroadcastEvent(Context context,
117                                                    HighlightImpl highlightImpl,
118                                                    HighLight.HighLightAction action) {
119         LocalBroadcastManager.getInstance(context).sendBroadcast(
120                 getHighlightBroadcastIntent(highlightImpl, action));
121     }
122
123     public static Intent getHighlightBroadcastIntent(HighlightImpl highlightImpl,
124                                                      HighLight.HighLightAction modify) {
125         Bundle bundle = new Bundle();
126         bundle.putParcelable(HighlightImpl.INTENT, highlightImpl);
127         bundle.putSerializable(HighLight.HighLightAction.class.getName(), modify);
128         return new Intent(HighlightImpl.BROADCAST_EVENT).putExtras(bundle);
129     }
130 }