From fa9a814885cf4ce2f3d159b7b04d91797c071275 Mon Sep 17 00:00:00 2001 From: titovtima Date: Thu, 26 Mar 2020 15:11:18 +0300 Subject: [PATCH 01/13] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=BE=D0=BB=D1=8F=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D1=8F=20id?= =?UTF-8?q?=20=D0=B3=D1=83=D0=B3=D0=BB-=D0=BF=D0=B0=D0=BF=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=B2=20=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B9=D0=BA=D0=B0?= =?UTF-8?q?=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/ru/spb/speech/SettingsActivity.kt | 1 + app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/pref_general.xml | 7 +++++++ 3 files changed, 9 insertions(+) diff --git a/app/src/main/java/ru/spb/speech/SettingsActivity.kt b/app/src/main/java/ru/spb/speech/SettingsActivity.kt index b5b2a1f5..f51f3b6b 100644 --- a/app/src/main/java/ru/spb/speech/SettingsActivity.kt +++ b/app/src/main/java/ru/spb/speech/SettingsActivity.kt @@ -103,6 +103,7 @@ class SettingsActivity : AppCompatPreferenceActivity() { bindPreferenceSummaryToValue(findPreference("example_list")) bindPreferenceSummaryToValue(findPreference(getString(R.string.speed_key))) bindPreferenceSummaryToValue(findPreference("statistics_collection")) + bindPreferenceSummaryToValue(findPreference("drive_folder_key")) } override fun onOptionsItemSelected(item: MenuItem): Boolean { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index adcfe36e..5e5b269f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -361,6 +361,7 @@ увеличить время выступления. Отсутствие слов паразитов: Калибровка уровня шума в помещении,\n сохраняйте тишину + Папка для сохранения на Google-диск а именно а не то diff --git a/app/src/main/res/xml/pref_general.xml b/app/src/main/res/xml/pref_general.xml index 1396ae20..1d7c96c6 100644 --- a/app/src/main/res/xml/pref_general.xml +++ b/app/src/main/res/xml/pref_general.xml @@ -47,5 +47,12 @@ android:singleLine="true" android:title="@string/speech_speed" /> + + + From ec1be5cfdec4003d94135f466d76decab36d8260 Mon Sep 17 00:00:00 2001 From: titovtima Date: Thu, 2 Apr 2020 08:53:13 +0300 Subject: [PATCH 02/13] =?UTF-8?q?=D0=A1=D0=BE=D1=85=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BD=D0=B0=20=D0=B4=D0=B8=D1=81?= =?UTF-8?q?=D0=BA=20=D0=B2=20=D0=BE=D1=81=D0=BD=D0=BE=D0=B2=D0=BD=D0=BE?= =?UTF-8?q?=D0=BC=20=D0=BF=D1=80=D0=B8=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 28 +++++++++ .../java/ru/spb/speech/StartPageActivity.kt | 57 +++++++++++++++++++ .../ru/spb/speech/appSupport/AudioAnalyzer.kt | 33 +++++++++++ 3 files changed, 118 insertions(+) diff --git a/app/build.gradle b/app/build.gradle index ff0cedb3..134f3b04 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -36,6 +36,17 @@ android { androidExtensions { experimental = true } + + packagingOptions { + exclude 'META-INF/DEPENDENCIES' + exclude 'META-INF/LICENSE' + exclude 'META-INF/LICENSE.txt' + exclude 'META-INF/license.txt' + exclude 'META-INF/NOTICE' + exclude 'META-INF/NOTICE.txt' + exclude 'META-INF/notice.txt' + exclude 'META-INF/ASL2.0' + } } dependencies { @@ -79,3 +90,20 @@ apply plugin: 'com.google.gms.google-services' dependencies { implementation 'com.android.support.constraint:constraint-layout:1.1.3' } + +dependencies { + // Google Drive +// implementation "com.google.android.gms:play-services-auth:15.0.1" +// implementation "com.google.android.gms:play-services-drive:17.0.0" +// implementation 'com.squareup.okio:okio:1.14.0' + + + implementation 'com.google.android.gms:play-services-auth:15.0.1' + implementation 'com.google.http-client:google-http-client-gson:1.26.0' + implementation('com.google.api-client:google-api-client-android:1.26.0') { + exclude group: 'org.apache.httpcomponents' + } + implementation('com.google.apis:google-api-services-drive:v3-rev136-1.25.0') { + exclude group: 'org.apache.httpcomponents' + } +} diff --git a/app/src/main/java/ru/spb/speech/StartPageActivity.kt b/app/src/main/java/ru/spb/speech/StartPageActivity.kt index 031de9f9..b1a722f0 100644 --- a/app/src/main/java/ru/spb/speech/StartPageActivity.kt +++ b/app/src/main/java/ru/spb/speech/StartPageActivity.kt @@ -18,6 +18,14 @@ import android.support.v7.app.AppCompatActivity import android.util.Log import android.view.Menu import android.view.MenuItem +import com.google.android.gms.auth.api.signin.GoogleSignIn +import com.google.android.gms.auth.api.signin.GoogleSignInOptions +import com.google.android.gms.common.api.Scope +import com.google.api.client.extensions.android.http.AndroidHttp +import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential +import com.google.api.client.json.gson.GsonFactory +import com.google.api.services.drive.Drive +import com.google.api.services.drive.DriveScopes import ru.spb.speech.database.interfaces.PresentationDataDao import ru.spb.speech.database.SpeechDataBase import com.xwray.groupie.GroupAdapter @@ -26,17 +34,25 @@ import kotlinx.android.synthetic.main.activity_start_page.* import ru.spb.speech.appSupport.* import ru.spb.speech.measurementAutomation.RunningTraining import java.io.File +import java.util.* +import kotlin.collections.ArrayList const val debugSpeechAudio = R.raw.assembler // Путь к файлу в raw, // который запускается в виде тестовой звуковой дорожки. const val SHARED_PREFERENCES_FILE_NAME = "ru.spb.speech.prefs" +const val DRIVE_TAG = "DRIVE.TRAINING_ACTIVITY" + class StartPageActivity : AppCompatActivity(), UpdateAdapterListener { companion object { private const val testPresentationFolderFlag = false private const val OPEN_FOLDER_REQ_CODE = 133 + + private const val REQUEST_CODE_SIGN_IN = 0 + + var driveService: Drive? = null } private var testFolderRunner: RunningTraining? = null @@ -100,6 +116,25 @@ class StartPageActivity : AppCompatActivity(), UpdateAdapterListener { val intent = Intent(this, CreatePresentationActivity::class.java) startActivity(intent) } + + requestSignIn() + } + + private fun requestSignIn() { + Log.d(DRIVE_TAG, "Requesting sign-in") + + if (driveService == null) { + val signInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) + .requestEmail() + .requestScopes(Scope(DriveScopes.DRIVE_FILE)) + .build() + val client = GoogleSignIn.getClient(this, signInOptions) + + // The result of the sign-in Intent is handled in onActivityResult. + startActivityForResult(client.signInIntent, REQUEST_CODE_SIGN_IN) + } else { + Log.d(DRIVE_TAG, "Already signed in") + } } override fun onStart() { @@ -214,6 +249,28 @@ class StartPageActivity : AppCompatActivity(), UpdateAdapterListener { Log.d(RunningTraining.LOG, data?.data?.toString()) testFolderRunner?.startTrainings(DocumentFile.fromTreeUri(this, selectedFile!!)!!) } + + REQUEST_CODE_SIGN_IN -> { + GoogleSignIn.getSignedInAccountFromIntent(data) + .addOnSuccessListener { googleAccount -> + Log.d(DRIVE_TAG, "Signed in as " + googleAccount.email!!) + + val credential = GoogleAccountCredential.usingOAuth2( + this, Collections.singleton(DriveScopes.DRIVE_FILE)) + credential.setSelectedAccount(googleAccount.account) + val googleDriveService = Drive.Builder( + AndroidHttp.newCompatibleTransport(), + GsonFactory(), + credential) + .setApplicationName("Drive API Migration") + .build() + + driveService = googleDriveService + } + .addOnFailureListener { exception -> + Log.e(DRIVE_TAG, "Unable to sign in.", exception) + } + } } } } diff --git a/app/src/main/java/ru/spb/speech/appSupport/AudioAnalyzer.kt b/app/src/main/java/ru/spb/speech/appSupport/AudioAnalyzer.kt index e08d05a1..67659694 100644 --- a/app/src/main/java/ru/spb/speech/appSupport/AudioAnalyzer.kt +++ b/app/src/main/java/ru/spb/speech/appSupport/AudioAnalyzer.kt @@ -8,12 +8,15 @@ import android.media.AudioFormat import android.media.AudioRecord import android.media.MediaRecorder import android.os.* +import android.preference.PreferenceManager import android.support.v4.app.DialogFragment import android.support.v4.app.FragmentTransaction import android.support.v4.content.LocalBroadcastManager import android.util.Log import android.view.* import android.widget.TextView +import com.google.api.client.http.ByteArrayContent +import com.google.api.services.drive.Drive import kotlinx.android.parcel.Parcelize import ru.spb.speech.* import java.io.* @@ -256,6 +259,7 @@ class AudioAnalyzer(private val activity: Activity, controller: MutableLiveData< logStatistics() saveFile(byteArrayOutputStream) + saveFileToDrive(byteArrayOutputStream) } } @@ -393,6 +397,35 @@ class AudioAnalyzer(private val activity: Activity, controller: MutableLiveData< } } + private fun saveFileToDrive(byteArrayOutputStream: ByteArrayOutputStream) { + if (StartPageActivity.driveService != null) { + val driveService = (StartPageActivity.driveService as Drive) + + val parentId = PreferenceManager + .getDefaultSharedPreferences(activity) + .getString("drive_folder_key", "root") + .toString() + + var metadata = com.google.api.services.drive.model.File() + .setParents(Collections.singletonList(parentId)) + .setMimeType("audio/vnd.wave") + .setName("My Recording.wav") + + val resultFile = driveService.files().create(metadata).execute() + if (resultFile == null) { + Log.e(DRIVE_TAG, "Null result when creating file") + return + } + + metadata = com.google.api.services.drive.model.File().setName("My Recording.wav") + + val mediaContent = ByteArrayContent.fromString("audio/vnd.wave", byteArrayOutputStream.toString()) + driveService.files().update(resultFile.id, metadata, mediaContent).execute() + + Log.d(DRIVE_TAG, "File saved") + } + } + // used for naming the audio recording file private fun getCurrentDateForName(): String { return SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date()) From 1ecd76fb15363f5eeae6bf1b0ac12633090067fc Mon Sep 17 00:00:00 2001 From: titovtima Date: Thu, 2 Apr 2020 11:30:04 +0300 Subject: [PATCH 03/13] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BB=D0=B8=D1=88=D0=BD=D0=B5=D0=B3=D0=BE=20?= =?UTF-8?q?=D0=B2=20build.gradle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 6 ------ 1 file changed, 6 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 134f3b04..7c7222c3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -92,12 +92,6 @@ dependencies { } dependencies { - // Google Drive -// implementation "com.google.android.gms:play-services-auth:15.0.1" -// implementation "com.google.android.gms:play-services-drive:17.0.0" -// implementation 'com.squareup.okio:okio:1.14.0' - - implementation 'com.google.android.gms:play-services-auth:15.0.1' implementation 'com.google.http-client:google-http-client-gson:1.26.0' implementation('com.google.api-client:google-api-client-android:1.26.0') { From ab1d77fdbf3bb684ca3a4da61a808b72ce7d7ac5 Mon Sep 17 00:00:00 2001 From: titovtima Date: Thu, 2 Apr 2020 11:35:55 +0300 Subject: [PATCH 04/13] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=82=D0=B5=D0=B3=D0=B0=20=D0=B3=D1=83?= =?UTF-8?q?=D0=B3=D0=BB-=D0=B4=D0=B8=D1=81=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/ru/spb/speech/StartPageActivity.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/ru/spb/speech/StartPageActivity.kt b/app/src/main/java/ru/spb/speech/StartPageActivity.kt index b1a722f0..5146daf8 100644 --- a/app/src/main/java/ru/spb/speech/StartPageActivity.kt +++ b/app/src/main/java/ru/spb/speech/StartPageActivity.kt @@ -42,7 +42,7 @@ const val debugSpeechAudio = R.raw.assembler // Путь к файлу в raw, const val SHARED_PREFERENCES_FILE_NAME = "ru.spb.speech.prefs" -const val DRIVE_TAG = "DRIVE.TRAINING_ACTIVITY" +const val DRIVE_TAG = "GOOGLE_DRIVE_TAG" class StartPageActivity : AppCompatActivity(), UpdateAdapterListener { companion object { From 04fd6c839fd44f6d77b1d3cb55de70a372b61bb2 Mon Sep 17 00:00:00 2001 From: titovtima Date: Fri, 3 Apr 2020 23:41:08 +0300 Subject: [PATCH 05/13] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D0=B9=20htt?= =?UTF-8?q?p-=20=D0=B8=20api-=20=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=BE=D1=87=D0=B8=D0=BD?= =?UTF-8?q?=D0=B8=D0=BA=D0=B8=20=D1=81=D0=B1=D0=BE=D1=80=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 7c7222c3..0fdb7c36 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -93,8 +93,8 @@ dependencies { dependencies { implementation 'com.google.android.gms:play-services-auth:15.0.1' - implementation 'com.google.http-client:google-http-client-gson:1.26.0' - implementation('com.google.api-client:google-api-client-android:1.26.0') { + implementation 'com.google.http-client:google-http-client-gson:1.28.0' + implementation('com.google.api-client:google-api-client-android:1.28.0') { exclude group: 'org.apache.httpcomponents' } implementation('com.google.apis:google-api-services-drive:v3-rev136-1.25.0') { From 30fefa0a2f351e9bfec7d636bb753611413c2c03 Mon Sep 17 00:00:00 2001 From: titovtima Date: Mon, 6 Apr 2020 10:24:31 +0300 Subject: [PATCH 06/13] =?UTF-8?q?Null-=D0=B1=D0=B5=D0=B7=D0=BE=D0=BF=D0=B0?= =?UTF-8?q?=D1=81=D0=BD=D0=BE=D1=81=D1=82=D1=8C=20=D0=BF=D1=80=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D0=B8=20ID=20?= =?UTF-8?q?=D0=BF=D0=B0=D0=BF=D0=BA=D0=B8=20=D0=B8=D0=B7=20=D0=BD=D0=B0?= =?UTF-8?q?=D1=81=D1=82=D1=80=D0=BE=D0=B5=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/ru/spb/speech/appSupport/AudioAnalyzer.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/ru/spb/speech/appSupport/AudioAnalyzer.kt b/app/src/main/java/ru/spb/speech/appSupport/AudioAnalyzer.kt index 67659694..605392e5 100644 --- a/app/src/main/java/ru/spb/speech/appSupport/AudioAnalyzer.kt +++ b/app/src/main/java/ru/spb/speech/appSupport/AudioAnalyzer.kt @@ -15,6 +15,7 @@ import android.support.v4.content.LocalBroadcastManager import android.util.Log import android.view.* import android.widget.TextView +import android.widget.Toast import com.google.api.client.http.ByteArrayContent import com.google.api.services.drive.Drive import kotlinx.android.parcel.Parcelize @@ -404,7 +405,11 @@ class AudioAnalyzer(private val activity: Activity, controller: MutableLiveData< val parentId = PreferenceManager .getDefaultSharedPreferences(activity) .getString("drive_folder_key", "root") - .toString() + if (parentId == null) { + Log.e(DRIVE_TAG, "Error while getting folder id from preferences") + Toast.makeText(activity, "Ошибка при получении ID папки Google-диска из настроек", + Toast.LENGTH_SHORT).show() + } var metadata = com.google.api.services.drive.model.File() .setParents(Collections.singletonList(parentId)) From 7687a98fb534326362c96c887b5c559a56fb2567 Mon Sep 17 00:00:00 2001 From: titovtima Date: Thu, 9 Apr 2020 10:09:39 +0300 Subject: [PATCH 07/13] =?UTF-8?q?=D0=92=D1=8B=D0=B4=D0=B5=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D0=BE=D0=B3=D0=BE=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=B4=D0=B5=D0=B9=D1=81=D1=82=D0=B2=D0=B8?= =?UTF-8?q?=D0=B9=20=D1=81=20=D0=B3=D1=83=D0=B3=D0=BB-=D0=B4=D0=B8=D1=81?= =?UTF-8?q?=D0=BA=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/ru/spb/speech/SettingsActivity.kt | 5 + .../java/ru/spb/speech/StartPageActivity.kt | 60 ++-------- .../ru/spb/speech/appSupport/AudioAnalyzer.kt | 39 +------ .../speech/appSupport/GoogleDriveHelper.kt | 105 ++++++++++++++++++ 4 files changed, 118 insertions(+), 91 deletions(-) create mode 100644 app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt diff --git a/app/src/main/java/ru/spb/speech/SettingsActivity.kt b/app/src/main/java/ru/spb/speech/SettingsActivity.kt index f51f3b6b..8f011584 100644 --- a/app/src/main/java/ru/spb/speech/SettingsActivity.kt +++ b/app/src/main/java/ru/spb/speech/SettingsActivity.kt @@ -316,6 +316,11 @@ class SettingsActivity : AppCompatPreferenceActivity() { .getString(preference.key, "")) } + if (preference.key == "drive_folder_key") { + + } } } + + } diff --git a/app/src/main/java/ru/spb/speech/StartPageActivity.kt b/app/src/main/java/ru/spb/speech/StartPageActivity.kt index 5146daf8..8fb6a97e 100644 --- a/app/src/main/java/ru/spb/speech/StartPageActivity.kt +++ b/app/src/main/java/ru/spb/speech/StartPageActivity.kt @@ -18,14 +18,6 @@ import android.support.v7.app.AppCompatActivity import android.util.Log import android.view.Menu import android.view.MenuItem -import com.google.android.gms.auth.api.signin.GoogleSignIn -import com.google.android.gms.auth.api.signin.GoogleSignInOptions -import com.google.android.gms.common.api.Scope -import com.google.api.client.extensions.android.http.AndroidHttp -import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential -import com.google.api.client.json.gson.GsonFactory -import com.google.api.services.drive.Drive -import com.google.api.services.drive.DriveScopes import ru.spb.speech.database.interfaces.PresentationDataDao import ru.spb.speech.database.SpeechDataBase import com.xwray.groupie.GroupAdapter @@ -34,7 +26,6 @@ import kotlinx.android.synthetic.main.activity_start_page.* import ru.spb.speech.appSupport.* import ru.spb.speech.measurementAutomation.RunningTraining import java.io.File -import java.util.* import kotlin.collections.ArrayList const val debugSpeechAudio = R.raw.assembler // Путь к файлу в raw, @@ -42,17 +33,11 @@ const val debugSpeechAudio = R.raw.assembler // Путь к файлу в raw, const val SHARED_PREFERENCES_FILE_NAME = "ru.spb.speech.prefs" -const val DRIVE_TAG = "GOOGLE_DRIVE_TAG" - class StartPageActivity : AppCompatActivity(), UpdateAdapterListener { companion object { private const val testPresentationFolderFlag = false private const val OPEN_FOLDER_REQ_CODE = 133 - - private const val REQUEST_CODE_SIGN_IN = 0 - - var driveService: Drive? = null } private var testFolderRunner: RunningTraining? = null @@ -117,24 +102,11 @@ class StartPageActivity : AppCompatActivity(), UpdateAdapterListener { startActivity(intent) } - requestSignIn() - } - - private fun requestSignIn() { - Log.d(DRIVE_TAG, "Requesting sign-in") - - if (driveService == null) { - val signInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) - .requestEmail() - .requestScopes(Scope(DriveScopes.DRIVE_FILE)) - .build() - val client = GoogleSignIn.getClient(this, signInOptions) - - // The result of the sign-in Intent is handled in onActivityResult. - startActivityForResult(client.signInIntent, REQUEST_CODE_SIGN_IN) - } else { - Log.d(DRIVE_TAG, "Already signed in") - } + val driveFolderId = PreferenceManager + .getDefaultSharedPreferences(this) + .getString("drive_folder_key", "") + if (driveFolderId != null && driveFolderId != "") + GoogleDriveHelper.getInstance().requestSignIn(this) } override fun onStart() { @@ -250,26 +222,8 @@ class StartPageActivity : AppCompatActivity(), UpdateAdapterListener { testFolderRunner?.startTrainings(DocumentFile.fromTreeUri(this, selectedFile!!)!!) } - REQUEST_CODE_SIGN_IN -> { - GoogleSignIn.getSignedInAccountFromIntent(data) - .addOnSuccessListener { googleAccount -> - Log.d(DRIVE_TAG, "Signed in as " + googleAccount.email!!) - - val credential = GoogleAccountCredential.usingOAuth2( - this, Collections.singleton(DriveScopes.DRIVE_FILE)) - credential.setSelectedAccount(googleAccount.account) - val googleDriveService = Drive.Builder( - AndroidHttp.newCompatibleTransport(), - GsonFactory(), - credential) - .setApplicationName("Drive API Migration") - .build() - - driveService = googleDriveService - } - .addOnFailureListener { exception -> - Log.e(DRIVE_TAG, "Unable to sign in.", exception) - } + GoogleDriveHelper.REQUEST_CODE_SIGN_IN -> { + GoogleDriveHelper.getInstance().heldSignInResult(this, data) } } } diff --git a/app/src/main/java/ru/spb/speech/appSupport/AudioAnalyzer.kt b/app/src/main/java/ru/spb/speech/appSupport/AudioAnalyzer.kt index 605392e5..fd1505c5 100644 --- a/app/src/main/java/ru/spb/speech/appSupport/AudioAnalyzer.kt +++ b/app/src/main/java/ru/spb/speech/appSupport/AudioAnalyzer.kt @@ -8,16 +8,12 @@ import android.media.AudioFormat import android.media.AudioRecord import android.media.MediaRecorder import android.os.* -import android.preference.PreferenceManager import android.support.v4.app.DialogFragment import android.support.v4.app.FragmentTransaction import android.support.v4.content.LocalBroadcastManager import android.util.Log import android.view.* import android.widget.TextView -import android.widget.Toast -import com.google.api.client.http.ByteArrayContent -import com.google.api.services.drive.Drive import kotlinx.android.parcel.Parcelize import ru.spb.speech.* import java.io.* @@ -260,7 +256,7 @@ class AudioAnalyzer(private val activity: Activity, controller: MutableLiveData< logStatistics() saveFile(byteArrayOutputStream) - saveFileToDrive(byteArrayOutputStream) + GoogleDriveHelper.getInstance().saveFileToDrive(activity, byteArrayOutputStream) } } @@ -398,39 +394,6 @@ class AudioAnalyzer(private val activity: Activity, controller: MutableLiveData< } } - private fun saveFileToDrive(byteArrayOutputStream: ByteArrayOutputStream) { - if (StartPageActivity.driveService != null) { - val driveService = (StartPageActivity.driveService as Drive) - - val parentId = PreferenceManager - .getDefaultSharedPreferences(activity) - .getString("drive_folder_key", "root") - if (parentId == null) { - Log.e(DRIVE_TAG, "Error while getting folder id from preferences") - Toast.makeText(activity, "Ошибка при получении ID папки Google-диска из настроек", - Toast.LENGTH_SHORT).show() - } - - var metadata = com.google.api.services.drive.model.File() - .setParents(Collections.singletonList(parentId)) - .setMimeType("audio/vnd.wave") - .setName("My Recording.wav") - - val resultFile = driveService.files().create(metadata).execute() - if (resultFile == null) { - Log.e(DRIVE_TAG, "Null result when creating file") - return - } - - metadata = com.google.api.services.drive.model.File().setName("My Recording.wav") - - val mediaContent = ByteArrayContent.fromString("audio/vnd.wave", byteArrayOutputStream.toString()) - driveService.files().update(resultFile.id, metadata, mediaContent).execute() - - Log.d(DRIVE_TAG, "File saved") - } - } - // used for naming the audio recording file private fun getCurrentDateForName(): String { return SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date()) diff --git a/app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt b/app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt new file mode 100644 index 00000000..94513d77 --- /dev/null +++ b/app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt @@ -0,0 +1,105 @@ +package ru.spb.speech.appSupport + +import android.app.Activity +import android.content.Intent +import android.preference.PreferenceManager +import android.util.Log +import android.widget.Toast +import com.google.android.gms.auth.api.signin.GoogleSignIn +import com.google.android.gms.auth.api.signin.GoogleSignInOptions +import com.google.android.gms.common.api.Scope +import com.google.api.client.extensions.android.http.AndroidHttp +import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential +import com.google.api.client.http.ByteArrayContent +import com.google.api.client.json.gson.GsonFactory +import com.google.api.services.drive.Drive +import com.google.api.services.drive.DriveScopes +import java.io.ByteArrayOutputStream +import java.util.* + + +const val DRIVE_TAG = "GOOGLE_DRIVE_TAG" + +class GoogleDriveHelper { + companion object { + const val REQUEST_CODE_SIGN_IN = 0 + + private val driveHelper = GoogleDriveHelper() + + fun getInstance() = driveHelper + } + + var driveService: Drive? = null + + fun requestSignIn(activity: Activity) { + Log.d(DRIVE_TAG, "Requesting sign-in") + + if (driveService == null) { + val signInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) + .requestEmail() + .requestScopes(Scope(DriveScopes.DRIVE_FILE)) + .build() + val client = GoogleSignIn.getClient(activity, signInOptions) + + // The result of the sign-in Intent is handled in onActivityResult. + activity.startActivityForResult(client.signInIntent, REQUEST_CODE_SIGN_IN) + } else { + Log.d(DRIVE_TAG, "Already signed in") + } + } + + fun heldSignInResult(activity: Activity, data: Intent?) { + GoogleSignIn.getSignedInAccountFromIntent(data) + .addOnSuccessListener { googleAccount -> + Log.d(DRIVE_TAG, "Signed in as " + googleAccount.email!!) + + val credential = GoogleAccountCredential.usingOAuth2( + activity, Collections.singleton(DriveScopes.DRIVE_FILE)) + credential.setSelectedAccount(googleAccount.account) + val googleDriveService = Drive.Builder( + AndroidHttp.newCompatibleTransport(), + GsonFactory(), + credential) + .setApplicationName("Drive API Migration") + .build() + + driveService = googleDriveService + } + .addOnFailureListener { exception -> + Log.e(DRIVE_TAG, "Unable to sign in.", exception) + } + } + + fun saveFileToDrive(activity: Activity, byteArrayOutputStream: ByteArrayOutputStream) { + if (driveService != null) { + val driveService = (driveService as Drive) + + val parentId = PreferenceManager + .getDefaultSharedPreferences(activity) + .getString("drive_folder_key", "root") + if (parentId == null) { + Log.e(DRIVE_TAG, "Error while getting folder id from preferences") + Toast.makeText(activity, "Ошибка при получении ID папки Google-диска из настроек", + Toast.LENGTH_SHORT).show() + } + + var metadata = com.google.api.services.drive.model.File() + .setParents(Collections.singletonList(parentId)) + .setMimeType("audio/vnd.wave") + .setName("My Recording.wav") + + val resultFile = driveService.files().create(metadata).execute() + if (resultFile == null) { + Log.e(DRIVE_TAG, "Null result when creating file") + return + } + + metadata = com.google.api.services.drive.model.File().setName("My Recording.wav") + + val mediaContent = ByteArrayContent.fromString("audio/vnd.wave", byteArrayOutputStream.toString()) + driveService.files().update(resultFile.id, metadata, mediaContent).execute() + + Log.d(DRIVE_TAG, "File saved") + } + } +} \ No newline at end of file From 277e656bd712cd6e5a767c9b01dd75d32587f945 Mon Sep 17 00:00:00 2001 From: titovtima Date: Thu, 9 Apr 2020 10:24:36 +0300 Subject: [PATCH 08/13] =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D1=83=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B8=D0=BC=D0=B5=D0=BD=D0=B8=20=D0=B0?= =?UTF-8?q?=D0=BA=D0=BA=D0=B0=D1=83=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/spb/speech/appSupport/GoogleDriveHelper.kt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt b/app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt index 94513d77..818d9380 100644 --- a/app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt +++ b/app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt @@ -30,6 +30,7 @@ class GoogleDriveHelper { } var driveService: Drive? = null + var accountName = "Unnamed" fun requestSignIn(activity: Activity) { Log.d(DRIVE_TAG, "Requesting sign-in") @@ -51,7 +52,15 @@ class GoogleDriveHelper { fun heldSignInResult(activity: Activity, data: Intent?) { GoogleSignIn.getSignedInAccountFromIntent(data) .addOnSuccessListener { googleAccount -> - Log.d(DRIVE_TAG, "Signed in as " + googleAccount.email!!) + val name = googleAccount.displayName + if (name != null) + accountName = name + else { + accountName = "Unnamed" + Log.e(DRIVE_TAG, "Failed getting google account name") + } + + Log.d(DRIVE_TAG, "Signed in as $name") val credential = GoogleAccountCredential.usingOAuth2( activity, Collections.singleton(DriveScopes.DRIVE_FILE)) From 23681f0b117f4f2732d82a2e106d6958c6e5e3dc Mon Sep 17 00:00:00 2001 From: titovtima Date: Thu, 9 Apr 2020 10:41:49 +0300 Subject: [PATCH 09/13] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BB=D0=BE=D0=B3=D0=B3=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=B8=20Toast'=D0=BE?= =?UTF-8?q?=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/spb/speech/appSupport/GoogleDriveHelper.kt | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt b/app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt index 818d9380..a423daf5 100644 --- a/app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt +++ b/app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt @@ -42,7 +42,7 @@ class GoogleDriveHelper { .build() val client = GoogleSignIn.getClient(activity, signInOptions) - // The result of the sign-in Intent is handled in onActivityResult. + Log.d(DRIVE_TAG, "Starting activity for result") activity.startActivityForResult(client.signInIntent, REQUEST_CODE_SIGN_IN) } else { Log.d(DRIVE_TAG, "Already signed in") @@ -52,15 +52,22 @@ class GoogleDriveHelper { fun heldSignInResult(activity: Activity, data: Intent?) { GoogleSignIn.getSignedInAccountFromIntent(data) .addOnSuccessListener { googleAccount -> + Log.d(DRIVE_TAG, "Logged in successfully") + val name = googleAccount.displayName if (name != null) accountName = name else { accountName = "Unnamed" Log.e(DRIVE_TAG, "Failed getting google account name") + Toast.makeText(activity, + "Ошибка при получении имени из Google-аккаунта", + Toast.LENGTH_SHORT).show() } Log.d(DRIVE_TAG, "Signed in as $name") + Toast.makeText(activity, "Вы вошли в Google как $name", Toast.LENGTH_SHORT) + .show() val credential = GoogleAccountCredential.usingOAuth2( activity, Collections.singleton(DriveScopes.DRIVE_FILE)) @@ -73,9 +80,12 @@ class GoogleDriveHelper { .build() driveService = googleDriveService + Log.d(DRIVE_TAG, "Drive service is ready") } .addOnFailureListener { exception -> Log.e(DRIVE_TAG, "Unable to sign in.", exception) + Toast.makeText(activity, "Ошибка при входе в Google-аккаунт", + Toast.LENGTH_SHORT).show() } } @@ -100,6 +110,8 @@ class GoogleDriveHelper { val resultFile = driveService.files().create(metadata).execute() if (resultFile == null) { Log.e(DRIVE_TAG, "Null result when creating file") + Toast.makeText(activity, "Ошибка при создании файла на Google-диске", + Toast.LENGTH_SHORT).show() return } From 31f9fce84a234dcf13df4db02b279c075ed60c19 Mon Sep 17 00:00:00 2001 From: titovtima Date: Thu, 9 Apr 2020 11:05:59 +0300 Subject: [PATCH 10/13] =?UTF-8?q?=D0=A1=D0=BE=D1=81=D1=82=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B8=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0=20=D0=B8=D0=B7=20=D0=B8=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D0=B5=D0=BB=D1=8F=20=D0=B8=20=D0=B2=D1=80=D0=B5?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D0=B8=20=D1=82=D1=80=D0=B5=D0=BD=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/ru/spb/speech/appSupport/GoogleDriveHelper.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt b/app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt index a423daf5..6651bc88 100644 --- a/app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt +++ b/app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt @@ -15,6 +15,7 @@ import com.google.api.client.json.gson.GsonFactory import com.google.api.services.drive.Drive import com.google.api.services.drive.DriveScopes import java.io.ByteArrayOutputStream +import java.text.SimpleDateFormat import java.util.* @@ -102,10 +103,13 @@ class GoogleDriveHelper { Toast.LENGTH_SHORT).show() } + val fileName = "$accountName " + + SimpleDateFormat.getDateTimeInstance().format(Date()) + ".wav" + var metadata = com.google.api.services.drive.model.File() .setParents(Collections.singletonList(parentId)) .setMimeType("audio/vnd.wave") - .setName("My Recording.wav") + .setName(fileName) val resultFile = driveService.files().create(metadata).execute() if (resultFile == null) { @@ -115,7 +119,7 @@ class GoogleDriveHelper { return } - metadata = com.google.api.services.drive.model.File().setName("My Recording.wav") + metadata = com.google.api.services.drive.model.File().setName(fileName) val mediaContent = ByteArrayContent.fromString("audio/vnd.wave", byteArrayOutputStream.toString()) driveService.files().update(resultFile.id, metadata, mediaContent).execute() From b1ac964f1588af8d4afaeecd9e04932a5ef7c93b Mon Sep 17 00:00:00 2001 From: titovtima Date: Thu, 9 Apr 2020 17:46:21 +0300 Subject: [PATCH 11/13] =?UTF-8?q?=D0=92=D1=8B=D0=BD=D0=B5=D1=81=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D1=8E=D1=87=D0=B0=20=D0=BF?= =?UTF-8?q?=D0=B0=D0=BF=D0=BA=D0=B8=20=D0=B4=D0=B8=D1=81=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B2=20=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B9=D0=BA=D0=B0?= =?UTF-8?q?=D1=85=20=D0=B2=20=D1=81=D1=82=D1=80=D0=BE=D0=BA=D0=BE=D0=B2?= =?UTF-8?q?=D1=8B=D0=B9=20=D1=80=D0=B5=D1=81=D1=83=D1=80=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/ru/spb/speech/SettingsActivity.kt | 2 +- app/src/main/java/ru/spb/speech/StartPageActivity.kt | 2 +- .../main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt | 3 ++- app/src/main/res/values/strings.xml | 1 + 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/ru/spb/speech/SettingsActivity.kt b/app/src/main/java/ru/spb/speech/SettingsActivity.kt index 8f011584..46f791bd 100644 --- a/app/src/main/java/ru/spb/speech/SettingsActivity.kt +++ b/app/src/main/java/ru/spb/speech/SettingsActivity.kt @@ -316,7 +316,7 @@ class SettingsActivity : AppCompatPreferenceActivity() { .getString(preference.key, "")) } - if (preference.key == "drive_folder_key") { + if (preference.key == preference.context.getString(R.string.drive_folder_key)) { } } diff --git a/app/src/main/java/ru/spb/speech/StartPageActivity.kt b/app/src/main/java/ru/spb/speech/StartPageActivity.kt index 8fb6a97e..ff613485 100644 --- a/app/src/main/java/ru/spb/speech/StartPageActivity.kt +++ b/app/src/main/java/ru/spb/speech/StartPageActivity.kt @@ -104,7 +104,7 @@ class StartPageActivity : AppCompatActivity(), UpdateAdapterListener { val driveFolderId = PreferenceManager .getDefaultSharedPreferences(this) - .getString("drive_folder_key", "") + .getString(getString(R.string.drive_folder_key), "") if (driveFolderId != null && driveFolderId != "") GoogleDriveHelper.getInstance().requestSignIn(this) } diff --git a/app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt b/app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt index 6651bc88..19c817c1 100644 --- a/app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt +++ b/app/src/main/java/ru/spb/speech/appSupport/GoogleDriveHelper.kt @@ -14,6 +14,7 @@ import com.google.api.client.http.ByteArrayContent import com.google.api.client.json.gson.GsonFactory import com.google.api.services.drive.Drive import com.google.api.services.drive.DriveScopes +import ru.spb.speech.R import java.io.ByteArrayOutputStream import java.text.SimpleDateFormat import java.util.* @@ -96,7 +97,7 @@ class GoogleDriveHelper { val parentId = PreferenceManager .getDefaultSharedPreferences(activity) - .getString("drive_folder_key", "root") + .getString(activity.getString(R.string.drive_folder_key), "root") if (parentId == null) { Log.e(DRIVE_TAG, "Error while getting folder id from preferences") Toast.makeText(activity, "Ошибка при получении ID папки Google-диска из настроек", diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5e5b269f..1e796fed 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -362,6 +362,7 @@ Отсутствие слов паразитов: Калибровка уровня шума в помещении,\n сохраняйте тишину Папка для сохранения на Google-диск + drive_folder_key а именно а не то From 59ea7e5125df0ca630cf41a7faede39dd9aa7036 Mon Sep 17 00:00:00 2001 From: titovtima Date: Thu, 9 Apr 2020 17:52:49 +0300 Subject: [PATCH 12/13] =?UTF-8?q?=D0=92=D1=8B=D0=BD=D0=B5=D1=81=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D1=8E=D1=87=D0=B0=20=D0=BF?= =?UTF-8?q?=D0=B0=D0=BF=D0=BA=D0=B8=20=D0=B4=D0=B8=D1=81=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B2=20=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B9=D0=BA=D0=B0?= =?UTF-8?q?=D1=85=20=D0=B2=20=D1=81=D1=82=D1=80=D0=BE=D0=BA=D0=BE=D0=B2?= =?UTF-8?q?=D1=8B=D0=B9=20=D1=80=D0=B5=D1=81=D1=83=D1=80=D1=81=20(=D0=B4?= =?UTF-8?q?=D0=BE=20=D0=BA=D0=BE=D0=BD=D1=86=D0=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/ru/spb/speech/SettingsActivity.kt | 4 +--- app/src/main/res/xml/pref_general.xml | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/ru/spb/speech/SettingsActivity.kt b/app/src/main/java/ru/spb/speech/SettingsActivity.kt index 46f791bd..ab53ce5d 100644 --- a/app/src/main/java/ru/spb/speech/SettingsActivity.kt +++ b/app/src/main/java/ru/spb/speech/SettingsActivity.kt @@ -103,7 +103,7 @@ class SettingsActivity : AppCompatPreferenceActivity() { bindPreferenceSummaryToValue(findPreference("example_list")) bindPreferenceSummaryToValue(findPreference(getString(R.string.speed_key))) bindPreferenceSummaryToValue(findPreference("statistics_collection")) - bindPreferenceSummaryToValue(findPreference("drive_folder_key")) + bindPreferenceSummaryToValue(findPreference(getString(R.string.drive_folder_key))) } override fun onOptionsItemSelected(item: MenuItem): Boolean { @@ -321,6 +321,4 @@ class SettingsActivity : AppCompatPreferenceActivity() { } } } - - } diff --git a/app/src/main/res/xml/pref_general.xml b/app/src/main/res/xml/pref_general.xml index 1d7c96c6..c84da5c3 100644 --- a/app/src/main/res/xml/pref_general.xml +++ b/app/src/main/res/xml/pref_general.xml @@ -50,7 +50,7 @@ From 002a89fddebd1a21b7c2dd84b87b04481d947c64 Mon Sep 17 00:00:00 2001 From: titovtima Date: Fri, 17 Apr 2020 09:19:36 +0300 Subject: [PATCH 13/13] =?UTF-8?q?=D0=92=D1=85=D0=BE=D0=B4=20=D0=B2=20Googl?= =?UTF-8?q?e=20=D0=BF=D1=80=D0=B8=20=D0=B2=D1=8B=D1=85=D0=BE=D0=B4=D0=B5?= =?UTF-8?q?=20=D0=B8=D0=B7=20=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B5?= =?UTF-8?q?=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/ru/spb/speech/SettingsActivity.kt | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/ru/spb/speech/SettingsActivity.kt b/app/src/main/java/ru/spb/speech/SettingsActivity.kt index ab53ce5d..b74cc423 100644 --- a/app/src/main/java/ru/spb/speech/SettingsActivity.kt +++ b/app/src/main/java/ru/spb/speech/SettingsActivity.kt @@ -2,6 +2,7 @@ package ru.spb.speech import android.annotation.SuppressLint import android.annotation.TargetApi +import android.app.Activity import android.app.AlarmManager import android.app.PendingIntent import android.content.ComponentName @@ -17,6 +18,7 @@ import android.preference.* import android.text.TextUtils import android.util.Log import android.view.MenuItem +import ru.spb.speech.appSupport.GoogleDriveHelper import ru.spb.speech.notifications.AlarmBootReceiver import ru.spb.speech.notifications.AlarmReceiver import ru.spb.speech.notifications.NotificationsHelper @@ -52,6 +54,12 @@ class SettingsActivity : AppCompatPreferenceActivity() { override fun onOptionsItemSelected(item: MenuItem): Boolean { val id = item.itemId if (id == android.R.id.home) { + val driveFolderId = PreferenceManager + .getDefaultSharedPreferences(this) + .getString(getString(R.string.drive_folder_key), "") + if (driveFolderId != null && driveFolderId != "") + GoogleDriveHelper.getInstance().requestSignIn(this) + onBackPressed() return true } @@ -220,6 +228,18 @@ class SettingsActivity : AppCompatPreferenceActivity() { } } + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + + if (resultCode == Activity.RESULT_OK) { + when (requestCode) { + GoogleDriveHelper.REQUEST_CODE_SIGN_IN -> { + GoogleDriveHelper.getInstance().heldSignInResult(this, data) + } + } + } + } + companion object { /** @@ -315,10 +335,6 @@ class SettingsActivity : AppCompatPreferenceActivity() { .getDefaultSharedPreferences(preference.context) .getString(preference.key, "")) } - - if (preference.key == preference.context.getString(R.string.drive_folder_key)) { - - } } } }