Skip to content

Commit

Permalink
Add image queueing to drive uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPickles committed May 9, 2017
1 parent 953b8b1 commit e5ef09e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
4 changes: 4 additions & 0 deletions app/src/main/java/edu/umd/sheets436/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,18 @@ protected void onCreate(Bundle savedInstanceState) {

// Grab some random picture from the Internet.
Bitmap bitmap = null;
Bitmap otherBitmap = null;
try {
URL url = new URL("https://www.umiacs.umd.edu/sites/default/files/styles/medium/public/web-Memon09.jpg");
bitmap = BitmapFactory.decodeStream((InputStream) url.getContent());
url = new URL("https://developer.android.com/_static/c4075bed68/images/android/touchicon-180.png");
otherBitmap = BitmapFactory.decodeStream((InputStream) url.getContent());
} catch (IOException e) {
e.printStackTrace();
}

sheet.uploadToDrive(getString(R.string.CMSC436_test_folder), getString(R.string.image_name), bitmap);
sheet.uploadToDrive(getString(R.string.CMSC436_test_folder), getString(R.string.other_image_name), otherBitmap);
}

public void sendToSheets(View v) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
<string name="CMSC436_test_folder">0B3RViSRC0aoYVDN6MWZUb1RDSVU</string>
<string name="user_id">Your user ID belongs here!</string>
<string name="image_name">The name of your image upload goes here!</string>
<string name="other_image_name">The name of another image should be here.</string>
<string name="button_text">Send random data to test sheet</string>
</resources>
22 changes: 15 additions & 7 deletions sheets436/src/main/java/edu/umd/cmsc436/sheets/Sheets.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.api.services.sheets.v4.SheetsScopes;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

Expand All @@ -54,8 +55,6 @@ public class Sheets implements GoogleApiClient.ConnectionCallbacks, GoogleApiCli
private TestType cache_type;
private String cache_userId;
private String cache_folderId;
private String cache_fileName;
private Bitmap cache_image;
private float cache_value;
private float[] cache_trials;
private OnPrescriptionFetchedListener cache_prescriptionlistener;
Expand All @@ -65,6 +64,7 @@ public class Sheets implements GoogleApiClient.ConnectionCallbacks, GoogleApiCli

private Map<String, Float> cache_versionmap;
private DriveApkTask.OnFinishListener cache_finishlistener;
private List<UploadToDriveTask.DrivePayload> cache_imageQueue;

private enum ServiceType {
UploadPhoto,
Expand All @@ -80,6 +80,7 @@ public Sheets(Host host, Activity hostActivity, String appName, String spreadshe
this.appName = appName;
this.spreadsheetId = spreadsheetId;
this.privateSpreadsheetId = privateSpreadsheetId;
this.cache_imageQueue = new LinkedList<>();

credentials = GoogleAccountCredential.usingOAuth2(hostActivity,
Arrays.asList(SheetsScopes.SPREADSHEETS, DriveScopes.DRIVE)).setBackOff(new ExponentialBackOff());
Expand Down Expand Up @@ -133,10 +134,14 @@ public void writeTrials (TestType testType, String userId, float[] trials) {

public void uploadToDrive(String folderId, String fileName, Bitmap image) {
cache_service = ServiceType.UploadPhoto;
cache_folderId = folderId;
cache_fileName = fileName;
cache_image = image;

UploadToDriveTask.DrivePayload payload = new UploadToDriveTask.DrivePayload(folderId, fileName, image);
cache_imageQueue.add(payload);

resumeUploadToDrive();
}

private void resumeUploadToDrive() {
if (checkConnection()) {
new GoogleApiClient.Builder(hostActivity)
.addApi(Drive.API)
Expand Down Expand Up @@ -185,7 +190,10 @@ public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

private void launchUploadToDriveTask() {
UploadToDriveTask uploadToDriveTask = new UploadToDriveTask(credentials, appName, host, hostActivity);
uploadToDriveTask.execute(new UploadToDriveTask.DrivePayload(cache_folderId, cache_fileName, cache_image));
UploadToDriveTask.DrivePayload[] payloads = new UploadToDriveTask.DrivePayload[cache_imageQueue.size()];
payloads = cache_imageQueue.toArray(payloads);
cache_imageQueue.clear();
uploadToDriveTask.execute(payloads);
}

public void onRequestPermissionsResult (int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
Expand Down Expand Up @@ -236,7 +244,7 @@ private void resume() {
writeTrials(cache_type, cache_userId, cache_trials);
break;
case UploadPhoto:
uploadToDrive(cache_folderId, cache_fileName, cache_image);
resumeUploadToDrive();
case FetchPrescription:
fetchPrescription(cache_userId, cache_prescriptionlistener);
break;
Expand Down

0 comments on commit e5ef09e

Please sign in to comment.