/tags/20100328/src/com/menny/android/anysoftkeyboard/Workarounds.java

http://softkeyboard.googlecode.com/ · Java · 116 lines · 83 code · 14 blank · 19 comment · 14 complexity · e09ce8f7dab33dbc3c5f2452a145ec52 MD5 · raw file

  1. package com.menny.android.anysoftkeyboard;
  2. import java.lang.reflect.Field;
  3. public class Workarounds
  4. {
  5. //Determine whether this device has the fix for RTL in the suggestions list
  6. private static final boolean ms_requiresRtlWorkaround;
  7. private static final boolean ms_isDonut;
  8. private static final boolean ms_isEclair;
  9. static
  10. {
  11. boolean requiresRtlWorkaround = true;//all devices required this fix (in 1.6 it is still required)
  12. if (android.os.Build.MODEL.toLowerCase().contains("galaxy"))
  13. {
  14. //see issue 132
  15. //and issue 285
  16. //no fix: 1251851795000
  17. //fix: 1251970876000
  18. //no fix: 1251851795000
  19. //fix: 1251970876000
  20. //fix: 1261367883000
  21. // //final int buildInc = Integer.parseInt(android.os.Build.VERSION.INCREMENTAL);
  22. // //requiresRtlWorkaround = (buildInc < 20090831);
  23. requiresRtlWorkaround = (android.os.Build.TIME <= 1251851795000l);
  24. }
  25. else if (android.os.Build.MODEL.toLowerCase().contains("spica"))
  26. {
  27. //(see issue 285):
  28. //fix: 1263807011000
  29. requiresRtlWorkaround = (android.os.Build.TIME < 1263807011000l);
  30. }
  31. ms_requiresRtlWorkaround = requiresRtlWorkaround;
  32. //checking f/w API is a bit tricky, we need to do it by reflection
  33. boolean isDonut = false;
  34. boolean isEclair = false;
  35. try
  36. {
  37. Field sdkInt = android.os.Build.VERSION.class.getField("SDK_INT");
  38. if (sdkInt != null)
  39. {
  40. //NOTE: I can not use the field here, since this code MAY run in cupcake, and therefore
  41. //fail in JIT compile. I need to perform this function with reflection...
  42. isDonut = (sdkInt.getInt(null) >= 4);
  43. isEclair = (sdkInt.getInt(null) >= 5);
  44. }
  45. }
  46. catch(Exception ex)
  47. {
  48. }
  49. ms_isDonut = isDonut;
  50. ms_isEclair = isEclair;
  51. }
  52. public static boolean isRightToLeftCharacter(final char key)
  53. {
  54. final byte direction = Character.getDirectionality(key);
  55. switch(direction)
  56. {
  57. case Character.DIRECTIONALITY_RIGHT_TO_LEFT:
  58. case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:
  59. case Character.DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING:
  60. case Character.DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE:
  61. return true;
  62. default:
  63. return false;
  64. }
  65. }
  66. public static int workaroundParenthesisDirectionFix(int primaryCode)
  67. {
  68. //Android does not support the correct direction of parenthesis in right-to-left langs.
  69. if (!ms_requiresRtlWorkaround)
  70. return primaryCode;//I hope Galaxy has the fix...
  71. if (primaryCode == (int)')')
  72. return '(';
  73. else if (primaryCode == (int)'(')
  74. return ')';
  75. return primaryCode;
  76. }
  77. public static CharSequence workaroundCorrectStringDirection(CharSequence suggestion)
  78. {
  79. //Hebrew letters are to be drawn in the other direction.
  80. //Also, this is not valid for Galaxy (Israel's Cellcom Android)
  81. if (!ms_requiresRtlWorkaround)
  82. return suggestion;
  83. //this function is a workaround! In the official 1.5 firmware, there is a RTL bug.
  84. if (isRightToLeftCharacter(suggestion.charAt(0)))
  85. {
  86. String reveresed = "";
  87. for(int charIndex = suggestion.length() - 1; charIndex>=0; charIndex--)
  88. {
  89. reveresed = reveresed + suggestion.charAt(charIndex);
  90. }
  91. return reveresed;
  92. }
  93. else
  94. return suggestion;
  95. }
  96. public static boolean isDonut() {
  97. return ms_isDonut;
  98. }
  99. public static boolean isEclair() {
  100. return ms_isEclair;
  101. }
  102. }