| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.text.method; |
| 17 |
|
| 18 |
import android.annotation.NonNull; |
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.content.Context; |
| 22 |
import android.graphics.Rect; |
| 23 |
import android.text.Spanned; |
| 24 |
import android.text.TextUtils; |
| 25 |
import android.util.Log; |
| 26 |
import android.view.View; |
| 27 |
import android.widget.TextView; |
| 28 |
|
| 29 |
import java.util.Locale; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class AllCapsTransformationMethod implements TransformationMethod2 { |
| 37 |
private static final String TAG = "AllCapsTransformationMethod"; |
| 38 |
|
| 39 |
private boolean mEnabled; |
| 40 |
private Locale mLocale; |
| 41 |
|
| 42 |
@UnsupportedAppUsage |
| 43 |
public AllCapsTransformationMethod(@NonNull Context context) { |
| 44 |
mLocale = context.getResources().getConfiguration().getLocales().get(0); |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public CharSequence getTransformation(@Nullable CharSequence source, View view) { |
| 49 |
if (!mEnabled) { |
| 50 |
Log.w(TAG, "Caller did not enable length changes; not transforming text"); |
| 51 |
return source; |
| 52 |
} |
| 53 |
|
| 54 |
if (source == null) { |
| 55 |
return null; |
| 56 |
} |
| 57 |
|
| 58 |
Locale locale = null; |
| 59 |
if (view instanceof TextView) { |
| 60 |
locale = ((TextView)view).getTextLocale(); |
| 61 |
} |
| 62 |
if (locale == null) { |
| 63 |
locale = mLocale; |
| 64 |
} |
| 65 |
final boolean copySpans = source instanceof Spanned; |
| 66 |
return TextUtils.toUpperCase(locale, source, copySpans); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public void onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, |
| 71 |
Rect previouslyFocusedRect) { |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public void setLengthChangesAllowed(boolean allowLengthChanges) { |
| 76 |
mEnabled = allowLengthChanges; |
| 77 |
} |
| 78 |
|
| 79 |
} |