/tags/20110907/src/com/anysoftkeyboard/utils/IMEUtil.java

http://softkeyboard.googlecode.com/
Java | 173 lines | 134 code | 13 blank | 26 comment | 25 complexity | dc9a2fc4ccb14810da83e64f961f741f MD5 | raw file

✨ Summary
  1. /*
  2. * Copyright (C) 2011 AnySoftKeyboardt
  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. package com.anysoftkeyboard.utils;
  17. import com.anysoftkeyboard.AnySoftKeyboard;
  18. import android.view.inputmethod.InputMethodManager;
  19. import android.content.Context;
  20. import android.os.AsyncTask;
  21. import android.text.format.DateUtils;
  22. import android.util.Log;
  23. public class IMEUtil {
  24. /**
  25. * Cancel an {@link AsyncTask}.
  26. *
  27. * @param mayInterruptIfRunning <tt>true</tt> if the thread executing this
  28. * task should be interrupted; otherwise, in-progress tasks are allowed
  29. * to complete.
  30. */
  31. public static void cancelTask(AsyncTask<?, ?, ?> task, boolean mayInterruptIfRunning) {
  32. if (task != null && task.getStatus() != AsyncTask.Status.FINISHED) {
  33. task.cancel(mayInterruptIfRunning);
  34. }
  35. }
  36. public static class GCUtils {
  37. private static final String TAG = "GCUtils";
  38. public static final int GC_TRY_COUNT = 2;
  39. // GC_TRY_LOOP_MAX is used for the hard limit of GC wait,
  40. // GC_TRY_LOOP_MAX should be greater than GC_TRY_COUNT.
  41. public static final int GC_TRY_LOOP_MAX = 5;
  42. private static final long GC_INTERVAL = DateUtils.SECOND_IN_MILLIS;
  43. private static GCUtils sInstance = new GCUtils();
  44. private int mGCTryCount = 0;
  45. public static GCUtils getInstance() {
  46. return sInstance;
  47. }
  48. public void reset() {
  49. mGCTryCount = 0;
  50. }
  51. public boolean tryGCOrWait(String metaData, Throwable t) {
  52. if (mGCTryCount == 0) {
  53. System.gc();
  54. }
  55. if (++mGCTryCount > GC_TRY_COUNT) {
  56. //ImeLogger.logOnException(metaData, t);
  57. return false;
  58. } else {
  59. try {
  60. Thread.sleep(GC_INTERVAL);
  61. return true;
  62. } catch (InterruptedException e) {
  63. Log.e(TAG, "Sleep was interrupted.");
  64. //ImeLogger.logOnException(metaData, t);
  65. return false;
  66. }
  67. }
  68. }
  69. }
  70. public static boolean hasMultipleEnabledIMEs(Context context) {
  71. return ((InputMethodManager) context.getSystemService(
  72. Context.INPUT_METHOD_SERVICE)).getEnabledInputMethodList().size() > 1;
  73. }
  74. /* package */ static class RingCharBuffer {
  75. private static RingCharBuffer sRingCharBuffer = new RingCharBuffer();
  76. private static final char PLACEHOLDER_DELIMITER_CHAR = '\uFFFC';
  77. private static final int INVALID_COORDINATE = -2;
  78. /* package */ static final int BUFSIZE = 20;
  79. private Context mContext;
  80. private boolean mEnabled = false;
  81. private int mEnd = 0;
  82. /* package */ int mLength = 0;
  83. private char[] mCharBuf = new char[BUFSIZE];
  84. private int[] mXBuf = new int[BUFSIZE];
  85. private int[] mYBuf = new int[BUFSIZE];
  86. private RingCharBuffer() {
  87. }
  88. public static RingCharBuffer getInstance() {
  89. return sRingCharBuffer;
  90. }
  91. public static RingCharBuffer init(Context context, boolean enabled) {
  92. sRingCharBuffer.mContext = context;
  93. sRingCharBuffer.mEnabled = enabled;
  94. return sRingCharBuffer;
  95. }
  96. private int normalize(int in) {
  97. int ret = in % BUFSIZE;
  98. return ret < 0 ? ret + BUFSIZE : ret;
  99. }
  100. public void push(char c, int x, int y) {
  101. if (!mEnabled) return;
  102. mCharBuf[mEnd] = c;
  103. mXBuf[mEnd] = x;
  104. mYBuf[mEnd] = y;
  105. mEnd = normalize(mEnd + 1);
  106. if (mLength < BUFSIZE) {
  107. ++mLength;
  108. }
  109. }
  110. public char pop() {
  111. if (mLength < 1) {
  112. return PLACEHOLDER_DELIMITER_CHAR;
  113. } else {
  114. mEnd = normalize(mEnd - 1);
  115. --mLength;
  116. return mCharBuf[mEnd];
  117. }
  118. }
  119. public char getLastChar() {
  120. if (mLength < 1) {
  121. return PLACEHOLDER_DELIMITER_CHAR;
  122. } else {
  123. return mCharBuf[normalize(mEnd - 1)];
  124. }
  125. }
  126. public int getPreviousX(char c, int back) {
  127. int index = normalize(mEnd - 2 - back);
  128. if (mLength <= back
  129. || Character.toLowerCase(c) != Character.toLowerCase(mCharBuf[index])) {
  130. return INVALID_COORDINATE;
  131. } else {
  132. return mXBuf[index];
  133. }
  134. }
  135. public int getPreviousY(char c, int back) {
  136. int index = normalize(mEnd - 2 - back);
  137. if (mLength <= back
  138. || Character.toLowerCase(c) != Character.toLowerCase(mCharBuf[index])) {
  139. return INVALID_COORDINATE;
  140. } else {
  141. return mYBuf[index];
  142. }
  143. }
  144. public String getLastString() {
  145. StringBuffer sb = new StringBuffer();
  146. for (int i = 0; i < mLength; ++i) {
  147. char c = mCharBuf[normalize(mEnd - 1 - i)];
  148. if (!((AnySoftKeyboard)mContext).isWordSeparator(c)) {
  149. sb.append(c);
  150. } else {
  151. break;
  152. }
  153. }
  154. return sb.reverse().toString();
  155. }
  156. public void reset() {
  157. mLength = 0;
  158. }
  159. }
  160. }