Skip to content

Commit

Permalink
Google Drive token acquisition in the Job instance itself
Browse files Browse the repository at this point in the history
  • Loading branch information
mendhak committed Mar 16, 2015
1 parent 9cbc946 commit dd32bb7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public static JobManager GetJobManager(){
return jobManager;
}


private static AppSettings instance;
public AppSettings() {
instance = this;
}

public static AppSettings getInstance() {
return instance;
}

// ---------------------------------------------------
// User Preferences
// ---------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,11 @@ public void UploadFile(final String fileName) {
File gpsDir = new File(AppSettings.getGpsLoggerFolder());
File gpxFile = new File(gpsDir, fileName);

tracer.debug("Sending file to GDocs: " + fileName);
new GDocsTokenAsyncTask(gpxFile).execute(context);
tracer.debug("Submitting Google Docs job");

JobManager jobManager = AppSettings.GetJobManager();
jobManager.addJobInBackground(new GDocsJob(gpxFile));

} catch (Exception e) {
EventBus.getDefault().post(new UploadEvents.GDocs(false));
tracer.error("GDocsHelper.UploadFile", e);
Expand All @@ -151,33 +154,4 @@ public boolean accept(File dir, String name) {
return true;
}

private class GDocsTokenAsyncTask extends AsyncTask<Context, Void, String> {

File gpxFile;
public GDocsTokenAsyncTask(File gpxFile){
this.gpxFile = gpxFile;
}

@Override
protected String doInBackground(Context... params) {
try {
return GoogleAuthUtil.getTokenWithNotification(params[0], GetAccountName(params[0]), GetOauth2Scope(), new Bundle());
}
catch (Exception e) {
tracer.error("Could not get token",e);
}

return null;
}

@Override
protected void onPostExecute(String token) {
tracer.debug("GDocs token: " + token);
GDocsHelper.SaveAuthToken(context, token);

JobManager jobManager = AppSettings.GetJobManager();
jobManager.addJobInBackground(new GDocsJob(token, gpxFile));
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package com.mendhak.gpslogger.senders.gdocs;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;

import com.google.android.gms.auth.GoogleAuthUtil;
import com.mendhak.gpslogger.common.AppSettings;
import com.mendhak.gpslogger.common.Utilities;
import com.mendhak.gpslogger.common.events.UploadEvents;
import com.path.android.jobqueue.Job;
Expand All @@ -21,11 +28,9 @@ public class GDocsJob extends Job {
String token;
File gpxFile;

protected GDocsJob(String token, File gpxFile) {
protected GDocsJob(File gpxFile) {
super(new Params(1).requireNetwork().persist());
this.token = token;
this.gpxFile = gpxFile;

}

@Override
Expand All @@ -35,6 +40,11 @@ public void onAdded() {

@Override
public void onRun() throws Throwable {

token = GoogleAuthUtil.getTokenWithNotification(AppSettings.getInstance(), GetAccountName(AppSettings.getInstance()), GetOauth2Scope(), new Bundle());
tracer.debug("GDocs token: " + token);
GDocsHelper.SaveAuthToken(AppSettings.getInstance(), token);

FileInputStream fis = new FileInputStream(gpxFile);
String fileName = gpxFile.getName();

Expand Down Expand Up @@ -70,6 +80,18 @@ public void onRun() throws Throwable {
EventBus.getDefault().post(new UploadEvents.GDocs(true));
}

private static String GetOauth2Scope() {
return "oauth2:https://www.googleapis.com/auth/drive.file";
}

/**
* Gets the stored account name
*/
private String GetAccountName(Context applicationContext) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext);
return prefs.getString("GDRIVE_ACCOUNT_NAME", "");
}

private String UpdateFileContents(String authToken, String gpxFileId, byte[] fileContents, String fileName) {
HttpURLConnection conn = null;
String fileId = null;
Expand Down

0 comments on commit dd32bb7

Please sign in to comment.