Home | History | Annotate | Download | only in dialpadview
      1 /*
      2  * Copyright (C) 2012 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 
     17 package com.android.dialer.dialpadview;
     18 
     19 import android.telephony.PhoneNumberUtils;
     20 import android.text.Spanned;
     21 import android.text.method.DialerKeyListener;
     22 
     23 /**
     24  * {@link DialerKeyListener} with Unicode support. Converts any Unicode(e.g. Arabic) characters that
     25  * represent digits into digits before filtering the results so that we can support pasted digits
     26  * from Unicode languages.
     27  */
     28 public class UnicodeDialerKeyListener extends DialerKeyListener {
     29 
     30   public static final UnicodeDialerKeyListener INSTANCE = new UnicodeDialerKeyListener();
     31 
     32   @Override
     33   public CharSequence filter(
     34       CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
     35     final String converted =
     36         PhoneNumberUtils.convertKeypadLettersToDigits(
     37             PhoneNumberUtils.replaceUnicodeDigits(source.toString()));
     38     // PhoneNumberUtils.replaceUnicodeDigits performs a character for character replacement,
     39     // so we can assume that start and end positions should remain unchanged.
     40     CharSequence result = super.filter(converted, start, end, dest, dstart, dend);
     41     if (result == null) {
     42       if (source.equals(converted)) {
     43         // There was no conversion or filtering performed. Just return null according to
     44         // the behavior of DialerKeyListener.
     45         return null;
     46       } else {
     47         // filter returns null if the charsequence is to be returned unchanged/unfiltered.
     48         // But in this case we do want to return a modified character string (even if
     49         // none of the characters in the modified string are filtered). So if
     50         // result == null we return the unfiltered but converted numeric string instead.
     51         return converted.subSequence(start, end);
     52       }
     53     }
     54     return result;
     55   }
     56 }
     57