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