首页 > 代码库 > android Copy联系人到SIM卡时,如何对姓名自动截断
android Copy联系人到SIM卡时,如何对姓名自动截断
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<len; i+=4, j++) {
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<len; i++) {
int needSpecialEncoding = 0;
if((tmpAlphaId[i] & 0x8000) > 0) return 0;
for(int k=0; k<SpecEncodingArrLen; 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<len; 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 = http://www.mamicode.com/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 = http://www.mamicode.com/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<len; i+=4, j++) {
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);
...
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<len; i+=4, j++) {
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<len; i++) {
int needSpecialEncoding = 0;
if((tmpAlphaId[i] & 0x8000) > 0) return 0;
for(int k=0; k<SpecEncodingArrLen; 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<len; 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 = http://www.mamicode.com/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 = http://www.mamicode.com/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<len; i+=4, j++) {
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);
...
android Copy联系人到SIM卡时,如何对姓名自动截断
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。