public void OnCompleted(int task, int splitLineIndex, string pastedText, List <List <object> > tempLines, int curPos, long tempStartPos, long tempEndPos, bool isRemaining, List <Bookmark> foundBookmarks) { System.Console.WriteLine("UpdateLinesAnnTask => OnCompleted()"); if (Activity.curSaveName.Equals("") && Activity.curPos == 0) { //System.Console.WriteLine("UpdateLinesAnnTask => Step 1"); StringBuilder fName = new StringBuilder(); int p = 0, r = 0; while (fName.Length < 16 && r < tempLines.Count && (p < tempLines[r].Count || r < tempLines.Count - 1)) { var word = tempLines.ElementAt(r).ElementAt(p); if (word is int) { System.Console.WriteLine("UpdateLinesAnnTask => IS INT"); if (fName.Length > 0) { fName.Append("-"); } fName.Append(Regex.Replace(Dict.GetPinyin((int)word), @"[^a-zA-Z]+", "")); } else { System.Console.WriteLine("UpdateLinesAnnTask => IS STRING"); System.Console.WriteLine("word => " + (string)word); string s = Regex.Replace((string)word, @"[^a-zA-Z0-9]+", ""); fName.Append(s.Substring(0, Math.Min(s.Length, 16))); } if (++p == tempLines[r].Count) { p = 0; r++; } } Activity.curSaveName = fName.ToString(); } Activity.curPos = curPos; Activity.linesRecyclerView.SetItemAnimator(Activity.defaultItemAnimator); switch (task) { case AnnTask.TASK_ANNOTATE: case AnnTask.TASK_SPLIT: if (isRemaining) { Activity.linesAdapter.ShowFooter = true; } else { Activity.linesAdapter.ShowFooter = false; } int firstVisiblePosition = Activity.linesLayoutManager.FindFirstVisibleItemPosition(); View firstVisible = Activity.linesLayoutManager.FindViewByPosition(firstVisiblePosition); int top = firstVisible != null ? firstVisible.Top : 0; if (task == AnnTask.TASK_SPLIT) { Activity.linesRecyclerView.SetItemAnimator(null); int toRemove = Activity.lines.Count - splitLineIndex; while (toRemove-- > 0) { Activity.lines.RemoveAt(splitLineIndex); Activity.linesAdapter.NotifyItemRemoved(splitLineIndex + 1); } if (Activity.annoMode == ANNOTATE_FILE && Activity.mBookmarks.Count > 0) { int bookmarksRemoveFrom = Bookmark.SearchClosest(Activity.endPos, Activity.mFoundBookmarks); while (Activity.mFoundBookmarks.Count > bookmarksRemoveFrom) { Activity.mFoundBookmarks.RemoveAt(bookmarksRemoveFrom); } } } int rmCount = -1; if (Activity.annoMode == ANNOTATE_FILE && !Activity.isFirstFileAnnotation && firstVisiblePosition > AnnTask.visibleLines) { rmCount = firstVisiblePosition - AnnTask.visibleLines; tempStartPos = Activity.GetPosition(Activity.lines, rmCount + 1, 0, true); for (int i = 0; i < rmCount; i++) { Activity.lines.RemoveAt(0); Activity.linesAdapter.NotifyItemRemoved(1); } int bookmarksRemoveUntil = Bookmark.SearchClosest(tempStartPos, Activity.mFoundBookmarks); for (int i = 0; i < bookmarksRemoveUntil; i++) { Activity.mFoundBookmarks.RemoveAt(0); } for (int i = 0; i < Activity.mFoundBookmarks.Count; i++) { Activity.mFoundBookmarks[i].mLine -= rmCount; } } for (int i = 0; i < foundBookmarks.Count; i++) { foundBookmarks[i].mLine += Activity.lines.Count; } Activity.mFoundBookmarks.AddRange(foundBookmarks); Activity.lines.AddRange(tempLines); Activity.linesAdapter.NotifyItemRangeInserted(Activity.lines.Count - tempLines.Count + 1, tempLines.Count); if (tempStartPos > 0) { Activity.linesAdapter.ShowHeader = true; if (Activity.isFirstFileAnnotation) { Activity.linesLayoutManager.ScrollToPositionWithOffset(1, 0); } } else { Activity.linesAdapter.ShowHeader = false; } if (rmCount > -1) { Activity.linesLayoutManager.ScrollToPositionWithOffset(AnnTask.visibleLines, top); } tempLines.Clear(); break; case AnnTask.TASK_ANNOTATE_BACK: int remainCount = Activity.linesLayoutManager.FindFirstVisibleItemPosition() + AnnTask.visibleLines * 2; if (Activity.lines.Count > remainCount) { rmCount = Activity.lines.Count - remainCount; for (int i = 0; i < rmCount; i++) { List <object> rmLine = Activity.lines[remainCount]; Activity.lines.RemoveAt(remainCount); Activity.linesAdapter.NotifyItemRemoved(remainCount + 1); tempEndPos -= LineView.GetLineSize(rmLine, Activity.annoMode == ANNOTATE_FILE); } if (Activity.annoMode == ANNOTATE_FILE && Activity.mFoundBookmarks.Count > 0) { int bookmarksRemoveFrom = Bookmark.SearchClosest(tempEndPos, Activity.mFoundBookmarks); while (Activity.mFoundBookmarks.Count > bookmarksRemoveFrom) { Activity.mFoundBookmarks.RemoveAt(bookmarksRemoveFrom); } } } firstVisiblePosition = Activity.linesLayoutManager.FindFirstVisibleItemPosition(); int newFirstVisiblePosition = firstVisiblePosition + tempLines.Count; top = Activity.linesLayoutManager.FindViewByPosition(firstVisiblePosition).Top + Activity.linesLayoutManager.FindViewByPosition(firstVisiblePosition).Height - Activity.linesLayoutManager.FindViewByPosition(firstVisiblePosition + 1).Height; if (tempStartPos == 0) { Activity.linesAdapter.ShowHeader = false; } for (int i = 0; i < Activity.mFoundBookmarks.Count; i++) { Activity.mFoundBookmarks[i].mLine += tempLines.Count; } Activity.mFoundBookmarks.InsertRange(0, foundBookmarks); Activity.lines.InsertRange(0, tempLines); Activity.linesAdapter.NotifyItemRangeInserted(1, tempLines.Count); Activity.linesLayoutManager.ScrollToPositionWithOffset(newFirstVisiblePosition, top); tempLines.Clear(); break; } Activity.isFirstAnnotation = false; Activity.isFirstFileAnnotation = false; Activity.startPos = tempStartPos; Activity.endPos = tempEndPos; if (Activity.settingsChanged) { Activity.Redraw(); } //update header and footer Activity.linesAdapter.NotifyItemChanged(0); Activity.linesAdapter.NotifyItemChanged(Activity.linesAdapter.ItemCount - 1); }
protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] native_parms) { try { switch (task) { case TASK_SPLIT: curPos = 0; BufText = ""; bufLen = 0; notFound = 0; break; case TASK_ANNOTATE: case TASK_ANNOTATE_BACK: curPos = 0; BufText = ""; bufLen = 0; curLine = new List <object>(); hMargin = TestView != null ? TestView.hMargin : 0; curWidth = hMargin; notFound = 0; break; } tempEndPos = endPos; tempStartPos = startPos; mHopelessBreak = false; while ((task == TASK_ANNOTATE || task == TASK_SPLIT) && (curPos < bufLen || curPos == bufLen && tempEndPos < textLen) && (tempLines.Count < visibleLines * 2 || (!Formatting && tempEndPos - endPos < 500)) || task == TASK_ANNOTATE_BACK && (curPos < bufLen || curPos == bufLen && tempStartPos > 0)) { if ((task == TASK_ANNOTATE || task == TASK_SPLIT) && bufLen - curPos < 18 && tempEndPos < textLen) { if (notFound > 0) { curLine = AddNotFound(notFound, curLine); notFound = 0; } BufText = NextBuffer; bufLen = BufText.Length; } else if (task == TASK_ANNOTATE_BACK && curPos == bufLen) { if (notFound > 0) { curLine = AddNotFound(notFound, curLine); notFound = 0; } if (curLine.Count > 0) { tempLines.Add(curLine); curLine = new List <object>(); } tempBackLines.InsertRange(0, tempLines); tempLines.Clear(); if (tempBackLines.Count < visibleLines * 2) { BufText = PrevBuffer; bufLen = BufText.Length; } else { break; } } if (BufText[curPos] < '\u25CB' || BufText[curPos] > '\u9FA5') { if (CheckCancelled()) { //return (-1); return(null); } notFound++; if (BufText[curPos] == ' ' && notFound > 1) { curPos++; curLine = AddNotFound(notFound, curLine); notFound = 0; if (curFilePos != -1) { curFilePos++; } continue; } if (notFound > perLine * visibleLines * 2 && task == TASK_ANNOTATE) { notFound--; break; } if (curFilePos != -1) { curFilePos += Encoding.UTF8.GetBytes(BufText.SubstringSpecial(curPos, curPos + 1)).Length; } curPos++; continue; } if (notFound > 0) { curLine = AddNotFound(notFound, curLine); notFound = 0; } int last = -1; int i = 3; for (; i >= 0; i--) { int j = 1; for (; j <= i && curPos + j < bufLen; j++) { if (BufText[curPos + j] < '\u25CB' || BufText[curPos + j] > '\u9FA5') { break; } } if (j == i + 1) { if (i == 3) { last = Dict.BinarySearch(BufText.SubstringSpecial(curPos, curPos + i + 1), true); } else { if (last >= 0) { last = Dict.BinarySearch(BufText.SubstringSpecial(curPos, curPos + i + 1), 0, last - 1, false); } else { last = Dict.BinarySearch(BufText.SubstringSpecial(curPos, curPos + i + 1), false); } } if (last >= 0) { if (i == 3) { //the found entry may be longer than 4 (3 + 1) if (Dict.GetLength(last) > bufLen - curPos) { //the found may be longer than the ending continue; } string word = BufText.SubstringSpecial(curPos, curPos + Dict.GetLength(last)); if (Dict.Equals(last, word)) { curLine = AddWord(last, curLine); Bookmark bookmark = Bookmark.Search(curFilePos, Bookmarks); if (bookmark != null) { bookmark.SetAnnotatedPosition(tempLines.Count, curLine.Count - 1); FoundBookmarks.Add(bookmark); } if (CheckCancelled()) { return(null); //return (-1); } if (curFilePos != -1) { curFilePos += word.Length * 3; } curPos += word.Length; break; } } else { curLine = AddWord(last, curLine); Bookmark bookmark = Bookmark.Search(curFilePos, Bookmarks); if (bookmark != null) { bookmark.SetAnnotatedPosition(tempLines.Count, curLine.Count - 1); FoundBookmarks.Add(bookmark); } if (CheckCancelled()) { //return (-1); return(null); } if (curFilePos != -1) { curFilePos += (i + 1) * 3; } curPos += i + 1; break; } } } } if (i < 0) { notFound++; if (curFilePos != -1) { curFilePos += Encoding.UTF8.GetBytes(BufText.SubstringSpecial(curPos, curPos + 1)).Length; } curPos++; } } if (notFound > 0) { curLine = AddNotFound(notFound, curLine); notFound = 0; } if (curLine.Count > 0) { if (task == TASK_ANNOTATE_BACK || tempEndPos == textLen && curPos == bufLen || tempLines.Count == 0) { //back annotation or end of text or 1-line text tempLines.Add(curLine); curLine = new List <object>(); } else { curPos -= (int)LineView.GetLineSize(curLine, false); } } if (task == TASK_ANNOTATE || task == TASK_SPLIT) { if (annoMode == AnnotationActivity.ANNOTATE_FILE) { tempEndPos -= Encoding.UTF8.GetBytes(BufText.Substring(curPos)).Length; } else { tempEndPos -= bufLen - curPos; } } if (task == TASK_ANNOTATE_BACK) { tempBackLines.InsertRange(0, tempLines); tempLines = tempBackLines; } return(task); } catch (Java.Lang.Exception e) { status = Status.Finished; System.Console.WriteLine("Annotation DoInBackground ERROR => " + e.Message); Log.Equals("ChineseReader", "Annotation error"); } return(task); }
private Task <object> CreateTask() { CancelToken = new CancellationTokenSource(); var t = new Task <object>(() => { try { //System.Console.WriteLine("STEP 1"); switch (task) { case TASK_SPLIT: curPos = 0; BufText = ""; bufLen = 0; notFound = 0; break; case TASK_ANNOTATE: case TASK_ANNOTATE_BACK: curPos = 0; BufText = ""; bufLen = 0; curLine = new List <object>(); hMargin = TestView != null ? TestView.hMargin : 0; curWidth = hMargin; notFound = 0; break; } tempEndPos = endPos; tempStartPos = startPos; mHopelessBreak = false; //System.Console.WriteLine("STEP 2"); while ((task == TASK_ANNOTATE || task == TASK_SPLIT) && (curPos < bufLen || curPos == bufLen && tempEndPos < textLen) && (tempLines.Count < visibleLines * 2 || (!Formatting && tempEndPos - endPos < 500)) || task == TASK_ANNOTATE_BACK && (curPos < bufLen || curPos == bufLen && tempStartPos > 0)) { //System.Console.WriteLine("STEP 3"); if ((task == TASK_ANNOTATE || task == TASK_SPLIT) && bufLen - curPos < 18 && tempEndPos < textLen) { if (notFound > 0) { curLine = AddNotFound(notFound, curLine); notFound = 0; } BufText = NextBuffer; bufLen = BufText.Length; } else if (task == TASK_ANNOTATE_BACK && curPos == bufLen) { if (notFound > 0) { curLine = AddNotFound(notFound, curLine); notFound = 0; } if (curLine.Count > 0) { tempLines.Add(curLine); curLine = new List <object>(); } tempBackLines.InsertRange(0, tempLines); tempLines.Clear(); if (tempBackLines.Count < visibleLines * 2) { BufText = PrevBuffer; bufLen = BufText.Length; } else { break; } } //System.Console.WriteLine("STEP 4"); if (BufText[curPos] < '\u25CB' || BufText[curPos] > '\u9FA5') { if (CheckCancelled()) { //return (-1); return(null); } notFound++; if (BufText[curPos] == ' ' && notFound > 1) { curPos++; curLine = AddNotFound(notFound, curLine); notFound = 0; if (curFilePos != -1) { curFilePos++; } continue; } if (notFound > perLine * visibleLines * 2 && task == TASK_ANNOTATE) { notFound--; break; } if (curFilePos != -1) { curFilePos += Encoding.UTF8.GetBytes(BufText.SubstringSpecial(curPos, curPos + 1)).Length; } curPos++; continue; } //System.Console.WriteLine("STEP 5"); if (notFound > 0) { curLine = AddNotFound(notFound, curLine); notFound = 0; } int last = -1; //System.Console.WriteLine("STEP 6"); int i = 3; for (; i >= 0; i--) { int j = 1; for (; j <= i && curPos + j < bufLen; j++) { if (BufText[curPos + j] < '\u25CB' || BufText[curPos + j] > '\u9FA5') { break; } } //System.Console.WriteLine("STEP 7"); if (j == i + 1) { if (i == 3) { last = Dict.BinarySearch(BufText.SubstringSpecial(curPos, curPos + i + 1), true); } else { if (last >= 0) { last = Dict.BinarySearch(BufText.SubstringSpecial(curPos, curPos + i + 1), 0, last - 1, false); } else { last = Dict.BinarySearch(BufText.SubstringSpecial(curPos, curPos + i + 1), false); } } //System.Console.WriteLine("STEP 8"); if (last >= 0) { if (i == 3) { //the found entry may be longer than 4 (3 + 1) if (Dict.GetLength(last) > bufLen - curPos) { //the found may be longer than the ending continue; } string word = BufText.SubstringSpecial(curPos, curPos + Dict.GetLength(last)); if (Dict.Equals(last, word)) { curLine = AddWord(last, curLine); Bookmark bookmark = Bookmark.Search(curFilePos, Bookmarks); if (bookmark != null) { bookmark.SetAnnotatedPosition(tempLines.Count, curLine.Count - 1); FoundBookmarks.Add(bookmark); } if (CheckCancelled()) { return(null); //return (-1); } if (curFilePos != -1) { curFilePos += word.Length * 3; } curPos += word.Length; break; } } else { curLine = AddWord(last, curLine); Bookmark bookmark = Bookmark.Search(curFilePos, Bookmarks); if (bookmark != null) { bookmark.SetAnnotatedPosition(tempLines.Count, curLine.Count - 1); FoundBookmarks.Add(bookmark); } if (CheckCancelled()) { //return (-1); return(null); } if (curFilePos != -1) { curFilePos += (i + 1) * 3; } curPos += i + 1; break; } } } } //System.Console.WriteLine("STEP 9"); if (i < 0) { notFound++; if (curFilePos != -1) { curFilePos += Encoding.UTF8.GetBytes(BufText.SubstringSpecial(curPos, curPos + 1)).Length; } curPos++; } } //System.Console.WriteLine("STEP 10"); if (notFound > 0) { curLine = AddNotFound(notFound, curLine); notFound = 0; } //System.Console.WriteLine("STEP 11"); if (curLine.Count > 0) { if (task == TASK_ANNOTATE_BACK || tempEndPos == textLen && curPos == bufLen || tempLines.Count == 0) { //back annotation or end of text or 1-line text tempLines.Add(curLine); curLine = new List <object>(); } else { curPos -= (int)LineView.GetLineSize(curLine, false); } } //System.Console.WriteLine("STEP 12"); if (task == TASK_ANNOTATE || task == TASK_SPLIT) { if (annoMode == AnnotationActivity.ANNOTATE_FILE) { tempEndPos -= Encoding.UTF8.GetBytes(BufText.Substring(curPos)).Length; } else { tempEndPos -= bufLen - curPos; } } if (task == TASK_ANNOTATE_BACK) { tempBackLines.InsertRange(0, tempLines); tempLines = tempBackLines; } return(task); } catch (Exception e) { status = Status.Finished; System.Console.WriteLine("Annotation CreateTask ERROR => " + e.Message); Log.Equals("ChineseReader", "Annotation error"); } return(task); }, CancelToken.Token, TaskCreationOptions.None); t.ContinueWith(antecendent => { status = Status.Finished; System.Console.WriteLine("C# Post Execute"); if (CheckCancelled()) { return; } inter.OnCompleted(task, splitLineIndex, PastedText, tempLines, curPos, tempStartPos, tempEndPos, curPos < bufLen || (annoMode == AnnotationActivity.ANNOTATE_FILE && tempEndPos + Encoding.UTF8.GetBytes(BufText.SubstringSpecial(0, curPos)).Length < textLen), FoundBookmarks); }, CancelToken.Token, TaskContinuationOptions.None, Scheduler); return(t); }
public void RedrawLines(RecyclerView listView) { int currentLine = Math.Max(((LinearLayoutManager)listView.GetLayoutManager()).FindFirstVisibleItemPosition() - 1, 0); int linesSize = lines.Count; for (int j = 0; j < currentLine && j < linesSize; j++) { List <object> line = lines[j]; startPos += LineView.GetLineSize(line, annoMode == AnnotationActivity.ANNOTATE_FILE); } List <object> curLine = new List <object>(); tempLines = new List <List <object> >(); curWidth = hMargin; int linesCount = lines.Count; int i = 0; for (i = currentLine; i < linesCount && tempLines.Count < visibleLines * 2; i++) { List <object> line = lines[i]; int wordCount = line.Count; for (int j = 0; j < wordCount; j++) { object word = line[j]; if (word is string && ((string)word).Length == 0) { curLine.Add(""); tempLines.Add(curLine); curLine = new List <object>(); curWidth = hMargin; } else { curLine = AddWord(word, curLine); } } } if (curLine.Count > 0 && endPos >= textLen) { tempLines.Add(curLine); } else { endPos -= LineView.GetLineSize(curLine, annoMode == AnnotationActivity.ANNOTATE_FILE); } if (i < linesCount) { for (; i < linesCount; i++) { endPos -= LineView.GetLineSize(lines[i], annoMode == AnnotationActivity.ANNOTATE_FILE); } } lines.Clear(); lines.AddRange(tempLines); status = Status.Finished; }