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 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 * Transforms source text into an ALL CAPS string, locale-aware. 33 * 34 * @hide 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 AllCapsTransformationMethod(@onNull Context context)43 public AllCapsTransformationMethod(@NonNull Context context) { 44 mLocale = context.getResources().getConfiguration().getLocales().get(0); 45 } 46 47 @Override getTransformation(@ullable CharSequence source, View view)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 onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, Rect previouslyFocusedRect)70 public void onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, 71 Rect previouslyFocusedRect) { 72 } 73 74 @Override setLengthChangesAllowed(boolean allowLengthChanges)75 public void setLengthChangesAllowed(boolean allowLengthChanges) { 76 mEnabled = allowLengthChanges; 77 } 78 79 } 80