task for Piotr

This commit is contained in:
Damian 2019-12-16 18:50:31 +01:00
parent e58557ee4b
commit deb32911ac
7 changed files with 170 additions and 1 deletions

View File

@ -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 {

View File

@ -2,6 +2,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.foodinder_app">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"

View File

@ -4,10 +4,18 @@ import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.io.IOException;
import java.security.GeneralSecurityException;
public class Main2Activity extends AppCompatActivity {
private View button;
private View button2;
private TextView text;
private SheetsQuickstart sheetsQuickstart;
public static String stringen;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -15,6 +23,9 @@ public class Main2Activity extends AppCompatActivity {
setContentView(R.layout.activity_main2);
button = findViewById(R.id.button);
text = (TextView) findViewById(R.id.textView);
button2 = findViewById(R.id.button2);
sheetsQuickstart = new SheetsQuickstart();
button.setOnClickListener(new View.OnClickListener() {
@Override
@ -24,5 +35,25 @@ public class Main2Activity extends AppCompatActivity {
startActivity(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
new Thread() {
@Override
public void run() {
try {
stringen = sheetsQuickstart.return_sheet();
} catch (IOException e) {
stringen = "Problem";
} catch (GeneralSecurityException e) {
stringen = "Problem";
}
}
}.start();
text.setText(stringen);
}
});
}
}

View File

@ -0,0 +1,108 @@
package com.example.foodinder_app;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.ValueRange;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class SheetsQuickstart {
private static final String APPLICATION_NAME = "@string/app_name";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens";
/**
* Global instance of the scopes required by this quickstart.
* If modifying these scopes, delete your previously saved tokens/ folder.
*/
private static final List<String> 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<List<Object>> 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]

View File

@ -16,4 +16,20 @@
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
tools:layout_editor_absoluteX="181dp"
tools:layout_editor_absoluteY="420dp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Google sheet"
tools:layout_editor_absoluteX="160dp"
tools:layout_editor_absoluteY="280dp" />
</android.support.constraint.ConstraintLayout>

View File

@ -0,0 +1 @@
{"installed":{"client_id":"938133417828-a6lfc898k3di2e41uvab0q3825b3v0p7.apps.googleusercontent.com","project_id":"quickstart-1574691190977","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"Nfw-2UKc-zK81mm-AHTK3x4O","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}

View File

@ -7,7 +7,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong