Example #1
0
        public AnnTask(Context activity, int task, int annoMode, int curPos, long startPos, long endPos, int curWidth, int splitLineIndex, List <List <object> > lines, List <object> curLine, string pastedText, long textLen, RandomAccessFile openedFile, LineView testView, IAnnTask inter, bool formatting, List <Bookmark> bookmarks) : base()
        {
            this.status = Status.Running;

            this.Activity       = activity;
            this.task           = task;
            this.annoMode       = annoMode;
            this.curPos         = curPos;
            this.startPos       = startPos;
            this.endPos         = endPos;
            this.curWidth       = curWidth;
            this.splitLineIndex = splitLineIndex;
            this.lines          = lines;
            this.curLine        = curLine;
            this.PastedText     = pastedText;
            this.textLen        = textLen;
            this.OpenedFile     = openedFile;
            TestView            = testView;
            this.inter          = inter;
            Formatting          = formatting;
            this.Bookmarks      = bookmarks;
            this.FoundBookmarks = new List <Bookmark>();
            curFilePos          = -1;

            Scheduler = TaskScheduler.FromCurrentSynchronizationContext();

            UpdateVars(activity);
        }
Example #2
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            System.Console.WriteLine("AnnotationActivity >> OnCreate");

            app = this;

            try
            {
                if (FindViewById(Resource.Id.lines) != null)
                {
                    AnnTask.UpdateVars(this);
                    CheckIsShared();

                    return;
                }

                curFilePath           = "";
                isFirstAnnotation     = true;
                isFirstFileAnnotation = true;

                PreferenceManager.SetDefaultValues(this, Resource.Xml.preferences, false);
                sharedPrefs = PreferenceManager.GetDefaultSharedPreferences(this);

                SetContentView(Resource.Layout.MainActivity);
                mToolbar = (Toolbar)FindViewById(Resource.Id.toolbar); // Attaching the layout to the toolbar object
                SetSupportActionBar(mToolbar);
                SupportActionBar.SetDisplayShowHomeEnabled(true);
                SupportActionBar.SetIcon(Resource.Drawable.logo_toolbar);

                wPopup   = new WordPopup(this);
                testView = new LineView(this);
                testView.UpdateVars();

                lines             = new List <List <object> >();
                linesRecyclerView = (CustomRecyclerView)FindViewById(Resource.Id.lines);
                linesRecyclerView.mMainActivity = this;
                linesLayoutManager = new LinearLayoutManager(this);
                linesRecyclerView.SetLayoutManager(linesLayoutManager);
                linesAdapter = new AnnListAdapter(this, this.lines, linesRecyclerView, wPopup);
                linesRecyclerView.SetAdapter(linesAdapter);
                defaultItemAnimator = linesRecyclerView.GetItemAnimator();

                linesRecyclerView.AddOnScrollListener(new RecyclerViewOnScrollListener(this));

                mBookmarks      = new List <Bookmark>();
                mFoundBookmarks = new List <Bookmark>();
            }
            catch (Exception e)
            {
                Toast.MakeText(this, "Error: " + e.Message, ToastLength.Long).Show();
            }
        }
Example #3
0
            public void OnClick(View view)
            {
                LineView lv = (LineView)view;

                int[] touchedData = new int[2];
                lv.GetTouchedWord(touchedData);
                int lineNum = adapter.context.lines.IndexOf(lv.line);

                if (touchedData[0] == -1 || lv.line[(touchedData[0])] is string || adapter.wPopup.Showing && adapter.context.hlIndex.Y == lineNum && adapter.context.hlIndex.X == touchedData[0])
                {
                    adapter.wPopup.dismiss();
                }
                else
                {
                    adapter.wPopup.show(view, lv.line, touchedData[0], touchedData[1], true);
                    adapter.context.hlIndex.Y = lineNum;
                    adapter.context.hlIndex.X = touchedData[0];
                    lv.Invalidate();
                }
            }
Example #4
0
        public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            RecyclerView.ViewHolder vh = null;

            if (viewType == VIEW_ITEM)
            {
                LineView v = new LineView(this.context);

                vh = new LineViewHolder(this, v);
            }
            else if (viewType == VIEW_HEADER)
            {
                View v = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.ListHeader, parent, false);

                vh = new ProgressViewHolder(this, v);
            }
            else if (viewType == VIEW_FOOTER)
            {
                View v = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.ListFooter, parent, false);

                vh = new ProgressViewHolder(this, v);
            }
            return(vh);
        }
Example #5
0
            public void OnCompleted(int task, int splitLineIndex, string pastedText, List <List <object> > tempLines, int curPos, long tempStartPos, long tempEndPos, bool isRemaining, List <Bookmark> foundBookmarks)
            {
                StringBuilder pinyin = new StringBuilder("");

                RemoteViews smallView = null;

                if (Build.VERSION.SdkInt < BuildVersionCodes.JellyBean)
                {
                    foreach (List <object> line in tempLines)
                    {
                        object lastWord = null;

                        foreach (object word in line)
                        {
                            if (lastWord != null && (lastWord is int? || lastWord.GetType() != word.GetType()))
                            {
                                pinyin.Append(" ");
                            }

                            if (word is string)
                            {
                                pinyin.Append((string)word);
                            }
                            else
                            {
                                pinyin.Append(Dict.PinyinToTones(Dict.GetPinyin((int)word)));
                            }

                            lastWord = word;
                        }
                    }

                    smallView = new RemoteViews(Service.PackageName, Resource.Layout.Notification_Small);
                    smallView.SetTextViewText(Resource.Id.notifsmall_text, pinyin);
                }
                else
                {
                    smallView = new RemoteViews(Service.PackageName, Resource.Layout.Notification_Big);
                }

                Service.dict = null;

                NotificationCompat.Builder mBuilder = (new NotificationCompat.Builder(Service.ApplicationContext))
                                                      .SetSmallIcon(Resource.Drawable.notification)
                                                      .SetContentTitle("ChineseReader")
                                                      .SetContentText(pinyin)
                                                      .SetContent(smallView)
                                                      .SetPriority(NotificationCompat.PriorityMax)
                                                      .SetVibrate(new long[0]);


                Intent resultIntent = new Intent(Service.Application, typeof(MainActivity));

                resultIntent.SetAction(Intent.ActionSend);
                resultIntent.SetType("text/plain");
                resultIntent.PutExtra(Intent.ExtraText, pastedText);

                // The stack builder object will contain an artificial back stack for the
                // started Activity.
                // This ensures that navigating backward from the Activity leads out of
                // your application to the Home screen.
                global::Android.Support.V4.App.TaskStackBuilder stackBuilder = global::Android.Support.V4.App.TaskStackBuilder.Create(Service.ApplicationContext);

                // Adds the back stack for the Intent (but not the Intent itself)
                stackBuilder.AddParentStack(Class.FromType(typeof(MainActivity)));

                // Adds the Intent that starts the Activity to the top of the stack
                stackBuilder.AddNextIntent(resultIntent);
                PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);

                mBuilder.SetContentIntent(resultPendingIntent);
                NotificationManager mNotificationManager = (NotificationManager)Service.GetSystemService(Context.NotificationService);

                mNotificationManager.CancelAll();

                // mId allows you to update the notification_big later on.
                Notification notif = mBuilder.Build();

                if (Build.VERSION.SdkInt >= BuildVersionCodes.JellyBean)
                {
                    LineView lv = new LineView(Service.context);
                    lv.line         = tempLines[0];
                    lv.lines        = tempLines;
                    lv.hlIndex      = new Point(-1, -1);
                    lv.top          = new List <string>();
                    lv.bottom       = new List <string>();
                    lv.tones        = new List <int>();
                    lv.charTypeface = Typeface.Default;

                    int wordHeight = (int)(lv.WordHeight);
                    int lineCount  = (int)System.Math.Min(tempLines.Count, System.Math.Floor(256 * lv.scale / wordHeight) + 1);
                    int width      = (int)System.Math.Round((double)AnnTask.screenWidth);
                    lv.Measure(width, wordHeight);
                    lv.Layout(0, 0, width, wordHeight);
                    Bitmap bitmap  = Bitmap.CreateBitmap(width, (int)System.Math.Min(System.Math.Max(64 * lv.scale, wordHeight * lineCount), 256 * lv.scale), Bitmap.Config.Argb8888);
                    Canvas canvas  = new Canvas(bitmap);
                    Paint  whiteBg = new Paint();
                    whiteBg.Color = Color.ParseColor("FFFFFFFF");
                    canvas.DrawRect(0, 0, canvas.Width, canvas.Height, whiteBg);

                    for (int i = 0; i < lineCount; i++)
                    {
                        lv.lines = tempLines;
                        lv.line  = tempLines[i];
                        lv.bottom.Clear();
                        lv.top.Clear();
                        lv.tones.Clear();
                        int count = lv.lines[i].Count;

                        if (count == 0 || lv.line[count - 1] is string && ((string)lv.line[count - 1]).Length == 0 || tempEndPos >= pastedText.Length && i == tempLines.Count - 1)
                        {
                            lv.lastLine = true;
                        }
                        else
                        {
                            lv.lastLine = false;
                        }

                        for (int j = 0; j < count; j++)
                        {
                            object word = lv.lines[i][j];

                            if (word is string)
                            {
                                lv.bottom.Add((string)word);
                                lv.top.Add("");
                                lv.tones.Add(0);
                            }
                            else
                            {
                                int    entry = (int)word;
                                string key   = Dict.GetCh(entry);
                                lv.bottom.Add(key);
                                if (Service.sharedPrefs.GetString("pref_pinyinType", "marks").Equals("none"))
                                {
                                    lv.top.Add("");
                                }
                                else
                                {
                                    lv.top.Add(Dict.PinyinToTones(Dict.GetPinyin(entry)));
                                }

                                if (Service.sharedPrefs.GetString("pref_toneColors", "none").Equals("none"))
                                {
                                    lv.tones.Add(0);
                                }
                                else
                                {
                                    int tones        = int.Parse(Regex.Replace(Dict.GetPinyin(entry), @"[\\D]", ""));
                                    int reverseTones = 0;
                                    while (tones != 0)
                                    {
                                        reverseTones = reverseTones * 10 + tones % 10;
                                        tones        = tones / 10;
                                    }
                                    lv.tones.Add(reverseTones);
                                }
                            }
                        }

                        lv.Draw(canvas);
                        canvas.Translate(0, wordHeight);
                    }

                    RemoteViews bigView = new RemoteViews(Service.PackageName, Resource.Layout.Notification_Big);
                    bigView.SetImageViewBitmap(Resource.Id.notif_img, bitmap);
                    smallView.SetImageViewBitmap(Resource.Id.notif_img, Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, (int)(64 * lv.scale)));
                    notif.BigContentView = bigView;
                }

                mNotificationManager.Notify(NOTIFICATION_ID, notif);
            }
Example #6
0
        public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            if (holder is LineViewHolder)
            {
                List <object> line = mLines[position - 1];

                LineView curView = ((LineViewHolder)holder).mLineView;
                curView.line    = line;
                curView.lines   = mLines;
                curView.hlIndex = context.hlIndex;
                curView.bookmarks.Clear();

                if (context.mFoundBookmarks.Count > 0)
                {
                    for (int i = 0; i < line.Count; i++)
                    {
                        Bookmark itsBookmark = Bookmark.SearchAnnotated(position - 1, i, context.mFoundBookmarks);
                        if (itsBookmark != null)
                        {
                            curView.bookmarks.Add(itsBookmark);
                        }
                        else
                        {
                            curView.bookmarks.Add(null);
                        }
                    }
                }

                if (curView.top == null)
                {
                    curView.top = new List <string>();
                }
                else
                {
                    curView.top.Clear();
                }

                if (curView.bottom == null)
                {
                    curView.bottom = new List <string>();
                }
                else
                {
                    curView.bottom.Clear();
                }

                if (curView.tones == null)
                {
                    curView.tones = new List <int>();
                }
                else
                {
                    curView.tones.Clear();
                }

                int count = line.Count;
                for (int i = 0; i < count; i++)
                {
                    var word = line[i];

                    if (word is string)
                    {
                        curView.bottom.Add((string)word);
                        curView.top.Add("");
                        curView.tones.Add(0);
                    }
                    else
                    {
                        int    entry = (int)word;
                        string key   = Dict.GetCh(entry);
                        curView.bottom.Add(key);
                        if (AnnotationActivity.sharedPrefs.GetString("pref_pinyinType", "marks").Equals("none"))
                        {
                            curView.top.Add("");
                        }
                        else
                        {
                            curView.top.Add(Dict.PinyinToTones(Dict.GetPinyin(entry)));
                        }

                        if (AnnotationActivity.sharedPrefs.GetString("pref_toneColors", "none").Equals("none"))
                        {
                            curView.tones.Add(0);
                        }
                        else
                        {
                            int tones        = int.Parse(Regex.Replace(Dict.GetPinyin(entry), @"[\d]", ""));
                            int reverseTones = 0;
                            while (tones != 0)
                            {
                                reverseTones = reverseTones * 10 + tones % 10;
                                tones        = tones / 10;
                            }
                            curView.tones.Add(reverseTones);
                        }
                    }
                }

                if (count == 0 || line[count - 1] is string && ((string)line[count - 1]).Length == 0 || context.endPos >= context.textLen && position - 1 == ItemCount - 3)
                {
                    curView.lastLine = true;
                }
                else
                {
                    curView.lastLine = false;
                }

                curView.UpdateVars();
            }
            else
            {
                if (position == 0 && ShowHeader || position == mLines.Count + 1 && ShowFooter)
                {
                    ((ProgressViewHolder)holder).progressBar.Visibility = ViewStates.Visible;
                }
                else
                {
                    ((ProgressViewHolder)holder).progressBar.Visibility = ViewStates.Gone;
                }
            }
        }
Example #7
0
 public LineViewHolder(AnnListAdapter adapter, View v) : base(v)
 {
     this.adapter = adapter;
     mLineView    = (LineView)v;
     mLineView.SetOnClickListener(this);
 }
Example #8
0
 public ViewHolder(LineView v) : base(v)
 {
     mLineView = v;
 }
Example #9
0
            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);
            }
Example #10
0
        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);
        }
Example #11
0
        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);
        }
Example #12
0
        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;
        }