Skip to content

Commit

Permalink
Authentication workflow up to getting an Oauth2 token
Browse files Browse the repository at this point in the history
  • Loading branch information
mendhak committed Jun 22, 2013
1 parent 5f51c9c commit 96ad303
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 5 deletions.
22 changes: 20 additions & 2 deletions GPSLogger/src/com/mendhak/gpslogger/senders/gdocs/GDocsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public class GDocsHelper implements IActionListener, IFileSender
private static final int USER_RECOVERABLE_AUTH = 5;
private static final int ACCOUNT_PICKER = 2;

/*
To revoke permissions:
(new Android)
./adb -e shell 'sqlite3 /data/system/users/0/accounts.db "delete from grants;"'
or
(old Android)
./adb -e shell 'sqlite3 /data/system/accounts.db "delete from grants;"'
*/

public GDocsHelper(Context applicationContext, IActionListener callback)
{
Expand Down Expand Up @@ -83,10 +93,19 @@ public static String GetAccountName(Context applicationContext)
return prefs.getString("GDOCS_ACCOUNT_NAME", "");
}

public static void SetAccountName(Context applicationContext, String accountName)
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext);
SharedPreferences.Editor editor = prefs.edit();

editor.putString("GDOCS_ACCOUNT_NAME", accountName);
editor.commit();
}

/**
* Saves the authToken and account name into shared preferences
*/
public static void SaveAuthToken(Context applicationContext, String authToken, String accountName)
public static void SaveAuthToken(Context applicationContext, String authToken)
{
try
{
Expand All @@ -97,7 +116,6 @@ public static void SaveAuthToken(Context applicationContext, String authToken, S

Utilities.LogDebug("Saving GDocs authToken: " + authToken);
editor.putString("GDOCS_AUTH_TOKEN", authToken);
editor.putString("GDOCS_ACCOUNT_NAME", accountName);
editor.commit();
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
import android.accounts.AccountManager;
import android.app.Dialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.preference.Preference;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockPreferenceActivity;
import com.actionbarsherlock.view.MenuItem;
import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.GooglePlayServicesAvailabilityException;
import com.google.android.gms.auth.UserRecoverableAuthException;
import com.google.android.gms.common.AccountPicker;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
Expand All @@ -35,15 +39,20 @@
import com.mendhak.gpslogger.common.IActionListener;
import com.mendhak.gpslogger.common.Utilities;

import java.io.IOException;


public class GDocsSettingsActivity extends SherlockPreferenceActivity
implements Preference.OnPreferenceClickListener, IActionListener
{
private final Handler handler = new Handler();
boolean messageShown = false;
String accountName;

static final String GOOGLE_DRIVE_SCOPE = "oauth2:https://www.googleapis.com/auth/drive.file";
static final int REQUEST_CODE_MISSING_GPSF = 1;
static final int REQUEST_CODE_ACCOUNT_PICKER = 2;
static final int REQUEST_CODE_RECOVERED=3;


public void onCreate(Bundle savedInstanceState)
Expand Down Expand Up @@ -190,15 +199,100 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data)
String accountName = data.getStringExtra(
AccountManager.KEY_ACCOUNT_NAME);

GDocsHelper.SetAccountName(getApplicationContext(),accountName);
Toast.makeText(this, accountName, Toast.LENGTH_LONG).show();

getAndUseAuthTokenInAsyncTask();
}

return;
break;
case REQUEST_CODE_RECOVERED:
if(resultCode == RESULT_OK)
{
getAndUseAuthTokenInAsyncTask();
}
break;
}

super.onActivityResult(requestCode, resultCode, data);
}

// Example of how to use the GoogleAuthUtil in a blocking, non-main thread context
String getAndUseAuthTokenBlocking()
{
try
{
// Retrieve a token for the given account and scope. It will always return either
// a non-empty String or throw an exception.
final String token = GoogleAuthUtil.getToken(getApplicationContext(), GDocsHelper.GetAccountName(getApplicationContext()), GOOGLE_DRIVE_SCOPE);
// Do work with token.

// if (server indicates token is invalid) {
// // invalidate the token that we found is bad so that GoogleAuthUtil won't
// // return it next time (it may have cached it)
// GoogleAuthUtil.invalidateToken(Context, String)(context, token);
// // consider retrying getAndUseTokenBlocking() once more
// return;
// }
return token;
}
catch (GooglePlayServicesAvailabilityException playEx)
{
Dialog alert = GooglePlayServicesUtil.getErrorDialog(
playEx.getConnectionStatusCode(),
this,
REQUEST_CODE_RECOVERED);
alert.show();

}
catch (UserRecoverableAuthException userAuthEx)
{
// Start the user recoverable action using the intent returned by
// getIntent()
startActivityForResult(
userAuthEx.getIntent(),
REQUEST_CODE_RECOVERED);

}
catch (IOException transientEx)
{
// network or server error, the call is expected to succeed if you try again later.
// Don't attempt to call again immediately - the request is likely to
// fail, you'll hit quotas or back-off.


}
catch (GoogleAuthException authEx)
{
// Failure. The call is not expected to ever succeed so it should not be
// retried.

}
return null;
}


// Example of how to use AsyncTask to call blocking code on a background thread.
void getAndUseAuthTokenInAsyncTask()
{
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>()
{
@Override
protected String doInBackground(Void... params)
{
return getAndUseAuthTokenBlocking();

}

@Override
protected void onPostExecute(String authToken)
{
GDocsHelper.SaveAuthToken(getApplicationContext(),authToken);
Toast.makeText(getApplicationContext(), authToken, Toast.LENGTH_SHORT).show();
VerifyGooglePlayServices();
}
};
task.execute();
}


private void UploadTestFileToGoogleDocs()
{
Expand Down

0 comments on commit 96ad303

Please sign in to comment.