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