diff --git a/foodinder_app/app/build.gradle b/foodinder_app/app/build.gradle index 16f10d6..d04c3d8 100644 --- a/foodinder_app/app/build.gradle +++ b/foodinder_app/app/build.gradle @@ -3,12 +3,16 @@ apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-android' android { + packagingOptions { + exclude 'META-INF/DEPENDENCIES' + } compileSdkVersion 28 buildToolsVersion "28.0.3" defaultConfig { applicationId "com.example.foodinder_app" minSdkVersion 16 targetSdkVersion 28 + multiDexEnabled true versionCode 1 versionName "1.0" } @@ -27,6 +31,9 @@ dependencies { implementation 'com.android.support:cardview-v7:25.0.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'com.android.support.constraint:constraint-layout:1.1.3' + implementation 'com.google.api-client:google-api-client:1.30.6' + implementation 'com.google.oauth-client:google-oauth-client-jetty:1.30.4' + implementation 'com.google.apis:google-api-services-sheets:v4-rev9-1.22.0' } allprojects { repositories { diff --git a/foodinder_app/app/src/main/AndroidManifest.xml b/foodinder_app/app/src/main/AndroidManifest.xml index 1b2e72e..9d6d0b3 100644 --- a/foodinder_app/app/src/main/AndroidManifest.xml +++ b/foodinder_app/app/src/main/AndroidManifest.xml @@ -2,6 +2,12 @@ + + + + + + SCOPES = Collections.singletonList(SheetsScopes.SPREADSHEETS); //_READONLY + private static final String CREDENTIALS_FILE_PATH = "/credentials.json"; + + /** + * Creates an authorized Credential object. + * @param HTTP_TRANSPORT The network HTTP Transport. + * @return An authorized Credential object. + * @throws IOException If the credentials.json file cannot be found. + */ + private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException { + // Load client secrets. + InputStream in = SheetsQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH); + if (in == null) { + throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH); + } + GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); + + // Build flow and trigger user authorization request. + GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( + HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) + .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH))) + .setAccessType("offline") + .build(); + LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build(); + return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); + } + + /** + * Prints the names and majors of students in a sample spreadsheet: + * https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit + */ + public String return_sheet() throws IOException, GeneralSecurityException { + // Build a new authorized API client service. + + final NetHttpTransport HTTP_TRANSPORT = new com.google.api.client.http.javanet.NetHttpTransport(); + final String spreadsheetId = "10nkzldxOYVa1OoFbdC8aLUGEqq3tgLsF-Jz_XlQfj0s"; + final String range = "(Interfejs + Logi uzytkownika)!D10"; + Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)) + .setApplicationName(APPLICATION_NAME) + .build(); + ValueRange response = service.spreadsheets().values() + .get(spreadsheetId, range) + .execute(); + List> values = response.getValues(); + + /* + ValueRange requestBody = new ValueRange(); + requestBody.setValues( + Arrays.asList( + Arrays.asList("Row 1 Cell 1", "Row 1 Cell 2", "Row 1 Cell 3"), + Arrays.asList("Row 2 Cell 1", "Row 2 Cell 2", "Row 2 Cell 3"))); + + Sheets.Spreadsheets.Values.Update request2 = + service.spreadsheets().values().update(spreadsheetId, "(Interfejs + Logi użytkownika)!K32", requestBody).setValueInputOption("RAW"); //USER_ENTERED? + request2.execute(); + */ + + StringBuilder stringBuilder = new StringBuilder(); + + if (values == null || values.isEmpty()) { + stringBuilder.append("No data found."); + } else { + stringBuilder.append("Name, Major"); + for (List row : values) { + // Print columns A and E, which correspond to indices 0 and 4. + stringBuilder.append(row.get(0)); + } + } + + String string = stringBuilder.toString(); + return string; + } +} +// [END sheets_quickstart] diff --git a/foodinder_app/app/src/main/res/layout/activity_main2.xml b/foodinder_app/app/src/main/res/layout/activity_main2.xml index b4e1297..8f259f1 100644 --- a/foodinder_app/app/src/main/res/layout/activity_main2.xml +++ b/foodinder_app/app/src/main/res/layout/activity_main2.xml @@ -16,4 +16,20 @@ app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> + + + +