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