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 */
16
17package com.android.settings.bluetooth;
18
19import android.text.InputFilter;
20import android.text.Spanned;
21
22/**
23 * This filter will constrain edits so that the text length is not
24 * greater than the specified number of bytes using UTF-8 encoding.
25 * <p>The JNI method used by {@link android.server.BluetoothService}
26 * to convert UTF-16 to UTF-8 doesn't support surrogate pairs,
27 * therefore code points outside of the basic multilingual plane
28 * (0000-FFFF) will be encoded as a pair of 3-byte UTF-8 characters,
29 * rather than a single 4-byte UTF-8 encoding. Dalvik implements this
30 * conversion in {@code convertUtf16ToUtf8()} in
31 * {@code dalvik/vm/UtfString.c}.
32 * <p>This JNI method is unlikely to change in the future due to
33 * backwards compatibility requirements. It's also unclear whether
34 * the installed base of Bluetooth devices would correctly handle the
35 * encoding of surrogate pairs in UTF-8 as 4 bytes rather than 6.
36 * However, this filter will still work in scenarios where surrogate
37 * pairs are encoded as 4 bytes, with the caveat that the maximum
38 * length will be constrained more conservatively than necessary.
39 */
40class Utf8ByteLengthFilter implements InputFilter {
41    private final int mMaxBytes;
42
43    Utf8ByteLengthFilter(int maxBytes) {
44        mMaxBytes = maxBytes;
45    }
46
47    public CharSequence filter(CharSequence source, int start, int end,
48                               Spanned dest, int dstart, int dend) {
49        int srcByteCount = 0;
50        // count UTF-8 bytes in source substring
51        for (int i = start; i < end; i++) {
52            char c = source.charAt(i);
53            srcByteCount += (c < (char) 0x0080) ? 1 : (c < (char) 0x0800 ? 2 : 3);
54        }
55        int destLen = dest.length();
56        int destByteCount = 0;
57        // count UTF-8 bytes in destination excluding replaced section
58        for (int i = 0; i < destLen; i++) {
59            if (i < dstart || i >= dend) {
60                char c = dest.charAt(i);
61                destByteCount += (c < (char) 0x0080) ? 1 : (c < (char) 0x0800 ? 2 : 3);
62            }
63        }
64        int keepBytes = mMaxBytes - destByteCount;
65        if (keepBytes <= 0) {
66            return "";
67        } else if (keepBytes >= srcByteCount) {
68            return null; // use original dest string
69        } else {
70            // find end position of largest sequence that fits in keepBytes
71            for (int i = start; i < end; i++) {
72                char c = source.charAt(i);
73                keepBytes -= (c < (char) 0x0080) ? 1 : (c < (char) 0x0800 ? 2 : 3);
74                if (keepBytes < 0) {
75                    return source.subSequence(start, i);
76                }
77            }
78            // If the entire substring fits, we should have returned null
79            // above, so this line should not be reached. If for some
80            // reason it is, return null to use the original dest string.
81            return null;
82        }
83    }
84}
85