1:添加相關函數和變量
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;
}
//進行UCS編碼
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 should 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 >= 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;
}
//返回根據SIM卡能保存的最大姓名長度,使用UCS2編碼裁剪過後的姓名,也就是我們需要使用的自動截斷後的姓名
//simTag 傳入的姓名
//dstSlotId 卡槽ID (0或者1)
//iTel ITelepony的接口,用於獲取對應SIM卡能保存的姓名的最大長度
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:將相關的頭文件包含進去:
import com.android.internal.telephony.ITelephony;
import com.android.internal.telephony.EncodeException;
import android.os.ServiceManager;
3:上面部分的code,完整的實現了cutLongNameByte 這個函數,該函數實現了對姓名進行自動截斷的功能
,可保證編碼一致性。
所以上面一段code,是可以放到聯系人中的任何java文件中的。
以在聯系人中批量copy聯系人到SIM卡為例,
在ICS及ICS以上的版本中,Copy聯系人到SIM卡是在CopyProcessor.java中完成的(GB版本無copy到SIM的feature),
所以上面的修改都可以放到CopyProcessor.java中.
在CopyProcessor.java文件中的
private void copyContactsToSim() {方法中,
找到for (int i = 0; i < maxCount; i++) {循環,在這個循環的上面加入聲明語句:
final ITelephony iTel = ITelephony.Stub.asInterface(ServiceManager
.getService(Context.TELEPHONY_SERVICE));
在循環的下面加入
if (!TextUtils.isEmpty(simTag)) {
simTag = cutLongNameByte(simTag,dstSlotId,iTel);
}語句.
同樣如果其它任何流程有需要截斷姓名以適應SIM卡,黑體部分的code(主要實現的是cutLongNameByte 這個函數,
它實現了根據SIM卡的需求對姓名進行截斷的功能),都是可以單獨移植到對應流程中。
[KK版本]:
KK上對getAdnStorageInfo的實現做了改動.
1 需要import 下面的包
import com.mediatek.common.telephony.ITelephonyEx;
import com.mediatek.telephony.TelephonyManagerEx;
import android.os.ServiceManager;
2 getAdnStorageInfo的調用參考下面的代碼:
int[] storageInfos = null;
try {
ITelephonyEx phoneEx = ITelephonyEx.Stub.asInterface(ServiceManager
.checkService("phoneEx"));
if (!mIsCancelled && phoneEx != null)
storageInfos = phoneEx.getAdnStorageInfo(simInfo.mSimSlotId);
...