How to automatically remove a name when the android Copy contact arrives at the SIM card

Source: Internet
Author: User

How to automatically remove a name when the android Copy contact arrives at the SIM card
1: Add related functions and variables
Private int SpecEncodingArrLen = 44;

Private short [] [] specialEncoding = {
{0x0040, 0x0000 },//@
{0x00A3, 0x0001 },
{0x0024, 0x0002 },
{0x00A5, 0x0003 },
{0x00E8, 0x0004 },
{0x00E9, 0x0005 },
{0x00F9, 0x0006 },
{0x00EC, 0x0007 },
{0x00F2, 0x0008 },
{0x00C7, 0x0009 },
{0x0020, 0x000A },
{0x00D8, 0x000B },
{0x00F8, 0x000C },
{0x0020, 0x000D },
{0x00C5, 0x000E },
{0x00E5, 0x000F },
{0x0394, 0x0010 },
{0x005F, 0x0011 },
{0x03A6, 0x0012 },
{0x0393, 0x0013 },
{0x039B, 0x0014 },
{0x03A9, 0x0015 },
{0x03A0, 0x0016 },
{0x03A8, 0x0017 },
{0x03A3, 0x0018 },
{0x0398, 0x0019 },
{0x039E, 0x001A },
{0x00C6, 0x001C },
{0x00E6, 0x001D },
{0x00DF, 0x001E },
{0x00C9, 0x001F },
{0x00A4, 0x0024 },
{0x00A1, 0x0040 },
{0x00C4, 0x005B },
{0x00D6, 0x005C },
{0x00D1, 0x005D },
{0x00DC, 0x005E },
{0x00A7, 0x005F },
{0x00BF, 0x0060 },
{0x00E4, 0x007B },
{0x00F6, 0x007C },
{0x00F1, 0x007D },
{0x00FC, 0x007E },
{0x00E0, 0x007F },
};


Public int encodeUCS2_0x81 (char [] src, char [] des, int maxLen)
{
Int I, j, len;
Int base = 0xFF000000;
Short [] tmpAlphaId = new short [40*4 + 4 + 1];
Char [] temp = new char [5];

Len = src. length;
For (I = 0, j = 0; I Temp [0] = src [I];
Temp [1] = src [I + 1];
Temp [2] = src [I + 2];
Temp [3] = src [I + 3];
Temp [4] = 0;
TmpAlphaId [j] = (short) rild_sms_hexCharToDecInt (temp, 4 );
}
TmpAlphaId [j] = '\ 0 ';
Len = j;

If (len <= 3) // at least 3 characters
Return 0;
If (len + 3) * 2 + 1)> maxLen) // the destinaiton buffer is not enough (include '\ 0 ')
Return 0;
For (I = 0; I Int needSpecialEncoding = 0;
If (tmpAlphaId [I] & 0x8000)> 0) return 0;
For (int k = 0; k If (tmpAlphaId [I] = specialEncoding [k] [0]) {
TmpAlphaId [I] = specialEncoding [k] [1];
NeedSpecialEncoding = 1;
Break;
}
}
If (needSpecialEncoding! = 1 ){
If (tmpAlphaId [I] <128 ){
If (tmpAlphaId [I] = 0x0020 |
TmpAlphaId [I] = 0x005E |
TmpAlphaId [I] = 0x007B |
TmpAlphaId [I] = 0x007D |
TmpAlphaId [I] = 0x005B |
TmpAlphaId [I] = 0x007E |
TmpAlphaId [I] = 0x005D |
TmpAlphaId [I] = 0x005C |
TmpAlphaId [I] = 0x007C)
Return 0;
Else
{
If (tmpAlphaId [I] = 0x0060 ){
If (base = 0xFF000000 ){
Base = 0;
TmpAlphaId [I] = 0x00E0;
} Else {
Return 0;
}
}
Continue;
}
}
If (base = 0xFF000000 ){
Base = tmpAlphaId [I] & 0x7f80;
}
TmpAlphaId [I] ^ = base;
If (tmpAlphaId [I] >=128)
Break;
TmpAlphaId [I] | = 0x80;
}

}

If (I! = Len)
Return 0;

Int realLen = 0;
For (I = 0; I If (tmpAlphaId [I] & 0xFF00 )! = 0x1B00 ){
// Do nothing
}
Else {
RealLen ++;
}
}
RealLen + = len;

Return realLen;
}


