Change the the return value of SyncUtils.getMyTracksFolder(context, drive) from string to File object.

This commit is contained in:
youtaol
2013-02-18 19:50:21 +08:00
parent 3bf9e9343d
commit 9d6ba74a6d
5 changed files with 8 additions and 33 deletions
@@ -76,7 +76,7 @@ public class SendDriveAsyncTask extends AbstractSendAsyncTask {
return false; return false;
} }
Drive drive = SyncUtils.getDriveService(credential); Drive drive = SyncUtils.getDriveService(credential);
String folderId = SyncUtils.getMyTracksFolder(context, drive); String folderId = SyncUtils.getMyTracksFolder(context, drive).getId();
if (folderId == null) { if (folderId == null) {
return false; return false;
} }
@@ -113,7 +113,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
driveAccountName = account.name; driveAccountName = account.name;
} }
String folderId = SyncUtils.getMyTracksFolder(context, drive); String folderId = SyncUtils.getMyTracksFolder(context, drive).getId();
if (folderId == null) { if (folderId == null) {
return; return;
} }
@@ -153,20 +153,20 @@ public class SyncUtils {
* @param context the context * @param context the context
* @param drive the drive * @param drive the drive
*/ */
public static String getMyTracksFolder(Context context, Drive drive) throws IOException { public static File getMyTracksFolder(Context context, Drive drive) throws IOException {
String folderName = context.getString(R.string.my_tracks_app_name); String folderName = context.getString(R.string.my_tracks_app_name);
List list = drive.files() List list = drive.files()
.list().setQ(String.format(Locale.US, MY_TRACKS_FOLDER_QUERY, folderName)); .list().setQ(String.format(Locale.US, MY_TRACKS_FOLDER_QUERY, folderName));
FileList result = list.execute(); FileList result = list.execute();
for (File file : result.getItems()) { for (File file : result.getItems()) {
if (file.getSharedWithMeDate() == null) { if (file.getSharedWithMeDate() == null) {
return file.getId(); return file;
} }
} }
File file = new File(); File file = new File();
file.setTitle(folderName); file.setTitle(folderName);
file.setMimeType(FOLDER_MIME_TYPE); file.setMimeType(FOLDER_MIME_TYPE);
return drive.files().insert(file).execute().getId(); return drive.files().insert(file).execute();
} }
/** /**
@@ -17,6 +17,7 @@ package com.google.android.apps.mytracks.endtoendtest.sync;
import com.google.android.apps.mytracks.TrackListActivity; import com.google.android.apps.mytracks.TrackListActivity;
import com.google.android.apps.mytracks.endtoendtest.EndToEndTestUtils; import com.google.android.apps.mytracks.endtoendtest.EndToEndTestUtils;
import com.google.android.apps.mytracks.io.sync.SyncUtils;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import com.google.api.services.drive.Drive; import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File; import com.google.api.services.drive.model.File;
@@ -110,7 +111,7 @@ public class SyncMyTracksWithDriveTest extends ActivityInstrumentationTestCase2<
EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true); EndToEndTestUtils.activityMytracks.getString(R.string.menu_sync_now), true);
instrumentation.waitForIdleSync(); instrumentation.waitForIdleSync();
SyncTestUtils.checkFilesNumber(drive); SyncTestUtils.checkFilesNumber(drive);
File folder = SyncTestUtils.getMyTracksFolder(trackListActivity.getApplicationContext(), drive); File folder = SyncUtils.getMyTracksFolder(trackListActivity.getApplicationContext(), drive);
assertNotNull(folder); assertNotNull(folder);
SyncTestUtils.removeFile(folder, drive); SyncTestUtils.removeFile(folder, drive);
@@ -29,11 +29,9 @@ import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpResponse; import com.google.api.client.http.HttpResponse;
import com.google.api.services.drive.Drive; import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File; import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;
import android.app.Instrumentation; import android.app.Instrumentation;
import android.content.Context; import android.content.Context;
import android.util.Log;
import android.widget.CheckBox; import android.widget.CheckBox;
import java.io.BufferedReader; import java.io.BufferedReader;
@@ -134,7 +132,7 @@ public class SyncTestUtils {
* @throws IOException * @throws IOException
*/ */
public static List<File> getDriveFiles(Context context, Drive drive) throws IOException { public static List<File> getDriveFiles(Context context, Drive drive) throws IOException {
File folder = SyncTestUtils.getMyTracksFolder(context, drive); File folder = SyncUtils.getMyTracksFolder(context, drive);
if (folder == null) { if (folder == null) {
return new ArrayList<File>(); return new ArrayList<File>();
} }
@@ -320,28 +318,4 @@ public class SyncTestUtils {
br.close(); br.close();
return fileContent; return fileContent;
} }
/**
* Gets the MyTracks folder on Google Drive.
*
* @param context context of application
* @param drive a Google Drive object
* @return the MyTracks folder on Google Drive
*/
public static File getMyTracksFolder(Context context, Drive drive) {
try {
String folderName = context.getString(R.string.my_tracks_app_name);
com.google.api.services.drive.Drive.Files.List list = drive.files().list()
.setQ(String.format(Locale.US, SyncUtils.MY_TRACKS_FOLDER_QUERY, folderName));
FileList result = list.execute();
for (File file : result.getItems()) {
if (file.getTitle().equals(folderName)) {
return file;
}
}
} catch (IOException e) {
Log.e(EndToEndTestUtils.LOG_TAG, "IOException", e);
}
return null;
}
} }