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 */ 16package android.text.method; 17 18import android.annotation.NonNull; 19import android.annotation.Nullable; 20import android.content.Context; 21import android.graphics.Rect; 22import android.text.Spanned; 23import android.text.TextUtils; 24import android.util.Log; 25import android.view.View; 26import android.widget.TextView; 27 28import java.util.Locale; 29 30/** 31 * Transforms source text into an ALL CAPS string, locale-aware. 32 * 33 * @hide 34 */ 35public class AllCapsTransformationMethod implements TransformationMethod2 { 36 private static final String TAG = "AllCapsTransformationMethod"; 37 38 private boolean mEnabled; 39 private Locale mLocale; 40 41 public AllCapsTransformationMethod(@NonNull Context context) { 42 mLocale = context.getResources().getConfiguration().getLocales().get(0); 43 } 44 45 @Override 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 68 public void onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, 69 Rect previouslyFocusedRect) { 70 } 71 72 @Override 73 public void setLengthChangesAllowed(boolean allowLengthChanges) { 74 mEnabled = allowLengthChanges; 75 } 76 77} 78