118d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard/*
218d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard * Copyright (C) 2012 The Android Open Source Project
318d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard *
48aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * Licensed under the Apache License, Version 2.0 (the "License");
58aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * you may not use this file except in compliance with the License.
68aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * You may obtain a copy of the License at
718d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard *
88aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka *      http://www.apache.org/licenses/LICENSE-2.0
918d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard *
1018d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard * Unless required by applicable law or agreed to in writing, software
118aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * distributed under the License is distributed on an "AS IS" BASIS,
128aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * See the License for the specific language governing permissions and
148aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * limitations under the License.
1518d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard */
1618d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard
1718d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalardpackage com.android.inputmethod.latin;
1818d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard
1918d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalardimport android.view.inputmethod.EditorInfo;
2018d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard
2196845ecff62ac5a1131ce3eb8e6a06d3298dd984Jean Chalardimport java.util.Locale;
2296845ecff62ac5a1131ce3eb8e6a06d3298dd984Jean Chalard
2318d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard/**
2418d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard * Holder class for data about a word already committed but that may still be edited.
2518d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard *
2618d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard * When the user chooses to add a word to the user dictionary by pressing the appropriate
2718d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard * suggestion, a dialog is presented to give a chance to edit the word before it is actually
2818d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard * registered as a user dictionary word. If the word is actually modified, the IME needs to
2918d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard * go back and replace the word that was committed with the amended version.
3018d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard * The word we need to replace with will only be known after it's actually committed, so
3118d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard * the IME needs to take a note of what it has to replace and where it is.
3218d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard * This class encapsulates this data.
3318d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard */
34b6ca354431367b625daf9fff5fbe4b1f5ef996abKen Wakasapublic final class PositionalInfoForUserDictPendingAddition {
3518d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard    final private String mOriginalWord;
3618d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard    final private int mCursorPos; // Position of the cursor after the word
3718d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard    final private EditorInfo mEditorInfo; // On what binding this has been added
38b6b7f5e39e9ea1bf9a05203c536327a6be7e7214Jean Chalard    final private int mCapitalizedMode;
3918d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard    private String mActualWordBeingAdded;
4018d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard
4118d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard    public PositionalInfoForUserDictPendingAddition(final String word, final int cursorPos,
42b6b7f5e39e9ea1bf9a05203c536327a6be7e7214Jean Chalard            final EditorInfo editorInfo, final int capitalizedMode) {
4318d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        mOriginalWord = word;
4418d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        mCursorPos = cursorPos;
4518d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        mEditorInfo = editorInfo;
46b6b7f5e39e9ea1bf9a05203c536327a6be7e7214Jean Chalard        mCapitalizedMode = capitalizedMode;
4718d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard    }
4818d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard
4918d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard    public void setActualWordBeingAdded(final String actualWordBeingAdded) {
5018d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        mActualWordBeingAdded = actualWordBeingAdded;
5118d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard    }
5218d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard
5318d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard    /**
5418d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * Try to replace the string at the remembered position with the actual word being added.
5518d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     *
5618d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * After the user validated the word being added, the IME has to replace the old version
5718d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * (which has been committed in the text view) with the amended version if it's different.
5818d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * This method tries to do that, but may fail because the IME is not yet ready to do so -
5918d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * for example, it is still waiting for the new string, or it is waiting to return to the text
6018d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * view in which the amendment should be made. In these cases, we should keep the data
6118d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * and wait until all conditions are met.
6218d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * This method returns true if the replacement has been successfully made and this data
6318d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * can be forgotten; it returns false if the replacement can't be made yet and we need to
6418d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * keep this until a later time.
6518d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * The IME knows about the actual word being added through a callback called by the
6618d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * user dictionary facility of the device. When this callback comes, the keyboard may still
6718d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * be connected to the edition dialog, or it may have already returned to the original text
6818d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * field. Replacement has to work in both cases.
6918d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * Accordingly, this method is called at two different points in time : upon getting the
7018d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * event that a new word was added to the user dictionary, and upon starting up in a
7118d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * new text field.
7218d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * @param connection The RichInputConnection through which to contact the editor.
7318d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * @param editorInfo Information pertaining to the editor we are currently in.
7418d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * @param currentCursorPosition The current cursor position, for checking purposes.
7596845ecff62ac5a1131ce3eb8e6a06d3298dd984Jean Chalard     * @param locale The locale for changing case, if necessary
7618d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     * @return true if the edit has been successfully made, false if we need to try again later
7718d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard     */
7818d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard    public boolean tryReplaceWithActualWord(final RichInputConnection connection,
7996845ecff62ac5a1131ce3eb8e6a06d3298dd984Jean Chalard            final EditorInfo editorInfo, final int currentCursorPosition, final Locale locale) {
8018d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        // If we still don't know the actual word being added, we need to try again later.
8118d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        if (null == mActualWordBeingAdded) return false;
8218d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        // The entered text and the registered text were the same anyway : we can
8318d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        // return success right away even if focus has not returned yet to the text field we
8418d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        // want to amend.
8518d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        if (mActualWordBeingAdded.equals(mOriginalWord)) return true;
8618d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        // Not the same text field : we need to try again later. This happens when the addition
8718d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        // is reported by the user dictionary provider before the focus has moved back to the
8818d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        // original text view, so the IME is still in the text view of the dialog and has no way to
8918d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        // edit the original text view at this time.
9018d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        if (!mEditorInfo.packageName.equals(editorInfo.packageName)
9118d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard                || mEditorInfo.fieldId != editorInfo.fieldId) {
9218d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard            return false;
9318d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        }
9418d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        // Same text field, but not the same cursor position : we give up, so we return success
9518d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        // so that it won't be tried again
9618d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        if (currentCursorPosition != mCursorPos) return true;
9718d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        // We have made all the checks : do the replacement and report success
9896845ecff62ac5a1131ce3eb8e6a06d3298dd984Jean Chalard        // If this was auto-capitalized, we need to restore the case before committing
99ce6bcdd1a547c9874f05a08074cafdfea16196d6Tadashi G. Takaoka        final String wordWithCaseFixed = CapsModeUtils.applyAutoCapsMode(mActualWordBeingAdded,
10096845ecff62ac5a1131ce3eb8e6a06d3298dd984Jean Chalard                mCapitalizedMode, locale);
10118d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        connection.setComposingRegion(currentCursorPosition - mOriginalWord.length(),
10218d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard                currentCursorPosition);
10396845ecff62ac5a1131ce3eb8e6a06d3298dd984Jean Chalard        connection.commitText(wordWithCaseFixed, wordWithCaseFixed.length());
10418d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard        return true;
10518d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard    }
10618d688c94bb8e1e26de2d12445cb3096c6126f75Jean Chalard}
107