dropbox - How to passing session object between Android Activities -
i'm working on project used dropbox android apis implementation upload , download functions. first step used uploadbutton trigger fileselectoractivity function use intent method implemented jump next activity,but intent method unable bring dropbox session other activity (probably dropbox session type not string). afer executed file selector activity selected specific file path upload dropbox,
i want know how passing dropbox session fileselectoractivity after selecting target file send uploadactivity. intent method , bundle used not useful. here code.
mainactivity code
public class mainactivity extends activity implements onclicklistener { private dropboxapi<androidauthsession> dropbox; private final static string file_dir = "/"; private final static string dropbox_name = "dropbox_prefs"; private final static string access_key = "***************"; private final static string access_secret = "************"; private string path = "/"; private boolean isloggedin; private button login; private button uploadfile; private button encryption; private button decryption; private button listfiles; private arrayadapter<string> listadapter; private listview itemlist; arraylist<string> items = new arraylist<string>(); @suppresswarnings("deprecation") @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); login = (button) findviewbyid(r.id.dropbox_login); login.setonclicklistener(this); uploadfile = (button) findviewbyid(r.id.uploadbutton); uploadfile.setonclicklistener(this); decryption=(button) findviewbyid(r.id.decryptionbutton); decryption.setonclicklistener(this); encryption=(button) findviewbyid(r.id.encryptionbutton); encryption.setonclicklistener(this); listfiles = (button) findviewbyid(r.id.list_files); listfiles.setonclicklistener(this); // itemlist itemlist = (listview) findviewbyid(r.id.itemlist); // encryption(); loggedin(false); androidauthsession session; appkeypair pair = new appkeypair(access_key, access_secret); sharedpreferences prefs = getsharedpreferences(dropbox_name, 0); string key = prefs.getstring(access_key, null); string secret = prefs.getstring(access_secret, null); if (key != null && secret != null) { accesstokenpair token = new accesstokenpair(key, secret); session = new androidauthsession(pair, accesstype.dropbox, token); } else { session = new androidauthsession(pair, accesstype.dropbox); } dropbox = new dropboxapi<androidauthsession>(session); } @override protected void onresume() { super.onresume(); androidauthsession session = dropbox.getsession(); if (session.authenticationsuccessful()) { try { session.finishauthentication(); tokenpair tokens = session.getaccesstokenpair(); sharedpreferences prefs = getsharedpreferences(dropbox_name, 0); editor editor = prefs.edit(); editor.putstring(access_key, tokens.key); editor.putstring(access_secret, tokens.secret); editor.commit(); loggedin(true); } catch (illegalstateexception e) { toast.maketext(this, "error during dropbox authentication", toast.length_short).show(); } } } public void loggedin(boolean islogged) { isloggedin = islogged; uploadfile.setenabled(islogged); listfiles.setenabled(islogged); decryption.setenabled(islogged); encryption.setenabled(islogged); login.settext(islogged ? "log out" : "log in"); } private final handler handler = new handler() { public void handlemessage(message msg) { items.clear(); arraylist<string> result = msg.getdata().getstringarraylist("data"); (string filename : result) { items.add(filename); } handleitems(); } }; public void handleitems() { listadapter = new arrayadapter<string>(this, r.layout.simplerow, items); itemlist.setadapter(listadapter); itemlist.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view,int position, long id) { string item = ((textview) view).gettext().tostring(); toast.maketext(getbasecontext(), item, toast.length_long).show(); downloadfilefromdropbox download2=new downloadfilefromdropbox(parent.getcontext(), dropbox, path,item); download2.execute(); } }); } @suppresswarnings("deprecation") @override public void onclick(view v) { switch (v.getid()) { case r.id.dropbox_login: if (isloggedin) { dropbox.getsession().unlink(); loggedin(false); } else { dropbox.getsession().startauthentication(mainactivity.this); } break; case r.id.list_files: listdropboxfiles list = new listdropboxfiles(dropbox, file_dir,handler); list.execute(); break; case r.id.uploadbutton: intent intent = new intent(mainactivity.this, filechooser.class); startactivity(intent); // uploadfiletodropbox uploader=new uploadfiletodropbox(this, dropbox, path); // uploader.execute(); break; default: break; } } } fileselector code.
package filechooser; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.util.arraylist; import java.util.collections; import java.util.list; import com.dropbox.client2.dropboxapi; import com.dropbox.client2.android.androidauthsession; import com.dropbox.client2.exception.dropboxexception; import com.dropbox.client2.exception.dropboxunlinkedexception; import com.example.ik2000dropbox.mainactivity; import com.example.ik2000dropbox.r; import com.example.ik2000dropbox.uploadfiletodropbox; import android.app.listactivity; import android.content.intent; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.listview; import android.widget.toast; public class filechooser extends listactivity { private file currentdir; filearrayadapter adapter; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); currentdir = new file("/sdcard/download"); fill(currentdir); } private void fill(file f) { file[] dirs = f.listfiles(); this.settitle("current dir: " + f.getname()); list<option> dir = new arraylist<option>(); list<option> fls = new arraylist<option>(); try { (file ff : dirs) { if (ff.isdirectory()) dir.add(new option(ff.getname(), "folder", ff .getabsolutepath())); else { fls.add(new option(ff.getname(), "file size: " + ff.length(), ff.getabsolutepath())); } } } catch (exception e) { } collections.sort(dir); collections.sort(fls); dir.addall(fls); if (!f.getname().equalsignorecase("sdcard")) dir.add(0, new option("..", "parent directory", f.getparent())); adapter = new filearrayadapter(filechooser.this, r.layout.file_view,dir); this.setlistadapter(adapter); } protected void onlistitemclick(listview l, view v, int position, long id) { super.onlistitemclick(l, v, position, id); option o = adapter.getitem(position); if (o.getdata().equalsignorecase("folder") || o.getdata().equalsignorecase("parent directory")) { currentdir = new file(o.getpath()); fill(currentdir); } else { onfileclick(o); } } private void onfileclick(option o) { toast.maketext(this, "file clicked: " + o.getname(), toast.length_short).show(); } } my upload activity
public class uploadfiletodropbox extends asynctask<void, void, boolean> { private dropboxapi<?> dropbox; private string path; private context context; public uploadfiletodropbox(context context, dropboxapi<?> dropbox, string path) { this.context = context.getapplicationcontext(); this.dropbox = dropbox; this.path = path; } @override protected boolean doinbackground(void... params) { try { file file = new file("/sdcard/download/lecture.pdf.p7m"); fileinputstream inputstream =new fileinputstream(file); dropbox.putfile(path + "lecture.pdf.p7m",inputstream, file.length(), null, null); return true; } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); }catch (ioexception e) { e.printstacktrace(); } catch (dropboxexception e) { e.printstacktrace(); } return false; } @override protected void onpostexecute(boolean result) { if (result) { toast.maketext(context, "file uploaded sucesfully!", toast.length_long).show(); } else { toast.maketext(context, "failed upload file", toast.length_long).show(); } } }
create application object
public class myapplication extends application implements application.activitylifecyclecallbacks{ private final static string access_key = "***************"; private final static string access_secret = "************"; private static myapplication singleton; private dropboxapi<androidauthsession> dropbox; private final static string dropbox_name = "dropbox_prefs"; public dropboxapi<androidauthsession> getdropbox() { return dropbox; } public void setdropbox(dropboxapi<androidauthsession> dropbox) { this.dropbox = dropbox; } @override public void oncreate() { super.oncreate(); singleton = this; sharedpreferences prefs = getsharedpreferences(dropbox_name, 0); string key = prefs.getstring(access_key, null); string secret = prefs.getstring(access_secret, null); if (key != null && secret != null) { accesstokenpair token = new accesstokenpair(key, secret); session = new androidauthsession(pair, accesstype.dropbox, token); } else { session = new androidauthsession(pair, accesstype.dropbox); } dropbox = new dropboxapi<androidauthsession>(session); }} add manifest
<application android:name=".myapplication" android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > use in activity
myapplication.getinstance().getdropbox()
Comments
Post a Comment