PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/20100328/src/com/menny/android/anysoftkeyboard/keyboards/ExternalAnyKeyboard.java

http://softkeyboard.googlecode.com/
Java | 322 lines | 286 code | 25 blank | 11 comment | 57 complexity | 4b17df227860deeda2412ed017dfd087 MD5 | raw file
Possible License(s): CC-BY-SA-4.0, CC-BY-SA-3.0
  1. package com.menny.android.anysoftkeyboard.keyboards;
  2. import org.xmlpull.v1.XmlPullParser;
  3. import android.content.Context;
  4. import android.content.res.Configuration;
  5. import android.content.res.Resources;
  6. import android.util.AttributeSet;
  7. import android.util.Log;
  8. import android.util.Xml;
  9. import com.menny.android.anysoftkeyboard.AnyKeyboardContextProvider;
  10. import com.menny.android.anysoftkeyboard.AnySoftKeyboardConfiguration;
  11. import com.menny.android.anysoftkeyboard.R;
  12. import com.menny.android.anysoftkeyboard.keyboards.AnyKeyboard.HardKeyboardTranslator;
  13. public class ExternalAnyKeyboard extends AnyKeyboard implements HardKeyboardTranslator {
  14. private static final String XML_TRANSLATION_TAG = "PhysicalTranslation";
  15. private static final String XML_QWERTY_ATTRIBUTE = "QwertyTranslation";
  16. private static final String XML_SEQUENCE_TAG = "SequenceMapping";
  17. private static final String XML_KEYS_ATTRIBUTE = "keySequence";
  18. private static final String XML_ALT_ATTRIBUTE = "altModifier";
  19. private static final String XML_SHIFT_ATTRIBUTE = "shiftModifier";
  20. private static final String XML_TARGET_ATTRIBUTE = "targetChar";
  21. private static final String XML_TARGET_CHAR_CODE_ATTRIBUTE = "targetCharCode";
  22. private final String mPrefId;
  23. private final int mNameResId;
  24. private final int mIconId;
  25. private final String mDefaultDictionary;
  26. private final HardKeyboardSequenceHandler mHardKeyboardTranslator;
  27. private final String mAdditionalIsLetterExceptions;
  28. protected ExternalAnyKeyboard(AnyKeyboardContextProvider askContext, Context context,
  29. int xmlLayoutResId,
  30. int xmlLandscapeResId,
  31. String prefId,
  32. int nameResId,
  33. int iconResId,
  34. int qwertyTranslationId,
  35. String defaultDictionary,
  36. String additionalIsLetterExceptions) {
  37. super(askContext, context, getKeyboardId(context, xmlLayoutResId, xmlLandscapeResId));
  38. mPrefId = prefId;
  39. mNameResId = nameResId;
  40. mIconId = iconResId;
  41. mDefaultDictionary = defaultDictionary;
  42. if (qwertyTranslationId != -1)
  43. {
  44. mHardKeyboardTranslator = createPhysicalTranslatorFromResourceId(context.getApplicationContext(), qwertyTranslationId);
  45. }
  46. else
  47. {
  48. mHardKeyboardTranslator = null;
  49. }
  50. mAdditionalIsLetterExceptions = additionalIsLetterExceptions;
  51. for(final Key key : getKeys())
  52. {
  53. final Resources localResources = getASKContext().getApplicationContext().getResources();
  54. //adding icons
  55. switch(key.codes[0])
  56. {
  57. case AnyKeyboard.KEYCODE_DELETE:
  58. key.icon = localResources.getDrawable(R.drawable.sym_keyboard_delete_small);
  59. break;
  60. case AnyKeyboard.KEYCODE_SHIFT:
  61. key.icon = localResources.getDrawable(R.drawable.sym_keyboard_shift);
  62. break;
  63. case AnyKeyboard.KEYCODE_CTRL:
  64. key.icon = localResources.getDrawable(R.drawable.sym_keyboard_ctrl);
  65. break;
  66. case 32://SPACE
  67. key.icon = localResources.getDrawable(R.drawable.sym_keyboard_space);
  68. break;
  69. case 9://TAB
  70. key.icon = localResources.getDrawable(R.drawable.tab_key);
  71. break;
  72. //these two will be set upon calling setTextVariation
  73. // case AnyKeyboard.KEYCODE_SMILEY:
  74. // key.icon = res.getDrawable(R.drawable.sym_keyboard_smiley);
  75. // key.popupResId = R.xml.popup_smileys;
  76. // break;
  77. // case 10://ENTER
  78. // key.icon = res.getDrawable(R.drawable.sym_keyboard_return);
  79. // break;
  80. }
  81. }
  82. }
  83. private HardKeyboardSequenceHandler createPhysicalTranslatorFromResourceId(Context context, int qwertyTranslationId) {
  84. HardKeyboardSequenceHandler translator = new HardKeyboardSequenceHandler();
  85. XmlPullParser parser = context.getResources().getXml(qwertyTranslationId);
  86. final String TAG = "ASK Hard Translation Parser";
  87. try {
  88. int event;
  89. boolean inTranslations = false;
  90. while ((event = parser.next()) != XmlPullParser.END_DOCUMENT)
  91. {
  92. String tag = parser.getName();
  93. if (event == XmlPullParser.START_TAG) {
  94. if (XML_TRANSLATION_TAG.equals(tag)) {
  95. inTranslations = true;
  96. AttributeSet attrs = Xml.asAttributeSet(parser);
  97. final String qwerty = attrs.getAttributeValue(null, XML_QWERTY_ATTRIBUTE);
  98. translator.addQwertyTranslation(qwerty);
  99. if (AnySoftKeyboardConfiguration.getInstance().getDEBUG()) Log.d(TAG, "Starting parsing "+XML_TRANSLATION_TAG+". Qwerty:"+qwerty);
  100. }
  101. else if (inTranslations && XML_SEQUENCE_TAG.equals(tag))
  102. {
  103. if (AnySoftKeyboardConfiguration.getInstance().getDEBUG()) Log.d(TAG, "Starting parsing "+XML_SEQUENCE_TAG);
  104. AttributeSet attrs = Xml.asAttributeSet(parser);
  105. final int[] keyCodes = getKeyCodesFromPhysicalSequence(attrs.getAttributeValue(null, XML_KEYS_ATTRIBUTE));
  106. final boolean isAlt = attrs.getAttributeBooleanValue(null, XML_ALT_ATTRIBUTE, false);
  107. final boolean isShift = attrs.getAttributeBooleanValue(null, XML_SHIFT_ATTRIBUTE, false);
  108. final String targetChar = attrs.getAttributeValue(null, XML_TARGET_ATTRIBUTE);
  109. final String targetCharCode = attrs.getAttributeValue(null, XML_TARGET_CHAR_CODE_ATTRIBUTE);
  110. final String target;
  111. if (targetChar == null)
  112. target = Character.toString((char)Integer.parseInt(targetCharCode));
  113. else
  114. target = targetChar;
  115. //asserting
  116. if ((keyCodes == null) || (keyCodes.length == 0) || (target == null))
  117. {
  118. Log.e(TAG, "Physical translator sequence does not include mandatory fields "+XML_KEYS_ATTRIBUTE+" or "+XML_TARGET_ATTRIBUTE);
  119. }
  120. else
  121. {
  122. if (!isAlt && !isShift)
  123. {
  124. if (AnySoftKeyboardConfiguration.getInstance().getDEBUG()) Log.d(TAG, "Physical translation details: keys:"+printInts(keyCodes)+" target:"+target);
  125. translator.addSequence(keyCodes, target.charAt(0));
  126. }
  127. else if (isAlt)
  128. {
  129. final int keyCode = keyCodes[0];
  130. if (AnySoftKeyboardConfiguration.getInstance().getDEBUG()) Log.d(TAG, "Physical translation details: ALT+key:"+keyCode+" target:"+target);
  131. translator.addAltMapping(keyCode, target.charAt(0));
  132. }
  133. else if (isShift)
  134. {
  135. final int keyCode = keyCodes[0];
  136. if (AnySoftKeyboardConfiguration.getInstance().getDEBUG()) Log.d(TAG, "Physical translation details: ALT+key:"+keyCode+" target:"+target);
  137. translator.addShiftMapping(keyCode, target.charAt(0));
  138. }
  139. }
  140. }
  141. }
  142. else if (event == XmlPullParser.END_TAG) {
  143. if (XML_TRANSLATION_TAG.equals(tag)) {
  144. inTranslations = false;
  145. if (AnySoftKeyboardConfiguration.getInstance().getDEBUG()) Log.d(TAG, "Finished parsing "+XML_TRANSLATION_TAG);
  146. break;
  147. }
  148. else if (inTranslations && XML_SEQUENCE_TAG.equals(tag))
  149. {
  150. if (AnySoftKeyboardConfiguration.getInstance().getDEBUG()) Log.d(TAG, "Finished parsing "+XML_SEQUENCE_TAG);
  151. }
  152. }
  153. }
  154. } catch (Exception e) {
  155. Log.e(TAG, "Parse error:" + e);
  156. e.printStackTrace();
  157. }
  158. return translator;
  159. }
  160. private String printInts(int[] keyCodes) {
  161. String r = "";
  162. for(int code : keyCodes)
  163. r += (Integer.toString(code)+",");
  164. return r;
  165. }
  166. private int[] getKeyCodesFromPhysicalSequence(String keyCodesArray) {
  167. String[] splitted = keyCodesArray.split(",");
  168. int[] keyCodes = new int[splitted.length];
  169. for(int i=0;i<keyCodes.length;i++)
  170. {
  171. keyCodes[i] = Integer.parseInt(splitted[i]);
  172. }
  173. return keyCodes;
  174. }
  175. @Override
  176. public String getDefaultDictionaryLanguage() {
  177. return mDefaultDictionary;
  178. }
  179. @Override
  180. public String getKeyboardPrefId() {
  181. return mPrefId;
  182. }
  183. @Override
  184. public int getKeyboardIconResId() {
  185. return mIconId;
  186. }
  187. @Override
  188. public int getKeyboardNameResId() {
  189. return mNameResId;
  190. }
  191. private static int getKeyboardId(Context context, int portraitId, int landscapeId)
  192. {
  193. final boolean inPortraitMode =
  194. (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
  195. if (inPortraitMode)
  196. return portraitId;
  197. else
  198. return landscapeId;
  199. }
  200. //this class implements the HardKeyboardTranslator interface in an empty way, the physical keyboard is Latin...
  201. public void translatePhysicalCharacter(HardKeyboardAction action)
  202. {
  203. if (mHardKeyboardTranslator != null)
  204. {
  205. final char translated;
  206. if (action.isAltActive())
  207. translated = mHardKeyboardTranslator.getAltCharacter(action.getKeyCode());
  208. else if (action.isShiftActive())
  209. translated = mHardKeyboardTranslator.getShiftCharacter(action.getKeyCode());
  210. else
  211. translated = mHardKeyboardTranslator.getSequenceCharacter(action.getKeyCode(), getASKContext());
  212. if (translated != 0)
  213. action.setNewKeyCode(translated);
  214. }
  215. }
  216. @Override
  217. public boolean isLetter(char keyValue) {
  218. if (mAdditionalIsLetterExceptions == null)
  219. return super.isLetter(keyValue);
  220. else
  221. return super.isLetter(keyValue) ||
  222. (mAdditionalIsLetterExceptions.indexOf(keyValue) >= 0);
  223. }
  224. protected void setPopupKeyChars(Key aKey)
  225. {
  226. if (aKey.popupResId > 0)
  227. return;//if the keyboard XML already specified the popup, then no need to override
  228. if ((aKey.codes != null) && (aKey.codes.length > 0))
  229. {
  230. switch((char)aKey.codes[0])
  231. {
  232. case 'a':
  233. aKey.popupCharacters = "\u00e0\u00e1\u00e2\u00e3\u00e4\u00e5\u00e6\u0105";
  234. aKey.popupResId = com.menny.android.anysoftkeyboard.R.xml.popup;
  235. break;
  236. case 'c':
  237. aKey.popupCharacters = "\u00e7\u0107\u0109\u010d";
  238. aKey.popupResId = com.menny.android.anysoftkeyboard.R.xml.popup;
  239. break;
  240. case 'd':
  241. aKey.popupCharacters = "\u0111";
  242. aKey.popupResId = com.menny.android.anysoftkeyboard.R.xml.popup;
  243. break;
  244. case 'e':
  245. aKey.popupCharacters = "\u00e8\u00e9\u00ea\u00eb\u0119\u20ac";
  246. aKey.popupResId = com.menny.android.anysoftkeyboard.R.xml.popup;
  247. break;
  248. case 'g':
  249. aKey.popupCharacters = "\u011d";
  250. aKey.popupResId = com.menny.android.anysoftkeyboard.R.xml.popup;
  251. break;
  252. case 'h':
  253. aKey.popupCharacters = "\u0125";
  254. aKey.popupResId = com.menny.android.anysoftkeyboard.R.xml.popup;
  255. break;
  256. case 'i':
  257. aKey.popupCharacters = "\u00ec\u00ed\u00ee\u00ef\u0142";
  258. aKey.popupResId = com.menny.android.anysoftkeyboard.R.xml.popup;
  259. break;
  260. case 'j':
  261. aKey.popupCharacters = "\u0135";
  262. aKey.popupResId = com.menny.android.anysoftkeyboard.R.xml.popup;
  263. break;
  264. case 'l':
  265. aKey.popupCharacters = "\u0142";
  266. aKey.popupResId = com.menny.android.anysoftkeyboard.R.xml.popup;
  267. break;
  268. case 'o':
  269. aKey.popupCharacters = "\u00f2\u00f3\u00f4\u00f5\u00f6\u00f8\u0151\u0153";
  270. aKey.popupResId = com.menny.android.anysoftkeyboard.R.xml.popup;
  271. break;
  272. case 's':
  273. aKey.popupCharacters = "\u00a7\u00df\u015b\u015d\u0161";
  274. aKey.popupResId = com.menny.android.anysoftkeyboard.R.xml.popup;
  275. break;
  276. case 'u':
  277. aKey.popupCharacters = "\u00f9\u00fa\u00fb\u00fc\u016d\u0171";
  278. aKey.popupResId = com.menny.android.anysoftkeyboard.R.xml.popup;
  279. break;
  280. case 'n':
  281. aKey.popupCharacters = "\u00f1";
  282. aKey.popupResId = com.menny.android.anysoftkeyboard.R.xml.popup;
  283. break;
  284. case 'y':
  285. aKey.popupCharacters = "\u00fd\u00ff";
  286. aKey.popupResId = com.menny.android.anysoftkeyboard.R.xml.popup;
  287. break;
  288. case 'z':
  289. aKey.popupCharacters = "\u017c\u017e";
  290. aKey.popupResId = com.menny.android.anysoftkeyboard.R.xml.popup;
  291. break;
  292. default:
  293. super.setPopupKeyChars(aKey);
  294. }
  295. }
  296. }
  297. }