SuggestionSpan.java revision b3fc1a5b8b8f88eaf00b05957523cbdc0944b24b
1/* 2 * Copyright (C) 2011 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 android.text.style; 18 19import android.content.Context; 20import android.os.Parcel; 21import android.os.Parcelable; 22import android.text.ParcelableSpan; 23import android.text.TextUtils; 24 25import java.util.Arrays; 26import java.util.Locale; 27 28/** 29 * Sets correction candidates of words under this span. 30 */ 31public class SuggestionSpan implements ParcelableSpan { 32 33 /** 34 * Flag for indicating that the input is verbatim. TextView refers to this flag to determine 35 * how it displays a word with SuggestionSpan. 36 */ 37 public static final int FLAG_VERBATIM = 0x0001; 38 39 private static final int SUGGESTIONS_MAX_SIZE = 5; 40 41 /* 42 * TODO: Needs to check the validity and add a feature that TextView will change 43 * the current IME to the other IME which is specified in SuggestionSpan. 44 * An IME needs to set the span by specifying the target IME and Subtype of SuggestionSpan. 45 * And the current IME might want to specify any IME as the target IME including other IMEs. 46 */ 47 48 private final int mFlags; 49 private final String[] mSuggestions; 50 private final String mLocaleString; 51 private final String mOriginalString; 52 /* 53 * TODO: If switching IME is required, needs to add parameters for ids of InputMethodInfo 54 * and InputMethodSubtype. 55 */ 56 57 /** 58 * @param context Context for the application 59 * @param suggestions Suggestions for the string under the span 60 * @param flags Additional flags indicating how this span is handled in TextView 61 */ 62 public SuggestionSpan(Context context, String[] suggestions, int flags) { 63 this(context, null, suggestions, flags, null); 64 } 65 66 /** 67 * @param locale Locale of the suggestions 68 * @param suggestions Suggestions for the string under the span 69 * @param flags Additional flags indicating how this span is handled in TextView 70 */ 71 public SuggestionSpan(Locale locale, String[] suggestions, int flags) { 72 this(null, locale, suggestions, flags, null); 73 } 74 75 /** 76 * @param context Context for the application 77 * @param locale locale Locale of the suggestions 78 * @param suggestions Suggestions for the string under the span 79 * @param flags Additional flags indicating how this span is handled in TextView 80 * @param originalString originalString for suggestions 81 */ 82 public SuggestionSpan(Context context, Locale locale, String[] suggestions, int flags, 83 String originalString) { 84 final int N = Math.min(SUGGESTIONS_MAX_SIZE, suggestions.length); 85 mSuggestions = Arrays.copyOf(suggestions, N); 86 mFlags = flags; 87 if (context != null && locale == null) { 88 mLocaleString = context.getResources().getConfiguration().locale.toString(); 89 } else { 90 mLocaleString = locale.toString(); 91 } 92 mOriginalString = originalString; 93 } 94 95 public SuggestionSpan(Parcel src) { 96 mSuggestions = src.readStringArray(); 97 mFlags = src.readInt(); 98 mLocaleString = src.readString(); 99 mOriginalString = src.readString(); 100 } 101 102 /** 103 * @return suggestions 104 */ 105 public String[] getSuggestions() { 106 return Arrays.copyOf(mSuggestions, mSuggestions.length); 107 } 108 109 /** 110 * @return locale of suggestions 111 */ 112 public String getLocale() { 113 return mLocaleString; 114 } 115 116 /** 117 * @return original string of suggestions 118 */ 119 public String getOriginalString() { 120 return mOriginalString; 121 } 122 123 public int getFlags() { 124 return mFlags; 125 } 126 127 @Override 128 public int describeContents() { 129 return 0; 130 } 131 132 @Override 133 public void writeToParcel(Parcel dest, int flags) { 134 dest.writeStringArray(mSuggestions); 135 dest.writeInt(mFlags); 136 dest.writeString(mLocaleString); 137 dest.writeString(mOriginalString); 138 } 139 140 @Override 141 public int getSpanTypeId() { 142 return TextUtils.CORRECTION_SPAN; 143 } 144 145 public static final Parcelable.Creator<SuggestionSpan> CREATOR = 146 new Parcelable.Creator<SuggestionSpan>() { 147 @Override 148 public SuggestionSpan createFromParcel(Parcel source) { 149 return new SuggestionSpan(source); 150 } 151 152 @Override 153 public SuggestionSpan[] newArray(int size) { 154 return new SuggestionSpan[size]; 155 } 156 }; 157} 158