Skip to content

Instantly share code, notes, and snippets.

@StanislavK
Created February 10, 2011 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StanislavK/820512 to your computer and use it in GitHub Desktop.
Save StanislavK/820512 to your computer and use it in GitHub Desktop.
This is a test.
package com.dropbox.android.sample;
import android.os.AsyncTask;
import android.util.Log;
import com.dropbox.client.DropboxAPI;
public class LoginAsyncTask extends AsyncTask<Void, Void, Integer> {
private static final String TAG = "LoginAsyncTask";
String mUser;
String mPassword;
String mErrorMessage="";
DropboxSample mDropboxSample;
DropboxAPI.Config mConfig;
DropboxAPI.Account mAccount;
// Will just log in
public LoginAsyncTask(DropboxSample act, String user, String password, DropboxAPI.Config config) {
super();
mDropboxSample = act;
mUser = user;
mPassword = password;
mConfig = config;
}
@Override
protected Integer doInBackground(Void... params) {
try {
DropboxAPI api = mDropboxSample.getAPI();
int success = DropboxAPI.STATUS_NONE;
if (!api.isAuthenticated()) {
mConfig = api.authenticate(mConfig, mUser, mPassword);
mDropboxSample.setConfig(mConfig);
success = mConfig.authStatus;
if (success != DropboxAPI.STATUS_SUCCESS) {
return success;
}
}
mAccount = api.accountInfo();
if (!mAccount.isError()) {
return DropboxAPI.STATUS_SUCCESS;
} else {
Log.e(TAG, "Account info error: " + mAccount.httpCode + " " + mAccount.httpReason);
return DropboxAPI.STATUS_FAILURE;
}
} catch (Exception e) {
Log.e(TAG, "Error in logging in.", e);
return DropboxAPI.STATUS_NETWORK_ERROR;
}
}
@Override
protected void onPostExecute(Integer result) {
if (result == DropboxAPI.STATUS_SUCCESS) {
if (mConfig != null && mConfig.authStatus == DropboxAPI.STATUS_SUCCESS) {
mDropboxSample.storeKeys(mConfig.accessTokenKey, mConfig.accessTokenSecret);
mDropboxSample.setLoggedIn(true);
mDropboxSample.showToast("Logged into Dropbox");
}
if (mAccount != null) {
mDropboxSample.displayAccountInfo(mAccount);
}
} else {
if (result == DropboxAPI.STATUS_NETWORK_ERROR) {
mDropboxSample.showToast("Network error: " + mConfig.authDetail);
} else {
mDropboxSample.showToast("Unsuccessful login.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment