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