Base backup restore activity.
parent
0cd24905b7
commit
255271bfaf
@ -0,0 +1,95 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
<data>
|
||||
<import type="org.thoughtcrime.securesms.loki.activities.RestoreBackupViewModel"/>
|
||||
<import type="android.view.View"/>
|
||||
<variable
|
||||
name="viewModel"
|
||||
type="org.thoughtcrime.securesms.loki.activities.RestoreBackupViewModel" />
|
||||
</data>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<View
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="@dimen/very_large_spacing"
|
||||
android:layout_marginRight="@dimen/very_large_spacing"
|
||||
android:text="Restore from backup"
|
||||
android:textColor="@color/text"
|
||||
android:textSize="@dimen/large_font_size"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="@dimen/very_large_spacing"
|
||||
android:layout_marginTop="4dp"
|
||||
android:layout_marginRight="@dimen/very_large_spacing"
|
||||
android:text="Go on and pick the backup file to restore from."
|
||||
android:textColor="@color/text"
|
||||
android:textSize="@dimen/small_font_size" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/buttonSelectFile"
|
||||
style="@style/Button.Primary"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="@dimen/very_large_spacing"
|
||||
android:layout_marginTop="4dp"
|
||||
android:layout_marginRight="@dimen/very_large_spacing"
|
||||
android:text="@{RestoreBackupViewModel.uriToFileName(buttonSelectFile, viewModel.backupFile), default=`Select a file`}"/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/backupCode"
|
||||
style="@style/SmallSessionEditText"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="@dimen/very_large_spacing"
|
||||
android:layout_marginTop="10dp"
|
||||
android:layout_marginRight="@dimen/very_large_spacing"
|
||||
android:hint="Backup code"
|
||||
android:inputType="numberDecimal"
|
||||
android:text="@={viewModel.backupPassphrase}" />
|
||||
|
||||
<View
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/restoreButton"
|
||||
style="@style/Widget.Session.Button.Common.ProminentFilled"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="@dimen/medium_button_height"
|
||||
android:layout_marginLeft="@dimen/massive_spacing"
|
||||
android:layout_marginRight="@dimen/massive_spacing"
|
||||
android:text="@string/continue_2"
|
||||
android:visibility="@{RestoreBackupViewModel.validateData(viewModel.backupFile, viewModel.backupPassphrase) ? View.VISIBLE : View.INVISIBLE}"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/termsTextView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="@dimen/onboarding_button_bottom_offset"
|
||||
android:layout_marginLeft="@dimen/massive_spacing"
|
||||
android:layout_marginRight="@dimen/massive_spacing"
|
||||
android:gravity="center"
|
||||
android:text="By using this service, you agree to our Terms of Service and Privacy Policy"
|
||||
android:textColor="@color/text"
|
||||
android:textColorLink="@color/text"
|
||||
android:textSize="@dimen/very_small_font_size" /> <!-- Intentionally not yet translated -->
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</layout>
|
@ -0,0 +1,231 @@
|
||||
package org.thoughtcrime.securesms.loki.activities
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.Typeface
|
||||
import android.net.Uri
|
||||
import android.os.AsyncTask
|
||||
import android.os.Bundle
|
||||
import android.provider.OpenableColumns
|
||||
import android.text.Spannable
|
||||
import android.text.SpannableStringBuilder
|
||||
import android.text.method.LinkMovementMethod
|
||||
import android.text.style.ClickableSpan
|
||||
import android.text.style.StyleSpan
|
||||
import android.view.View
|
||||
import android.widget.Toast
|
||||
import androidx.activity.viewModels
|
||||
import androidx.core.widget.addTextChangedListener
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import com.google.android.gms.common.util.Strings
|
||||
import kotlinx.android.synthetic.main.activity_pn_mode.*
|
||||
import network.loki.messenger.R
|
||||
import network.loki.messenger.databinding.ActivityBackupRestoreBinding
|
||||
import org.thoughtcrime.securesms.ApplicationContext
|
||||
import org.thoughtcrime.securesms.BaseActionBarActivity
|
||||
import org.thoughtcrime.securesms.RegistrationActivity
|
||||
import org.thoughtcrime.securesms.backup.FullBackupImporter
|
||||
import org.thoughtcrime.securesms.backup.FullBackupImporter.DatabaseDowngradeException
|
||||
import org.thoughtcrime.securesms.crypto.AttachmentSecretProvider
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory
|
||||
import org.thoughtcrime.securesms.logging.Log
|
||||
import org.thoughtcrime.securesms.loki.utilities.setUpActionBarSessionLogo
|
||||
import org.thoughtcrime.securesms.loki.utilities.show
|
||||
import org.thoughtcrime.securesms.notifications.NotificationChannels
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
||||
import java.io.IOException
|
||||
|
||||
class BackupRestoreActivity : BaseActionBarActivity() {
|
||||
|
||||
companion object {
|
||||
private const val TAG = "BackupRestoreActivity"
|
||||
private const val REQUEST_CODE_BACKUP_FILE = 779955
|
||||
}
|
||||
|
||||
private val viewModel by viewModels<RestoreBackupViewModel>()
|
||||
|
||||
// region Lifecycle
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setUpActionBarSessionLogo()
|
||||
val dataBinding = DataBindingUtil.setContentView<ActivityBackupRestoreBinding>(this, R.layout.activity_backup_restore)
|
||||
dataBinding.lifecycleOwner = this
|
||||
dataBinding.viewModel = viewModel
|
||||
// setContentView(R.layout.activity_backup_restore)
|
||||
|
||||
dataBinding.restoreButton.setOnClickListener { restore() }
|
||||
|
||||
dataBinding.buttonSelectFile.setOnClickListener {
|
||||
// Let user pick a file.
|
||||
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
|
||||
// type = BackupUtil.BACKUP_FILE_MIME_TYPE
|
||||
type = "*/*"
|
||||
}
|
||||
startActivityForResult(intent, REQUEST_CODE_BACKUP_FILE)
|
||||
}
|
||||
|
||||
dataBinding.backupCode.addTextChangedListener { text -> viewModel.backupPassphrase.value = text.toString() }
|
||||
|
||||
//region Legal info views
|
||||
val termsExplanation = SpannableStringBuilder("By using this service, you agree to our Terms of Service and Privacy Policy")
|
||||
termsExplanation.setSpan(StyleSpan(Typeface.BOLD), 40, 56, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
termsExplanation.setSpan(object : ClickableSpan() {
|
||||
|
||||
override fun onClick(widget: View) {
|
||||
openURL("https://getsession.org/terms-of-service/")
|
||||
}
|
||||
}, 40, 56, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
termsExplanation.setSpan(StyleSpan(Typeface.BOLD), 61, 75, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
termsExplanation.setSpan(object : ClickableSpan() {
|
||||
|
||||
override fun onClick(widget: View) {
|
||||
openURL("https://getsession.org/privacy-policy/")
|
||||
}
|
||||
}, 61, 75, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
dataBinding.termsTextView.movementMethod = LinkMovementMethod.getInstance()
|
||||
dataBinding.termsTextView.text = termsExplanation
|
||||
//endregion
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
|
||||
when (requestCode) {
|
||||
REQUEST_CODE_BACKUP_FILE -> {
|
||||
if (resultCode == Activity.RESULT_OK && data != null && data.data != null) {
|
||||
// // Acquire persistent access permissions for the file selected.
|
||||
// val persistentFlags: Int = data.flags and
|
||||
// (Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
|
||||
// context.contentResolver.takePersistableUriPermission(data.data!!, persistentFlags)
|
||||
|
||||
viewModel.onBackupFileSelected(data.data!!)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region Interaction
|
||||
private fun restore() {
|
||||
if (viewModel.backupFile.value == null && Strings.isEmptyOrWhitespace(viewModel.backupPassphrase.value)) return
|
||||
|
||||
// val backupFile = viewModel.backupFile.value!!
|
||||
// val password = viewModel.backupPassphrase.value!!.trim()
|
||||
//
|
||||
// try {
|
||||
// FullBackupImporter.importFile(
|
||||
// this,
|
||||
// AttachmentSecretProvider.getInstance(this).getOrCreateAttachmentSecret(),
|
||||
// DatabaseFactory.getBackupDatabase(this),
|
||||
// backupFile,
|
||||
// password
|
||||
// )
|
||||
// } catch (e: IOException) {
|
||||
// Log.e(TAG, "Failed to restore from the backup file \"$backupFile\"", e)
|
||||
// }
|
||||
|
||||
val backupFile = viewModel.backupFile.value!!
|
||||
val passphrase = viewModel.backupPassphrase.value!!.trim()
|
||||
|
||||
object : AsyncTask<Void?, Void?, BackupImportResult>() {
|
||||
override fun doInBackground(vararg params: Void?): BackupImportResult {
|
||||
return try {
|
||||
val context: Context = this@BackupRestoreActivity
|
||||
val database = DatabaseFactory.getBackupDatabase(context)
|
||||
FullBackupImporter.importFile(
|
||||
context,
|
||||
AttachmentSecretProvider.getInstance(context).getOrCreateAttachmentSecret(),
|
||||
DatabaseFactory.getBackupDatabase(context),
|
||||
backupFile,
|
||||
passphrase
|
||||
)
|
||||
DatabaseFactory.upgradeRestored(context, database)
|
||||
NotificationChannels.restoreContactNotificationChannels(context)
|
||||
TextSecurePreferences.setBackupEnabled(context, true)
|
||||
TextSecurePreferences.setBackupPassphrase(context, passphrase)
|
||||
BackupImportResult.SUCCESS
|
||||
} catch (e: DatabaseDowngradeException) {
|
||||
Log.w(TAG, "Failed due to the backup being from a newer version of Signal.", e)
|
||||
BackupImportResult.FAILURE_VERSION_DOWNGRADE
|
||||
} catch (e: IOException) {
|
||||
Log.w(TAG, e)
|
||||
BackupImportResult.FAILURE_UNKNOWN
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPostExecute(result: BackupImportResult) {
|
||||
val context = this@BackupRestoreActivity
|
||||
when (result) {
|
||||
BackupImportResult.SUCCESS -> {
|
||||
TextSecurePreferences.setHasSeenWelcomeScreen(context, true)
|
||||
TextSecurePreferences.setPromptedPushRegistration(context, true)
|
||||
TextSecurePreferences.setIsUsingFCM(context, true)
|
||||
TextSecurePreferences.setHasSeenMultiDeviceRemovalSheet(context)
|
||||
TextSecurePreferences.setHasSeenLightThemeIntroSheet(context)
|
||||
val application = ApplicationContext.getInstance(context)
|
||||
application.setUpStorageAPIIfNeeded()
|
||||
application.setUpP2PAPIIfNeeded()
|
||||
|
||||
val intent = Intent(context, HomeActivity::class.java)
|
||||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||
show(intent)
|
||||
}
|
||||
BackupImportResult.FAILURE_VERSION_DOWNGRADE ->
|
||||
Toast.makeText(context, R.string.RegistrationActivity_backup_failure_downgrade, Toast.LENGTH_LONG).show()
|
||||
BackupImportResult.FAILURE_UNKNOWN ->
|
||||
Toast.makeText(context, R.string.RegistrationActivity_incorrect_backup_passphrase, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
}
|
||||
}.execute()
|
||||
}
|
||||
|
||||
private fun openURL(url: String) {
|
||||
try {
|
||||
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
|
||||
startActivity(intent)
|
||||
} catch (e: Exception) {
|
||||
Toast.makeText(this, R.string.invalid_url, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
enum class BackupImportResult {
|
||||
SUCCESS, FAILURE_VERSION_DOWNGRADE, FAILURE_UNKNOWN
|
||||
}
|
||||
// endregion
|
||||
}
|
||||
|
||||
class RestoreBackupViewModel(application: Application): AndroidViewModel(application) {
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun uriToFileName(view: View, fileUri: Uri?): String? {
|
||||
fileUri ?: return null
|
||||
|
||||
view.context.contentResolver.query(fileUri, null, null, null, null).use {
|
||||
val nameIndex = it!!.getColumnIndex(OpenableColumns.DISPLAY_NAME)
|
||||
it.moveToFirst()
|
||||
return it.getString(nameIndex)
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun validateData(fileUri: Uri?, passphrase: String?): Boolean {
|
||||
return fileUri != null && !Strings.isEmptyOrWhitespace(passphrase)
|
||||
}
|
||||
}
|
||||
|
||||
val backupFile = MutableLiveData<Uri>()
|
||||
val backupPassphrase = MutableLiveData<String>()
|
||||
|
||||
fun onBackupFileSelected(backupFile: Uri) {
|
||||
//TODO Check if backup file is correct.
|
||||
this.backupFile.value = backupFile
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue