133dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee/*
233dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee * Copyright (C) 2012 The Android Open Source Project
333dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee *
433dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee * Licensed under the Apache License, Version 2.0 (the "License");
533dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee * you may not use this file except in compliance with the License.
633dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee * You may obtain a copy of the License at
733dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee *
833dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee *      http://www.apache.org/licenses/LICENSE-2.0
933dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee *
1033dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee * Unless required by applicable law or agreed to in writing, software
1133dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee * distributed under the License is distributed on an "AS IS" BASIS,
1233dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1333dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee * See the License for the specific language governing permissions and
1433dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee * limitations under the License.
1533dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee */
1633dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee
1733dff202e94cb50c0566becdd240a015fecb4f49Yorke Leepackage com.android.dialer.dialpad;
1833dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee
1933dff202e94cb50c0566becdd240a015fecb4f49Yorke Leeimport android.telephony.PhoneNumberUtils;
2033dff202e94cb50c0566becdd240a015fecb4f49Yorke Leeimport android.text.Spanned;
2133dff202e94cb50c0566becdd240a015fecb4f49Yorke Leeimport android.text.method.DialerKeyListener;
2233dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee
2333dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee/**
2433dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee * {@link DialerKeyListener} with Unicode support. Converts any Unicode(e.g. Arabic) characters
2533dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee * that represent digits into digits before filtering the results so that we can support
2633dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee * pasted digits from Unicode languages.
2733dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee */
2833dff202e94cb50c0566becdd240a015fecb4f49Yorke Leepublic class UnicodeDialerKeyListener extends DialerKeyListener {
2933dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee    public static final UnicodeDialerKeyListener INSTANCE = new UnicodeDialerKeyListener();
3033dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee
3133dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee    @Override
3233dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee    public CharSequence filter(CharSequence source, int start, int end,
3333dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee            Spanned dest, int dstart, int dend) {
34f744f53ed980ba901b458b070a1b5eb4526b69f3Yorke Lee        final String converted = PhoneNumberUtils.convertKeypadLettersToDigits(
35f744f53ed980ba901b458b070a1b5eb4526b69f3Yorke Lee                PhoneNumberUtils.replaceUnicodeDigits(source.toString()));
3633dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee        // PhoneNumberUtils.replaceUnicodeDigits performs a character for character replacement,
3733dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee        // so we can assume that start and end positions should remain unchanged.
3833dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee        CharSequence result = super.filter(converted, start, end, dest, dstart, dend);
3933dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee        if (result == null) {
4033dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee            if (source.equals(converted)) {
4133dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee                // There was no conversion or filtering performed. Just return null according to
4233dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee                // the behavior of DialerKeyListener.
4333dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee                return null;
4433dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee            } else {
4533dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee                // filter returns null if the charsequence is to be returned unchanged/unfiltered.
4633dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee                // But in this case we do want to return a modified character string (even if
4733dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee                // none of the characters in the modified string are filtered). So if
4833dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee                // result == null we return the unfiltered but converted numeric string instead.
4933dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee                return converted.subSequence(start, end);
5033dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee            }
5133dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee        }
5233dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee        return result;
5333dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee    }
5433dff202e94cb50c0566becdd240a015fecb4f49Yorke Lee}
55