1 /* 2 * Copyright (C) 2014 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 com.android.inputmethod.keyboard.internal.KeySpecParser; 20 import com.android.inputmethod.latin.utils.StringUtils; 21 22 import java.util.ArrayList; 23 import java.util.Arrays; 24 25 /** 26 * The extended {@link SuggestedWords} class to represent punctuation suggestions. 27 * 28 * Each punctuation specification string is the key specification that can be parsed by 29 * {@link KeySpecParser}. 30 */ 31 public final class PunctuationSuggestions extends SuggestedWords { PunctuationSuggestions(final ArrayList<SuggestedWordInfo> punctuationsList)32 private PunctuationSuggestions(final ArrayList<SuggestedWordInfo> punctuationsList) { 33 super(punctuationsList, 34 null /* rawSuggestions */, 35 false /* typedWordValid */, 36 false /* hasAutoCorrectionCandidate */, 37 false /* isObsoleteSuggestions */, 38 INPUT_STYLE_NONE /* inputStyle */); 39 } 40 41 /** 42 * Create new instance of {@link PunctuationSuggestions} from the array of punctuation key 43 * specifications. 44 * 45 * @param punctuationSpecs The array of punctuation key specifications. 46 * @return The {@link PunctuationSuggestions} object. 47 */ newPunctuationSuggestions( final String[] punctuationSpecs)48 public static PunctuationSuggestions newPunctuationSuggestions( 49 final String[] punctuationSpecs) { 50 final ArrayList<SuggestedWordInfo> puncuationsList = new ArrayList<>(); 51 for (final String puncSpec : punctuationSpecs) { 52 puncuationsList.add(newHardCodedWordInfo(puncSpec)); 53 } 54 return new PunctuationSuggestions(puncuationsList); 55 } 56 57 /** 58 * {@inheritDoc} 59 * Note that {@link super#getWord(int)} returns a punctuation key specification text. 60 * The suggested punctuation should be gotten by parsing the key specification. 61 */ 62 @Override getWord(final int index)63 public String getWord(final int index) { 64 final String keySpec = super.getWord(index); 65 final int code = KeySpecParser.getCode(keySpec); 66 return (code == Constants.CODE_OUTPUT_TEXT) 67 ? KeySpecParser.getOutputText(keySpec) 68 : StringUtils.newSingleCodePointString(code); 69 } 70 71 /** 72 * {@inheritDoc} 73 * Note that {@link super#getWord(int)} returns a punctuation key specification text. 74 * The displayed text should be gotten by parsing the key specification. 75 */ 76 @Override getLabel(final int index)77 public String getLabel(final int index) { 78 final String keySpec = super.getWord(index); 79 return KeySpecParser.getLabel(keySpec); 80 } 81 82 /** 83 * {@inheritDoc} 84 * Note that {@link #getWord(int)} returns a suggested punctuation. We should create a 85 * {@link SuggestedWordInfo} object that represents a hard coded word. 86 */ 87 @Override getInfo(final int index)88 public SuggestedWordInfo getInfo(final int index) { 89 return newHardCodedWordInfo(getWord(index)); 90 } 91 92 /** 93 * The predicator to tell whether this object represents punctuation suggestions. 94 * @return true if this object represents punctuation suggestions. 95 */ 96 @Override isPunctuationSuggestions()97 public boolean isPunctuationSuggestions() { 98 return true; 99 } 100 101 @Override toString()102 public String toString() { 103 return "PunctuationSuggestions: " 104 + " words=" + Arrays.toString(mSuggestedWordInfoList.toArray()); 105 } 106 newHardCodedWordInfo(final String keySpec)107 private static SuggestedWordInfo newHardCodedWordInfo(final String keySpec) { 108 return new SuggestedWordInfo(keySpec, SuggestedWordInfo.MAX_SCORE, 109 SuggestedWordInfo.KIND_HARDCODED, 110 Dictionary.DICTIONARY_HARDCODED, 111 SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, 112 SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */); 113 } 114 } 115