/** * Searches a fusion table in user's Google tables. * * @param tableName name of fusion table to search * @param context android context * @param accountName name of account * @param isDelete whether delete this track * @return true means find and delete fusion table */ public static boolean searchFusionTableByTitle( String tableName, Context context, String accountName, boolean isDelete) { try { GoogleAccountCredential credential = SendToGoogleUtils.getGoogleAccountCredential( context, accountName, SendToGoogleUtils.FUSION_TABLES_SCOPE); if (credential == null) { return false; } Fusiontables fusiontables = new Fusiontables.Builder( AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential) .build(); List<Table> tables = fusiontables.table().list().execute().getItems(); for (Iterator<Table> iterator = tables.iterator(); iterator.hasNext(); ) { Table table = (Table) iterator.next(); String title = table.getName(); if (title.equals(tableName)) { if (isDelete) { fusiontables.table().delete(table.getTableId()).execute(); } return true; } } } catch (Exception e) { Log.d(EndToEndTestUtils.LOG_TAG, "Failed when operate fusion table.", e); } return false; }
/** * Delete spreadsheet which name is title. * * @param title the name of spreadsheet * @param activity to get context * @param accountName the name of Google account * @return true means delete successfully */ public static boolean deleteSpreadsheetByTitle( String title, Activity activity, String accountName) { try { GoogleAccountCredential driveCredential = SendToGoogleUtils.getGoogleAccountCredential( activity.getApplicationContext(), accountName, SendToGoogleUtils.DRIVE_SCOPE); if (driveCredential == null) { return false; } Drive drive = SyncUtils.getDriveService(driveCredential); com.google.api.services.drive.Drive.Files.List list = drive .files() .list() .setQ( String.format(Locale.US, SendSpreadsheetsAsyncTask.GET_SPREADSHEET_QUERY, title)); List<File> files = list.execute().getItems(); for (Iterator<File> iterator = files.iterator(); iterator.hasNext(); ) { File file = (File) iterator.next(); drive.files().delete(file.getId()).execute(); } return true; } catch (Exception e) { Log.e(EndToEndTestUtils.LOG_TAG, "Search spreadsheet failed."); } return false; }
/** * Searches a track title in a spreadsheet. * * @param trackName the track name to search * @param activity to get context * @param spreadsheetTitle the title of spreadsheet * @param isDelete whether delete the information of this track in the document * @param accountName the name of Google account * @return true means find the track name in the spreadsheet */ private static boolean searchTrackTitleInSpreadsheet( String trackName, Activity activity, String spreadsheetTitle, boolean isDelete, String accountName) { try { // Get spreadsheet Id. String spreadsheetId = searchAllSpreadsheetByTitle(spreadsheetTitle, activity, accountName).get(0).getId(); // Get spreadsheet service. SpreadsheetService spreadsheetService = new SpreadsheetService(spreadsheetTitle); Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod()); credential.setAccessToken( SendToGoogleUtils.getToken( activity.getApplicationContext(), accountName, SendToGoogleUtils.SPREADSHEETS_SCOPE)); spreadsheetService.setOAuth2Credentials(credential); // Get work sheet. WorksheetFeed feed = spreadsheetService.getFeed( new URL( String.format( Locale.US, SendSpreadsheetsAsyncTask.GET_WORKSHEETS_URI, spreadsheetId)), WorksheetFeed.class); List<WorksheetEntry> data = feed.getEntries(); for (Iterator<WorksheetEntry> iterator = data.iterator(); iterator.hasNext(); ) { WorksheetEntry worksheetEntry = (WorksheetEntry) iterator.next(); String title = worksheetEntry.getTitle().getPlainText(); if (title.equals(WORK_SHEET_NAME)) { URL listFeedUrl = worksheetEntry.getListFeedUrl(); List<ListEntry> listFeed = spreadsheetService.getFeed(listFeedUrl, ListFeed.class).getEntries(); for (Iterator<ListEntry> iterator2 = listFeed.iterator(); iterator2.hasNext(); ) { ListEntry listEntry = (ListEntry) iterator2.next(); String name = listEntry.getCustomElements().getValue(TRANCK_NAME_COLUMN); if (name.equals(trackName)) { if (isDelete) { listEntry.delete(); } return true; } } } } } catch (Exception e) { Log.e(EndToEndTestUtils.LOG_TAG, "Search spreadsheet failed."); } return false; }
/** * Removes old tracks created by MyTracks test. * * @param activity * @param accountName */ public static void deleteTestTracksOnGoogleDrive(Activity activity, String accountName) { try { GoogleAccountCredential driveCredential = SendToGoogleUtils.getGoogleAccountCredential( activity.getApplicationContext(), accountName, SendToGoogleUtils.DRIVE_SCOPE); if (driveCredential == null) { return; } Drive drive = SyncUtils.getDriveService(driveCredential); com.google.api.services.drive.Drive.Files.List list = drive.files().list().setQ(TEST_TRACKS_QUERY); List<File> files = list.execute().getItems(); for (Iterator<File> iterator = files.iterator(); iterator.hasNext(); ) { File file = (File) iterator.next(); drive.files().delete(file.getId()).execute(); } } catch (Exception e) { Log.e(EndToEndTestUtils.LOG_TAG, "Delete test tracks failed."); } }
/** * Searches docs in user's Google Documents. * * @param title the title of doc * @param activity to get context * @param accountName the name of Google account * @return the file list of the document, null means can not find the spreadsheets */ public static List<File> searchAllSpreadsheetByTitle( String title, Activity activity, String accountName) { try { GoogleAccountCredential driveCredential = SendToGoogleUtils.getGoogleAccountCredential( activity.getApplicationContext(), accountName, SendToGoogleUtils.DRIVE_SCOPE); if (driveCredential == null) { return null; } Drive drive = SyncUtils.getDriveService(driveCredential); com.google.api.services.drive.Drive.Files.List list = drive .files() .list() .setQ( String.format(Locale.US, SendSpreadsheetsAsyncTask.GET_SPREADSHEET_QUERY, title)); FileList result = list.execute(); return result.getItems(); } catch (Exception e) { Log.e(EndToEndTestUtils.LOG_TAG, "Search spreadsheet failed."); } return null; }