1 package com.folioreader.util;
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;
9 import com.folioreader.model.HighLight;
10 import com.folioreader.model.HighlightImpl;
11 import com.folioreader.model.sqlite.HighLightTable;
13 import org.json.JSONException;
14 import org.json.JSONObject;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.Calendar;
19 import java.util.List;
22 * Created by priyank on 5/12/16.
24 public class HighlightUtil {
26 private static final String TAG = "HighlightUtil";
28 public static String createHighlightRangy(Context context,
35 JSONObject jObject = new JSONObject(content);
37 String rangy = jObject.getString("rangy");
38 String textContent = jObject.getString("content");
39 String color = jObject.getString("color");
41 String rangyHighlightElement = getRangyString(rangy, oldRangy);
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);
54 highlightImpl.setId((int) id);
55 sendHighlightBroadcastEvent(context, highlightImpl, HighLight.HighLightAction.NEW);
58 } catch (JSONException e) {
59 Log.e(TAG, "createHighlightRangy failed", e);
65 * function extracts rangy element corresponding to latest highlight.
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.
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);
78 if (rangyList.size() >= 1) {
79 return rangyList.get(0);
86 * function converts Rangy text into each individual element
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
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<>();
100 return rangyElementList;
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) {
110 builder.append(rangy);
113 return builder.toString();
116 public static void sendHighlightBroadcastEvent(Context context,
117 HighlightImpl highlightImpl,
118 HighLight.HighLightAction action) {
119 LocalBroadcastManager.getInstance(context).sendBroadcast(
120 getHighlightBroadcastIntent(highlightImpl, action));
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);