/branches/theme-support/project/src/com/menny/android/anysoftkeyboard/SoftKeyboard.java

http://softkeyboard.googlecode.com/ · Java · 1127 lines · 4 code · 1 blank · 1122 comment · 0 complexity · 67cc9ab44e198a5aede5a8f497a4ccaf MD5 · raw file

  1. package com.menny.android.anysoftkeyboard;
  2. //
  3. //import java.util.ArrayList;
  4. //import java.util.List;
  5. //
  6. //import android.app.Notification;
  7. //import android.app.NotificationManager;
  8. //import android.app.PendingIntent;
  9. //import android.content.Context;
  10. //import android.content.Intent;
  11. //import android.content.SharedPreferences;
  12. //import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
  13. //import android.content.res.Configuration;
  14. //import android.inputmethodservice.InputMethodService;
  15. //import android.inputmethodservice.Keyboard;
  16. //import android.inputmethodservice.KeyboardView;
  17. //import android.inputmethodservice.Keyboard.Key;
  18. //import android.media.AudioManager;
  19. //import android.os.Vibrator;
  20. //import android.preference.PreferenceManager;
  21. //import android.util.Log;
  22. //import android.view.KeyEvent;
  23. //import android.view.View;
  24. //import android.view.inputmethod.CompletionInfo;
  25. //import android.view.inputmethod.EditorInfo;
  26. //import android.view.inputmethod.InputConnection;
  27. //
  28. //import com.menny.android.anysoftkeyboard.keyboards.AnyKeyboard;
  29. //import com.menny.android.anysoftkeyboard.keyboards.GenericKeyboard;
  30. //import com.menny.android.anysoftkeyboard.keyboards.KeyboardFactory;
  31. //import com.menny.android.anysoftkeyboard.keyboards.AnyKeyboard.HardKeyboardTranslator;
  32. //
  33. ///**
  34. // * Example of writing an input method for a soft keyboard. This code is
  35. // * focused on simplicity over completeness, so it should in no way be considered
  36. // * to be a complete soft keyboard implementation. Its purpose is to provide
  37. // * a basic example for how you would get started writing an input method, to
  38. // * be fleshed out as appropriate.
  39. // */
  40. public class SoftKeyboard extends AnySoftKeyboard
  41. {
  42. }
  43. // implements KeyboardView.OnKeyboardActionListener, OnSharedPreferenceChangeListener,
  44. // AnyKeyboardContextProvider
  45. // {
  46. //
  47. // private enum NextKeyboardType
  48. // {
  49. // Alphabet,
  50. // SupportsPhysical,
  51. // Any
  52. // }
  53. //
  54. // public static SoftKeyboard msCurrentInstance;
  55. //
  56. // private static final int KEYBOARD_NOTIFICATION_ID = 1;
  57. //
  58. // private KeyboardView mInputView;
  59. // private CandidateView mCandidateView;
  60. // private CompletionInfo[] mCompletions;
  61. // private Suggest mSuggest;
  62. // private WordComposer mWordComposer = new WordComposer();
  63. // //private StringBuilder mComposing = new StringBuilder();
  64. // //private boolean mPredictionOn;
  65. // private boolean mCompletionOn;
  66. //
  67. // private int mLastDisplayWidth;
  68. // private boolean mCapsLock;
  69. // private long mLastShiftTime;
  70. //
  71. // private AnyKeyboard mSymbolsKeyboard;
  72. // private AnyKeyboard mSymbolsShiftedKeyboard;
  73. // //private AnyKeyboard mInternetKeyboard;
  74. // private AnyKeyboard mSimpleNumbersKeyboard;
  75. // //my working keyboards
  76. // private ArrayList<AnyKeyboard> mEnabledKeyboards = null;
  77. // private int mLastSelectedKeyboard = 0;
  78. //
  79. // private AnyKeyboard mCurKeyboard;
  80. //
  81. // private boolean mVibrateOnKeyPress = false;
  82. // private boolean mSoundOnKeyPress = false;
  83. // private boolean mAutoCaps = false;
  84. // private boolean mShowCandidates = false;
  85. //
  86. // private boolean mKeyboardChangeNotification;
  87. //
  88. // private boolean mCurrentTextDirectionIsLeftToRight = true;
  89. // private boolean mShouldAddDirectionMarkOnNextCharacter = true;
  90. //
  91. // public static String mChangeKeysMode;
  92. //
  93. // public SoftKeyboard()
  94. // {
  95. // super();
  96. // msCurrentInstance = this;
  97. // }
  98. //
  99. // /**
  100. // * Main initialization of the input method component. Be sure to call
  101. // * to super class.
  102. // */
  103. // @Override public void onCreate() {
  104. // super.onCreate();
  105. // Log.i("AnySoftKeyboard", "onCreate");
  106. // SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
  107. // sp.registerOnSharedPreferenceChangeListener(this);
  108. // }
  109. //
  110. // @Override
  111. // public void onDestroy() {
  112. // super.onDestroy();
  113. // SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
  114. // sp.unregisterOnSharedPreferenceChangeListener(this);
  115. // if (mSoundOnKeyPress)
  116. // {
  117. // Log.i("AnySoftKeyboard", "Releasing sounds effects from AUDIO_SERVICE");
  118. // ((AudioManager)getSystemService(Context.AUDIO_SERVICE)).unloadSoundEffects();
  119. // }
  120. // }
  121. //
  122. // /**
  123. // * This is the point where you can do all of your UI initialization. It
  124. // * is called after creation and any configuration change.
  125. // */
  126. // @Override public void onInitializeInterface()
  127. // {
  128. // Log.i("AnySoftKeyboard", "onInitializeInterface: Have keyboards="+(mEnabledKeyboards != null)+". isFullScreen="+super.isFullscreenMode()+". isInputViewShown="+super.isInputViewShown());
  129. // reloadConfiguration();
  130. //
  131. // if (mEnabledKeyboards != null)
  132. // {
  133. // // Configuration changes can happen after the keyboard gets recreated,
  134. // // so we need to be able to re-build the keyboards if the available
  135. // // space has changed.
  136. // int displayWidth = getMaxWidth();
  137. // if (displayWidth == mLastDisplayWidth) return;
  138. // }
  139. // mLastDisplayWidth = getMaxWidth();
  140. //
  141. // //we'll create the keyboards
  142. // createKeyboards();
  143. // ensureCurrentKeyboardIsOk();
  144. // }
  145. //
  146. // private void createKeyboards() {
  147. // mSymbolsKeyboard = new GenericKeyboard(this, R.xml.symbols, false, -1);
  148. // mSymbolsShiftedKeyboard = new GenericKeyboard(this, R.xml.symbols_shift, false, -1);
  149. // //mInternetKeyboard = new InternetKeyboard(this);
  150. // mSimpleNumbersKeyboard = new GenericKeyboard(this, R.xml.simple_numbers, false, -1);
  151. //
  152. // mEnabledKeyboards = KeyboardFactory.createAlphaBetKeyboards(this);
  153. // //clearing memory of old keyboards.
  154. // java.lang.System.gc();
  155. // }
  156. //
  157. // private void reloadConfiguration()
  158. // {
  159. // SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
  160. // mVibrateOnKeyPress = sp.getBoolean("vibrate_on", false);
  161. // mSoundOnKeyPress = sp.getBoolean("sound_on", false);
  162. // mKeyboardChangeNotification = sp.getBoolean("physical_keyboard_change_notification", true);
  163. //
  164. // if (mSoundOnKeyPress)
  165. // {
  166. // Log.i("AnySoftKeyboard", "Loading sounds effects from AUDIO_SERVICE");
  167. // ((AudioManager)getSystemService(Context.AUDIO_SERVICE)).loadSoundEffects();
  168. // }
  169. //
  170. // mAutoCaps = sp.getBoolean("auto_caps", true);
  171. // mShowCandidates = sp.getBoolean("candidates_on", true);
  172. //
  173. // mChangeKeysMode = sp.getString("keyboard_layout_change_method", "1");
  174. //
  175. // Log.d("AnySoftKeyboard", "Configuration loaded: " +
  176. // "mVibrateOnKeyPress: "+mVibrateOnKeyPress+
  177. // "mSoundOnKeyPress: "+mSoundOnKeyPress+
  178. // "mKeyboardChangeNotification: "+mKeyboardChangeNotification+
  179. // "mAutoCaps: "+mAutoCaps+
  180. // "mShowCandidates: "+mShowCandidates+
  181. // "mChangeKeysMode: "+mChangeKeysMode);
  182. // }
  183. //
  184. // private void ensureCurrentKeyboardIsOk()
  185. // {
  186. // //need to check that current keyboard and mLastSelectedKeyboard are enabled.
  187. //// if (!mKeyboards.get(mLastSelectedKeyboard).isEnabled())
  188. //// {
  189. //// //ALWAYS starting in Alphabet!
  190. //// nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.Alphabet);
  191. //// }
  192. // //in the weird case (impossible?) that the 'mLastSelectedKeyboard' is enabled,
  193. // //but the mCurKeyboard is null.
  194. // if ((mCurKeyboard == null)/* || (!mCurKeyboard.isEnabled())*/)
  195. // mCurKeyboard = getLastSelectedKeyboard();
  196. //
  197. // if (mInputView != null)
  198. // mInputView.setKeyboard(mCurKeyboard);
  199. // }
  200. //
  201. // private AnyKeyboard getLastSelectedKeyboard() {
  202. // if (mLastSelectedKeyboard >= mEnabledKeyboards.size())
  203. // mLastSelectedKeyboard = 0;
  204. //
  205. // return mEnabledKeyboards.get(mLastSelectedKeyboard);
  206. // }
  207. //
  208. // /**
  209. // * Called by the framework when your view for creating input needs to
  210. // * be generated. This will be called the first time your input method
  211. // * is displayed, and every time it needs to be re-created such as due to
  212. // * a configuration change.
  213. // */
  214. // @Override public View onCreateInputView()
  215. // {
  216. // Log.i("AnySoftKeyboard", "onCreateInputView");
  217. //
  218. // mInputView = (KeyboardView) getLayoutInflater().inflate(R.layout.input, null);
  219. // mInputView.setOnKeyboardActionListener(this);
  220. // mInputView.setKeyboard(getLastSelectedKeyboard());
  221. // //reloadConfiguration();
  222. //
  223. // return mInputView;
  224. // }
  225. //
  226. //// @Override
  227. //// public void onWindowShown()
  228. //// {
  229. //// super.onWindowShown();
  230. //// TutorialsProvider.ShowTutorialsIfNeeded(this);
  231. //// }
  232. //
  233. // /**
  234. // * Called by the framework when your view for showing candidates needs to
  235. // * be generated, like {@link #onCreateInputView}.
  236. // */
  237. // @Override public View onCreateCandidatesView()
  238. // {
  239. // Log.i("AnySoftKeyboard", "onCreateCandidatesView. mCompletionOn:"+mCompletionOn);
  240. // if (mCompletionOn)
  241. // {
  242. // mCandidateView = new CandidateView(this);
  243. // mCandidateView.setService(this);
  244. //
  245. // return mCandidateView;
  246. // }
  247. // else
  248. // {
  249. // return null;
  250. // }
  251. // }
  252. //
  253. // /**
  254. // * This is the main point where we do our initialization of the input method
  255. // * to begin operating on an application. At this point we have been
  256. // * bound to the client, and are now receiving all of the detailed information
  257. // * about the target of our edits.
  258. // */
  259. // @Override public void onStartInput(EditorInfo attribute, boolean restarting) {
  260. // super.onStartInput(attribute, restarting);
  261. // Log.i("AnySoftKeyboard", "onStartInput. restarting:"+restarting);
  262. // //reloadConfiguration();
  263. // // Reset our state. We want to do this even if restarting, because
  264. // // the underlying state of the text editor could have changed in any way.
  265. // //mComposing.setLength(0);
  266. // mWordComposer.reset();
  267. // updateCandidates();
  268. //
  269. // if (!restarting)
  270. // {
  271. // // Clear shift states.
  272. // //mMetaState = 0;
  273. // }
  274. //
  275. // mCompletions = null;
  276. //
  277. // // We are now going to initialize our state based on the type of
  278. // // text being edited.
  279. // switch (attribute.inputType&EditorInfo.TYPE_MASK_CLASS) {
  280. // case EditorInfo.TYPE_CLASS_NUMBER:
  281. // case EditorInfo.TYPE_CLASS_DATETIME:
  282. // // Numbers and dates default to the symbols keyboard, with
  283. // // no extra features.
  284. // //mCurKeyboard = mSymbolsKeyboard;
  285. // mCurKeyboard = mSimpleNumbersKeyboard;
  286. // break;
  287. //
  288. // case EditorInfo.TYPE_CLASS_PHONE:
  289. // // Phones will also default to the symbols keyboard, though
  290. // // often you will want to have a dedicated phone keyboard.
  291. // //mCurKeyboard = mSymbolsKeyboard;
  292. // mCurKeyboard = mSimpleNumbersKeyboard;
  293. // break;
  294. //
  295. // case EditorInfo.TYPE_CLASS_TEXT:
  296. // // This is general text editing. We will default to the
  297. // // normal alphabetic keyboard, and assume that we should
  298. // // be doing predictive text (showing candidates as the
  299. // // user types).
  300. // mCurKeyboard = getLastSelectedKeyboard();
  301. // //mPredictionOn = mShowCandidates;
  302. // mCompletionOn = mShowCandidates;
  303. //
  304. // // We now look for a few special variations of text that will
  305. // // modify our behavior.
  306. // int variation = attribute.inputType & EditorInfo.TYPE_MASK_VARIATION;
  307. // if (variation == EditorInfo.TYPE_TEXT_VARIATION_PASSWORD ||
  308. // variation == EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
  309. // // Do not display predictions / what the user is typing
  310. // // when they are entering a password.
  311. // //mPredictionOn = false;
  312. // mCompletionOn = false;
  313. // }
  314. //
  315. // if (variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
  316. // || variation == EditorInfo.TYPE_TEXT_VARIATION_URI
  317. // || variation == EditorInfo.TYPE_TEXT_VARIATION_FILTER) {
  318. // // Our predictions are not useful for e-mail addresses
  319. // // or URIs.
  320. // //mPredictionOn = false;
  321. // mCompletionOn = false;
  322. // }
  323. //
  324. //// if (variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
  325. //// || variation == EditorInfo.TYPE_TEXT_VARIATION_URI) {
  326. //// //special keyboard
  327. //// if (mInternetKeyboard.isEnabled())
  328. //// mCurKeyboard = mInternetKeyboard;
  329. //// else
  330. //// mCurKeyboard = mKeyboards.get(mLastSelectedKeyboard);
  331. //// }
  332. // mCurKeyboard = getLastSelectedKeyboard();
  333. //
  334. // if ((attribute.inputType&EditorInfo.TYPE_TEXT_FLAG_AUTO_COMPLETE) != 0) {
  335. // // If this is an auto-complete text view, then our predictions
  336. // // will not be shown and instead we will allow the editor
  337. // // to supply their own. We only show the editor's
  338. // // candidates when in fullscreen mode, otherwise relying
  339. // // own it displaying its own UI.
  340. // //mPredictionOn = false;
  341. // mCompletionOn = isFullscreenMode() && mShowCandidates;
  342. // }
  343. //
  344. // // We also want to look at the current state of the editor
  345. // // to decide whether our alphabetic keyboard should start out
  346. // // shifted.
  347. // updateShiftKeyState(attribute);
  348. // break;
  349. //
  350. // default:
  351. // // For all unknown input types, default to the alphabetic
  352. // // keyboard with no special features.
  353. // mCurKeyboard = getLastSelectedKeyboard();
  354. // updateShiftKeyState(attribute);
  355. // }
  356. // //turning on suggestions if needed
  357. // if (mCompletionOn)
  358. // {
  359. // Log.i("AnySoftKeyboard", "Creating Suggestion manager");
  360. // mSuggest = new Suggest(this);
  361. // mSuggest.setUserDictionary(new UserDictionary(this));
  362. // super.setCandidatesViewShown(true);
  363. // }
  364. // else
  365. // {
  366. // Log.i("AnySoftKeyboard", "Suggestion manager is not required (mShowCandidates: "+mShowCandidates+")");
  367. // mSuggest = null;
  368. // System.gc();
  369. // }
  370. // // Update the label on the enter key, depending on what the application
  371. // // says it will do.
  372. // mCurKeyboard.setImeOptions(getResources(), attribute.imeOptions);
  373. // mCurKeyboard.setTextVariation(getResources(), attribute.inputType);
  374. // }
  375. //
  376. // /**
  377. // * This is called when the user is done editing a field. We can use
  378. // * this to reset our state.
  379. // */
  380. // @Override public void onFinishInput() {
  381. // super.onFinishInput();
  382. // Log.i("AnySoftKeyboard", "onFinishInput");
  383. // // Clear current composing text and candidates.
  384. // //mComposing.setLength(0);
  385. // mWordComposer.reset();
  386. // updateCandidates();
  387. //
  388. // // We only hide the candidates window when finishing input on
  389. // // a particular editor, to avoid popping the underlying application
  390. // // up and down if the user is entering text into the bottom of
  391. // // its window.
  392. // setCandidatesViewShown(false);
  393. //
  394. // mCurKeyboard = getLastSelectedKeyboard();
  395. // if (mInputView != null) {
  396. // mInputView.closing();
  397. // }
  398. //
  399. // NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  400. // notificationManager.cancel(KEYBOARD_NOTIFICATION_ID);
  401. // }
  402. //
  403. // @Override public void onStartInputView(EditorInfo attribute, boolean restarting) {
  404. // super.onStartInputView(attribute, restarting);
  405. // Log.i("AnySoftKeyboard", "onStartInputView. restarting:"+restarting);
  406. // mInputView.setKeyboard(mCurKeyboard);
  407. // //no need?
  408. // //mInputView.closing();
  409. // }
  410. //
  411. // /**
  412. // * Deal with the editor reporting movement of its cursor.
  413. // */
  414. // @Override public void onUpdateSelection(int oldSelStart, int oldSelEnd,
  415. // int newSelStart, int newSelEnd,
  416. // int candidatesStart, int candidatesEnd) {
  417. // super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd,
  418. // candidatesStart, candidatesEnd);
  419. //
  420. // // If the current selection in the text view changes, we should
  421. // // clear whatever candidate text we have.
  422. // if (mWordComposer.getTypedWord().length() > 0 && (newSelStart != candidatesEnd
  423. // || newSelEnd != candidatesEnd)) {
  424. // //mComposing.setLength(0);
  425. // mWordComposer.reset();
  426. // updateCandidates();
  427. // InputConnection ic = getCurrentInputConnection();
  428. // if (ic != null) {
  429. // ic.finishComposingText();
  430. // }
  431. // }
  432. // }
  433. //
  434. // /**
  435. // * This tells us about completions that the editor has determined based
  436. // * on the current text in it. We want to use this in fullscreen mode
  437. // * to show the completions ourself, since the editor can not be seen
  438. // * in that situation.
  439. // */
  440. // @Override public void onDisplayCompletions(CompletionInfo[] completions) {
  441. // String paramDetails = "NULL";
  442. // if (completions != null)
  443. // paramDetails = "" + completions.length;
  444. // Log.i("Completions", "onDisplayCompletions was called with "+paramDetails);
  445. // if (mCompletionOn)
  446. // {
  447. // mCompletions = completions;
  448. // if (completions == null) {
  449. // setSuggestions(null, false, false);
  450. // return;
  451. // }
  452. //
  453. // List<CharSequence> stringList = new ArrayList<CharSequence>();
  454. // for (int i=0; i<(completions != null ? completions.length : 0); i++) {
  455. // CompletionInfo ci = completions[i];
  456. // if (ci != null) stringList.add(ci.getText());
  457. // }
  458. // setSuggestions(stringList, true, true);
  459. // }
  460. // }
  461. //
  462. // /**
  463. // * Use this to monitor key events being delivered to the application.
  464. // * We get first crack at them, and can either resume them or let them
  465. // * continue to the app.
  466. // */
  467. // @Override public boolean onKeyDown(int keyCode, KeyEvent event) {
  468. // switch (keyCode)
  469. // {
  470. // case KeyEvent.KEYCODE_BACK:
  471. // // The InputMethodService already takes care of the back
  472. // // key for us, to dismiss the input method if it is shown.
  473. // // However, our keyboard could be showing a pop-up window
  474. // // that back should dismiss, so we first allow it to do that.
  475. // if (event.getRepeatCount() == 0 && mInputView != null) {
  476. // if (mInputView.handleBack()) {
  477. // return true;
  478. // }
  479. // }
  480. // break;
  481. //
  482. // case KeyEvent.KEYCODE_DEL:
  483. // // Special handling of the delete key: if we currently are
  484. // // composing text for the user, we want to modify that instead
  485. // // of let the application to the delete itself.
  486. // if (mWordComposer.getTypedWord().length() > 0) {
  487. // onKey(Keyboard.KEYCODE_DELETE, null);
  488. // return true;
  489. // }
  490. // break;
  491. //
  492. // case KeyEvent.KEYCODE_ENTER:
  493. // // Let the underlying text editor always handle these.
  494. // return false;
  495. //
  496. // default:
  497. // // For all other keys, if we want to do transformations on
  498. // // text being entered with a hard keyboard, we need to process
  499. // // it and do the appropriate action.
  500. // // using physical keyboard is more annoying with candidate view in the way
  501. // // so we disable it.
  502. // mCompletionOn = false;
  503. //
  504. // if (keyCode == KeyEvent.KEYCODE_SPACE
  505. // && (event.getMetaState()&KeyEvent.META_ALT_ON) != 0) {
  506. // InputConnection ic = getCurrentInputConnection();
  507. // if (ic != null)
  508. // {
  509. // // First, tell the editor that it is no longer in the
  510. // // shift state, since we are consuming this.
  511. // ic.clearMetaKeyStates(KeyEvent.META_ALT_ON);
  512. // //only physical keyboard
  513. // nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.SupportsPhysical);
  514. // notifyKeyboardChangeIfNeeded();
  515. //
  516. // return true;
  517. // }
  518. // }
  519. // else if(/*keyCode >= KeyEvent.KEYCODE_A &&
  520. // keyCode <= KeyEvent.KEYCODE_COMMA &&*/
  521. // (mCurKeyboard != null) &&
  522. // (mCurKeyboard instanceof HardKeyboardTranslator)/* &&
  523. // ((event.getMetaState()&KeyEvent.META_ALT_ON) == 0) &&
  524. // ((event.getMetaState()&KeyEvent.META_SHIFT_ON) == 0)*/)
  525. // {
  526. // Log.d("AnySoftKeyborad", "Asking '"+mCurKeyboard.getKeyboardName()+"' to translate key: "+keyCode);
  527. // char translatedChar = ((HardKeyboardTranslator)mCurKeyboard).translatePhysicalCharacter(keyCode, event.getMetaState());
  528. // if (translatedChar != 0)
  529. // {
  530. // //consuming the meta keys
  531. // InputConnection ic = getCurrentInputConnection();
  532. // if (ic != null)
  533. // {
  534. // ic.clearMetaKeyStates(event.getMetaState());
  535. // }
  536. // Log.d("AnySoftKeyborad", "'"+mCurKeyboard.getKeyboardName()+"' translated key "+keyCode+" to "+translatedChar);
  537. // sendKey(translatedChar);
  538. // return true;
  539. // }
  540. // else
  541. // {
  542. // Log.d("AnySoftKeyborad", "'"+mCurKeyboard.getKeyboardName()+"' did not translated key "+keyCode+".");
  543. // }
  544. // }
  545. // }
  546. //
  547. // return super.onKeyDown(keyCode, event);
  548. // }
  549. //
  550. //
  551. // /**
  552. // * Helper function to commit any text being composed in to the editor.
  553. // */
  554. // private void commitTyped(InputConnection inputConnection) {
  555. // if (mWordComposer.getTypedWord().length() > 0)
  556. // {
  557. // inputConnection.commitText(mWordComposer.getTypedWord(), mWordComposer.getTypedWord().length());
  558. // //mComposing.setLength(0);
  559. // mWordComposer.reset();
  560. // updateCandidates();
  561. // }
  562. // }
  563. //
  564. // private void handleTextDirection()
  565. // {
  566. // return;
  567. //// //OK, it is official, we entered text using this keyboard
  568. //// mCurrentTextDirectionIsLeftToRight = mCurKeyboard.isLeftToRightLanguage();
  569. //// if (mShouldAddDirectionMarkOnNextCharacter)
  570. //// {
  571. //// //ho! it is a different direction!
  572. //// char directionCharacter = mCurrentTextDirectionIsLeftToRight?
  573. //// ((char)8206):((char)8207);
  574. //// mComposing.insert(0, directionCharacter);
  575. //// mShouldAddDirectionMarkOnNextCharacter = false;//once is enough
  576. //// }
  577. // }
  578. //
  579. // /**
  580. // * Helper to update the shift state of our keyboard based on the initial
  581. // * editor state.
  582. // */
  583. // private void updateShiftKeyState(EditorInfo attr)
  584. // {
  585. // if (mInputView != null)
  586. // {
  587. // int caps = 0;
  588. //
  589. // if ((attr != null) && mAutoCaps)
  590. // {
  591. // InputConnection ci = getCurrentInputConnection();
  592. // if (ci != null)
  593. // caps = ci.getCursorCapsMode(attr.inputType);
  594. // }
  595. // mInputView.setShifted(mCapsLock || caps != 0);
  596. // }
  597. // }
  598. //
  599. // /**
  600. // * Helper to determine if a given character code is alphabetic.
  601. // */
  602. // private boolean isAlphabet(int code)
  603. // {
  604. // return mCurKeyboard.isLetter((char)code);
  605. // }
  606. //
  607. // /**
  608. // * Helper to send a key down / key up pair to the current editor.
  609. // */
  610. // private void keyDownUp(int keyEventCode) {
  611. // getCurrentInputConnection().sendKeyEvent(
  612. // new KeyEvent(KeyEvent.ACTION_DOWN, keyEventCode));
  613. // getCurrentInputConnection().sendKeyEvent(
  614. // new KeyEvent(KeyEvent.ACTION_UP, keyEventCode));
  615. // }
  616. //
  617. // /**
  618. // * Helper to send a character to the editor as raw key events.
  619. // */
  620. // private void sendKey(int keyCode) {
  621. // switch (keyCode) {
  622. // case '\n':
  623. // keyDownUp(KeyEvent.KEYCODE_ENTER);
  624. // break;
  625. // case ' '://testing
  626. // keyDownUp(KeyEvent.KEYCODE_SPACE);
  627. // break;
  628. // default:
  629. // if (keyCode >= '0' && keyCode <= '9') {
  630. // keyDownUp(keyCode - '0' + KeyEvent.KEYCODE_0);
  631. // } else {
  632. // handleTextDirection();
  633. // //mComposing.append((char) keyCode);
  634. // mWordComposer.add(keyCode, new int[]{keyCode});
  635. // commitTyped(getCurrentInputConnection());
  636. // }
  637. // break;
  638. // }
  639. // }
  640. //
  641. // // Implementation of KeyboardViewListener
  642. //
  643. // public void onKey(int primaryCode, int[] keyCodes)
  644. // {
  645. // if (primaryCode > 0)
  646. // Log.v("AnySoftKeyboard", "onKey char: "+(char)primaryCode);
  647. // else
  648. // Log.v("AnySoftKeyboard", "onKey code: "+primaryCode);
  649. // EditorInfo currentEditorInfo = getCurrentInputEditorInfo();
  650. // InputConnection currentInputConnection = getCurrentInputConnection();
  651. // if (primaryCode == Keyboard.KEYCODE_DELETE) {
  652. // deleteLastCharactersFromInput(1);
  653. // } else if (primaryCode == Keyboard.KEYCODE_SHIFT) {
  654. // handleShift();
  655. // } else if (primaryCode == Keyboard.KEYCODE_CANCEL) {
  656. // handleClose();
  657. // return;
  658. // } else if (primaryCode == AnyKeyboardView.KEYCODE_OPTIONS) {
  659. // // Show a menu or somethin'
  660. // }
  661. // else if (primaryCode == AnyKeyboard.KEYCODE_LANG_CHANGE//my special lang key
  662. // && mInputView != null) {
  663. // nextKeyboard(currentEditorInfo, NextKeyboardType.Any);//false - not just alphabet
  664. // } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE
  665. // && mInputView != null)
  666. // {
  667. // nextSymbolsKeyboard();
  668. // }
  669. // else if (!isAlphabet(primaryCode))
  670. // {
  671. // // Handle separator
  672. // if (mWordComposer.getTypedWord().length() > 0) {
  673. // commitTyped(currentInputConnection);
  674. // }
  675. // primaryCode = translatePrimaryCodeFromCurrentKeyboard(primaryCode);
  676. // sendKey(primaryCode);
  677. //
  678. // updateShiftKeyState(currentEditorInfo);
  679. // }
  680. // else
  681. // {
  682. // handleCharacter(primaryCode, keyCodes);
  683. // }
  684. // }
  685. //
  686. // private void nextSymbolsKeyboard()
  687. // {
  688. // boolean shifted = false;
  689. // if (mCurKeyboard != mSymbolsKeyboard)
  690. // {
  691. // mCurKeyboard = mSymbolsKeyboard;
  692. // shifted = false;
  693. // }
  694. // else
  695. // {
  696. // mCurKeyboard = mSymbolsShiftedKeyboard;
  697. // shifted = true;
  698. // }
  699. // Log.d("AnySoftKeyboard", "nextSymbolsKeyboard: Keyboard:"+mCurKeyboard.getKeyboardName()+". Shift:"+shifted);
  700. // //setting the keyboard
  701. // mInputView.setKeyboard(mCurKeyboard);
  702. // mCurKeyboard.setShifted(shifted);
  703. // }
  704. //
  705. // private void nextKeyboard(EditorInfo currentEditorInfo, NextKeyboardType keyboardType)
  706. // {
  707. // Log.d("AnySoftKeyboard", "nextKeyboard: keyboardType="+keyboardType+". currentEditorInfo.inputType="+currentEditorInfo.inputType);
  708. //// int variation = currentEditorInfo.inputType & EditorInfo.TYPE_MASK_VARIATION;
  709. //// if ((keyboardType == NextKeyboardType.Any) &&
  710. //// mInternetKeyboard.isEnabled() &&
  711. //// (variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
  712. //// || variation == EditorInfo.TYPE_TEXT_VARIATION_URI)) {
  713. //// //special keyboard
  714. //// Log.d("AnySoftKeyboard", "nextKeyboard: Starting in internet textbox.");
  715. //// mCurKeyboard = mInternetKeyboard;
  716. //// }
  717. //// else
  718. // {
  719. // if (mCurKeyboard == null)
  720. // {
  721. // Log.d("AnySoftKeyboard", "nextKeyboard: Looking for next keyboard. No current keyboard. mLastSelectedKeyboard:"+mLastSelectedKeyboard+".");
  722. // }
  723. // else
  724. // {
  725. // Log.d("AnySoftKeyboard", "nextKeyboard: Looking for next keyboard. Current keyboard is:"+mCurKeyboard.getKeyboardName()+". mLastSelectedKeyboard:"+mLastSelectedKeyboard/*+". isEnabled:"+mCurKeyboard.isEnabled()*/);
  726. // }
  727. // //in numeric keyboards, the LANG key will go back to the original alphabet keyboard-
  728. // //so no need to look for the next keyboard, 'mLastSelectedKeyboard' holds the last
  729. // //keyboard used.
  730. //
  731. // if ((mCurKeyboard == null) || isAlphaBetKeyboard(mCurKeyboard))
  732. // {
  733. // Log.d("AnySoftKeyboard", "nextKeyboard: Current keyboard is alphabet (or null), so i'll look for the next");
  734. // int maxTries = mEnabledKeyboards.size();
  735. // do
  736. // {
  737. // mLastSelectedKeyboard++;
  738. // if (mLastSelectedKeyboard >= mEnabledKeyboards.size())
  739. // mLastSelectedKeyboard = 0;
  740. //
  741. // AnyKeyboard aKeyboard = getLastSelectedKeyboard();
  742. // Log.d("AnySoftKeyboard", "nextKeyboard: testing: "+aKeyboard.getKeyboardName()/*+", which is "+mKeyboards.get(mLastSelectedKeyboard).isEnabled()*/+". index="+mLastSelectedKeyboard);
  743. // //if (aKeyboard.isEnabled())
  744. // //{
  745. // //we found an enabled keyboard - need to check that it OK
  746. // //for the keyboardType parameter.
  747. // if ((keyboardType == NextKeyboardType.SupportsPhysical) &&
  748. // (!(aKeyboard instanceof HardKeyboardTranslator)))
  749. // {
  750. // //not a valid keyboard, since we wanted physical handling
  751. // //and it is not
  752. // }
  753. // else
  754. // {
  755. // //HOHO! We found a valid keyboard!
  756. // maxTries = 0;
  757. // }
  758. // //}
  759. // maxTries--;
  760. // }while(maxTries > 0);
  761. // }
  762. // mCurKeyboard = getLastSelectedKeyboard();
  763. // }
  764. // Log.i("AnySoftKeyboard", "nextKeyboard: Setting next keyboard to: "+mCurKeyboard.getKeyboardName());
  765. // if (mInputView != null)
  766. // mInputView.setKeyboard(mCurKeyboard);
  767. // updateShiftKeyState(currentEditorInfo);
  768. // mCurKeyboard.setImeOptions(getResources(), currentEditorInfo.imeOptions);
  769. // mCurKeyboard.setTextVariation(getResources(), currentEditorInfo.inputType);
  770. // //now, this is a test for LTR and RTL special character.
  771. // if (mCurrentTextDirectionIsLeftToRight != mCurKeyboard.isLeftToRightLanguage())
  772. // {
  773. // //we switched directions, we need to add a character before the next key.
  774. // mShouldAddDirectionMarkOnNextCharacter = true;
  775. // }
  776. // else
  777. // {
  778. // //resetting
  779. // mShouldAddDirectionMarkOnNextCharacter = false;
  780. // }
  781. // }
  782. //
  783. // private void notifyKeyboardChangeIfNeeded()
  784. // {
  785. // if (mKeyboardChangeNotification)
  786. // {
  787. // //notifying the user about the keyboard. This should be done in open keyboard only.
  788. // //getting the manager
  789. // NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  790. // //removing last notification
  791. // notificationManager.cancel(KEYBOARD_NOTIFICATION_ID);
  792. // //creating the message
  793. // Notification notification = new Notification(mCurKeyboard.getKeyboardIcon(), mCurKeyboard.getKeyboardName(), System.currentTimeMillis());
  794. //
  795. // Intent notificationIntent = new Intent();
  796. // PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
  797. //
  798. // notification.setLatestEventInfo(getApplicationContext(), "Any Soft Keyboard", mCurKeyboard.getKeyboardName(), contentIntent);
  799. // notification.flags |= Notification.FLAG_ONGOING_EVENT;
  800. // notification.flags |= Notification.FLAG_NO_CLEAR;
  801. // notification.defaults = 0;//no sound, vibrate, etc.
  802. // //notifying
  803. // notificationManager.notify(KEYBOARD_NOTIFICATION_ID, notification);
  804. // }
  805. // }
  806. //
  807. // private boolean isAlphaBetKeyboard(AnyKeyboard viewedKeyboard)
  808. // {
  809. // for(int i=0; i<mEnabledKeyboards.size(); i++)
  810. // {
  811. // if (viewedKeyboard.getKeyboardName().equalsIgnoreCase(mEnabledKeyboards.get(i).getKeyboardName()))
  812. // return true;
  813. // }
  814. //
  815. // return false;
  816. // }
  817. //
  818. // public void onText(CharSequence text) {
  819. // InputConnection ic = getCurrentInputConnection();
  820. // if (ic == null) return;
  821. // ic.beginBatchEdit();
  822. // if (mWordComposer.getTypedWord().length() > 0) {
  823. // commitTyped(ic);
  824. // }
  825. // ic.commitText(text, 0);
  826. // ic.endBatchEdit();
  827. // updateShiftKeyState(getCurrentInputEditorInfo());
  828. // }
  829. //
  830. // /**
  831. // * Update the list of available candidates from the current composing
  832. // * text. This will need to be filled in by however you are determining
  833. // * candidates.
  834. // */
  835. // private void updateCandidates()
  836. // {
  837. // Log.v("AnySoftKeyboard", "updateCandidates() entered");
  838. // if (mCompletionOn)
  839. // {
  840. // Log.v("AnySoftKeyboard", "updateCandidates() mCompletionOn");
  841. // if (mWordComposer.getTypedWord().length() > 0)
  842. // {
  843. // Log.v("AnySoftKeyboard", "updateCandidates() WordComposer.getTypedWord().length() > 0");
  844. //// ArrayList<String> list = new ArrayList<String>();
  845. //// String currentWord = mComposing.toString();
  846. //// if (mCurKeyboard.isLeftToRightLanguage())
  847. //// currentWord = ((char)8207)+currentWord;
  848. ////
  849. //// list.add(currentWord);
  850. // //asking current keyboard for suggestions
  851. // //mCurKeyboard.addSuggestions(currentWord, list);
  852. // List<CharSequence> list = mSuggest.getSuggestions(mInputView, mWordComposer, true);
  853. // Log.d("AnySoftKeyboard", "updateCandidates() get "+list.size()+" suggestions from suggestion manager.");
  854. // setSuggestions(list, true, true);
  855. // }
  856. // else
  857. // {
  858. // Log.v("AnySoftKeyboard", "updateCandidates() mCompletionOn==false");
  859. // setSuggestions(null, false, false);
  860. // }
  861. // }
  862. // Log.v("AnySoftKeyboard", "updateCandidates() exit");
  863. // }
  864. //
  865. // public void setSuggestions(List<CharSequence> suggestions, boolean completions, boolean typedWordValid) {
  866. // if (suggestions != null && suggestions.size() > 0) {
  867. // setCandidatesViewShown(true);
  868. // } else if (isExtractViewShown()) {
  869. // setCandidatesViewShown(true);
  870. // }
  871. //
  872. // if (mCompletionOn && (mCandidateView == null))
  873. // {
  874. // mCandidateView = onCreateCandidatesView();
  875. // }
  876. // if (mCandidateView != null /*&& mPredictionOn*/) {
  877. // mCandidateView.setSuggestions(suggestions, completions, typedWordValid);
  878. // }
  879. // }
  880. //
  881. // private void handleShift() {
  882. // if (mInputView == null) {
  883. // return;
  884. // }
  885. //
  886. // AnyKeyboard currentKeyboard = (AnyKeyboard)mInputView.getKeyboard();
  887. // if (isAlphaBetKeyboard(currentKeyboard))
  888. // {
  889. // // Alphabet with shift support keyboard
  890. // checkToggleCapsLock();
  891. // mInputView.setShifted(mCapsLock || !mInputView.isShifted());
  892. // }
  893. // else
  894. // {//not alpha-bet keyboard
  895. // nextSymbolsKeyboard();
  896. // }
  897. // }
  898. //
  899. // private void handleCharacter(int primaryCode, int[] keyCodes) {
  900. // primaryCode = translatePrimaryCodeFromCurrentKeyboard(primaryCode);
  901. // mWordComposer.add(primaryCode, keyCodes);
  902. //
  903. // //it is an alphabet character
  904. // CharSequence textToCommit = String.valueOf((char) primaryCode);
  905. // appendStringToInput(textToCommit);
  906. // }
  907. //
  908. // private int translatePrimaryCodeFromCurrentKeyboard(int primaryCode) {
  909. // if (isInputViewShown())
  910. // {
  911. // if (mInputView.isShifted())
  912. // {
  913. // for(Key aKey : mCurKeyboard.getKeys())
  914. // {
  915. // final int[] aKeyCodes = aKey.codes;
  916. // if (aKeyCodes[0] == primaryCode)
  917. // {
  918. // if (aKeyCodes.length > 1)
  919. // return aKeyCodes[1];//keyboard specified the shift character
  920. // else
  921. // return Character.toUpperCase(primaryCode);
  922. // }
  923. // }
  924. // //if I got here, then I'm shifted, and couldn't locate the key
  925. // //Is it pop-up?
  926. // return Character.toUpperCase(primaryCode);
  927. // }
  928. // }
  929. // return primaryCode;
  930. // }
  931. //
  932. // private void handleClose() {
  933. // commitTyped(getCurrentInputConnection());
  934. // requestHideSelf(0);
  935. // if (mInputView != null)
  936. // mInputView.closing();
  937. // }
  938. //
  939. // private void checkToggleCapsLock()
  940. // {
  941. // long now = System.currentTimeMillis();
  942. // if (mLastShiftTime + 400 > now) {
  943. // mCapsLock = !mCapsLock;
  944. // mLastShiftTime = 0;
  945. // } else {
  946. // mLastShiftTime = now;
  947. // }
  948. // }
  949. //
  950. //
  951. // public void pickSuggestionManually(CharSequence word)
  952. // {
  953. //// if (mCompletionOn && mCompletions != null && index >= 0
  954. //// && index < mCompletions.length) {
  955. //// CompletionInfo ci = mCompletions[index];
  956. //// getCurrentInputConnection().commitCompletion(ci);
  957. //// if (mCandidateView != null) {
  958. //// mCandidateView.clear();
  959. //// }
  960. //// updateShiftKeyState(getCurrentInputEditorInfo());
  961. //// } else if (mComposing.length() > 0) {
  962. //// // If we were generating candidate suggestions for the current
  963. //// // text, we would commit one of them here. But for this sample,
  964. //// // we will just commit the current text.
  965. //// commitTyped(getCurrentInputConnection());
  966. //// }
  967. // getCurrentInputConnection().commitText(word, word.length());
  968. // //mComposing.setLength(0);
  969. // mWordComposer.reset();
  970. // //simulating space
  971. // onKey((int)' ', null);
  972. // if (mCandidateView != null)
  973. // {
  974. // mCandidateView.clear();
  975. // }
  976. // }
  977. //
  978. // public void swipeRight()
  979. // {
  980. // //this should be done only if swipe was enabled
  981. // if (mChangeKeysMode.equals("3"))
  982. // {
  983. // //TODO: space/backspace (depends on direction of keyboard)
  984. // }
  985. // else
  986. // {
  987. // nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.Alphabet);
  988. // }
  989. // }
  990. //
  991. // public void swipeLeft()
  992. // {
  993. // //this should be done only if swipe was enabled
  994. // if (mChangeKeysMode.equals("3"))
  995. // {
  996. // //TODO: space/backspace (depends on direction of keyboard)
  997. // }
  998. // else
  999. // {
  1000. // nextSymbolsKeyboard();
  1001. // }
  1002. // }
  1003. //
  1004. // public void swipeDown()
  1005. // {
  1006. // handleClose();
  1007. // }
  1008. //
  1009. // public void swipeUp()
  1010. // {
  1011. // handleShift();
  1012. // }
  1013. //
  1014. // public void onPress(int primaryCode) {
  1015. // if(mVibrateOnKeyPress)
  1016. // {
  1017. // Log.d("AnySoftKeyboard", "Vibrating on key-pressed");
  1018. // ((Vibrator)getSystemService(Context.VIBRATOR_SERVICE)).vibrate(25);
  1019. // }
  1020. // if(mSoundOnKeyPress)
  1021. // {
  1022. // AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
  1023. // //Will use sound effects ONLY if the device is not muted.
  1024. // if (manager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL)
  1025. // {
  1026. // int keyFX = AudioManager.FX_KEY_CLICK;
  1027. // switch(primaryCode)
  1028. // {
  1029. // case 13:
  1030. // keyFX = AudioManager.FX_KEYPRESS_RETURN;
  1031. // case Keyboard.KEYCODE_DELETE:
  1032. // keyFX = AudioManager.FX_KEYPRESS_DELETE;
  1033. // case 32:
  1034. // keyFX = AudioManager.FX_KEYPRESS_SPACEBAR;
  1035. // }
  1036. // int volume = manager.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
  1037. // Log.d("AnySoftKeyboard", "Sound on key-pressed. Sound ID:"+keyFX+" with volume "+volume);
  1038. //
  1039. // manager.playSoundEffect(keyFX, volume);
  1040. // }
  1041. // else
  1042. // {
  1043. // Log.d("AnySoftKeyboard", "Devices is muted. No sounds on key-pressed.");
  1044. // }
  1045. // }
  1046. // }
  1047. //
  1048. // public void onRelease(int primaryCode) {
  1049. // Log.v("AnySoftKeyboard", "onRelease:"+primaryCode);
  1050. // }
  1051. //
  1052. // public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
  1053. // String key) {
  1054. // Log.d("AnySoftKeyboard", "onSharedPreferenceChanged - key:"+key);
  1055. //
  1056. // mEnabledKeyboards = null;
  1057. //
  1058. // onInitializeInterface();
  1059. // }
  1060. //
  1061. // @Override
  1062. // public void onConfigurationChanged(Configuration newConfig) {
  1063. // Log.d("AnySoftKeyboard", "onConfigurationChanged - newConfig - newConfig.hardKeyboardHidden:"+newConfig.hardKeyboardHidden+" keyboard:"+newConfig.keyboard+" keyboardHidden:"+newConfig.keyboardHidden);
  1064. // super.onConfigurationChanged(newConfig);
  1065. // }
  1066. //
  1067. // public void appendCharactersToInput(CharSequence textToCommit)
  1068. // {
  1069. // mWordComposer.append(textToCommit);
  1070. // //mComposing.append(textToCommit);
  1071. // appendStringToInput(textToCommit);
  1072. // }
  1073. //
  1074. // private void appendStringToInput(CharSequence textToCommit) {
  1075. // handleTextDirection();
  1076. //
  1077. // if (mCompletionOn)
  1078. // {
  1079. // getCurrentInputConnection().setComposingText(mWordComposer.getTypedWord(), textToCommit.length());
  1080. // updateCandidates();
  1081. // }
  1082. // else
  1083. // commitTyped(getCurrentInputConnection());
  1084. //
  1085. // updateShiftKeyState(getCurrentInputEditorInfo());
  1086. // }
  1087. //
  1088. // public void deleteLastCharactersFromInput(int countToDelete)
  1089. // {
  1090. // final int currentLength = mWordComposer.getTypedWord().length();
  1091. // boolean shouldDeleteUsingCompletion;
  1092. // if (currentLength > 0)
  1093. // {
  1094. // shouldDeleteUsingCompletion = true;
  1095. // if (currentLength > countToDelete) {
  1096. // //mComposing.delete(currentLength - countToDelete, currentLength);
  1097. //
  1098. // mWordComposer.deleteLast(countToDelete);
  1099. // }
  1100. // else
  1101. // {
  1102. // //mComposing.setLength(0);
  1103. // mWordComposer.reset();
  1104. // }
  1105. // }
  1106. // else
  1107. // {
  1108. // shouldDeleteUsingCompletion = false;
  1109. // }
  1110. //
  1111. // if (mCompletionOn && shouldDeleteUsingCompletion)
  1112. // {
  1113. // getCurrentInputConnection().setComposingText(mWordComposer.getTypedWord(), 1);
  1114. // updateCandidates();
  1115. // }
  1116. // else
  1117. // {
  1118. // getCurrentInputConnection().deleteSurroundingText(countToDelete, 0);
  1119. // }
  1120. // updateShiftKeyState(getCurrentInputEditorInfo());
  1121. // }
  1122. //
  1123. // public SharedPreferences getSharedPreferences() {
  1124. // return PreferenceManager.getDefaultSharedPreferences(this);
  1125. // }
  1126. //}