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 @android.ravenwood.annotation.RavenwoodKeepWholeClass 37 public class AllCapsTransformationMethod implements TransformationMethod2 { 38 private static final String TAG = "AllCapsTransformationMethod"; 39 40 private boolean mEnabled; 41 private Locale mLocale; 42 43 @UnsupportedAppUsage AllCapsTransformationMethod(@onNull Context context)44 public AllCapsTransformationMethod(@NonNull Context context) { 45 mLocale = context.getResources().getConfiguration().getLocales().get(0); 46 } 47 48 @Override getTransformation(@ullable CharSequence source, View view)49 public CharSequence getTransformation(@Nullable CharSequence source, View view) { 50 if (!mEnabled) { 51 Log.w(TAG, "Caller did not enable length changes; not transforming text"); 52 return source; 53 } 54 55 if (source == null) { 56 return null; 57 } 58 59 Locale locale = null; 60 if (view instanceof TextView) { 61 locale = ((TextView)view).getTextLocale(); 62 } 63 if (locale == null) { 64 locale = mLocale; 65 } 66 final boolean copySpans = source instanceof Spanned; 67 return TextUtils.toUpperCase(locale, source, copySpans); 68 } 69 70 @Override onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, Rect previouslyFocusedRect)71 public void onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, 72 Rect previouslyFocusedRect) { 73 } 74 75 @Override setLengthChangesAllowed(boolean allowLengthChanges)76 public void setLengthChangesAllowed(boolean allowLengthChanges) { 77 mEnabled = allowLengthChanges; 78 } 79 80 } 81