1 /* 2 * Copyright (C) 2012 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 17 package com.android.inputmethod.latin; 18 19 import android.view.inputmethod.EditorInfo; 20 21 import java.util.Locale; 22 23 /** 24 * Holder class for data about a word already committed but that may still be edited. 25 * 26 * When the user chooses to add a word to the user dictionary by pressing the appropriate 27 * suggestion, a dialog is presented to give a chance to edit the word before it is actually 28 * registered as a user dictionary word. If the word is actually modified, the IME needs to 29 * go back and replace the word that was committed with the amended version. 30 * The word we need to replace with will only be known after it's actually committed, so 31 * the IME needs to take a note of what it has to replace and where it is. 32 * This class encapsulates this data. 33 */ 34 public final class PositionalInfoForUserDictPendingAddition { 35 final private String mOriginalWord; 36 final private int mCursorPos; // Position of the cursor after the word 37 final private EditorInfo mEditorInfo; // On what binding this has been added 38 final private int mCapitalizedMode; 39 private String mActualWordBeingAdded; 40 PositionalInfoForUserDictPendingAddition(final String word, final int cursorPos, final EditorInfo editorInfo, final int capitalizedMode)41 public PositionalInfoForUserDictPendingAddition(final String word, final int cursorPos, 42 final EditorInfo editorInfo, final int capitalizedMode) { 43 mOriginalWord = word; 44 mCursorPos = cursorPos; 45 mEditorInfo = editorInfo; 46 mCapitalizedMode = capitalizedMode; 47 } 48 setActualWordBeingAdded(final String actualWordBeingAdded)49 public void setActualWordBeingAdded(final String actualWordBeingAdded) { 50 mActualWordBeingAdded = actualWordBeingAdded; 51 } 52 53 /** 54 * Try to replace the string at the remembered position with the actual word being added. 55 * 56 * After the user validated the word being added, the IME has to replace the old version 57 * (which has been committed in the text view) with the amended version if it's different. 58 * This method tries to do that, but may fail because the IME is not yet ready to do so - 59 * for example, it is still waiting for the new string, or it is waiting to return to the text 60 * view in which the amendment should be made. In these cases, we should keep the data 61 * and wait until all conditions are met. 62 * This method returns true if the replacement has been successfully made and this data 63 * can be forgotten; it returns false if the replacement can't be made yet and we need to 64 * keep this until a later time. 65 * The IME knows about the actual word being added through a callback called by the 66 * user dictionary facility of the device. When this callback comes, the keyboard may still 67 * be connected to the edition dialog, or it may have already returned to the original text 68 * field. Replacement has to work in both cases. 69 * Accordingly, this method is called at two different points in time : upon getting the 70 * event that a new word was added to the user dictionary, and upon starting up in a 71 * new text field. 72 * @param connection The RichInputConnection through which to contact the editor. 73 * @param editorInfo Information pertaining to the editor we are currently in. 74 * @param currentCursorPosition The current cursor position, for checking purposes. 75 * @param locale The locale for changing case, if necessary 76 * @return true if the edit has been successfully made, false if we need to try again later 77 */ tryReplaceWithActualWord(final RichInputConnection connection, final EditorInfo editorInfo, final int currentCursorPosition, final Locale locale)78 public boolean tryReplaceWithActualWord(final RichInputConnection connection, 79 final EditorInfo editorInfo, final int currentCursorPosition, final Locale locale) { 80 // If we still don't know the actual word being added, we need to try again later. 81 if (null == mActualWordBeingAdded) return false; 82 // The entered text and the registered text were the same anyway : we can 83 // return success right away even if focus has not returned yet to the text field we 84 // want to amend. 85 if (mActualWordBeingAdded.equals(mOriginalWord)) return true; 86 // Not the same text field : we need to try again later. This happens when the addition 87 // is reported by the user dictionary provider before the focus has moved back to the 88 // original text view, so the IME is still in the text view of the dialog and has no way to 89 // edit the original text view at this time. 90 if (!mEditorInfo.packageName.equals(editorInfo.packageName) 91 || mEditorInfo.fieldId != editorInfo.fieldId) { 92 return false; 93 } 94 // Same text field, but not the same cursor position : we give up, so we return success 95 // so that it won't be tried again 96 if (currentCursorPosition != mCursorPos) return true; 97 // We have made all the checks : do the replacement and report success 98 // If this was auto-capitalized, we need to restore the case before committing 99 final String wordWithCaseFixed = CapsModeUtils.applyAutoCapsMode(mActualWordBeingAdded, 100 mCapitalizedMode, locale); 101 connection.setComposingRegion(currentCursorPosition - mOriginalWord.length(), 102 currentCursorPosition); 103 connection.commitText(wordWithCaseFixed, wordWithCaseFixed.length()); 104 return true; 105 } 106 } 107