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.content.Context; 21 import android.graphics.Rect; 22 import android.text.Spanned; 23 import android.text.TextUtils; 24 import android.util.Log; 25 import android.view.View; 26 import android.widget.TextView; 27 28 import java.util.Locale; 29 30 /** 31 * Transforms source text into an ALL CAPS string, locale-aware. 32 * 33 * @hide 34 */ 35 public class AllCapsTransformationMethod implements TransformationMethod2 { 36 private static final String TAG = "AllCapsTransformationMethod"; 37 38 private boolean mEnabled; 39 private Locale mLocale; 40 AllCapsTransformationMethod(@onNull Context context)41 public AllCapsTransformationMethod(@NonNull Context context) { 42 mLocale = context.getResources().getConfiguration().getLocales().get(0); 43 } 44 45 @Override getTransformation(@ullable CharSequence source, View view)46 public CharSequence getTransformation(@Nullable CharSequence source, View view) { 47 if (!mEnabled) { 48 Log.w(TAG, "Caller did not enable length changes; not transforming text"); 49 return source; 50 } 51 52 if (source == null) { 53 return null; 54 } 55 56 Locale locale = null; 57 if (view instanceof TextView) { 58 locale = ((TextView)view).getTextLocale(); 59 } 60 if (locale == null) { 61 locale = mLocale; 62 } 63 final boolean copySpans = source instanceof Spanned; 64 return TextUtils.toUpperCase(locale, source, copySpans); 65 } 66 67 @Override onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, Rect previouslyFocusedRect)68 public void onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, 69 Rect previouslyFocusedRect) { 70 } 71 72 @Override setLengthChangesAllowed(boolean allowLengthChanges)73 public void setLengthChangesAllowed(boolean allowLengthChanges) { 74 mEnabled = allowLengthChanges; 75 } 76 77 } 78