com.dropbox.client2.android
Class AndroidAuthSession

java.lang.Object
  extended by com.dropbox.client2.session.AbstractSession
      extended by com.dropbox.client2.android.AndroidAuthSession
All Implemented Interfaces:
Session

public class AndroidAuthSession
extends AbstractSession

Keeps track of a logged in user and contains configuration options for the DropboxAPI. Has methods specific to Android for authenticating users via the Dropbox app or web site.

A typical authentication flow when no user access token pair is saved is as follows:

 AndroidAuthSession session = new AndroidAuthSession(myAppKeys, myAccessType);

 // When user wants to link to Dropbox, within an activity:
 session.startAuthentication(this);

 // When user returns to your activity, after authentication:
 if (session.authenticationSuccessful()) {
   try {
     session.finishAuthentication();

     AccessTokenPair tokens = session.getAccessTokenPair();
     // Store tokens.key, tokens.secret somewhere
   } catch (IllegalStateException e) {
     // Error handling
   }
 }

When a user returns to your app and you have tokens stored, just create a new session with them:
 AndroidAuthSession session = new AndroidAuthSession(
     myAppKeys, myAccessType, new AccessTokenPair(storedAccessKey, storedAccessSecret));
 


Nested Class Summary
 
Nested classes/interfaces inherited from interface com.dropbox.client2.session.Session
Session.AccessType, Session.ProxyInfo
 
Constructor Summary
AndroidAuthSession(AppKeyPair appKeyPair, Session.AccessType type)
          Creates a new session to authenticate Android apps with the given app key pair and access type.
AndroidAuthSession(AppKeyPair appKeyPair, Session.AccessType type, AccessTokenPair accessTokenPair)
          Creates a new session to authenticate Android apps with the given app key pair and access type.
 
Method Summary
 boolean authenticationSuccessful()
          Returns whether the user successfully authenticated with Dropbox.
 java.lang.String finishAuthentication()
          Sets up a user's access token and secret in this session when you return to your activity from the Dropbox authentication process.
 void startAuthentication(android.content.Context context)
          Starts the Dropbox authentication process by launching an external app (either the Dropbox app if available or a web browser) where the user will log in and allow your app access.
 void unlink()
          Unlinks the session by removing any stored access token and secret.
 
Methods inherited from class com.dropbox.client2.session.AbstractSession
getAccessTokenPair, getAccessType, getAPIServer, getAppKeyPair, getContentServer, getHttpClient, getLocale, getProxyInfo, getWebServer, isLinked, setAccessTokenPair, setRequestTimeout, sign
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AndroidAuthSession

public AndroidAuthSession(AppKeyPair appKeyPair,
                          Session.AccessType type)
Creates a new session to authenticate Android apps with the given app key pair and access type. The session will not be linked because it has no access token or secret.


AndroidAuthSession

public AndroidAuthSession(AppKeyPair appKeyPair,
                          Session.AccessType type,
                          AccessTokenPair accessTokenPair)
Creates a new session to authenticate Android apps with the given app key pair and access type. The session will be linked to the account corresponding to the given access token pair.

Method Detail

startAuthentication

public void startAuthentication(android.content.Context context)
Starts the Dropbox authentication process by launching an external app (either the Dropbox app if available or a web browser) where the user will log in and allow your app access.

Parameters:
context - the Context which to use to launch the Dropbox authentication activity. This will typically be an Activity and the user will be taken back to that activity after authentication is complete (i.e., your activity will receive an onResume()).
Throws:
java.lang.IllegalStateException - if you have not correctly set up the AuthActivity in your manifest, meaning that the Dropbox app will not be able to redirect back to your app after auth.

authenticationSuccessful

public boolean authenticationSuccessful()
Returns whether the user successfully authenticated with Dropbox. Reasons for failure include the user canceling authentication, network errors, and improper setup from within your app.


finishAuthentication

public java.lang.String finishAuthentication()
                                      throws java.lang.IllegalStateException
Sets up a user's access token and secret in this session when you return to your activity from the Dropbox authentication process. Should be called from your activity's onActivityResult() method, but only after checking that authenticationSuccessful() is true.

Returns:
the authenticated user's Dropbox UID.
Throws:
java.lang.IllegalStateException - if authentication was not successful prior to this call (check with authenticationSuccessful().

unlink

public void unlink()
Description copied from interface: Session
Unlinks the session by removing any stored access token and secret.

Specified by:
unlink in interface Session
Overrides:
unlink in class AbstractSession