Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/fcat97/driveBackupApi
Browse files Browse the repository at this point in the history
  • Loading branch information
fcat97 committed Apr 13, 2023
2 parents b88b2e3 + e36de01 commit a547432
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 514 deletions.
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ android {

defaultConfig {
applicationId "com.tos.drivebackup"
minSdk 21
minSdk 19
targetSdk 33
versionCode 1
versionName "1.0"

multiDexEnabled true

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
Expand Down Expand Up @@ -50,6 +52,8 @@ dependencies {
implementation 'com.google.android.material:material:1.8.0'
implementation 'com.google.code.gson:gson:2.10.1'

implementation "androidx.multidex:multidex:2.0.1"

implementation project(':libDriveBackup')
// implementation 'com.github.fcat97:driveBackupApi:1.0.5'
// implementation 'com.github.fcat97:driveBackupApi:1.0.7'
}
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:name="androidx.multidex.MultiDexApplication"
android:allowBackup="false"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/com/tos/drivebackup/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.widget.TextView
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatDelegate
import androidx.lifecycle.MutableLiveData
import com.google.android.material.color.DynamicColors
import com.google.android.material.textfield.TextInputEditText
Expand Down Expand Up @@ -58,6 +59,8 @@ class MainActivity : ComponentActivity() {
DynamicColors.applyToActivitiesIfAvailable(this.application)
setContentView(R.layout.main_activity)

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)

rootFolderButton = findViewById(R.id.createFolderButton)
sendButton = findViewById(R.id.sendButton)
fetchButton = findViewById(R.id.fetchButton)
Expand Down Expand Up @@ -108,7 +111,8 @@ class MainActivity : ComponentActivity() {

private fun fetchFiles() {
googleDriveBackupManager.getFiles {
printToTerminal("[backups]: \n" + it.joinToString(separator = "\n"))
Log.d(TAG, "fetchFiles: ${it.joinToString(separator = "\n")}")
printToTerminal("[backups]: \n" + it.joinToString(separator = "\n\n"))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.tos.drivebackup.drive_backup

import android.content.Context
import android.util.Log
import androidx.annotation.RequiresApi
import com.google.gson.Gson
import com.tos.drivebackup.drive_backup.BackupSchema.CURRENT_BACKUP_SCHEMA_VER
import com.tos.drivebackup.drive_backup.BackupSchema.getBackupFile
Expand All @@ -12,10 +11,8 @@ import java.io.File
import java.io.FileReader
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.List

object DriveBackupUtils {
@RequiresApi(api = 21)
fun backupToDrive(
context: Context,
backupManager: GoogleDriveBackupManager,
Expand All @@ -42,7 +39,6 @@ object DriveBackupUtils {
}
}

@RequiresApi(api = 21)
fun restoreBackup(
context: Context,
backupManager: GoogleDriveBackupManager,
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/main_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
android:id="@+id/signInButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign Out"
android:text="Sign In"
android:layout_marginVertical="8dp"
style="@style/ThemeOverlay.Material3.Button.ElevatedButton"/>

Expand Down
20 changes: 13 additions & 7 deletions libDriveBackup/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
compileSdk 33

defaultConfig {
minSdk 21
minSdk 19
targetSdk 33

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -35,12 +35,18 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.documentfile:documentfile:1.0.1'

implementation 'com.google.api-client:google-api-client:2.2.0'
implementation 'com.google.apis:google-api-services-drive:v3-rev20220815-2.0.0'
implementation 'com.google.auth:google-auth-library-oauth2-http:1.16.0'
implementation('com.google.api-client:google-api-client:2.2.0')
implementation('com.google.apis:google-api-services-drive:v3-rev20220815-2.0.0')
implementation('com.google.auth:google-auth-library-oauth2-http:1.16.0')
implementation('com.google.api-client:google-api-client-android:2.2.0')
implementation('com.google.android.gms:play-services-auth:20.5.0')

implementation 'com.google.api-client:google-api-client-android:2.2.0'
implementation 'com.google.android.gms:play-services-auth:20.4.1'
// https://stackoverflow.com/a/71085378/8229399
implementation("com.google.guava:guava") {
version {
strictly '31.1-android'
}
}
}


Expand All @@ -50,7 +56,7 @@ afterEvaluate {
publications {
maven(MavenPublication) {
group = 'media.uqab.driveBackupApi'
version = '1.0.5'
version = '1.0.8'
artifactId = "driveBackupApi"
from components.release

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package media.uqab.libdrivebackup

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential
import media.uqab.libdrivebackup.model.*
import media.uqab.libdrivebackup.model.Constants
import media.uqab.libdrivebackup.useCase.*
import media.uqab.libdrivebackup.useCase.GetCredential.getCredential
import media.uqab.libdrivebackup.useCase.GetOneTapSignInIntent.getSignInIntent
import java.io.File
import java.io.FileOutputStream
import java.util.*
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

/**
* A manager class to view, update, delete and modify
Expand All @@ -34,19 +36,33 @@ import java.util.*
class GoogleDriveBackupManager(
appID: String,
private val activity: ComponentActivity,
private val credentialID: String,
private val credentialID: String
) {
init {
if (activity.lifecycle.currentState != Lifecycle.State.INITIALIZED) {
throw InitializationException("Must initialize before OnStart but initialized in ${activity.lifecycle.currentState}")
}

// attach an observer to cancel all running executor services
activity.lifecycle.addObserver(object : LifecycleEventObserver {
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
if (event == Lifecycle.Event.ON_DESTROY) {
_executor?.shutdown()
}
}
})

if (credentialID.isBlank()) throw InitializationException("Credential ID not provided")
if (appID.isEmpty()) throw InitializationException("App Name not provided")

Constants.APP_NAME = appID
}

/**
* A single threaded executor to run all the background tasks
*/
private var _executor: ExecutorService? = null

/**
* Run this block when user grants drive uses permission. Must be set before
* each operation.
Expand Down Expand Up @@ -86,7 +102,7 @@ class GoogleDriveBackupManager(
onFailed: ((Exception) -> Unit)? = null,
result: (List<FileInfo>) -> Unit
) = requestConsentAndProceed(onFailed) {
Thread {
execute(onFailed) {
try {
val files = GetFiles.getFiles(it).files.map {
FileInfo(
Expand All @@ -110,7 +126,7 @@ class GoogleDriveBackupManager(
onFailed?.invoke(e)
}
}
}.start()
}
}

/**
Expand All @@ -125,7 +141,7 @@ class GoogleDriveBackupManager(
onFailed: ((Exception) -> Unit)? = null,
result: (FileInfo) -> Unit
) = requestConsentAndProceed(onFailed) { c ->
Thread {
execute(onFailed) {
try {
val file = GetFile.getFile(fileID, c).let {
FileInfo(
Expand All @@ -145,7 +161,7 @@ class GoogleDriveBackupManager(
onFailed?.invoke(e)
}
}
}.start()
}
}

/**
Expand All @@ -157,7 +173,7 @@ class GoogleDriveBackupManager(
onFailed: ((Exception) -> Unit)? = null,
onUpload: (fileID: String) -> Unit
) = requestConsentAndProceed(onFailed) {
Thread {
execute(onFailed) {
try {
val fileID = UploadAppData.uploadAppData(it, file, mimeType)

Expand All @@ -170,7 +186,7 @@ class GoogleDriveBackupManager(
onFailed?.invoke(e)
}
}
}.start()
}
}

/**
Expand All @@ -188,7 +204,7 @@ class GoogleDriveBackupManager(
onFailed: ((Exception) -> Unit)? = null,
onDownload: (File) -> Unit
) = requestConsentAndProceed(onFailed) {
Thread {
execute(onFailed) {
val fos = FileOutputStream(outputFile)
try {
val baOs = DownloadFile.downloadFile(it, fileID)
Expand All @@ -207,7 +223,7 @@ class GoogleDriveBackupManager(
} finally {
fos.close()
}
}.start()
}
}

/**
Expand All @@ -220,7 +236,7 @@ class GoogleDriveBackupManager(
onFailed: ((Exception) -> Unit)? = null,
onCreate: (String) -> Unit
) = requestConsentAndProceed(onFailed) {
Thread {
execute(onFailed) {
try {
val folderID = CreateRootFolder.create(it)
activity.runOnUiThread {
Expand All @@ -233,7 +249,7 @@ class GoogleDriveBackupManager(
onFailed?.invoke(e)
}
}
}.start()
}
}

/**
Expand All @@ -247,7 +263,7 @@ class GoogleDriveBackupManager(
onFailed: ((Exception) -> Unit)? = null,
onDelete: () -> Unit
) = requestConsentAndProceed(onFailed) {
Thread {
execute(onFailed) {
try {
DeleteFile.delete(it, fileID)
activity.runOnUiThread {
Expand All @@ -259,7 +275,7 @@ class GoogleDriveBackupManager(
}
Log.w(TAG, "failed to delete file $fileID", e)
}
}.start()
}
}

fun signOut(
Expand Down Expand Up @@ -330,6 +346,27 @@ class GoogleDriveBackupManager(
consentLauncher.launch(signInIntent)
}

/**
* Execute the task on the executor.
*
*/
private fun execute(onFailed: ((Exception) -> Unit)?, task: Runnable) {
try {
getExecutor().submit(task)
} catch (e: Exception) {
activity.runOnUiThread {
onFailed?.invoke(e)
}
}
}

private fun getExecutor(): ExecutorService {
if (_executor == null) _executor = Executors.newSingleThreadExecutor()
else if (_executor!!.isTerminated) _executor = Executors.newSingleThreadExecutor()

return _executor!!
}

companion object {
private const val TAG = "BackupManager"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal object GetCredential {
return credential
} catch (e: Exception) {
if(e is ApiException && e.statusCode == 10) {
Log.e(TAG, "Not configured properly. Maybe you used wrong credential.")
Log.e(TAG, "GCP not configured properly. Maybe you used wrong credential ID or wrong SHA-1.")
} else {
Log.e(TAG, "Failed to get credential", e)
}
Expand Down
Loading

0 comments on commit a547432

Please sign in to comment.