PageRenderTime 17ms CodeModel.GetById 6ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/ime/latinime/src/com/googlecode/eyesfree/inputmethod/latin/LatinIMEUtil.java

http://eyes-free.googlecode.com/
Java | 170 lines | 135 code | 11 blank | 24 comment | 25 complexity | 1b30be2c835bfbfea209d391192dc575 MD5 | raw file
  1/*
  2 * Copyright (C) 2010 The Android Open Source Project
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 *      http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package com.googlecode.eyesfree.inputmethod.latin;
 18
 19import android.content.Context;
 20import android.os.AsyncTask;
 21import android.text.format.DateUtils;
 22import android.util.Log;
 23import android.view.inputmethod.InputMethodManager;
 24
 25public class LatinIMEUtil {
 26
 27    /**
 28     * Cancel an {@link AsyncTask}.
 29     *
 30     * @param mayInterruptIfRunning <tt>true</tt> if the thread executing this
 31     *        task should be interrupted; otherwise, in-progress tasks are allowed
 32     *        to complete.
 33     */
 34    public static void cancelTask(AsyncTask<?, ?, ?> task, boolean mayInterruptIfRunning) {
 35        if (task != null && task.getStatus() != AsyncTask.Status.FINISHED) {
 36            task.cancel(mayInterruptIfRunning);
 37        }
 38    }
 39
 40    public static class GCUtils {
 41        private static final String TAG = "GCUtils";
 42        public static final int GC_TRY_COUNT = 2;
 43        // GC_TRY_LOOP_MAX is used for the hard limit of GC wait,
 44        // GC_TRY_LOOP_MAX should be greater than GC_TRY_COUNT.
 45        public static final int GC_TRY_LOOP_MAX = 5;
 46        private static final long GC_INTERVAL = DateUtils.SECOND_IN_MILLIS;
 47        private static GCUtils sInstance = new GCUtils();
 48        private int mGCTryCount = 0;
 49
 50        public static GCUtils getInstance() {
 51            return sInstance;
 52        }
 53
 54        public void reset() {
 55            mGCTryCount = 0;
 56        }
 57
 58        public boolean tryGCOrWait(String metaData, Throwable t) {
 59            if (mGCTryCount == 0) {
 60                System.gc();
 61            }
 62            if (++mGCTryCount > GC_TRY_COUNT) {
 63                LatinImeLogger.logOnException(metaData, t);
 64                return false;
 65            } else {
 66                try {
 67                    Thread.sleep(GC_INTERVAL);
 68                    return true;
 69                } catch (InterruptedException e) {
 70                    Log.e(TAG, "Sleep was interrupted.");
 71                    LatinImeLogger.logOnException(metaData, t);
 72                    return false;
 73                }
 74            }
 75        }
 76    }
 77
 78    public static boolean hasMultipleEnabledIMEs(Context context) {
 79        return ((InputMethodManager) context.getSystemService(
 80                Context.INPUT_METHOD_SERVICE)).getEnabledInputMethodList().size() > 1;
 81    }
 82
 83    /* package */ static class RingCharBuffer {
 84        private static RingCharBuffer sRingCharBuffer = new RingCharBuffer();
 85        private static final char PLACEHOLDER_DELIMITER_CHAR = '\uFFFC';
 86        private static final int INVALID_COORDINATE = -2;
 87        /* package */ static final int BUFSIZE = 20;
 88        private Context mContext;
 89        private boolean mEnabled = false;
 90        private int mEnd = 0;
 91        /* package */ int mLength = 0;
 92        private char[] mCharBuf = new char[BUFSIZE];
 93        private int[] mXBuf = new int[BUFSIZE];
 94        private int[] mYBuf = new int[BUFSIZE];
 95
 96        private RingCharBuffer() {
 97        }
 98        public static RingCharBuffer getInstance() {
 99            return sRingCharBuffer;
100        }
101        public static RingCharBuffer init(Context context, boolean enabled) {
102            sRingCharBuffer.mContext = context;
103            sRingCharBuffer.mEnabled = enabled;
104            return sRingCharBuffer;
105        }
106        private int normalize(int in) {
107            int ret = in % BUFSIZE;
108            return ret < 0 ? ret + BUFSIZE : ret;
109        }
110        public void push(char c, int x, int y) {
111            if (!mEnabled) return;
112            mCharBuf[mEnd] = c;
113            mXBuf[mEnd] = x;
114            mYBuf[mEnd] = y;
115            mEnd = normalize(mEnd + 1);
116            if (mLength < BUFSIZE) {
117                ++mLength;
118            }
119        }
120        public char pop() {
121            if (mLength < 1) {
122                return PLACEHOLDER_DELIMITER_CHAR;
123            } else {
124                mEnd = normalize(mEnd - 1);
125                --mLength;
126                return mCharBuf[mEnd];
127            }
128        }
129        public char getLastChar() {
130            if (mLength < 1) {
131                return PLACEHOLDER_DELIMITER_CHAR;
132            } else {
133                return mCharBuf[normalize(mEnd - 1)];
134            }
135        }
136        public int getPreviousX(char c, int back) {
137            int index = normalize(mEnd - 2 - back);
138            if (mLength <= back
139                    || Character.toLowerCase(c) != Character.toLowerCase(mCharBuf[index])) {
140                return INVALID_COORDINATE;
141            } else {
142                return mXBuf[index];
143            }
144        }
145        public int getPreviousY(char c, int back) {
146            int index = normalize(mEnd - 2 - back);
147            if (mLength <= back
148                    || Character.toLowerCase(c) != Character.toLowerCase(mCharBuf[index])) {
149                return INVALID_COORDINATE;
150            } else {
151                return mYBuf[index];
152            }
153        }
154        public String getLastString() {
155            StringBuffer sb = new StringBuffer();
156            for (int i = 0; i < mLength; ++i) {
157                char c = mCharBuf[normalize(mEnd - 1 - i)];
158                if (!((LatinIME)mContext).isWordSeparator(c)) {
159                    sb.append(c);
160                } else {
161                    break;
162                }
163            }
164            return sb.reverse().toString();
165        }
166        public void reset() {
167            mLength = 0;
168        }
169    }
170}