📄 vcardprovider_v21v30.java
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3)
// Source File Name: vCardProvider_V21V30.java
package android.syncml.pim.vcard;
import android.content.*;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ContentURI;
import android.provider.MediaStore;
import android.provider.SyncMLTestContacts;
import android.syncml.pim.*;
import java.io.ByteArrayInputStream;
import java.io.PrintStream;
import java.util.*;
// Referenced classes of package android.syncml.pim.vcard:
// VCardParser, VCardParser_V30
public class vCardProvider_V21V30
{
public vCardProvider_V21V30()
{
provider = null;
mParser = null;
mBuilder = null;
version = null;
curid = -1;
mIContentProvider = null;
}
public void setParam(ContentProvider provider, Context context, String version)
{
this.provider = provider;
this.context = context;
this.version = version;
mBuilder = new VDataBuilder();
}
public void setParam(IContentProvider provider, Context context, String version)
{
mIContentProvider = provider;
this.context = context;
this.version = version;
mBuilder = new VDataBuilder();
}
public String addContact(String vcardStr)
throws Exception
{
vcardStr = verifyVCard(vcardStr);
boolean isSuccess = mParser.parse(new ByteArrayInputStream(vcardStr.getBytes()), "US-ASCII", mBuilder);
if(!isSuccess)
if(version.equals("vcard2.1"))
{
System.out.println("===Parse failed for vCard 2.1 parser. Try to use 3.0 parser.");
if(provider != null)
setParam(provider, context, "vcard3.0");
else
setParam(mIContentProvider, context, "vcard3.0");
return addContact(vcardStr);
} else
{
throw new Exception("parse failed.(even use 3.0 parser)");
}
int localid = 0;
for(Iterator i$ = mBuilder.vNodeList.iterator(); i$.hasNext();)
{
VNode vnode = (VNode)i$.next();
ContentValues mapPeople = new ContentValues();
ArrayList listPhone = new ArrayList();
ArrayList listContactM = new ArrayList();
PropertyNode propNode;
for(Iterator i$ = vnode.propList.iterator(); i$.hasNext(); matchValue(propNode, mapPeople, listPhone, listContactM))
propNode = (PropertyNode)i$.next();
localid = getNextMaxID();
mapPeople.put("_sync_id", (new StringBuilder()).append("content://syncmltestcontacts/people/").append(localid).toString());
ContentURI uri = insert(android.provider.SyncMLTestContacts.People.CONTENT_URI, mapPeople);
int i;
for(i = 0; i < listPhone.size(); i++)
insert(uri.addPath("phones"), (ContentValues)listPhone.get(i));
i = 0;
while(i < listContactM.size())
{
insert(uri.addPath("contact_methods"), (ContentValues)listContactM.get(i));
i++;
}
}
return (new StringBuilder()).append("content://syncmltestcontacts/people/").append(localid).toString();
}
private void judgeVersion(String vcardStr)
{
if(version == null)
{
int verIdx = vcardStr.indexOf("\nVERSION:");
if(verIdx == -1)
{
version = "vcard2.1";
} else
{
String verStr = vcardStr.substring(verIdx, vcardStr.indexOf("\n", verIdx + 1));
if(verStr.indexOf("2.1") > 0)
version = "vcard2.1";
else
if(verStr.indexOf("3.0") > 0)
version = "vcard3.0";
else
version = "vcard2.1";
}
}
if(version.equals("vcard2.1"))
mParser = new VCardParser();
if(version.equals("vcard3.0"))
mParser = new VCardParser_V30();
}
private void matchValue(PropertyNode propNode, ContentValues mapPeople, ArrayList listPhone, ArrayList listContactM)
{
if(propNode.propName.equalsIgnoreCase("ORG") && !isNull(propNode.propValue))
mapPeople.put("company", propNode.propValue);
if(propNode.propName.equalsIgnoreCase("FN") && !isNull(propNode.propValue))
mapPeople.put("name", propNode.propValue);
if(propNode.propName.equalsIgnoreCase("N") && !isNull(propNode.propValue))
mapPeople.put("name", propNode.propValue.replaceAll(";", " ").trim());
if(propNode.propName.equalsIgnoreCase("NOTE") && !isNull(propNode.propValue))
mapPeople.put("notes", propNode.propValue);
if(propNode.propName.equalsIgnoreCase("TITLE") && !isNull(propNode.propValue))
mapPeople.put("title", propNode.propValue);
if((propNode.propName.equalsIgnoreCase("PHOTO") || propNode.propName.equalsIgnoreCase("LOGO")) && !mapPeople.containsKey("photo"))
insertImg(mapPeople, propNode);
if(propNode.propName.equalsIgnoreCase("TEL") && !isNull(propNode.propValue))
{
ContentValues mapPhone = new ContentValues();
mapPhone.put("number", propNode.propValue);
StringBuffer typeList = new StringBuffer("");
int phoneType = 0;
for(int i = 0; i < propNode.paraMap_TYPE.size(); i++)
{
String type = (String)propNode.paraMap_TYPE.get(i);
typeList.append(type).append(";");
if(type.equalsIgnoreCase("FAX"))
phoneType = 3;
if(type.equalsIgnoreCase("PAGER"))
phoneType = 5;
if(type.equalsIgnoreCase("VOICE"))
phoneType = 1;
}
mapPhone.put("label", typeList.toString());
mapPhone.put("type", Integer.valueOf(phoneType));
listPhone.add(mapPhone);
}
if(propNode.propName.equalsIgnoreCase("EMAIL") && !isNull(propNode.propValue))
{
ContentValues mapContactM = new ContentValues();
mapContactM.put("data", propNode.propValue.replaceAll(";", " ").trim());
mapContactM.put("label", listToString(propNode.paraMap_TYPE));
mapContactM.put("kind", Integer.valueOf(1));
listContactM.add(mapContactM);
}
if(propNode.propName.equalsIgnoreCase("ADR") && !isNull(propNode.propValue))
{
ContentValues mapContactM = new ContentValues();
mapContactM.put("data", propNode.propValue.replaceAll(";", " ").trim());
mapContactM.put("label", listToString(propNode.paraMap_TYPE));
mapContactM.put("kind", Integer.valueOf(2));
listContactM.add(mapContactM);
}
}
private void insertImg(ContentValues mapPeople, PropertyNode propNode)
{
if(!propNode.paraMap_TYPE.contains("JPEG"))
return;
if(propNode.propValue_byts == null)
return;
Bitmap bmp = BitmapFactory.decodeByteArray(propNode.propValue_byts, 0, propNode.propValue_byts.length);
String imguri = insert_img(bmp);
if(imguri != null)
mapPeople.put("photo", imguri);
}
private String listToString(List list)
{
StringBuilder typeListB = new StringBuilder("");
for(int i = 0; i < list.size(); i++)
{
String type = (String)list.get(i);
typeListB.append(type).append(";");
}
String typeList = typeListB.toString();
if(typeList.endsWith(";"))
return typeList.substring(0, typeList.length() - 1);
else
return typeList;
}
private String verifyVCard(String vcardStr)
{
judgeVersion(vcardStr);
vcardStr = vcardStr.replaceAll("\r\n", "\n");
String strlist[] = vcardStr.split("\n");
StringBuilder v21str = new StringBuilder("");
for(int i = 0; i < strlist.length; i++)
if(strlist[i].indexOf(":") < 0)
{
if(strlist[i].length() == 0 && strlist[i + 1].indexOf(":") > 0)
v21str.append(strlist[i]).append("\r\n");
else
v21str.append(" ").append(strlist[i]).append("\r\n");
} else
{
v21str.append(strlist[i]).append("\r\n");
}
System.out.println((new StringBuilder()).append("==after verify:\r\n").append(v21str.toString()).toString());
return v21str.toString();
}
ContentURI insert(ContentURI uri, ContentValues map)
throws Exception
{
if(provider != null)
return provider.insert(uri, map);
else
return mIContentProvider.insert(uri, map);
}
String insert_img(Bitmap bmp)
{
ContentResolver resov = context.createPackageContext("com.google.android.camera", 2).getContentResolver();
return android.provider.MediaStore.Images.Media.insertImage(resov, bmp, "picname", "picdescription");
Exception e;
e;
e.printStackTrace();
return null;
}
public void deleteContact(String uri)
throws Exception
{
try
{
Integer.parseInt(uri.substring(uri.lastIndexOf("/") + 1));
ContentValues map = new ContentValues();
map.put("_sync_id", uri);
map.put("_sync_version", "1");
map.put("_sync_mark", "1");
if(provider != null)
provider.insert(android.provider.SyncMLTestContacts.People.DELETED_CONTENT_URI, map);
else
mIContentProvider.insert(android.provider.SyncMLTestContacts.People.DELETED_CONTENT_URI, map);
}
catch(Exception e)
{
e.printStackTrace();
throw new Exception((new StringBuilder()).append("Delete failed:").append(uri).toString());
}
}
public void updateContact(String uri, String vcardStr)
throws Exception
{
int localid;
try
{
localid = Integer.parseInt(uri.substring(uri.length() - 1));
}
catch(Exception e)
{
e.printStackTrace();
throw new Exception((new StringBuilder()).append("Update failed:").append(uri).toString());
}
vcardStr = verifyVCard(vcardStr);
boolean isSuccess = mParser.parse(new ByteArrayInputStream(vcardStr.getBytes()), "US-ASCII", mBuilder);
if(!isSuccess)
if(version.equals("vcard2.1"))
{
System.out.println("===Parse failed for vCard 2.1 parser. Try to use 3.0 parser.");
if(provider != null)
setParam(provider, context, "vcard3.0");
else
setParam(mIContentProvider, context, "vcard3.0");
updateContact(uri, vcardStr);
return;
} else
{
throw new Exception("parse failed.(even use 3.0 parser)");
}
for(Iterator i$ = mBuilder.vNodeList.iterator(); i$.hasNext();)
{
VNode vnode = (VNode)i$.next();
ContentValues mapPeople = new ContentValues();
ArrayList listPhone = new ArrayList();
ArrayList listContactM = new ArrayList();
PropertyNode propNode;
for(Iterator i$ = vnode.propList.iterator(); i$.hasNext(); matchValue(propNode, mapPeople, listPhone, listContactM))
propNode = (PropertyNode)i$.next();
mapPeople.put("_sync_id", uri);
mapPeople.put("_sync_local_id", Integer.valueOf(localid));
ContentURI curi = insert(android.provider.SyncMLTestContacts.People.CONTENT_URI, mapPeople);
int i;
for(i = 0; i < listPhone.size(); i++)
insert(curi.addPath("phones"), (ContentValues)listPhone.get(i));
i = 0;
while(i < listContactM.size())
{
insert(curi.addPath("contact_methods"), (ContentValues)listContactM.get(i));
i++;
}
}
}
private int getNextMaxID()
throws Exception
{
if(curid == -1)
{
Cursor c = context.getContentResolver().query(android.provider.SyncMLTestContacts.People.CONTENT_URI, null, null, null, "people._id desc");
int maxid = 0;
if(c.next())
maxid = c.getInt(c.getColumnIndex("_id"));
c.close();
curid = maxid + 1;
return curid;
} else
{
return ++curid;
}
}
private static boolean isNull(String str)
{
return str == null || str.trim().equals("");
}
ContentProvider provider;
VParser mParser;
VDataBuilder mBuilder;
public static final String VERSION_VCARD21 = "vcard2.1";
public static final String VERSION_VCARD30 = "vcard3.0";
String version;
Context context;
int curid;
private IContentProvider mIContentProvider;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -