android - update percentage to reclyerview -
i'm developing chat app using xmpp (samck).i have showed conversation using reclycerview. want show percentage while image uploading server(using rest api). used intentservice , update percentage through localbroastreceiver. couldn't able scroll reclycerview. couldn't able show percentage on image.
please give comments. progressrequestbody.java percentage have used class. public class progressrequestbody extends requestbody { private file mfile; private string mpath; private string stanzaid; private uploadcallbacks mlistener; private static final int default_buffer_size = 2048; public interface uploadcallbacks { void onprogressupdate(int percentage,string standzid); void onerror(); void onfinish(); } public progressrequestbody(final file file, string stanzaid, final uploadcallbacks listener) { mfile = file; mlistener = listener; this.stanzaid=stanzaid; } @override public mediatype contenttype() { // want upload images return mediatype.parse("image/*"); } @override public long contentlength() throws ioexception { return mfile.length(); } @override public void writeto(bufferedsink sink) throws ioexception { long filelength = mfile.length(); byte[] buffer = new byte[default_buffer_size]; fileinputstream in = new fileinputstream(mfile); long uploaded = 0; try { int read; handler handler = new handler(looper.getmainlooper()); while ((read = in.read(buffer)) != -1) { // update progress on ui thread handler.post(new progressupdater(uploaded, filelength,stanzaid)); uploaded += read; sink.write(buffer, 0, read); } } { in.close(); } } private class progressupdater implements runnable { private long muploaded; private string mstandzid; private long mtotal; public progressupdater(long uploaded, long total,string standzid) { muploaded = uploaded; mtotal = total; mstandzid=standzid; } @override public void run() { mlistener.onprogressupdate((int)(100 * muploaded / mtotal),mstandzid); } } } updateservice.class used call api in background. public class uploadservice extends intentservice implements progressrequestbody.uploadcallbacks { // todo: rename actions, choose action names describe tasks // intentservice can perform, e.g. action_fetch_new_items private static final string action_foo = "com.techno.chat_sdk.utilz.action.foo"; private static final string action_baz = "com.techno.chat_sdk.utilz.action.baz"; // todo: rename parameters private static final string extra_param1 = "com.techno.chat_sdk.utilz.extra.param1"; private static final string extra_param2 = "com.techno.chat_sdk.utilz.extra.param2"; public uploadservice() { super("uploadservice"); } /** * starts service perform action foo given parameters. if * service performing task action queued. * * @see intentservice */ // todo: customize helper method public static void startactionfoo(context context, string param1, string param2) { intent intent = new intent(context, uploadservice.class); intent.setaction(action_foo); intent.putextra(extra_param1, param1); intent.putextra(extra_param2, param2); context.startservice(intent); } /** * starts service perform action baz given parameters. if * service performing task action queued. * * @see intentservice */ // todo: customize helper method public static void startactionbaz(context context, string param1, string param2) { intent intent = new intent(context, uploadservice.class); intent.setaction(action_baz); intent.putextra(extra_param1, param1); intent.putextra(extra_param2, param2); context.startservice(intent); } @override protected void onhandleintent(intent intent) { if (intent != null) { /*final string action = intent.getaction(); if (action_foo.equals(action)) { final string param1 = intent.getstringextra(extra_param1); final string param2 = intent.getstringextra(extra_param2); handleactionfoo(param1, param2); } else if (action_baz.equals(action)) { final string param1 = intent.getstringextra(extra_param1); final string param2 = intent.getstringextra(extra_param2); handleactionbaz(param1, param2); }*/ download download = intent.getparcelableextra("download"); file file=new file(download.getdestinationpath()); progressrequestbody filebody = new progressrequestbody(file,download.getstandzid(), uploadservice.this); multipartbody.part filepart = multipartbody.part.createformdata("file", file.getname(), filebody); fileupanddown medit = apiconfiguration.getinstance().getapibuilder().create(fileupanddown.class); final call<jsonobject> request = medit.fileupload(filepart); request.enqueue(new callback<jsonobject>() { @override public void onresponse(call<jsonobject> call, response<jsonobject> response) { if(response.body()!=null){ string url=response.body().get("file_url").getasstring(); if(url!=null){ if (commonclass.isnetworkconnected(uploadservice.this)){ /*json.items.get(0).url=url; // update url json. json.items.set(0,items); duce_xmppservice.sendmessage(juid, gson.tojson(json), message, false);*/} } } } @override public void onfailure(call<jsonobject> call, throwable t) { system.out.println("failure" + t.tostring()); } }); } } /** * handle action foo in provided background thread provided * parameters. */ private void handleactionfoo(string param1, string param2) { // todo: handle action foo throw new unsupportedoperationexception("not yet implemented"); } /** * handle action baz in provided background thread provided * parameters. */ private void handleactionbaz(string param1, string param2) { // todo: handle action baz throw new unsupportedoperationexception("not yet implemented"); } @override public void onprogressupdate(int percentage, string standzid) { contentvalues values = new contentvalues(); values.put(datahelper.chat_msg_downoruppercentage, percentage); myapplication.writable.update(datahelper.chat_table_name, values, datahelper.chat_msg_stanzaid + " = ?", new string[]{standzid}); intent intent = new intent(constants.progreessupdate); localbroadcastmanager.getinstance(myapplication.getappcontext()).sendbroadcast(intent); } @override public void onerror() { } @override public void onfinish() { } } singlechatactivity.java (udpate data list ocalbroastreceiver) for(chatmessages chatmess: section.chatmessages){ if(chatmess.stanzaid.equals(standzid)){ chatmess.downuppercentage=percentage; section.chatmessages.set(i,chatmess); strickyadapter.notifysectionitemchanged(j,i); } i++; } //strickyadapter switch (jsonmaking.messagetype){ case "text": ivh.outgoing_msg.settext(jsonmaking.items.get(0).body); ivh.outgoing_time.settext(commonclass.gettime(chat.datetime)); //set tick status txt if(chat.offlinestatus==1){ ivh.tick_status.setimageresource(r.drawable.offlinestatus); }else if(chat.deliverystatus==1){ ivh.tick_status.setimageresource(r.drawable.doubletick); }else { ivh.tick_status.setimageresource(r.drawable.singletick); } if(chat.readstatus==1){ ivh.tick_status.setimageresource(r.drawable.colortick); } break; case "image": //set imagecmd if(!commonclass.isemptystring(jsonmaking.items.get(0).imagecmdtxt)) ivh.out_imagecmdtxt.settext(jsonmaking.items.get(0).imagecmdtxt); else ivh.out_imagecmdtxt.setvisibility(view.gone); //set image if(!commonclass.isemptystring(jsonmaking.items.get(0).base64)){ ivh.outgoing_image.setimagebitmap(commonclass.decodebase64(jsonmaking.items.get(0).base64)); /*file file = new file(jsonmaking.items.get(0).localurl); if(file.exists()){ bitmap mybitmap = bitmapfactory.decodefile(file.getabsolutepath()); ivh.outgoing_image.setimagebitmap(mybitmap); }*/ } //set image time ivh.outgoing_image_time.settext(commonclass.gettime(chat.datetime)); //set tick status image if(chat.offlinestatus==1){ ivh.image_tick_status.setimageresource(r.drawable.offlinestatus); }else if(chat.deliverystatus==1){ ivh.image_tick_status.setimageresource(r.drawable.doubletick); }else { ivh.image_tick_status.setimageresource(r.drawable.singletick); } if(chat.readstatus==1){ ivh.image_tick_status.setimageresource(r.drawable.colortick); } //set uploading percentage context.runonuithread(new runnable() { @override public void run() { if(chat.downuppercentage!=0&&chat.downuppercentage<90){ ivh.outgoing_circle_progress.setvisibility(view.visible); ivh.outgoing_circle_progress.setprogress(chat.downuppercentage); }else { ivh.outgoing_circle_progress.setvisibility(view.gone); } } }); break; }
Comments
Post a Comment