1/*
2 * Copyright (C) 2006 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.internal.telephony;
18
19import android.content.res.Resources;
20import android.util.SparseIntArray;
21
22import android.util.Log;
23
24import com.android.internal.R;
25
26import java.util.ArrayList;
27import java.util.List;
28
29import static android.telephony.SmsMessage.ENCODING_7BIT;
30import static android.telephony.SmsMessage.MAX_USER_DATA_SEPTETS;
31import static android.telephony.SmsMessage.MAX_USER_DATA_SEPTETS_WITH_HEADER;
32
33/**
34 * This class implements the character set mapping between
35 * the GSM SMS 7-bit alphabet specified in TS 23.038 6.2.1
36 * and UTF-16
37 *
38 * {@hide}
39 */
40public class GsmAlphabet {
41    private static final String TAG = "GSM";
42
43    private GsmAlphabet() { }
44
45    //***** Constants
46
47    /**
48     * This escapes extended characters, and when present indicates that the
49     * following character should be looked up in the "extended" table.
50     *
51     * gsmToChar(GSM_EXTENDED_ESCAPE) returns 0xffff
52     */
53    public static final byte GSM_EXTENDED_ESCAPE = 0x1B;
54
55    /**
56     * User data header requires one octet for length. Count as one septet, because
57     * all combinations of header elements below will have at least one free bit
58     * when padding to the nearest septet boundary.
59     */
60    private static final int UDH_SEPTET_COST_LENGTH = 1;
61
62    /**
63     * Using a non-default language locking shift table OR single shift table
64     * requires a user data header of 3 octets, or 4 septets, plus UDH length.
65     */
66    private static final int UDH_SEPTET_COST_ONE_SHIFT_TABLE = 4;
67
68    /**
69     * Using a non-default language locking shift table AND single shift table
70     * requires a user data header of 6 octets, or 7 septets, plus UDH length.
71     */
72    private static final int UDH_SEPTET_COST_TWO_SHIFT_TABLES = 7;
73
74    /**
75     * Multi-part messages require a user data header of 5 octets, or 6 septets,
76     * plus UDH length.
77     */
78    private static final int UDH_SEPTET_COST_CONCATENATED_MESSAGE = 6;
79
80    /**
81     * Converts a char to a GSM 7 bit table index.
82     * Returns ' ' in GSM alphabet if there's no possible match. Returns
83     * GSM_EXTENDED_ESCAPE if this character is in the extended table.
84     * In this case, you must call charToGsmExtended() for the value
85     * that should follow GSM_EXTENDED_ESCAPE in the GSM alphabet string.
86     * @param c the character to convert
87     * @return the GSM 7 bit table index for the specified character
88     */
89    public static int
90    charToGsm(char c) {
91        try {
92            return charToGsm(c, false);
93        } catch (EncodeException ex) {
94            // this should never happen
95            return sCharsToGsmTables[0].get(' ', ' ');
96        }
97    }
98
99    /**
100     * Converts a char to a GSM 7 bit table index.
101     * Returns GSM_EXTENDED_ESCAPE if this character is in the extended table.
102     * In this case, you must call charToGsmExtended() for the value that
103     * should follow GSM_EXTENDED_ESCAPE in the GSM alphabet string.
104     *
105     * @param c the character to convert
106     * @param throwException If true, throws EncodeException on invalid char.
107     *   If false, returns GSM alphabet ' ' char.
108     * @throws EncodeException encode error when throwException is true
109     * @return the GSM 7 bit table index for the specified character
110     */
111    public static int
112    charToGsm(char c, boolean throwException) throws EncodeException {
113        int ret;
114
115        ret = sCharsToGsmTables[0].get(c, -1);
116
117        if (ret == -1) {
118            ret = sCharsToShiftTables[0].get(c, -1);
119
120            if (ret == -1) {
121                if (throwException) {
122                    throw new EncodeException(c);
123                } else {
124                    return sCharsToGsmTables[0].get(' ', ' ');
125                }
126            } else {
127                return GSM_EXTENDED_ESCAPE;
128            }
129        }
130
131        return ret;
132    }
133
134    /**
135     * Converts a char to an extended GSM 7 bit table index.
136     * Extended chars should be escaped with GSM_EXTENDED_ESCAPE.
137     * Returns ' ' in GSM alphabet if there's no possible match.
138     * @param c the character to convert
139     * @return the GSM 7 bit extended table index for the specified character
140     */
141    public static int
142    charToGsmExtended(char c) {
143        int ret;
144
145        ret = sCharsToShiftTables[0].get(c, -1);
146
147        if (ret == -1) {
148            return sCharsToGsmTables[0].get(' ', ' ');
149        }
150
151        return ret;
152    }
153
154    /**
155     * Converts a character in the GSM alphabet into a char.
156     *
157     * If GSM_EXTENDED_ESCAPE is passed, 0xffff is returned. In this case,
158     * the following character in the stream should be decoded with
159     * gsmExtendedToChar().
160     *
161     * If an unmappable value is passed (one greater than 127), ' ' is returned.
162     *
163     * @param gsmChar the GSM 7 bit table index to convert
164     * @return the decoded character
165     */
166    public static char
167    gsmToChar(int gsmChar) {
168        if (gsmChar >= 0 && gsmChar < 128) {
169            return sLanguageTables[0].charAt(gsmChar);
170        } else {
171            return ' ';
172        }
173    }
174
175    /**
176     * Converts a character in the extended GSM alphabet into a char
177     *
178     * if GSM_EXTENDED_ESCAPE is passed, ' ' is returned since no second
179     * extension page has yet been defined (see Note 1 in table 6.2.1.1 of
180     * TS 23.038 v7.00)
181     *
182     * If an unmappable value is passed, the character from the GSM 7 bit
183     * default table will be used (table 6.2.1.1 of TS 23.038).
184     *
185     * @param gsmChar the GSM 7 bit extended table index to convert
186     * @return the decoded character
187     */
188    public static char
189    gsmExtendedToChar(int gsmChar) {
190        if (gsmChar == GSM_EXTENDED_ESCAPE) {
191            return ' ';
192        } else if (gsmChar >= 0 && gsmChar < 128) {
193            char c = sLanguageShiftTables[0].charAt(gsmChar);
194            if (c == ' ') {
195                return sLanguageTables[0].charAt(gsmChar);
196            } else {
197                return c;
198            }
199        } else {
200            return ' ';     // out of range
201        }
202    }
203
204    /**
205     * Converts a String into a byte array containing the 7-bit packed
206     * GSM Alphabet representation of the string. If a header is provided,
207     * this is included in the returned byte array and padded to a septet
208     * boundary.
209     *
210     * Unencodable chars are encoded as spaces
211     *
212     * Byte 0 in the returned byte array is the count of septets used,
213     * including the header and header padding. The returned byte array is
214     * the minimum size required to store the packed septets. The returned
215     * array cannot contain more than 255 septets.
216     *
217     * @param data The text string to encode.
218     * @param header Optional header (including length byte) that precedes
219     * the encoded data, padded to septet boundary.
220     * @param languageTable the 7 bit language table, or 0 for the default GSM alphabet
221     * @param languageShiftTable the 7 bit single shift language table, or 0 for the default
222     *     GSM extension table
223     * @return Byte array containing header and encoded data.
224     * @throws EncodeException if String is too large to encode
225     */
226    public static byte[] stringToGsm7BitPackedWithHeader(String data, byte[] header,
227            int languageTable, int languageShiftTable)
228            throws EncodeException {
229        if (header == null || header.length == 0) {
230            return stringToGsm7BitPacked(data, languageTable, languageShiftTable);
231        }
232
233        int headerBits = (header.length + 1) * 8;
234        int headerSeptets = (headerBits + 6) / 7;
235
236        byte[] ret = stringToGsm7BitPacked(data, headerSeptets, true, languageTable,
237                languageShiftTable);
238
239        // Paste in the header
240        ret[1] = (byte)header.length;
241        System.arraycopy(header, 0, ret, 2, header.length);
242        return ret;
243    }
244
245    /**
246     * Converts a String into a byte array containing
247     * the 7-bit packed GSM Alphabet representation of the string.
248     *
249     * Unencodable chars are encoded as spaces
250     *
251     * Byte 0 in the returned byte array is the count of septets used
252     * The returned byte array is the minimum size required to store
253     * the packed septets. The returned array cannot contain more than 255
254     * septets.
255     *
256     * @param data the data string to encode
257     * @return the encoded string
258     * @throws EncodeException if String is too large to encode
259     */
260    public static byte[] stringToGsm7BitPacked(String data)
261            throws EncodeException {
262        return stringToGsm7BitPacked(data, 0, true, 0, 0);
263    }
264
265    /**
266     * Converts a String into a byte array containing
267     * the 7-bit packed GSM Alphabet representation of the string.
268     *
269     * Unencodable chars are encoded as spaces
270     *
271     * Byte 0 in the returned byte array is the count of septets used
272     * The returned byte array is the minimum size required to store
273     * the packed septets. The returned array cannot contain more than 255
274     * septets.
275     *
276     * @param data the data string to encode
277     * @param languageTable the 7 bit language table, or 0 for the default GSM alphabet
278     * @param languageShiftTable the 7 bit single shift language table, or 0 for the default
279     *     GSM extension table
280     * @return the encoded string
281     * @throws EncodeException if String is too large to encode
282     */
283    public static byte[] stringToGsm7BitPacked(String data, int languageTable,
284            int languageShiftTable)
285            throws EncodeException {
286        return stringToGsm7BitPacked(data, 0, true, languageTable, languageShiftTable);
287    }
288
289    /**
290     * Converts a String into a byte array containing
291     * the 7-bit packed GSM Alphabet representation of the string.
292     *
293     * Byte 0 in the returned byte array is the count of septets used
294     * The returned byte array is the minimum size required to store
295     * the packed septets. The returned array cannot contain more than 255
296     * septets.
297     *
298     * @param data the text to convert to septets
299     * @param startingSeptetOffset the number of padding septets to put before
300     *  the character data at the beginning of the array
301     * @param throwException If true, throws EncodeException on invalid char.
302     *   If false, replaces unencodable char with GSM alphabet space char.
303     * @param languageTable the 7 bit language table, or 0 for the default GSM alphabet
304     * @param languageShiftTable the 7 bit single shift language table, or 0 for the default
305     *     GSM extension table
306     * @return the encoded message
307     *
308     * @throws EncodeException if String is too large to encode
309     */
310    public static byte[] stringToGsm7BitPacked(String data, int startingSeptetOffset,
311            boolean throwException, int languageTable, int languageShiftTable)
312            throws EncodeException {
313        int dataLen = data.length();
314        int septetCount = countGsmSeptetsUsingTables(data, !throwException,
315                languageTable, languageShiftTable);
316        if (septetCount == -1) {
317            throw new EncodeException("countGsmSeptetsUsingTables(): unencodable char");
318        }
319        septetCount += startingSeptetOffset;
320        if (septetCount > 255) {
321            throw new EncodeException("Payload cannot exceed 255 septets");
322        }
323        int byteCount = ((septetCount * 7) + 7) / 8;
324        byte[] ret = new byte[byteCount + 1];  // Include space for one byte length prefix.
325        SparseIntArray charToLanguageTable = sCharsToGsmTables[languageTable];
326        SparseIntArray charToShiftTable = sCharsToShiftTables[languageShiftTable];
327        for (int i = 0, septets = startingSeptetOffset, bitOffset = startingSeptetOffset * 7;
328                 i < dataLen && septets < septetCount;
329                 i++, bitOffset += 7) {
330            char c = data.charAt(i);
331            int v = charToLanguageTable.get(c, -1);
332            if (v == -1) {
333                v = charToShiftTable.get(c, -1);  // Lookup the extended char.
334                if (v == -1) {
335                    if (throwException) {
336                        throw new EncodeException("stringToGsm7BitPacked(): unencodable char");
337                    } else {
338                        v = charToLanguageTable.get(' ', ' ');   // should return ASCII space
339                    }
340                } else {
341                    packSmsChar(ret, bitOffset, GSM_EXTENDED_ESCAPE);
342                    bitOffset += 7;
343                    septets++;
344                }
345            }
346            packSmsChar(ret, bitOffset, v);
347            septets++;
348        }
349        ret[0] = (byte) (septetCount);  // Validated by check above.
350        return ret;
351    }
352
353    /**
354     * Pack a 7-bit char into its appropriate place in a byte array
355     *
356     * @param packedChars the destination byte array
357     * @param bitOffset the bit offset that the septet should be packed at
358     *                  (septet index * 7)
359     * @param value the 7-bit character to store
360     */
361    private static void
362    packSmsChar(byte[] packedChars, int bitOffset, int value) {
363        int byteOffset = bitOffset / 8;
364        int shift = bitOffset % 8;
365
366        packedChars[++byteOffset] |= value << shift;
367
368        if (shift > 1) {
369            packedChars[++byteOffset] = (byte)(value >> (8 - shift));
370        }
371    }
372
373    /**
374     * Convert a GSM alphabet 7 bit packed string (SMS string) into a
375     * {@link java.lang.String}.
376     *
377     * See TS 23.038 6.1.2.1 for SMS Character Packing
378     *
379     * @param pdu the raw data from the pdu
380     * @param offset the byte offset of
381     * @param lengthSeptets string length in septets, not bytes
382     * @return String representation or null on decoding exception
383     */
384    public static String gsm7BitPackedToString(byte[] pdu, int offset,
385            int lengthSeptets) {
386        return gsm7BitPackedToString(pdu, offset, lengthSeptets, 0, 0, 0);
387    }
388
389    /**
390     * Convert a GSM alphabet 7 bit packed string (SMS string) into a
391     * {@link java.lang.String}.
392     *
393     * See TS 23.038 6.1.2.1 for SMS Character Packing
394     *
395     * @param pdu the raw data from the pdu
396     * @param offset the byte offset of
397     * @param lengthSeptets string length in septets, not bytes
398     * @param numPaddingBits the number of padding bits before the start of the
399     *  string in the first byte
400     * @param languageTable the 7 bit language table, or 0 for the default GSM alphabet
401     * @param shiftTable the 7 bit single shift language table, or 0 for the default
402     *     GSM extension table
403     * @return String representation or null on decoding exception
404     */
405    public static String gsm7BitPackedToString(byte[] pdu, int offset,
406            int lengthSeptets, int numPaddingBits, int languageTable, int shiftTable) {
407        StringBuilder ret = new StringBuilder(lengthSeptets);
408
409        if (languageTable < 0 || languageTable > sLanguageTables.length) {
410            Log.w(TAG, "unknown language table " + languageTable + ", using default");
411            languageTable = 0;
412        }
413        if (shiftTable < 0 || shiftTable > sLanguageShiftTables.length) {
414            Log.w(TAG, "unknown single shift table " + shiftTable + ", using default");
415            shiftTable = 0;
416        }
417
418        try {
419            boolean prevCharWasEscape = false;
420            String languageTableToChar = sLanguageTables[languageTable];
421            String shiftTableToChar = sLanguageShiftTables[shiftTable];
422
423            if (languageTableToChar.isEmpty()) {
424                Log.w(TAG, "no language table for code " + languageTable + ", using default");
425                languageTableToChar = sLanguageTables[0];
426            }
427            if (shiftTableToChar.isEmpty()) {
428                Log.w(TAG, "no single shift table for code " + shiftTable + ", using default");
429                shiftTableToChar = sLanguageShiftTables[0];
430            }
431
432            for (int i = 0 ; i < lengthSeptets ; i++) {
433                int bitOffset = (7 * i) + numPaddingBits;
434
435                int byteOffset = bitOffset / 8;
436                int shift = bitOffset % 8;
437                int gsmVal;
438
439                gsmVal = (0x7f & (pdu[offset + byteOffset] >> shift));
440
441                // if it crosses a byte boundary
442                if (shift > 1) {
443                    // set msb bits to 0
444                    gsmVal &= 0x7f >> (shift - 1);
445
446                    gsmVal |= 0x7f & (pdu[offset + byteOffset + 1] << (8 - shift));
447                }
448
449                if (prevCharWasEscape) {
450                    if (gsmVal == GSM_EXTENDED_ESCAPE) {
451                        ret.append(' ');    // display ' ' for reserved double escape sequence
452                    } else {
453                        char c = shiftTableToChar.charAt(gsmVal);
454                        if (c == ' ') {
455                            ret.append(languageTableToChar.charAt(gsmVal));
456                        } else {
457                            ret.append(c);
458                        }
459                    }
460                    prevCharWasEscape = false;
461                } else if (gsmVal == GSM_EXTENDED_ESCAPE) {
462                    prevCharWasEscape = true;
463                } else {
464                    ret.append(languageTableToChar.charAt(gsmVal));
465                }
466            }
467        } catch (RuntimeException ex) {
468            Log.e(TAG, "Error GSM 7 bit packed: ", ex);
469            return null;
470        }
471
472        return ret.toString();
473    }
474
475
476    /**
477     * Convert a GSM alphabet string that's stored in 8-bit unpacked
478     * format (as it often appears in SIM records) into a String
479     *
480     * Field may be padded with trailing 0xff's. The decode stops
481     * at the first 0xff encountered.
482     *
483     * @param data the byte array to decode
484     * @param offset array offset for the first character to decode
485     * @param length the number of bytes to decode
486     * @return the decoded string
487     */
488    public static String
489    gsm8BitUnpackedToString(byte[] data, int offset, int length) {
490        // Always use GSM 7 bit default alphabet table for this method
491        String languageTableToChar = sLanguageTables[0];
492        String shiftTableToChar = sLanguageShiftTables[0];
493
494        StringBuilder ret = new StringBuilder(length);
495        boolean prevWasEscape = false;
496        for (int i = offset ; i < offset + length ; i++) {
497            // Never underestimate the pain that can be caused
498            // by signed bytes
499            int c = data[i] & 0xff;
500
501            if (c == 0xff) {
502                break;
503            } else if (c == GSM_EXTENDED_ESCAPE) {
504                if (prevWasEscape) {
505                    // Two escape chars in a row
506                    // We treat this as a space
507                    // See Note 1 in table 6.2.1.1 of TS 23.038 v7.00
508                    ret.append(' ');
509                    prevWasEscape = false;
510                } else {
511                    prevWasEscape = true;
512                }
513            } else {
514                if (prevWasEscape) {
515                    char shiftChar = shiftTableToChar.charAt(c);
516                    if (shiftChar == ' ') {
517                        // display character from main table if not present in shift table
518                        ret.append(languageTableToChar.charAt(c));
519                    } else {
520                        ret.append(shiftChar);
521                    }
522                } else {
523                    ret.append(languageTableToChar.charAt(c));
524                }
525                prevWasEscape = false;
526            }
527        }
528
529        return ret.toString();
530    }
531
532    /**
533     * Convert a string into an 8-bit unpacked GSM alphabet byte array.
534     * Always uses GSM default 7-bit alphabet and extension table.
535     * @param s the string to encode
536     * @return the 8-bit GSM encoded byte array for the string
537     */
538    public static byte[]
539    stringToGsm8BitPacked(String s) {
540        byte[] ret;
541
542        int septets = countGsmSeptetsUsingTables(s, true, 0, 0);
543
544        // Enough for all the septets and the length byte prefix
545        ret = new byte[septets];
546
547        stringToGsm8BitUnpackedField(s, ret, 0, ret.length);
548
549        return ret;
550    }
551
552
553    /**
554     * Write a String into a GSM 8-bit unpacked field of
555     * Field is padded with 0xff's, string is truncated if necessary
556     *
557     * @param s the string to encode
558     * @param dest the destination byte array
559     * @param offset the starting offset for the encoded string
560     * @param length the maximum number of bytes to write
561     */
562    public static void
563    stringToGsm8BitUnpackedField(String s, byte dest[], int offset, int length) {
564        int outByteIndex = offset;
565        SparseIntArray charToLanguageTable = sCharsToGsmTables[0];
566        SparseIntArray charToShiftTable = sCharsToShiftTables[0];
567
568        // Septets are stored in byte-aligned octets
569        for (int i = 0, sz = s.length()
570                ; i < sz && (outByteIndex - offset) < length
571                ; i++
572        ) {
573            char c = s.charAt(i);
574
575            int v = charToLanguageTable.get(c, -1);
576
577            if (v == -1) {
578                v = charToShiftTable.get(c, -1);
579                if (v == -1) {
580                    v = charToLanguageTable.get(' ', ' ');  // fall back to ASCII space
581                } else {
582                    // make sure we can fit an escaped char
583                    if (! (outByteIndex + 1 - offset < length)) {
584                        break;
585                    }
586
587                    dest[outByteIndex++] = GSM_EXTENDED_ESCAPE;
588                }
589            }
590
591            dest[outByteIndex++] = (byte)v;
592        }
593
594        // pad with 0xff's
595        while((outByteIndex - offset) < length) {
596            dest[outByteIndex++] = (byte)0xff;
597        }
598    }
599
600    /**
601     * Returns the count of 7-bit GSM alphabet characters
602     * needed to represent this character. Counts unencodable char as 1 septet.
603     * @param c the character to examine
604     * @return the number of septets for this character
605     */
606    public static int
607    countGsmSeptets(char c) {
608        try {
609            return countGsmSeptets(c, false);
610        } catch (EncodeException ex) {
611            // This should never happen.
612            return 0;
613        }
614    }
615
616    /**
617     * Returns the count of 7-bit GSM alphabet characters
618     * needed to represent this character using the default 7 bit GSM alphabet.
619     * @param c the character to examine
620     * @param throwsException If true, throws EncodeException if unencodable
621     * char. Otherwise, counts invalid char as 1 septet.
622     * @return the number of septets for this character
623     * @throws EncodeException the character can't be encoded and throwsException is true
624     */
625    public static int
626    countGsmSeptets(char c, boolean throwsException) throws EncodeException {
627        if (sCharsToGsmTables[0].get(c, -1) != -1) {
628            return 1;
629        }
630
631        if (sCharsToShiftTables[0].get(c, -1) != -1) {
632            return 2;
633        }
634
635        if (throwsException) {
636            throw new EncodeException(c);
637        } else {
638            // count as a space char
639            return 1;
640        }
641    }
642
643    /**
644     * Returns the count of 7-bit GSM alphabet characters needed
645     * to represent this string, using the specified 7-bit language table
646     * and extension table (0 for GSM default tables).
647     * @param s the Unicode string that will be encoded
648     * @param use7bitOnly allow using space in place of unencodable character if true,
649     *     otherwise, return -1 if any characters are unencodable
650     * @param languageTable the 7 bit language table, or 0 for the default GSM alphabet
651     * @param languageShiftTable the 7 bit single shift language table, or 0 for the default
652     *     GSM extension table
653     * @return the septet count for s using the specified language tables, or -1 if any
654     *     characters are unencodable and use7bitOnly is false
655     */
656    public static int countGsmSeptetsUsingTables(CharSequence s, boolean use7bitOnly,
657            int languageTable, int languageShiftTable) {
658        int count = 0;
659        int sz = s.length();
660        SparseIntArray charToLanguageTable = sCharsToGsmTables[languageTable];
661        SparseIntArray charToShiftTable = sCharsToShiftTables[languageShiftTable];
662        for (int i = 0; i < sz; i++) {
663            char c = s.charAt(i);
664            if (c == GSM_EXTENDED_ESCAPE) {
665                Log.w(TAG, "countGsmSeptets() string contains Escape character, skipping.");
666                continue;
667            }
668            if (charToLanguageTable.get(c, -1) != -1) {
669                count++;
670            } else if (charToShiftTable.get(c, -1) != -1) {
671                count += 2; // escape + shift table index
672            } else if (use7bitOnly) {
673                count++;    // encode as space
674            } else {
675                return -1;  // caller must check for this case
676            }
677        }
678        return count;
679    }
680
681    /**
682     * Returns the count of 7-bit GSM alphabet characters
683     * needed to represent this string, and the language table and
684     * language shift table used to achieve this result.
685     * For multi-part text messages, each message part may use its
686     * own language table encoding as specified in the message header
687     * for that message. However, this method will only return the
688     * optimal encoding for the message as a whole. When the individual
689     * pieces are encoded, a more optimal encoding may be chosen for each
690     * piece of the message, but the message will be split into pieces
691     * based on the encoding chosen for the message as a whole.
692     * @param s the Unicode string that will be encoded
693     * @param use7bitOnly allow using space in place of unencodable character if true,
694     *     using the language table pair with the fewest unencodable characters
695     * @return a TextEncodingDetails object containing the message and
696     *     character counts for the most efficient 7-bit encoding,
697     *     or null if there are no suitable language tables to encode the string.
698     */
699    public static SmsMessageBase.TextEncodingDetails
700    countGsmSeptets(CharSequence s, boolean use7bitOnly) {
701        // fast path for common case where no national language shift tables are enabled
702        if (sEnabledSingleShiftTables.length + sEnabledLockingShiftTables.length == 0) {
703            SmsMessageBase.TextEncodingDetails ted = new SmsMessageBase.TextEncodingDetails();
704            int septets = GsmAlphabet.countGsmSeptetsUsingTables(s, use7bitOnly, 0, 0);
705            if (septets == -1) {
706                return null;
707            }
708            ted.codeUnitSize = ENCODING_7BIT;
709            ted.codeUnitCount = septets;
710            if (septets > MAX_USER_DATA_SEPTETS) {
711                ted.msgCount = (septets + (MAX_USER_DATA_SEPTETS_WITH_HEADER - 1)) /
712                        MAX_USER_DATA_SEPTETS_WITH_HEADER;
713                ted.codeUnitsRemaining = (ted.msgCount *
714                        MAX_USER_DATA_SEPTETS_WITH_HEADER) - septets;
715            } else {
716                ted.msgCount = 1;
717                ted.codeUnitsRemaining = MAX_USER_DATA_SEPTETS - septets;
718            }
719            ted.codeUnitSize = ENCODING_7BIT;
720            return ted;
721        }
722
723        int maxSingleShiftCode = sHighestEnabledSingleShiftCode;
724        List<LanguagePairCount> lpcList = new ArrayList<LanguagePairCount>(
725                sEnabledLockingShiftTables.length + 1);
726
727        // Always add default GSM 7-bit alphabet table
728        lpcList.add(new LanguagePairCount(0));
729        for (int i : sEnabledLockingShiftTables) {
730            // Avoid adding default table twice in case 0 is in the list of allowed tables
731            if (i != 0 && !sLanguageTables[i].isEmpty()) {
732                lpcList.add(new LanguagePairCount(i));
733            }
734        }
735
736        int sz = s.length();
737        // calculate septet count for each valid table / shift table pair
738        for (int i = 0; i < sz && !lpcList.isEmpty(); i++) {
739            char c = s.charAt(i);
740            if (c == GSM_EXTENDED_ESCAPE) {
741                Log.w(TAG, "countGsmSeptets() string contains Escape character, ignoring!");
742                continue;
743            }
744            // iterate through enabled locking shift tables
745            for (LanguagePairCount lpc : lpcList) {
746                int tableIndex = sCharsToGsmTables[lpc.languageCode].get(c, -1);
747                if (tableIndex == -1) {
748                    // iterate through single shift tables for this locking table
749                    for (int table = 0; table <= maxSingleShiftCode; table++) {
750                        if (lpc.septetCounts[table] != -1) {
751                            int shiftTableIndex = sCharsToShiftTables[table].get(c, -1);
752                            if (shiftTableIndex == -1) {
753                                if (use7bitOnly) {
754                                    // can't encode char, use space instead
755                                    lpc.septetCounts[table]++;
756                                    lpc.unencodableCounts[table]++;
757                                } else {
758                                    // can't encode char, remove language pair from list
759                                    lpc.septetCounts[table] = -1;
760                                }
761                            } else {
762                                // encode as Escape + index into shift table
763                                lpc.septetCounts[table] += 2;
764                            }
765                        }
766                    }
767                } else {
768                    // encode as index into locking shift table for all pairs
769                    for (int table = 0; table <= maxSingleShiftCode; table++) {
770                        if (lpc.septetCounts[table] != -1) {
771                            lpc.septetCounts[table]++;
772                        }
773                    }
774                }
775            }
776        }
777
778        // find the least cost encoding (lowest message count and most code units remaining)
779        SmsMessageBase.TextEncodingDetails ted = new SmsMessageBase.TextEncodingDetails();
780        ted.msgCount = Integer.MAX_VALUE;
781        ted.codeUnitSize = ENCODING_7BIT;
782        int minUnencodableCount = Integer.MAX_VALUE;
783        for (LanguagePairCount lpc : lpcList) {
784            for (int shiftTable = 0; shiftTable <= maxSingleShiftCode; shiftTable++) {
785                int septets = lpc.septetCounts[shiftTable];
786                if (septets == -1) {
787                    continue;
788                }
789                int udhLength;
790                if (lpc.languageCode != 0 && shiftTable != 0) {
791                    udhLength = UDH_SEPTET_COST_LENGTH + UDH_SEPTET_COST_TWO_SHIFT_TABLES;
792                } else if (lpc.languageCode != 0 || shiftTable != 0) {
793                    udhLength = UDH_SEPTET_COST_LENGTH + UDH_SEPTET_COST_ONE_SHIFT_TABLE;
794                } else {
795                    udhLength = 0;
796                }
797                int msgCount;
798                int septetsRemaining;
799                if (septets + udhLength > MAX_USER_DATA_SEPTETS) {
800                    if (udhLength == 0) {
801                        udhLength = UDH_SEPTET_COST_LENGTH;
802                    }
803                    udhLength += UDH_SEPTET_COST_CONCATENATED_MESSAGE;
804                    int septetsPerMessage = MAX_USER_DATA_SEPTETS - udhLength;
805                    msgCount = (septets + septetsPerMessage - 1) / septetsPerMessage;
806                    septetsRemaining = (msgCount * septetsPerMessage) - septets;
807                } else {
808                    msgCount = 1;
809                    septetsRemaining = MAX_USER_DATA_SEPTETS - udhLength - septets;
810                }
811                // for 7-bit only mode, use language pair with the least unencodable chars
812                int unencodableCount = lpc.unencodableCounts[shiftTable];
813                if (use7bitOnly && unencodableCount > minUnencodableCount) {
814                    continue;
815                }
816                if ((use7bitOnly && unencodableCount < minUnencodableCount)
817                        || msgCount < ted.msgCount || (msgCount == ted.msgCount
818                        && septetsRemaining > ted.codeUnitsRemaining)) {
819                    minUnencodableCount = unencodableCount;
820                    ted.msgCount = msgCount;
821                    ted.codeUnitCount = septets;
822                    ted.codeUnitsRemaining = septetsRemaining;
823                    ted.languageTable = lpc.languageCode;
824                    ted.languageShiftTable = shiftTable;
825                }
826            }
827        }
828
829        if (ted.msgCount == Integer.MAX_VALUE) {
830            return null;
831        }
832
833        return ted;
834    }
835
836    /**
837     * Returns the index into <code>s</code> of the first character
838     * after <code>limit</code> septets have been reached, starting at
839     * index <code>start</code>.  This is used when dividing messages
840     * into units within the SMS message size limit.
841     *
842     * @param s source string
843     * @param start index of where to start counting septets
844     * @param limit maximum septets to include,
845     *   e.g. <code>MAX_USER_DATA_SEPTETS</code>
846     * @param langTable the 7 bit character table to use (0 for default GSM 7-bit alphabet)
847     * @param langShiftTable the 7 bit shift table to use (0 for default GSM extension table)
848     * @return index of first character that won't fit, or the length
849     *   of the entire string if everything fits
850     */
851    public static int
852    findGsmSeptetLimitIndex(String s, int start, int limit, int langTable, int langShiftTable) {
853        int accumulator = 0;
854        int size = s.length();
855
856        SparseIntArray charToLangTable = sCharsToGsmTables[langTable];
857        SparseIntArray charToLangShiftTable = sCharsToShiftTables[langShiftTable];
858        for (int i = start; i < size; i++) {
859            int encodedSeptet = charToLangTable.get(s.charAt(i), -1);
860            if (encodedSeptet == -1) {
861                encodedSeptet = charToLangShiftTable.get(s.charAt(i), -1);
862                if (encodedSeptet == -1) {
863                    // char not found, assume we're replacing with space
864                    accumulator++;
865                } else {
866                    accumulator += 2;  // escape character + shift table index
867                }
868            } else {
869                accumulator++;
870            }
871            if (accumulator > limit) {
872                return i;
873            }
874        }
875        return size;
876    }
877
878    /**
879     * Modify the array of enabled national language single shift tables for SMS
880     * encoding. This is used for unit testing, but could also be used to
881     * modify the enabled encodings based on the active MCC/MNC, for example.
882     *
883     * @param tables the new list of enabled single shift tables
884     */
885    static synchronized void setEnabledSingleShiftTables(int[] tables) {
886        sEnabledSingleShiftTables = tables;
887
888        if (tables.length > 0) {
889            sHighestEnabledSingleShiftCode = tables[tables.length - 1];
890        } else {
891            sHighestEnabledSingleShiftCode = 0;
892        }
893    }
894
895    /**
896     * Modify the array of enabled national language locking shift tables for SMS
897     * encoding. This is used for unit testing, but could also be used to
898     * modify the enabled encodings based on the active MCC/MNC, for example.
899     *
900     * @param tables the new list of enabled locking shift tables
901     */
902    static synchronized void setEnabledLockingShiftTables(int[] tables) {
903        sEnabledLockingShiftTables = tables;
904    }
905
906    /**
907     * Return the array of enabled national language single shift tables for SMS
908     * encoding. This is used for unit testing. The returned array is not a copy, so
909     * the caller should be careful not to modify it.
910     *
911     * @return the list of enabled single shift tables
912     */
913    static synchronized int[] getEnabledSingleShiftTables() {
914        return sEnabledSingleShiftTables;
915    }
916
917    /**
918     * Return the array of enabled national language locking shift tables for SMS
919     * encoding. This is used for unit testing. The returned array is not a copy, so
920     * the caller should be careful not to modify it.
921     *
922     * @return the list of enabled locking shift tables
923     */
924    static synchronized int[] getEnabledLockingShiftTables() {
925        return sEnabledLockingShiftTables;
926    }
927
928    /** Reverse mapping from Unicode characters to indexes into language tables. */
929    private static final SparseIntArray[] sCharsToGsmTables;
930
931    /** Reverse mapping from Unicode characters to indexes into language shift tables. */
932    private static final SparseIntArray[] sCharsToShiftTables;
933
934    /** OEM configured list of enabled national language single shift tables for encoding. */
935    private static int[] sEnabledSingleShiftTables;
936
937    /** OEM configured list of enabled national language locking shift tables for encoding. */
938    private static int[] sEnabledLockingShiftTables;
939
940    /** Highest language code to include in array of single shift counters. */
941    private static int sHighestEnabledSingleShiftCode;
942
943    /**
944     * Septet counter for a specific locking shift table and all of
945     * the single shift tables that it can be paired with.
946     */
947    private static class LanguagePairCount {
948        final int languageCode;
949        final int[] septetCounts;
950        final int[] unencodableCounts;
951        LanguagePairCount(int code) {
952            this.languageCode = code;
953            int maxSingleShiftCode = sHighestEnabledSingleShiftCode;
954            septetCounts = new int[maxSingleShiftCode + 1];
955            unencodableCounts = new int[maxSingleShiftCode + 1];
956            // set counters for disabled single shift tables to -1
957            // (GSM default extension table index 0 is always enabled)
958            for (int i = 1, tableOffset = 0; i <= maxSingleShiftCode; i++) {
959                if (sEnabledSingleShiftTables[tableOffset] == i) {
960                    tableOffset++;
961                } else {
962                    septetCounts[i] = -1;   // disabled
963                }
964            }
965            // exclude Turkish locking + Turkish single shift table and
966            // Portuguese locking + Spanish single shift table (these
967            // combinations will never be optimal for any input).
968            if (code == 1 && maxSingleShiftCode >= 1) {
969                septetCounts[1] = -1;   // Turkish + Turkish
970            } else if (code == 3 && maxSingleShiftCode >= 2) {
971                septetCounts[2] = -1;   // Portuguese + Spanish
972            }
973        }
974    }
975
976    /**
977     * GSM default 7 bit alphabet plus national language locking shift character tables.
978     * Comment lines above strings indicate the lower four bits of the table position.
979     */
980    private static final String[] sLanguageTables = {
981        /* 3GPP TS 23.038 V9.1.1 section 6.2.1 - GSM 7 bit Default Alphabet
982         01.....23.....4.....5.....6.....7.....8.....9.....A.B.....C.....D.E.....F.....0.....1 */
983        "@\u00a3$\u00a5\u00e8\u00e9\u00f9\u00ec\u00f2\u00c7\n\u00d8\u00f8\r\u00c5\u00e5\u0394_"
984            // 2.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....
985            + "\u03a6\u0393\u039b\u03a9\u03a0\u03a8\u03a3\u0398\u039e\uffff\u00c6\u00e6\u00df"
986            // F.....012.34.....56789ABCDEF0123456789ABCDEF0.....123456789ABCDEF0123456789A
987            + "\u00c9 !\"#\u00a4%&'()*+,-./0123456789:;<=>?\u00a1ABCDEFGHIJKLMNOPQRSTUVWXYZ"
988            // B.....C.....D.....E.....F.....0.....123456789ABCDEF0123456789AB.....C.....D.....
989            + "\u00c4\u00d6\u00d1\u00dc\u00a7\u00bfabcdefghijklmnopqrstuvwxyz\u00e4\u00f6\u00f1"
990            // E.....F.....
991            + "\u00fc\u00e0",
992
993        /* A.3.1 Turkish National Language Locking Shift Table
994         01.....23.....4.....5.....6.....7.....8.....9.....A.B.....C.....D.E.....F.....0.....1 */
995        "@\u00a3$\u00a5\u20ac\u00e9\u00f9\u0131\u00f2\u00c7\n\u011e\u011f\r\u00c5\u00e5\u0394_"
996            // 2.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....
997            + "\u03a6\u0393\u039b\u03a9\u03a0\u03a8\u03a3\u0398\u039e\uffff\u015e\u015f\u00df"
998            // F.....012.34.....56789ABCDEF0123456789ABCDEF0.....123456789ABCDEF0123456789A
999            + "\u00c9 !\"#\u00a4%&'()*+,-./0123456789:;<=>?\u0130ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1000            // B.....C.....D.....E.....F.....0.....123456789ABCDEF0123456789AB.....C.....D.....
1001            + "\u00c4\u00d6\u00d1\u00dc\u00a7\u00e7abcdefghijklmnopqrstuvwxyz\u00e4\u00f6\u00f1"
1002            // E.....F.....
1003            + "\u00fc\u00e0",
1004
1005        /* A.3.2 Void (no locking shift table for Spanish) */
1006        "",
1007
1008        /* A.3.3 Portuguese National Language Locking Shift Table
1009         01.....23.....4.....5.....6.....7.....8.....9.....A.B.....C.....D.E.....F.....0.....1 */
1010        "@\u00a3$\u00a5\u00ea\u00e9\u00fa\u00ed\u00f3\u00e7\n\u00d4\u00f4\r\u00c1\u00e1\u0394_"
1011            // 2.....3.....4.....5.....67.8.....9.....AB.....C.....D.....E.....F.....012.34.....
1012            + "\u00aa\u00c7\u00c0\u221e^\\\u20ac\u00d3|\uffff\u00c2\u00e2\u00ca\u00c9 !\"#\u00ba"
1013            // 56789ABCDEF0123456789ABCDEF0.....123456789ABCDEF0123456789AB.....C.....D.....E.....
1014            + "%&'()*+,-./0123456789:;<=>?\u00cdABCDEFGHIJKLMNOPQRSTUVWXYZ\u00c3\u00d5\u00da\u00dc"
1015            // F.....0123456789ABCDEF0123456789AB.....C.....DE.....F.....
1016            + "\u00a7~abcdefghijklmnopqrstuvwxyz\u00e3\u00f5`\u00fc\u00e0",
1017
1018        /* A.3.4 Bengali National Language Locking Shift Table
1019         0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....CD.EF.....0..... */
1020        "\u0981\u0982\u0983\u0985\u0986\u0987\u0988\u0989\u098a\u098b\n\u098c \r \u098f\u0990"
1021            // 123.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....F.....
1022            + "  \u0993\u0994\u0995\u0996\u0997\u0998\u0999\u099a\uffff\u099b\u099c\u099d\u099e"
1023            // 012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF.....0123456789ABC
1024            + " !\u099f\u09a0\u09a1\u09a2\u09a3\u09a4)(\u09a5\u09a6,\u09a7.\u09a80123456789:; "
1025            // D.....E.....F0.....1.....2.....3.....4.....56.....789A.....B.....C.....D.....
1026            + "\u09aa\u09ab?\u09ac\u09ad\u09ae\u09af\u09b0 \u09b2   \u09b6\u09b7\u09b8\u09b9"
1027            // E.....F.....0.....1.....2.....3.....4.....5.....6.....789.....A.....BCD.....E.....
1028            + "\u09bc\u09bd\u09be\u09bf\u09c0\u09c1\u09c2\u09c3\u09c4  \u09c7\u09c8  \u09cb\u09cc"
1029            // F.....0.....123456789ABCDEF0123456789AB.....C.....D.....E.....F.....
1030            + "\u09cd\u09ceabcdefghijklmnopqrstuvwxyz\u09d7\u09dc\u09dd\u09f0\u09f1",
1031
1032        /* A.3.5 Gujarati National Language Locking Shift Table
1033         0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....C.....D.EF.....0.....*/
1034        "\u0a81\u0a82\u0a83\u0a85\u0a86\u0a87\u0a88\u0a89\u0a8a\u0a8b\n\u0a8c\u0a8d\r \u0a8f\u0a90"
1035            // 1.....23.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....
1036            + "\u0a91 \u0a93\u0a94\u0a95\u0a96\u0a97\u0a98\u0a99\u0a9a\uffff\u0a9b\u0a9c\u0a9d"
1037            // F.....012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF.....0123456789AB
1038            + "\u0a9e !\u0a9f\u0aa0\u0aa1\u0aa2\u0aa3\u0aa4)(\u0aa5\u0aa6,\u0aa7.\u0aa80123456789:;"
1039            // CD.....E.....F0.....1.....2.....3.....4.....56.....7.....89.....A.....B.....C.....
1040            + " \u0aaa\u0aab?\u0aac\u0aad\u0aae\u0aaf\u0ab0 \u0ab2\u0ab3 \u0ab5\u0ab6\u0ab7\u0ab8"
1041            // D.....E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89.....A.....
1042            + "\u0ab9\u0abc\u0abd\u0abe\u0abf\u0ac0\u0ac1\u0ac2\u0ac3\u0ac4\u0ac5 \u0ac7\u0ac8"
1043            // B.....CD.....E.....F.....0.....123456789ABCDEF0123456789AB.....C.....D.....E.....
1044            + "\u0ac9 \u0acb\u0acc\u0acd\u0ad0abcdefghijklmnopqrstuvwxyz\u0ae0\u0ae1\u0ae2\u0ae3"
1045            // F.....
1046            + "\u0af1",
1047
1048        /* A.3.6 Hindi National Language Locking Shift Table
1049         0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....C.....D.E.....F.....*/
1050        "\u0901\u0902\u0903\u0905\u0906\u0907\u0908\u0909\u090a\u090b\n\u090c\u090d\r\u090e\u090f"
1051            // 0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....
1052            + "\u0910\u0911\u0912\u0913\u0914\u0915\u0916\u0917\u0918\u0919\u091a\uffff\u091b\u091c"
1053            // E.....F.....012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF.....012345
1054            + "\u091d\u091e !\u091f\u0920\u0921\u0922\u0923\u0924)(\u0925\u0926,\u0927.\u0928012345"
1055            // 6789ABC.....D.....E.....F0.....1.....2.....3.....4.....5.....6.....7.....8.....
1056            + "6789:;\u0929\u092a\u092b?\u092c\u092d\u092e\u092f\u0930\u0931\u0932\u0933\u0934"
1057            // 9.....A.....B.....C.....D.....E.....F.....0.....1.....2.....3.....4.....5.....6.....
1058            + "\u0935\u0936\u0937\u0938\u0939\u093c\u093d\u093e\u093f\u0940\u0941\u0942\u0943\u0944"
1059            // 7.....8.....9.....A.....B.....C.....D.....E.....F.....0.....123456789ABCDEF012345678
1060            + "\u0945\u0946\u0947\u0948\u0949\u094a\u094b\u094c\u094d\u0950abcdefghijklmnopqrstuvwx"
1061            // 9AB.....C.....D.....E.....F.....
1062            + "yz\u0972\u097b\u097c\u097e\u097f",
1063
1064        /* A.3.7 Kannada National Language Locking Shift Table
1065           NOTE: TS 23.038 V9.1.1 shows code 0x24 as \u0caa, corrected to \u0ca1 (typo)
1066         01.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....CD.E.....F.....0.....1 */
1067        " \u0c82\u0c83\u0c85\u0c86\u0c87\u0c88\u0c89\u0c8a\u0c8b\n\u0c8c \r\u0c8e\u0c8f\u0c90 "
1068            // 2.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....F.....
1069            + "\u0c92\u0c93\u0c94\u0c95\u0c96\u0c97\u0c98\u0c99\u0c9a\uffff\u0c9b\u0c9c\u0c9d\u0c9e"
1070            // 012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF.....0123456789ABC
1071            + " !\u0c9f\u0ca0\u0ca1\u0ca2\u0ca3\u0ca4)(\u0ca5\u0ca6,\u0ca7.\u0ca80123456789:; "
1072            // D.....E.....F0.....1.....2.....3.....4.....5.....6.....7.....89.....A.....B.....
1073            + "\u0caa\u0cab?\u0cac\u0cad\u0cae\u0caf\u0cb0\u0cb1\u0cb2\u0cb3 \u0cb5\u0cb6\u0cb7"
1074            // C.....D.....E.....F.....0.....1.....2.....3.....4.....5.....6.....78.....9.....
1075            + "\u0cb8\u0cb9\u0cbc\u0cbd\u0cbe\u0cbf\u0cc0\u0cc1\u0cc2\u0cc3\u0cc4 \u0cc6\u0cc7"
1076            // A.....BC.....D.....E.....F.....0.....123456789ABCDEF0123456789AB.....C.....D.....
1077            + "\u0cc8 \u0cca\u0ccb\u0ccc\u0ccd\u0cd5abcdefghijklmnopqrstuvwxyz\u0cd6\u0ce0\u0ce1"
1078            // E.....F.....
1079            + "\u0ce2\u0ce3",
1080
1081        /* A.3.8 Malayalam National Language Locking Shift Table
1082         01.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....CD.E.....F.....0.....1 */
1083        " \u0d02\u0d03\u0d05\u0d06\u0d07\u0d08\u0d09\u0d0a\u0d0b\n\u0d0c \r\u0d0e\u0d0f\u0d10 "
1084            // 2.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....F.....
1085            + "\u0d12\u0d13\u0d14\u0d15\u0d16\u0d17\u0d18\u0d19\u0d1a\uffff\u0d1b\u0d1c\u0d1d\u0d1e"
1086            // 012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF.....0123456789ABC
1087            + " !\u0d1f\u0d20\u0d21\u0d22\u0d23\u0d24)(\u0d25\u0d26,\u0d27.\u0d280123456789:; "
1088            // D.....E.....F0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.....
1089            + "\u0d2a\u0d2b?\u0d2c\u0d2d\u0d2e\u0d2f\u0d30\u0d31\u0d32\u0d33\u0d34\u0d35\u0d36"
1090            // B.....C.....D.....EF.....0.....1.....2.....3.....4.....5.....6.....78.....9.....
1091            + "\u0d37\u0d38\u0d39 \u0d3d\u0d3e\u0d3f\u0d40\u0d41\u0d42\u0d43\u0d44 \u0d46\u0d47"
1092            // A.....BC.....D.....E.....F.....0.....123456789ABCDEF0123456789AB.....C.....D.....
1093            + "\u0d48 \u0d4a\u0d4b\u0d4c\u0d4d\u0d57abcdefghijklmnopqrstuvwxyz\u0d60\u0d61\u0d62"
1094            // E.....F.....
1095            + "\u0d63\u0d79",
1096
1097        /* A.3.9 Oriya National Language Locking Shift Table
1098         0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....CD.EF.....0.....12 */
1099        "\u0b01\u0b02\u0b03\u0b05\u0b06\u0b07\u0b08\u0b09\u0b0a\u0b0b\n\u0b0c \r \u0b0f\u0b10  "
1100            // 3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....F.....01
1101            + "\u0b13\u0b14\u0b15\u0b16\u0b17\u0b18\u0b19\u0b1a\uffff\u0b1b\u0b1c\u0b1d\u0b1e !"
1102            // 2.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF.....0123456789ABCD.....
1103            + "\u0b1f\u0b20\u0b21\u0b22\u0b23\u0b24)(\u0b25\u0b26,\u0b27.\u0b280123456789:; \u0b2a"
1104            // E.....F0.....1.....2.....3.....4.....56.....7.....89.....A.....B.....C.....D.....
1105            + "\u0b2b?\u0b2c\u0b2d\u0b2e\u0b2f\u0b30 \u0b32\u0b33 \u0b35\u0b36\u0b37\u0b38\u0b39"
1106            // E.....F.....0.....1.....2.....3.....4.....5.....6.....789.....A.....BCD.....E.....
1107            + "\u0b3c\u0b3d\u0b3e\u0b3f\u0b40\u0b41\u0b42\u0b43\u0b44  \u0b47\u0b48  \u0b4b\u0b4c"
1108            // F.....0.....123456789ABCDEF0123456789AB.....C.....D.....E.....F.....
1109            + "\u0b4d\u0b56abcdefghijklmnopqrstuvwxyz\u0b57\u0b60\u0b61\u0b62\u0b63",
1110
1111        /* A.3.10 Punjabi National Language Locking Shift Table
1112         0.....1.....2.....3.....4.....5.....6.....7.....8.....9A.BCD.EF.....0.....123.....4.....*/
1113        "\u0a01\u0a02\u0a03\u0a05\u0a06\u0a07\u0a08\u0a09\u0a0a \n  \r \u0a0f\u0a10  \u0a13\u0a14"
1114            // 5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....F.....012.....3.....
1115            + "\u0a15\u0a16\u0a17\u0a18\u0a19\u0a1a\uffff\u0a1b\u0a1c\u0a1d\u0a1e !\u0a1f\u0a20"
1116            // 4.....5.....6.....7.....89A.....B.....CD.....EF.....0123456789ABCD.....E.....F0.....
1117            + "\u0a21\u0a22\u0a23\u0a24)(\u0a25\u0a26,\u0a27.\u0a280123456789:; \u0a2a\u0a2b?\u0a2c"
1118            // 1.....2.....3.....4.....56.....7.....89.....A.....BC.....D.....E.....F0.....1.....
1119            + "\u0a2d\u0a2e\u0a2f\u0a30 \u0a32\u0a33 \u0a35\u0a36 \u0a38\u0a39\u0a3c \u0a3e\u0a3f"
1120            // 2.....3.....4.....56789.....A.....BCD.....E.....F.....0.....123456789ABCDEF012345678
1121            + "\u0a40\u0a41\u0a42    \u0a47\u0a48  \u0a4b\u0a4c\u0a4d\u0a51abcdefghijklmnopqrstuvwx"
1122            // 9AB.....C.....D.....E.....F.....
1123            + "yz\u0a70\u0a71\u0a72\u0a73\u0a74",
1124
1125        /* A.3.11 Tamil National Language Locking Shift Table
1126         01.....2.....3.....4.....5.....6.....7.....8.....9A.BCD.E.....F.....0.....12.....3..... */
1127        " \u0b82\u0b83\u0b85\u0b86\u0b87\u0b88\u0b89\u0b8a \n  \r\u0b8e\u0b8f\u0b90 \u0b92\u0b93"
1128            // 4.....5.....6789.....A.....B.....CD.....EF.....012.....3456.....7.....89ABCDEF.....
1129            + "\u0b94\u0b95   \u0b99\u0b9a\uffff \u0b9c \u0b9e !\u0b9f   \u0ba3\u0ba4)(  , .\u0ba8"
1130            // 0123456789ABC.....D.....EF012.....3.....4.....5.....6.....7.....8.....9.....A.....
1131            + "0123456789:;\u0ba9\u0baa ?  \u0bae\u0baf\u0bb0\u0bb1\u0bb2\u0bb3\u0bb4\u0bb5\u0bb6"
1132            // B.....C.....D.....EF0.....1.....2.....3.....4.....5678.....9.....A.....BC.....D.....
1133            + "\u0bb7\u0bb8\u0bb9  \u0bbe\u0bbf\u0bc0\u0bc1\u0bc2   \u0bc6\u0bc7\u0bc8 \u0bca\u0bcb"
1134            // E.....F.....0.....123456789ABCDEF0123456789AB.....C.....D.....E.....F.....
1135            + "\u0bcc\u0bcd\u0bd0abcdefghijklmnopqrstuvwxyz\u0bd7\u0bf0\u0bf1\u0bf2\u0bf9",
1136
1137        /* A.3.12 Telugu National Language Locking Shift Table
1138         0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....CD.E.....F.....0.....*/
1139        "\u0c01\u0c02\u0c03\u0c05\u0c06\u0c07\u0c08\u0c09\u0c0a\u0c0b\n\u0c0c \r\u0c0e\u0c0f\u0c10"
1140            // 12.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....E.....
1141            + " \u0c12\u0c13\u0c14\u0c15\u0c16\u0c17\u0c18\u0c19\u0c1a\uffff\u0c1b\u0c1c\u0c1d"
1142            // F.....012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF.....0123456789AB
1143            + "\u0c1e !\u0c1f\u0c20\u0c21\u0c22\u0c23\u0c24)(\u0c25\u0c26,\u0c27.\u0c280123456789:;"
1144            // CD.....E.....F0.....1.....2.....3.....4.....5.....6.....7.....89.....A.....B.....
1145            + " \u0c2a\u0c2b?\u0c2c\u0c2d\u0c2e\u0c2f\u0c30\u0c31\u0c32\u0c33 \u0c35\u0c36\u0c37"
1146            // C.....D.....EF.....0.....1.....2.....3.....4.....5.....6.....78.....9.....A.....B
1147            + "\u0c38\u0c39 \u0c3d\u0c3e\u0c3f\u0c40\u0c41\u0c42\u0c43\u0c44 \u0c46\u0c47\u0c48 "
1148            // C.....D.....E.....F.....0.....123456789ABCDEF0123456789AB.....C.....D.....E.....
1149            + "\u0c4a\u0c4b\u0c4c\u0c4d\u0c55abcdefghijklmnopqrstuvwxyz\u0c56\u0c60\u0c61\u0c62"
1150            // F.....
1151            + "\u0c63",
1152
1153        /* A.3.13 Urdu National Language Locking Shift Table
1154         0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.B.....C.....D.E.....F.....*/
1155        "\u0627\u0622\u0628\u067b\u0680\u067e\u06a6\u062a\u06c2\u067f\n\u0679\u067d\r\u067a\u067c"
1156            // 0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.....B.....C.....D.....
1157            + "\u062b\u062c\u0681\u0684\u0683\u0685\u0686\u0687\u062d\u062e\u062f\uffff\u068c\u0688"
1158            // E.....F.....012.....3.....4.....5.....6.....7.....89A.....B.....CD.....EF.....012345
1159            + "\u0689\u068a !\u068f\u068d\u0630\u0631\u0691\u0693)(\u0699\u0632,\u0696.\u0698012345"
1160            // 6789ABC.....D.....E.....F0.....1.....2.....3.....4.....5.....6.....7.....8.....
1161            + "6789:;\u069a\u0633\u0634?\u0635\u0636\u0637\u0638\u0639\u0641\u0642\u06a9\u06aa"
1162            // 9.....A.....B.....C.....D.....E.....F.....0.....1.....2.....3.....4.....5.....6.....
1163            + "\u06ab\u06af\u06b3\u06b1\u0644\u0645\u0646\u06ba\u06bb\u06bc\u0648\u06c4\u06d5\u06c1"
1164            // 7.....8.....9.....A.....B.....C.....D.....E.....F.....0.....123456789ABCDEF012345678
1165            + "\u06be\u0621\u06cc\u06d0\u06d2\u064d\u0650\u064f\u0657\u0654abcdefghijklmnopqrstuvwx"
1166            // 9AB.....C.....D.....E.....F.....
1167            + "yz\u0655\u0651\u0653\u0656\u0670"
1168    };
1169
1170    /**
1171     * GSM default extension table plus national language single shift character tables.
1172     */
1173    private static final String[] sLanguageShiftTables = new String[]{
1174        /* 6.2.1.1 GSM 7 bit Default Alphabet Extension Table
1175         0123456789A.....BCDEF0123456789ABCDEF0123456789ABCDEF.0123456789ABCDEF0123456789ABCDEF */
1176        "          \u000c         ^                   {}     \\            [~] |               "
1177            // 0123456789ABCDEF012345.....6789ABCDEF0123456789ABCDEF
1178            + "                     \u20ac                          ",
1179
1180        /* A.2.1 Turkish National Language Single Shift Table
1181         0123456789A.....BCDEF0123456789ABCDEF0123456789ABCDEF.0123456789ABCDEF01234567.....8 */
1182        "          \u000c         ^                   {}     \\            [~] |      \u011e "
1183            // 9.....ABCDEF0123.....456789ABCDEF0123.....45.....67.....89.....ABCDEF0123.....
1184            + "\u0130         \u015e               \u00e7 \u20ac \u011f \u0131         \u015f"
1185            // 456789ABCDEF
1186            + "            ",
1187
1188        /* A.2.2 Spanish National Language Single Shift Table
1189         0123456789.....A.....BCDEF0123456789ABCDEF0123456789ABCDEF.0123456789ABCDEF01.....23 */
1190        "         \u00e7\u000c         ^                   {}     \\            [~] |\u00c1  "
1191            // 456789.....ABCDEF.....012345.....6789ABCDEF01.....2345.....6789.....ABCDEF.....012
1192            + "     \u00cd     \u00d3     \u00da           \u00e1   \u20ac   \u00ed     \u00f3   "
1193            // 345.....6789ABCDEF
1194            + "  \u00fa          ",
1195
1196        /* A.2.3 Portuguese National Language Single Shift Table
1197         012345.....6789.....A.....B.....C.....DE.....F.....012.....3.....45.....6.....7.....8....*/
1198        "     \u00ea   \u00e7\u000c\u00d4\u00f4 \u00c1\u00e1  \u03a6\u0393^\u03a9\u03a0\u03a8\u03a3"
1199            // 9.....ABCDEF.....0123456789ABCDEF.0123456789ABCDEF01.....23456789.....ABCDE
1200            + "\u0398     \u00ca        {}     \\            [~] |\u00c0       \u00cd     "
1201            // F.....012345.....6789AB.....C.....DEF01.....2345.....6789.....ABCDEF.....01234
1202            + "\u00d3     \u00da     \u00c3\u00d5    \u00c2   \u20ac   \u00ed     \u00f3     "
1203            // 5.....6789AB.....C.....DEF.....
1204            + "\u00fa     \u00e3\u00f5  \u00e2",
1205
1206        /* A.2.4 Bengali National Language Single Shift Table
1207         01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D..... */
1208        "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u09e6\u09e7 \u09e8\u09e9"
1209            // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B.....C.....
1210            + "\u09ea\u09eb\u09ec\u09ed\u09ee\u09ef\u09df\u09e0\u09e1\u09e2{}\u09e3\u09f2\u09f3"
1211            // D.....E.....F.0.....1.....2.....3.....4.....56789ABCDEF0123456789ABCDEF
1212            + "\u09f4\u09f5\\\u09f6\u09f7\u09f8\u09f9\u09fa       [~] |ABCDEFGHIJKLMNO"
1213            // 0123456789ABCDEF012345.....6789ABCDEF0123456789ABCDEF
1214            + "PQRSTUVWXYZ          \u20ac                          ",
1215
1216        /* A.2.5 Gujarati National Language Single Shift Table
1217         01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D..... */
1218        "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0ae6\u0ae7"
1219            // E.....F.....0.....1.....2.....3.....4.....5.....6789ABCDEF.0123456789ABCDEF
1220            + "\u0ae8\u0ae9\u0aea\u0aeb\u0aec\u0aed\u0aee\u0aef  {}     \\            [~] "
1221            // 0123456789ABCDEF0123456789ABCDEF012345.....6789ABCDEF0123456789ABCDEF
1222            + "|ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac                          ",
1223
1224        /* A.2.6 Hindi National Language Single Shift Table
1225         01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D..... */
1226        "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0966\u0967"
1227            // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B.....C.....
1228            + "\u0968\u0969\u096a\u096b\u096c\u096d\u096e\u096f\u0951\u0952{}\u0953\u0954\u0958"
1229            // D.....E.....F.0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.....
1230            + "\u0959\u095a\\\u095b\u095c\u095d\u095e\u095f\u0960\u0961\u0962\u0963\u0970\u0971"
1231            // BCDEF0123456789ABCDEF0123456789ABCDEF012345.....6789ABCDEF0123456789ABCDEF
1232            + " [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac                          ",
1233
1234        /* A.2.7 Kannada National Language Single Shift Table
1235         01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D..... */
1236        "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0ce6\u0ce7"
1237            // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....BCDEF.01234567
1238            + "\u0ce8\u0ce9\u0cea\u0ceb\u0cec\u0ced\u0cee\u0cef\u0cde\u0cf1{}\u0cf2    \\        "
1239            // 89ABCDEF0123456789ABCDEF0123456789ABCDEF012345.....6789ABCDEF0123456789ABCDEF
1240            + "    [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac                          ",
1241
1242        /* A.2.8 Malayalam National Language Single Shift Table
1243         01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D..... */
1244        "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0d66\u0d67"
1245            // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B.....C.....
1246            + "\u0d68\u0d69\u0d6a\u0d6b\u0d6c\u0d6d\u0d6e\u0d6f\u0d70\u0d71{}\u0d72\u0d73\u0d74"
1247            // D.....E.....F.0.....1.....2.....3.....4.....56789ABCDEF0123456789ABCDEF0123456789A
1248            + "\u0d75\u0d7a\\\u0d7b\u0d7c\u0d7d\u0d7e\u0d7f       [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1249            // BCDEF012345.....6789ABCDEF0123456789ABCDEF
1250            + "          \u20ac                          ",
1251
1252        /* A.2.9 Oriya National Language Single Shift Table
1253         01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D..... */
1254        "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0b66\u0b67"
1255            // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B.....C.....DE
1256            + "\u0b68\u0b69\u0b6a\u0b6b\u0b6c\u0b6d\u0b6e\u0b6f\u0b5c\u0b5d{}\u0b5f\u0b70\u0b71  "
1257            // F.0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF012345.....6789ABCDEF0123456789A
1258            + "\\            [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac                     "
1259            // BCDEF
1260            + "     ",
1261
1262        /* A.2.10 Punjabi National Language Single Shift Table
1263         01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D..... */
1264        "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0a66\u0a67"
1265            // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B.....C.....
1266            + "\u0a68\u0a69\u0a6a\u0a6b\u0a6c\u0a6d\u0a6e\u0a6f\u0a59\u0a5a{}\u0a5b\u0a5c\u0a5e"
1267            // D.....EF.0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF012345.....6789ABCDEF01
1268            + "\u0a75 \\            [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac            "
1269            // 23456789ABCDEF
1270            + "              ",
1271
1272        /* A.2.11 Tamil National Language Single Shift Table
1273           NOTE: TS 23.038 V9.1.1 shows code 0x24 as \u0bef, corrected to \u0bee (typo)
1274         01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D..... */
1275        "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0964\u0965 \u0be6\u0be7"
1276            // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B.....C.....
1277            + "\u0be8\u0be9\u0bea\u0beb\u0bec\u0bed\u0bee\u0bef\u0bf3\u0bf4{}\u0bf5\u0bf6\u0bf7"
1278            // D.....E.....F.0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF012345.....6789ABC
1279            + "\u0bf8\u0bfa\\            [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac       "
1280            // DEF0123456789ABCDEF
1281            + "                   ",
1282
1283        /* A.2.12 Telugu National Language Single Shift Table
1284           NOTE: TS 23.038 V9.1.1 shows code 0x22-0x23 as \u06cc\u06cd, corrected to \u0c6c\u0c6d
1285         01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789ABC.....D.....E.....F..... */
1286        "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*   \u0c66\u0c67\u0c68\u0c69"
1287            // 0.....1.....2.....3.....4.....5.....6.....7.....89A.....B.....C.....D.....E.....F.
1288            + "\u0c6a\u0c6b\u0c6c\u0c6d\u0c6e\u0c6f\u0c58\u0c59{}\u0c78\u0c79\u0c7a\u0c7b\u0c7c\\"
1289            // 0.....1.....2.....3456789ABCDEF0123456789ABCDEF0123456789ABCDEF012345.....6789ABCD
1290            + "\u0c7d\u0c7e\u0c7f         [~] |ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac        "
1291            // EF0123456789ABCDEF
1292            + "                  ",
1293
1294        /* A.2.13 Urdu National Language Single Shift Table
1295         01.....23.....4.....5.6.....789A.....BCDEF0123.....45.....6789.....A.....BC.....D..... */
1296        "@\u00a3$\u00a5\u00bf\"\u00a4%&'\u000c*+ -/<=>\u00a1^\u00a1_#*\u0600\u0601 \u06f0\u06f1"
1297            // E.....F.....0.....1.....2.....3.....4.....5.....6.....7.....89A.....B.....C.....
1298            + "\u06f2\u06f3\u06f4\u06f5\u06f6\u06f7\u06f8\u06f9\u060c\u060d{}\u060e\u060f\u0610"
1299            // D.....E.....F.0.....1.....2.....3.....4.....5.....6.....7.....8.....9.....A.....
1300            + "\u0611\u0612\\\u0613\u0614\u061b\u061f\u0640\u0652\u0658\u066b\u066c\u0672\u0673"
1301            // B.....CDEF.....0123456789ABCDEF0123456789ABCDEF012345.....6789ABCDEF0123456789ABCDEF
1302            + "\u06cd[~]\u06d4|ABCDEFGHIJKLMNOPQRSTUVWXYZ          \u20ac                          "
1303    };
1304
1305    static {
1306        Resources r = Resources.getSystem();
1307        // See comments in frameworks/base/core/res/res/values/config.xml for allowed values
1308        sEnabledSingleShiftTables = r.getIntArray(R.array.config_sms_enabled_single_shift_tables);
1309        sEnabledLockingShiftTables = r.getIntArray(R.array.config_sms_enabled_locking_shift_tables);
1310        int numTables = sLanguageTables.length;
1311        int numShiftTables = sLanguageShiftTables.length;
1312        if (numTables != numShiftTables) {
1313            Log.e(TAG, "Error: language tables array length " + numTables +
1314                    " != shift tables array length " + numShiftTables);
1315        }
1316
1317        if (sEnabledSingleShiftTables.length > 0) {
1318            sHighestEnabledSingleShiftCode =
1319                    sEnabledSingleShiftTables[sEnabledSingleShiftTables.length-1];
1320        } else {
1321            sHighestEnabledSingleShiftCode = 0;
1322        }
1323
1324        sCharsToGsmTables = new SparseIntArray[numTables];
1325        for (int i = 0; i < numTables; i++) {
1326            String table = sLanguageTables[i];
1327
1328            int tableLen = table.length();
1329            if (tableLen != 0 && tableLen != 128) {
1330                Log.e(TAG, "Error: language tables index " + i +
1331                        " length " + tableLen + " (expected 128 or 0)");
1332            }
1333
1334            SparseIntArray charToGsmTable = new SparseIntArray(tableLen);
1335            sCharsToGsmTables[i] = charToGsmTable;
1336            for (int j = 0; j < tableLen; j++) {
1337                char c = table.charAt(j);
1338                charToGsmTable.put(c, j);
1339            }
1340        }
1341
1342        sCharsToShiftTables = new SparseIntArray[numTables];
1343        for (int i = 0; i < numShiftTables; i++) {
1344            String shiftTable = sLanguageShiftTables[i];
1345
1346            int shiftTableLen = shiftTable.length();
1347            if (shiftTableLen != 0 && shiftTableLen != 128) {
1348                Log.e(TAG, "Error: language shift tables index " + i +
1349                        " length " + shiftTableLen + " (expected 128 or 0)");
1350            }
1351
1352            SparseIntArray charToShiftTable = new SparseIntArray(shiftTableLen);
1353            sCharsToShiftTables[i] = charToShiftTable;
1354            for (int j = 0; j < shiftTableLen; j++) {
1355                char c = shiftTable.charAt(j);
1356                if (c != ' ') {
1357                    charToShiftTable.put(c, j);
1358                }
1359            }
1360        }
1361    }
1362}
1363