在Google云端硬盘中同步会议室数据库?

时间:2019-06-21 04:19:48

标签: android database kotlin android-room google-room

我正在尝试将会议室数据库与Google驱动器同步,但我已遵循本文档https://medium.com/glucosio-project/backup-restore-a-realm-database-on-google-drive-with-drive-api-c238515a5975,但无法同步驱动器中的数据库。

我还尝试了https://stackoverflow.com/a/49297966/8143436这个答案,并且尝试同步数据库,如下所示:

我在下面尝试过

  private fun singIn() {
    val requiredScopes = HashSet<Scope>(2)

    requiredScopes.add(Drive.SCOPE_FILE)
    val signInAccount = GoogleSignIn.getLastSignedInAccount(activity!!)

    if (signInAccount != null && signInAccount.grantedScopes.containsAll(requiredScopes)) {
        initializeDriveClient(signInAccount)
    } else {
        val signInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestScopes(Drive.SCOPE_FILE)
            .build()

        val googleSignInClient = GoogleSignIn.getClient(activity!!, signInOptions)
        startActivityForResult(googleSignInClient.signInIntent, REQUEST_CODE_SIGN_IN)
    }
}

private fun initializeDriveClient(signInAccount: GoogleSignInAccount) {
    driveClient = Drive.getDriveClient(activity!!, signInAccount)
    driveResourceClient = Drive.getDriveResourceClient(activity!!, signInAccount)
    onDriveClientReady()
}

   private fun onDriveClientReady() {

    getRootFolder()
  }

private fun getRootFolder() {
    /* Get the app folder */
    val appFolderTask = driveResourceClient!!.rootFolder

    appFolderTask.addOnSuccessListener {

        baseFolder = it
        checkForBackUp()
    }
    appFolderTask.addOnFailureListener {

    }

}

private fun checkForBackUp() {
    val query = Query.Builder()
        .addFilter(Filters.eq(SearchableField.TITLE, BACK_UP))
        .build()


    val queryTask = baseFolder?.let { driveResourceClient?.queryChildren(it, query) }
    queryTask!!.addOnSuccessListener(activity!!, object : OnSuccessListener<MetadataBuffer> {
        override fun onSuccess(metadataBuffer: MetadataBuffer?) {
            if (metadataBuffer?.count == 0) {
                Log.d("TTTT", "File $BACK_UP not found\n")

                /* Make file backup */
                backUpDatabase()
            } else {
                Log.d("TTTT", metadataBuffer?.count.toString() + " Instances of file " + BACK_UP + " found\n")
                val metadata = metadataBuffer?.get(metadataBuffer.count - 1)
                restoreBackUp(metadata?.driveId!!.asDriveFile())
            }
        }


    })
}

  private fun restoreBackUp(asDriveFile: DriveFile?) {
 //        if (!dbShm.exists()) {
 //            Log.d("Hello", "Local database not found?!\\n") 
//            return
 //        }
    val dbFileOld = activity!!.getDatabasePath("my_db")


    /* Check of dbFileExists on device, delete if it does because it needs to be completely over written */
    if (dbFileOld.exists()) {
        dbFileOld.delete()
    }


    val dbFileNew = activity!!.getDatabasePath("my_db")

    val fileOutputStream = FileOutputStream(dbFileNew)
    val openFileTask = driveResourceClient?.openFile(asDriveFile!!, DriveFile.MODE_READ_ONLY);
    openFileTask?.continueWith { p0 ->
        val backupContents = p0.result
        val inputStream = backupContents!!.inputStream
        Log.d("TTTT", "Attempting to restore from database\n")
        val buffer = ByteArray(1024)
        var c: Int = inputStream.read(buffer, 0, buffer.size)
        while (c != -1 && c > 0) {
            fileOutputStream.write(buffer, 0, c)
            c = inputStream.read(buffer, 0, buffer.size)
        }
        fileOutputStream.flush()
        fileOutputStream.close()
        fileOutputStream.flush()
        fileOutputStream.close()
        Log.d("TTTT", "Database restored\n")

        driveResourceClient!!.discardContents(backupContents)
    }?.addOnFailureListener {
        Log.d("TTTT", "Could not read file contents\\n")
    }


}

private fun backUpDatabase() {
    val db = activity!!.getDatabasePath("my_db")


    if (!db.exists()) {
        Log.d("TTTT", "Local database not found?!\\n")
        return
    }

    val fileInputStream = FileInputStream(db)
    val createContentsTask = driveResourceClient?.createContents()
    Log.d("TTTT", "Creating a back-up of the Database File\n")
    Tasks.whenAll(createContentsTask).continueWithTask {
        val contents = createContentsTask?.result
        val outputStream = contents?.outputStream
        Log.d("TTTT", "Attempting to write\\n")
        val buffer = ByteArray(1024)


        var c: Int = fileInputStream.read(buffer)

        while (c != -1) {
            outputStream?.write(buffer, 0, c)
            c = fileInputStream.read(buffer)
        }


        outputStream!!.flush()
        outputStream.close()
        fileInputStream.close()
        Log.d("TTTT", "Database written\\n")
        val changeSet = MetadataChangeSet.Builder()
            .setTitle(BACK_UP)
            .setMimeType("application/x-sqlite3")
            .setStarred(false)
            .build()
        driveResourceClient!!.createFile(baseFolder!!, changeSet, contents)
    }.addOnSuccessListener {
        Log.d("TTTT", "Back up file created\n")
    }.addOnFailureListener {
        Log.d("TTTT", "Could not create back up file\n")
    }

}

是否可以在Google驱动器中同步会议室数据库?

0 个答案:

没有答案