android - How to show app icon in front of a contact associated with the App like whats app -
i want show app icon in contact book each contact associated app
have followed https://github.com/c99koder/androidsyncproviderdemo , write own sync adapter whenever added contact associated app not show app icon in front of this here sync adapterpackage com.example.phonebook; import java.io.bytearrayoutputstream; import java.util.arraylist; import java.util.hashmap; import com.example.phonebook.database.databasehelper; import com.example.phonebook.model.contact; import android.r.string; import android.accounts.account; import android.accounts.operationcanceledexception; import android.app.service; import android.content.abstractthreadedsyncadapter; import android.content.contentproviderclient; import android.content.contentprovideroperation; import android.content.contentresolver; import android.content.contenturis; import android.content.context; import android.content.intent; import android.content.syncresult; import android.database.cursor; import android.graphics.bitmap; import android.graphics.bitmap.compressformat; import android.graphics.bitmapfactory; import android.graphics.drawable.bitmapdrawable; import android.net.uri; import android.os.build; import android.os.bundle; import android.os.ibinder; import android.provider.basecolumns; import android.provider.contactscontract; import android.provider.contactscontract.commondatakinds.phone; import android.provider.contactscontract.contacts.photo; import android.provider.contactscontract.rawcontacts; import android.provider.contactscontract.rawcontacts.data; import android.provider.contactscontract.rawcontacts.entity; import android.util.log; /** * @author sam * */ public class contactssyncadapterservice extends service { private static final string tag = "contactssyncadapterservice"; private static syncadapterimpl ssyncadapter = null; private static contentresolver mcontentresolver = null; private static string usernamecolumn = contactscontract.rawcontacts.sync1; private static string phototimestampcolumn = contactscontract.rawcontacts.sync2; static databasehelper databasehelper; public contactssyncadapterservice() { super(); databasehelper=new databasehelper(this); } public static class syncadapterimpl extends abstractthreadedsyncadapter { private context mcontext; public syncadapterimpl(context context) { super(context, true); mcontext = context; } @override public void onperformsync(account account, bundle extras, string authority, contentproviderclient provider, syncresult syncresult) { try { contactssyncadapterservice.performsync(mcontext, account, extras, authority, provider, syncresult); } catch (operationcanceledexception e) { } } } @override public ibinder onbind(intent intent) { ibinder ret = null; ret = getsyncadapter().getsyncadapterbinder(); return ret; } public syncadapterimpl getsyncadapter() { if (ssyncadapter == null) ssyncadapter = new syncadapterimpl(this); return ssyncadapter; } private static long addcontact(account account, string name, string username,bitmap bitmap) { log.i(tag, "adding contact: " + name); log.v("name", name); log.v("phonenumber", username); arraylist<contentprovideroperation> operationlist = new arraylist<contentprovideroperation>(); contentprovideroperation.builder builder = contentprovideroperation.newinsert(rawcontacts.content_uri); builder.withvalue(rawcontacts.account_name, account.name); builder.withvalue(rawcontacts.account_type, account.type); builder.withvalue(rawcontacts.sync1, username); operationlist.add(builder.build()); builder = contentprovideroperation.newinsert(contactscontract.data.content_uri); builder.withvaluebackreference(contactscontract.commondatakinds.structuredname.raw_contact_id, 0); builder.withvalue(contactscontract.data.mimetype, contactscontract.commondatakinds.structuredname.content_item_type); builder.withvalue(contactscontract.commondatakinds.structuredname.display_name, name); //builder.withvalue(contactscontract.commondatakinds.structuredname.middle_name, username); operationlist.add(builder.build()); builder = contentprovideroperation.newinsert(contactscontract.data.content_uri); bytearrayoutputstream stream = new bytearrayoutputstream(); if(bitmap!=null){ // if image selected log.v("adding image", ""+"contactimage"); bitmap.compress(bitmap.compressformat.png , 75, stream); builder = contentprovideroperation.newinsert(contactscontract.data.content_uri); builder.withvaluebackreference(contactscontract.data.raw_contact_id, 0); builder.withvalue(contactscontract.data.is_super_primary, 1); builder.withvalue(contactscontract.data.mimetype, photo.content_directory); builder.withvalue(contactscontract.commondatakinds.photo.photo,stream.tobytearray()); operationlist.add(builder.build()); } builder = contentprovideroperation.newinsert(contactscontract.data.content_uri); builder.withvaluebackreference(contactscontract.data.raw_contact_id, 0); builder.withvalue(contactscontract.data.mimetype, "vnd.android.cursor.item/vnd.com.example.phonebook"); //builder.withvalue(contactscontract.data.data1, username); builder.withvaluebackreference(contactscontract.commondatakinds.structuredname.raw_contact_id, 0); builder.withvalue(contactscontract.data.mimetype, contactscontract.commondatakinds.structuredname.content_item_type); builder.withvalue(data.mimetype, phone.content_item_type); builder.withvalue(phone.number, username); operationlist.add(builder.build()); try { mcontentresolver.applybatch(contactscontract.authority, operationlist); uri rawcontacturi = rawcontacts.content_uri.buildupon().appendqueryparameter(rawcontacts.account_name, account.name).appendqueryparameter( rawcontacts.account_type, account.type).build(); cursor c1 = mcontentresolver.query(rawcontacturi, new string[] { basecolumns._id, usernamecolumn }, usernamecolumn + " = '" + username + "'", null, null); if (c1.movetonext()) { return c1.getlong(0); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } return -1; } private static void updatecontactstatus(arraylist<contentprovideroperation> operationlist, long rawcontactid, string status) { log.v(tag, "in update contact status"); uri rawcontacturi = contenturis.withappendedid(rawcontacts.content_uri, rawcontactid); uri entityuri = uri.withappendedpath(rawcontacturi, entity.content_directory); cursor c = mcontentresolver.query(entityuri, new string[] { rawcontacts.source_id, entity.data_id, entity.mimetype, entity.data1 }, null, null, null); try { while (c.movetonext()) { if (!c.isnull(1)) { string mimetype = c.getstring(2); if (mimetype.equals("vnd.android.cursor.item/vnd.com.example.phonebook")) { contentprovideroperation.builder builder = contentprovideroperation.newinsert(contactscontract.statusupdates.content_uri); builder.withvalue(contactscontract.statusupdates.data_id, c.getlong(1)); builder.withvalue(contactscontract.statusupdates.status, status); builder.withvalue(contactscontract.statusupdates.status_res_package, "com.example.phonebook"); builder.withvalue(contactscontract.statusupdates.status_label, r.string.app_name); builder.withvalue(contactscontract.statusupdates.status_icon, r.drawable.logo); builder.withvalue(contactscontract.statusupdates.status_timestamp, system.currenttimemillis()); operationlist.add(builder.build()); //only change text of our custom entry status message pre-honeycomb, newer contacts app shows //statuses elsewhere if(integer.decode(build.version.sdk) < 11) { builder = contentprovideroperation.newupdate(contactscontract.data.content_uri); builder.withselection(basecolumns._id + " = '" + c.getlong(1) + "'", null); builder.withvalue(contactscontract.data.data3, status); operationlist.add(builder.build()); } } } } } { c.close(); } } private static void updatecontactphoto(arraylist<contentprovideroperation> operationlist, long rawcontactid, byte[] photo) { contentprovideroperation.builder builder = contentprovideroperation.newdelete(contactscontract.data.content_uri); builder.withselection(contactscontract.data.raw_contact_id + " = '" + rawcontactid + "' , " + contactscontract.data.mimetype + " = '" + contactscontract.commondatakinds.photo.content_item_type + "'", null); operationlist.add(builder.build()); try { if(photo != null) { builder = contentprovideroperation.newinsert(contactscontract.data.content_uri); builder.withvalue(contactscontract.commondatakinds.photo.raw_contact_id, rawcontactid); builder.withvalue(contactscontract.data.mimetype, contactscontract.commondatakinds.photo.content_item_type); builder.withvalue(contactscontract.commondatakinds.photo.photo, photo); operationlist.add(builder.build()); builder = contentprovideroperation.newupdate(contactscontract.rawcontacts.content_uri); builder.withselection(contactscontract.rawcontacts.contact_id + " = '" + rawcontactid + "'", null); builder.withvalue(phototimestampcolumn, string.valueof(system.currenttimemillis())); operationlist.add(builder.build()); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } private static class syncentry { public long raw_id = 0l; public long photo_timestamp = null; } private static void performsync(context context, account account, bundle extras, string authority, contentproviderclient provider, syncresult syncresult) throws operationcanceledexception { hashmap<string, syncentry> localcontacts = new hashmap<string, syncentry>(); mcontentresolver = context.getcontentresolver(); log.i(tag, "performsync: " + account.tostring()); // load local contacts uri rawcontacturi = rawcontacts.content_uri.buildupon().appendqueryparameter(rawcontacts.account_name, account.name).appendqueryparameter( rawcontacts.account_type, account.type).build(); cursor c1 = mcontentresolver.query(rawcontacturi, new string[] { basecolumns._id, usernamecolumn, phototimestampcolumn }, null, null, null); while (c1.movetonext()) { syncentry entry = new syncentry(); entry.raw_id = c1.getlong(c1.getcolumnindex(basecolumns._id)); entry.photo_timestamp = c1.getlong(c1.getcolumnindex(phototimestampcolumn)); localcontacts.put(c1.getstring(1), entry); } arraylist<contentprovideroperation> operationlist = new arraylist<contentprovideroperation>(); try { // if don't have contacts, create one. otherwise, set // status message arraylist<contact> contacts=contactssyncadapterservice.databasehelper.getallcontacts(); log.v("size of contacts", ""+localcontacts.size()); (int = 0; < contacts.size(); i++) { string name=contacts.get(i).getname(); string phone=contacts.get(i).getphone(); byte[] image=contacts.get(i).getimage(); //log.v("contactname", name); //log.v("phone", phone); //log.v("image", ""+contacts.get(i).getimage()); //log.v("size of local contacts", ""+localcontacts.size()); //log.v("size of local23 contacts", ""+localcontacts.get(phone)); if (localcontacts.get(phone) == null) { bitmap bitmap=null; if(image!=null){ bitmap= bitmapfactory.decodebytearray(image, 0, image.length); }else{ bitmap=((bitmapdrawable)context.getresources().getdrawable(r.drawable.profileimage)).getbitmap(); } long id=addcontact(account, name, phone,bitmap); if(id != -1) { syncentry entry = new syncentry(); entry.raw_id = id; localcontacts.put(name, entry); } } else { system.out.println(localcontacts.size()); log.v("phonenumber",""+ localcontacts.get(phone)); if (localcontacts.get((phone)).photo_timestamp == null || system.currenttimemillis() > (localcontacts.get(phone).photo_timestamp + 604800000l)) { //you download image file , pass bytes, sample doesn't use network we'll decode , re-compress icon resource bytes bytearrayoutputstream stream = new bytearrayoutputstream(); bitmap icon=null; if(image!=null){ log.v("having image", "image exist"); icon = bitmapfactory.decodebytearray(image, 0, image.length); }else{ log.v("not having image", "image not exist"); icon=((bitmapdrawable)context.getresources().getdrawable(r.drawable.profileimage)).getbitmap(); } icon.compress(compressformat.png, 0, stream); updatecontactphoto(operationlist, localcontacts.get(phone).raw_id, stream.tobytearray()); } updatecontactstatus(operationlist, localcontacts.get(phone).raw_id, "hunting wabbits"); } } if (operationlist.size() > 0) mcontentresolver.applybatch(contactscontract.authority, operationlist); contactssyncadapterservice.databasehelper.closedb(); } catch (exception e1) { // todo auto-generated catch block //contactssyncadapterservice.databasehelper.closedb();; e1.printstacktrace(); } } } }</p>
if find doing wrong please me in this.
Comments
Post a Comment