parent
e4aa792542
commit
b0d0a00f15
@ -0,0 +1,99 @@ |
|||||||
|
package net.pokeranalytics.android.ui.activity |
||||||
|
|
||||||
|
import android.Manifest |
||||||
|
import android.content.Context |
||||||
|
import android.content.Intent |
||||||
|
import android.net.Uri |
||||||
|
import android.os.Bundle |
||||||
|
import androidx.fragment.app.FragmentActivity |
||||||
|
import io.realm.Realm |
||||||
|
import net.pokeranalytics.android.R |
||||||
|
import net.pokeranalytics.android.exceptions.PAIllegalStateException |
||||||
|
import net.pokeranalytics.android.ui.activity.components.BaseActivity |
||||||
|
import net.pokeranalytics.android.ui.activity.components.RequestCode |
||||||
|
import net.pokeranalytics.android.ui.activity.components.ResultCode |
||||||
|
import net.pokeranalytics.android.ui.extensions.showAlertDialog |
||||||
|
import net.pokeranalytics.android.ui.extensions.toast |
||||||
|
import net.pokeranalytics.android.util.copyStreamToFile |
||||||
|
import timber.log.Timber |
||||||
|
import java.io.File |
||||||
|
|
||||||
|
class DatabaseCopyActivity : BaseActivity() { |
||||||
|
|
||||||
|
private lateinit var fileURI: Uri |
||||||
|
|
||||||
|
enum class IntentKey(val keyName: String) { |
||||||
|
URI("uri") |
||||||
|
} |
||||||
|
|
||||||
|
companion object { |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a new instance for result |
||||||
|
*/ |
||||||
|
fun newInstanceForResult(context: FragmentActivity, uri: Uri) { |
||||||
|
context.startActivityForResult(getIntent(context, uri), RequestCode.IMPORT.value) |
||||||
|
} |
||||||
|
|
||||||
|
private fun getIntent(context: Context, uri: Uri): Intent { |
||||||
|
Timber.d("getIntent") |
||||||
|
val intent = Intent(context, DatabaseCopyActivity::class.java) |
||||||
|
intent.putExtra(IntentKey.URI.keyName, uri) |
||||||
|
return intent |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) { |
||||||
|
super.onCreate(savedInstanceState) |
||||||
|
|
||||||
|
Timber.d("onCreate") |
||||||
|
|
||||||
|
intent?.data?.let { |
||||||
|
this.fileURI = it |
||||||
|
} ?: run { |
||||||
|
this.fileURI = intent.getParcelableExtra(IntentKey.URI.keyName) ?: throw PAIllegalStateException("Uri not found") |
||||||
|
} |
||||||
|
|
||||||
|
// setContentView(R.layout.activity_import) |
||||||
|
requestImportConfirmation() |
||||||
|
} |
||||||
|
|
||||||
|
private fun initUI() { |
||||||
|
|
||||||
|
askForPermission(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), RequestCode.PERMISSION_WRITE_EXTERNAL_STORAGE.value) { |
||||||
|
val path = Realm.getDefaultInstance().path |
||||||
|
contentResolver.openInputStream(fileURI)?.let { inputStream -> |
||||||
|
val destination = File(path) |
||||||
|
inputStream.copyStreamToFile(destination) |
||||||
|
toast("Please restart app") |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { |
||||||
|
super.onActivityResult(requestCode, resultCode, data) |
||||||
|
|
||||||
|
when (requestCode) { |
||||||
|
RequestCode.IMPORT.value -> { |
||||||
|
if (resultCode == ResultCode.IMPORT_UNRECOGNIZED_FORMAT.value) { |
||||||
|
showAlertDialog(context = this, messageResId = R.string.unknown_import_format_popup_message, positiveAction = { |
||||||
|
finish() |
||||||
|
}) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Import |
||||||
|
|
||||||
|
private fun requestImportConfirmation() { |
||||||
|
|
||||||
|
showAlertDialog(context = this, title = R.string.import_confirmation, showCancelButton = true, positiveAction = { |
||||||
|
initUI() |
||||||
|
}, negativeAction = { |
||||||
|
finish() |
||||||
|
}) |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue