public override void OnClick(View view)
            {
                int wordNum = Dict.BinarySearch(link, false);

                if (wordNum != -1)
                {
                    if (outerInstance.history.Count > 0)
                    {
                        outerInstance.history.Add(outerInstance.entry);
                    }
                    else
                    {
                        outerInstance.history.Add(outerInstance.wordIndex);
                    }
                    outerInstance.show(outerInstance.parent, null, wordNum, outerInstance.showX, false);
                }
                else
                {
                    Toast.MakeText(outerInstance.mContext, "The word is not in the dictionary", ToastLength.Long).Show();
                }
            }
        public static List <object> BreakWord(string theText)
        {
            int           textLen = theText.Length, curPos = 0, last;
            List <object> words = new List <object>();

            while (curPos < textLen)
            {
                int i = Math.Min(textLen - curPos - 1, 3);

                if (curPos == 0)
                {
                    i = Math.Min(textLen - curPos - 2, 3);
                }

                last = -1;
                for (; i >= 0; i--)
                {
                    if (i == 3 && curPos > 0)
                    {
                        last = Dict.BinarySearch(theText.SubstringSpecial(curPos, curPos + i + 1), true);
                    }
                    else
                    {
                        if (last >= 0)
                        {
                            last = Dict.BinarySearch(theText.SubstringSpecial(curPos, curPos + i + 1), 0, last - 1, false);
                        }
                        else
                        {
                            last = Dict.BinarySearch(theText.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) > textLen - curPos)
                            { //the found may be longer than the ending
                                continue;
                            }
                            string word = theText.SubstringSpecial(curPos, curPos + Dict.GetLength(last));
                            if (Dict.Equals(last, word))
                            {
                                words.Add(last);
                                curPos += word.Length;
                                break;
                            }
                        }
                        else
                        {
                            words.Add(last);
                            curPos += i + 1;
                            break;
                        }
                    }
                }

                if (i == -1 && last < 0)
                {
                    words.Add(theText.SubstringSpecial(curPos, curPos + 1));
                    curPos++;
                }
            }

            return(words);
        }
Пример #3
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);
        }
Пример #4
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);
        }
Пример #5
0
        public virtual void StarredExportPleco()
        {
            string stars = sharedPrefs.GetString("stars", "");

            if (stars.Length < 2)
            {
                Toast.MakeText(this, "Starred list is empty. Nothing to export.", ToastLength.Long).Show();
                return;
            }

            try
            {
                File dir = new File(global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "/ChineseReader");

                dir.Mkdirs();
                System.IO.FileStream os  = new System.IO.FileStream(dir.AbsolutePath + "/chinesereader_starred.txt", System.IO.FileMode.Create, System.IO.FileAccess.Write);
                BufferedOutputStream bos = new BufferedOutputStream(os);

                StringBuilder english = new StringBuilder();

                int oldIndex = 0, nIndex;
                while ((nIndex = stars.IndexOf("\n", oldIndex)) > -1)
                {
                    english.Length = 0;

                    int entry = Dict.BinarySearch(stars.SubstringSpecial(oldIndex, nIndex), false);
                    oldIndex = nIndex + 1;

                    if (entry == -1)
                    {
                        continue;
                    }

                    english.Append(Dict.GetCh(entry)).Append("\t").Append(Regex.Replace(Dict.GetPinyin(entry), @"(\\d)", "$1 ")).Append("\t");

                    string[] parts = Regex.Split(Regex.Replace(Dict.GetEnglish(entry), @"/", "; "), @"\\$");
                    int      j     = 0;
                    foreach (string str in parts)
                    {
                        if (j++ % 2 == 1)
                        {
                            english.Append(" [ ").Append(Regex.Replace(str, @"(\\d)", "$1 ")).Append("] ");
                        }
                        else
                        {
                            english.Append(str);
                        }
                    }
                    english.Append("\n");

                    bos.Write(Encoding.UTF8.GetBytes(english.ToString()));
                }
                os.Flush();
                bos.Flush();
                bos.Close();
                os.Close();

                Toast.MakeText(this, "Successfully exported to " + dir.AbsolutePath + "/chinesereader_starred.txt", ToastLength.Long).Show();
            }
            catch (Exception e)
            {
                Toast.MakeText(this, "Could not export: " + e.Message, ToastLength.Long).Show();
            }
        }
Пример #6
0
        public void StarredExport(int exportId)
        {
            string stars = sharedPrefs.GetString("stars", "");

            if (stars == null || stars.Length < 2)
            {
                Toast.MakeText(this, "Starred list is empty. Nothing to export.", ToastLength.Long).Show();
                return;
            }

            System.Random  random = new System.Random();
            SQLiteDatabase db     = OpenOrCreateDatabase("anki.db", FileCreationMode.Private, null);

            string[] commands = Regex.Split(GetString(Resource.String.anki_scheme), ";;");
            foreach (string command in commands)
            {
                db.ExecSQL(command);
            }

            int oldIndex = 0;
            int nIndex;

            while ((nIndex = stars.IndexOf("\n", oldIndex)) > -1)
            {
                int entry = Dict.BinarySearch(stars.SubstringSpecial(oldIndex, nIndex), false);
                oldIndex = nIndex + 1;

                if (entry == -1)
                {
                    continue;
                }

                string        id = Convert.ToString(Math.Abs(random.Next())), uuid = UUID.RandomUUID().ToString().SubstringSpecial(0, 10);
                StringBuilder english = new StringBuilder();

                if (exportId == Resource.Id.menu_starred_export_pinyin)
                {
                    english.Append(Dict.PinyinToTones(Dict.GetPinyin(entry))).Append("<br/>");
                }

                english.Append(Dict.GetCh(entry)).Append("\u001F");

                if (exportId == Resource.Id.menu_starred_export)
                {
                    english.Append("[ ").Append(Dict.PinyinToTones(Dict.GetPinyin(entry))).Append(" ]<br/>");
                }

                string[] parts = Regex.Split(Regex.Replace(Dict.GetEnglish(entry), @"/", "<br/>• "), @"\\$");
                int      j     = 0;
                foreach (string str in parts)
                {
                    if (j++ % 2 == 1)
                    {
                        english.Append("<br/><br/>[ ").Append(Dict.PinyinToTones(str)).Append(" ]<br/>");
                    }
                    else
                    {
                        english.Append("• ");

                        int bracketIndex, bracketEnd = 0;
                        while ((bracketIndex = str.IndexOf("[", bracketEnd)) > -1)
                        {
                            english.Append(str, bracketEnd, bracketIndex);
                            bracketEnd = str.IndexOf("]", bracketIndex);
                            english.Append(Dict.PinyinToTones(str.SubstringSpecial(bracketIndex, bracketEnd)));
                        }
                        english.Append(str, bracketEnd, str.Length);
                    }
                }

                db.ExecSQL("insert into notes values(?, ?, 1399962367564, 1400901624, -1, '', ?, 0, 147133787, 0, '')", new Java.Lang.Object[] { id, uuid, english.ToString() });

                db.ExecSQL("insert into cards values(?, ?, 1400901287521, 0, 1400901624, -1, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, '')", new Java.Lang.Object[] { Convert.ToString(random.Next()), id });
            }
            db.Close();

            try
            {
                File dir = new File(global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "/AnkiDroid");
                if (!dir.Exists())
                {
                    dir = new File(global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "/ChineseReader");
                }

                dir.Mkdirs();
                System.IO.Stream fos = new System.IO.FileStream(dir.AbsolutePath + "/chinesereader_starred.apkg", System.IO.FileMode.Create, System.IO.FileAccess.Write);
                System.IO.Stream fiz = new System.IO.FileStream(GetDatabasePath("anki.db").Path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                ZipOutputStream  zos = new ZipOutputStream(fos);

                zos.PutNextEntry(new ZipEntry("collection.anki2"));
                byte[] buffer  = new byte[1024];
                int    readLen = 0;
                while ((readLen = fiz.Read(buffer, 0, buffer.Length)) > 0)
                {
                    zos.Write(buffer, 0, readLen);
                }
                zos.CloseEntry();

                zos.PutNextEntry(new ZipEntry("media"));
                buffer[0] = 0x7b;
                buffer[1] = 0x7d;
                zos.Write(buffer, 0, 2);
                zos.CloseEntry();
                zos.Flush();
                zos.Close();
                fiz.Close();
                fos.Flush();
                fos.Close();

                Toast.MakeText(this, "Successfully exported to " + dir.AbsolutePath + "/chinesereader_starred.apkg", ToastLength.Long).Show();
            }
            catch (Exception e)
            {
                Toast.MakeText(this, "Could not export: " + e.Message, ToastLength.Long).Show();
            }
        }