// Complete the NFS encoding.
Public String encodeATUCS (String input ){
Byte [] textPart;
StringBuilder output;

Output = new StringBuilder ();
If (input. length ()> 40)
{
Input = input. substring (0, 40 );
}

For (int I = 0; I <input. length (); I ++ ){
String hexInt = Integer. toHexString (input. charAt (I ));
For (int j = 0; j <(4-hexInt. length (); j ++)
Output. append ("0 ");
Output. append (hexInt );
}

Return output. toString ();
}


Public int rild_sms_hexCharToDecInt (char [] hex, int length)
{
Int I = 0;
Int value, digit;

For (I = 0, value = 0; I <length & hex [I]! = '\ 0'; I ++)
{
If (hex [I]> = '0' & hex [I] <= '9 ')
{
Digit = hex [I]-'0 ';
}
Else if (hex [I]> = 'A' & hex [I] <= 'F ')
{
Digit = hex [I]-'A' + 10;
}
Else if (hex [I]> = 'A' & hex [I] <= 'F ')
{
Digit = hex [I]-'A' + 10;
}
Else
{
Return-1;
}
Value = value * 16 + digit;
}

Return value;
}




Public int countTheLength (char [] line, int maxlen)
{
Char [] alphaId = new char [40*4 + 4 + 1];
Char [] temp = new char [5];
Int tmp, I, j;
Int nameLimited = maxlen;

// Pack Alpha Id
Int len = line. length;
If (len % 4 )! = 0 ){
// LOGE ("The alphaId shoshould encode using Hexdecimal: % s", line );
} Else if (len> (40*4 )){
// LOGE ("The alphaId shouldn't longer than RIL_MAX_PHB_NAME_LEN ");
}

For (I = 0, j = 0; I Temp [0] = line [I];
Temp [1] = line [I + 1];
Temp [2] = line [I + 2];
Temp [3] = line [I + 3];
Temp [4] = 0;
Tmp = rild_sms_hexCharToDecInt (temp, 4 );

If (tmp & gt; = 128 ){
Break;
}
AlphaId [j] = (char) tmp;
// AlphaId [ril_max_phb_name_len] = '\ 0 ';
}
AlphaId [j] = '\ 0 ';


If (I! = Len ){
Len/= 4;

If (encodeUCS2_0x81 (line, alphaId, alphaId. length)> 0) {// try UCS2_0x81 coding
Return (nameLimited-3 );
}
Else {
// UCS2 coding
Return (nameLimited-2)/2;
}
}
Return nameLimited;
}

// Return the maximum name length that can be saved Based on the SIM card. The cropped name is the name after the UCS2 code is used, that is, the name after automatic truncation.
// Name passed in by simTag
// DstSlotId card slot ID (0 or 1)
// ITel ITelepony interface, used to obtain the maximum length of the name that can be saved by the corresponding SIM card
Private String cutLongNameByte (String simTag, int nameLimit ){
Int len = simTag. length ();
Try {
// 7-bit string
Byte [] iraResult = com. android. internal. telephony. GsmAlphabet. stringToGsm7BitPacked (simTag );
If (iraResult. length> nameLimit & simTag. length ()> 0 ){
SimTag = simTag. substring (0, simTag. length ()-1 );
SimTag = cutLongNameByte (simTag, nameLimit );
}
} Catch (EncodeException e ){
String temp = encodeATUCS (simTag );
Int length = countTheLength (temp. toCharArray (), nameLimit );
If (len> length ){
SimTag = simTag. substring (0, length );
}
}
Return simTag;
}
2: Include relevant header files:
Import com. android. internal. telephony. ITelephony;
Import com. android. internal. telephony. EncodeException;
Import android. OS. ServiceManager;


3: the code above fully implements the cutLongNameByte function, which implements the function of automatically truncating names.
To ensure encoding consistency.
Therefore, the above code can be stored in any java file in the contact.
Take the example of copying contacts to SIM cards in batches in contacts,
In ICS and later versions, the Copy contact to SIM card is completed in CopyProcessor. java (the GB version does not have the feature to copy to SIM ),
Therefore, all the above modifications can be placed in CopyProcessor. java.

In the CopyProcessor. java file
Private void copyContactsToSim () {method,
Find the for (int I = 0; I <maxCount; I ++) {loop, and add the declaration statement above this loop:
Final ITelephony iTel = ITelephony. Stub. asInterface (ServiceManager
. GetService (Context. TELEPHONY_SERVICE ));
Add
If (! TextUtils. isEmpty (simTag )){
SimTag = cutLongNameByte (simTag, dstSlotId, iTel );
} Statement.


Similarly, if any other process needs to truncate the name to adapt to the SIM card, the code in the simhei part (mainly implements the cutLongNameByte function,
It can be transplanted to the corresponding process separately.




[KK version]:

The implementation of getAdnStorageInfo is modified on KK.

1. import the following package
Import com. mediatek. common. telephony. ITelephonyEx;
Import com. mediatek. telephony. TelephonyManagerEx;
Import android. OS. ServiceManager;


2 For the call to getAdnStorageInfo, refer to the following code:

Int [] storageInfos = null;
Try {
ITelephonyEx phoneEx = ITelephonyEx. Stub. asInterface (ServiceManager
. CheckService ("phoneEx "));
If (! MIsCancelled & phoneEx! = Null)
StorageInfos = phoneEx. getAdnStorageInfo (simInfo. mSimSlotId );
...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.