| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.text.method; |
| 18 |
|
| 19 |
import android.graphics.Rect; |
| 20 |
import android.text.Editable; |
| 21 |
import android.text.GetChars; |
| 22 |
import android.text.Spannable; |
| 23 |
import android.text.Spanned; |
| 24 |
import android.text.SpannedString; |
| 25 |
import android.text.TextUtils; |
| 26 |
import android.view.View; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public abstract class ReplacementTransformationMethod |
| 34 |
implements TransformationMethod |
| 35 |
{ |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
protected abstract char[] getOriginal(); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
protected abstract char[] getReplacement(); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public CharSequence getTransformation(CharSequence source, View v) { |
| 53 |
char[] original = getOriginal(); |
| 54 |
char[] replacement = getReplacement(); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
if (!(source instanceof Editable)) { |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
boolean doNothing = true; |
| 65 |
int n = original.length; |
| 66 |
for (int i = 0; i < n; i++) { |
| 67 |
if (TextUtils.indexOf(source, original[i]) >= 0) { |
| 68 |
doNothing = false; |
| 69 |
break; |
| 70 |
} |
| 71 |
} |
| 72 |
if (doNothing) { |
| 73 |
return source; |
| 74 |
} |
| 75 |
|
| 76 |
if (!(source instanceof Spannable)) { |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
if (source instanceof Spanned) { |
| 83 |
return new SpannedString(new SpannedReplacementCharSequence( |
| 84 |
(Spanned) source, |
| 85 |
original, replacement)); |
| 86 |
} else { |
| 87 |
return new ReplacementCharSequence(source, |
| 88 |
original, |
| 89 |
replacement).toString(); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if (source instanceof Spanned) { |
| 95 |
return new SpannedReplacementCharSequence((Spanned) source, |
| 96 |
original, replacement); |
| 97 |
} else { |
| 98 |
return new ReplacementCharSequence(source, original, replacement); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
public void onFocusChanged(View view, CharSequence sourceText, |
| 103 |
boolean focused, int direction, |
| 104 |
Rect previouslyFocusedRect) { |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
private static class ReplacementCharSequence |
| 109 |
implements CharSequence, GetChars { |
| 110 |
private char[] mOriginal, mReplacement; |
| 111 |
|
| 112 |
public ReplacementCharSequence(CharSequence source, char[] original, |
| 113 |
char[] replacement) { |
| 114 |
mSource = source; |
| 115 |
mOriginal = original; |
| 116 |
mReplacement = replacement; |
| 117 |
} |
| 118 |
|
| 119 |
public int length() { |
| 120 |
return mSource.length(); |
| 121 |
} |
| 122 |
|
| 123 |
public char charAt(int i) { |
| 124 |
char c = mSource.charAt(i); |
| 125 |
|
| 126 |
int n = mOriginal.length; |
| 127 |
for (int j = 0; j < n; j++) { |
| 128 |
if (c == mOriginal[j]) { |
| 129 |
c = mReplacement[j]; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return c; |
| 134 |
} |
| 135 |
|
| 136 |
public CharSequence subSequence(int start, int end) { |
| 137 |
char[] c = new char[end - start]; |
| 138 |
|
| 139 |
getChars(start, end, c, 0); |
| 140 |
return new String(c); |
| 141 |
} |
| 142 |
|
| 143 |
public String toString() { |
| 144 |
char[] c = new char[length()]; |
| 145 |
|
| 146 |
getChars(0, length(), c, 0); |
| 147 |
return new String(c); |
| 148 |
} |
| 149 |
|
| 150 |
public void getChars(int start, int end, char[] dest, int off) { |
| 151 |
TextUtils.getChars(mSource, start, end, dest, off); |
| 152 |
int offend = end - start + off; |
| 153 |
int n = mOriginal.length; |
| 154 |
|
| 155 |
for (int i = off; i < offend; i++) { |
| 156 |
char c = dest[i]; |
| 157 |
|
| 158 |
for (int j = 0; j < n; j++) { |
| 159 |
if (c == mOriginal[j]) { |
| 160 |
dest[i] = mReplacement[j]; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
private CharSequence mSource; |
| 167 |
} |
| 168 |
|
| 169 |
private static class SpannedReplacementCharSequence |
| 170 |
extends ReplacementCharSequence |
| 171 |
implements Spanned |
| 172 |
{ |
| 173 |
public SpannedReplacementCharSequence(Spanned source, char[] original, |
| 174 |
char[] replacement) { |
| 175 |
super(source, original, replacement); |
| 176 |
mSpanned = source; |
| 177 |
} |
| 178 |
|
| 179 |
public CharSequence subSequence(int start, int end) { |
| 180 |
return new SpannedString(this).subSequence(start, end); |
| 181 |
} |
| 182 |
|
| 183 |
public <T> T[] getSpans(int start, int end, Class<T> type) { |
| 184 |
return mSpanned.getSpans(start, end, type); |
| 185 |
} |
| 186 |
|
| 187 |
public int getSpanStart(Object tag) { |
| 188 |
return mSpanned.getSpanStart(tag); |
| 189 |
} |
| 190 |
|
| 191 |
public int getSpanEnd(Object tag) { |
| 192 |
return mSpanned.getSpanEnd(tag); |
| 193 |
} |
| 194 |
|
| 195 |
public int getSpanFlags(Object tag) { |
| 196 |
return mSpanned.getSpanFlags(tag); |
| 197 |
} |
| 198 |
|
| 199 |
public int nextSpanTransition(int start, int end, Class type) { |
| 200 |
return mSpanned.nextSpanTransition(start, end, type); |
| 201 |
} |
| 202 |
|
| 203 |
private Spanned mSpanned; |
| 204 |
} |
| 205 |
} |