Remove redundent classes

This commit is contained in:
topjohnwu 2019-05-02 02:42:00 -04:00
parent 2c9586d811
commit ff20267b3f
3 changed files with 0 additions and 283 deletions

View File

@ -1,6 +1,5 @@
package com.topjohnwu.magisk.di
import android.content.Intent
import android.net.Uri
import com.topjohnwu.magisk.ui.MainViewModel
import com.topjohnwu.magisk.ui.flash.FlashViewModel
@ -10,7 +9,6 @@ import com.topjohnwu.magisk.ui.log.LogViewModel
import com.topjohnwu.magisk.ui.module.ModuleViewModel
import com.topjohnwu.magisk.ui.superuser.SuperuserViewModel
import com.topjohnwu.magisk.ui.surequest.SuRequestViewModel
import com.topjohnwu.magisk.ui.surequest._SuRequestViewModel
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module
@ -23,8 +21,5 @@ val viewModelModules = module {
viewModel { ModuleViewModel(get(), get()) }
viewModel { LogViewModel(get(), get()) }
viewModel { (action: String, uri: Uri?) -> FlashViewModel(action, uri, get()) }
viewModel { (intent: Intent, action: String?) ->
_SuRequestViewModel(intent, action.orEmpty(), get(), get())
}
viewModel { SuRequestViewModel(get(), get(), get(SUTimeout), get()) }
}

View File

@ -1,166 +0,0 @@
package com.topjohnwu.magisk.ui.surequest
import android.hardware.fingerprint.FingerprintManager
import android.os.CountDownTimer
import android.text.SpannableStringBuilder
import android.widget.Toast
import androidx.core.text.bold
import com.skoumal.teanity.viewevents.ViewEvent
import com.topjohnwu.magisk.Config
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.databinding.ActivitySuRequestBinding
import com.topjohnwu.magisk.model.entity.Policy
import com.topjohnwu.magisk.model.events.DieEvent
import com.topjohnwu.magisk.model.events.SuDialogEvent
import com.topjohnwu.magisk.ui.base.MagiskActivity
import com.topjohnwu.magisk.utils.FingerprintHelper
import com.topjohnwu.magisk.utils.feature.WIP
import com.topjohnwu.magisk.view.MagiskDialog
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.koin.core.parameter.parametersOf
import timber.log.Timber
import java.util.concurrent.TimeUnit.MILLISECONDS
import java.util.concurrent.TimeUnit.SECONDS
@WIP
open class _SuRequestActivity : MagiskActivity<_SuRequestViewModel, ActivitySuRequestBinding>() {
override val layoutRes: Int = R.layout.activity_su_request
override val viewModel: _SuRequestViewModel by viewModel {
parametersOf(intent, intent.action)
}
//private val timeoutPrefs: SharedPreferences by inject(SUTimeout)
private val canUseFingerprint get() = FingerprintHelper.useFingerprint()
private val countdown by lazy {
val seconds = Config.get<Int>(Config.Key.SU_REQUEST_TIMEOUT).toLong()
val millis = SECONDS.toMillis(seconds)
object : CountDownTimer(millis, 1000) {
override fun onFinish() {
viewModel.deny()
}
override fun onTick(millisUntilFinished: Long) {
dialog.applyButton(MagiskDialog.ButtonType.NEGATIVE) {
Timber.e("Tick, tock")
title = "%s (%d)".format(
getString(R.string.deny),
MILLISECONDS.toSeconds(millisUntilFinished)
)
}
}
}
}
private var fingerprintHelper: SuFingerprint? = null
private lateinit var dialog: MagiskDialog
override fun onEventDispatched(event: ViewEvent) {
super.onEventDispatched(event)
when (event) {
is SuDialogEvent -> showDialog(event.policy)
is DieEvent -> finish()
}
}
override fun onBackPressed() {
if (::dialog.isInitialized && dialog.isShowing) {
return
}
super.onBackPressed()
}
override fun onDestroy() {
if (this::dialog.isInitialized && dialog.isShowing) {
dialog.dismiss()
}
fingerprintHelper?.cancel()
countdown.cancel()
super.onDestroy()
}
private fun showDialog(policy: Policy) {
val titleText = SpannableStringBuilder("Allow ")
.bold { append(policy.appName) }
.append(" to access superuser rights?")
val messageText = StringBuilder()
.appendln(policy.packageName)
.append(getString(R.string.su_warning))
dialog = MagiskDialog(this)
.applyIcon(policy.info.loadIcon(packageManager))
.applyTitle(titleText)
.applyMessage(messageText)
//.applyView()) {} //todo add a spinner
.cancellable(false)
.applyButton(MagiskDialog.ButtonType.POSITIVE) {
titleRes = R.string.grant
onClick { viewModel.grant() }
if (canUseFingerprint) {
icon = R.drawable.ic_fingerprint
}
}
.applyButton(MagiskDialog.ButtonType.NEUTRAL) {
title = "%s %s".format(getString(R.string.grant), getString(R.string.once))
onClick { viewModel.grant(-1) }
}
.applyButton(MagiskDialog.ButtonType.NEGATIVE) {
titleRes = R.string.deny
onClick { viewModel.deny() }
}
.onDismiss { finish() }
.onShow {
startTimer().also { Timber.e("Starting timer") }
if (canUseFingerprint) {
startFingerprintQuery()
}
}
.reveal()
}
private fun startTimer() {
countdown.start()
}
private fun startFingerprintQuery() {
val result = runCatching {
fingerprintHelper = SuFingerprint().apply { authenticate() }
}
if (result.isFailure) {
dialog.applyButton(MagiskDialog.ButtonType.POSITIVE) {
icon = 0
}
}
}
private inner class SuFingerprint @Throws(Exception::class)
internal constructor() : FingerprintHelper() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
Toast.makeText(this@_SuRequestActivity, errString, Toast.LENGTH_LONG).show()
}
override fun onAuthenticationHelp(helpCode: Int, helpString: CharSequence) {
Toast.makeText(this@_SuRequestActivity, helpString, Toast.LENGTH_LONG).show()
}
override fun onAuthenticationSucceeded(result: FingerprintManager.AuthenticationResult) {
viewModel.grant()
}
override fun onAuthenticationFailed() {
Toast.makeText(this@_SuRequestActivity, R.string.auth_fail, Toast.LENGTH_LONG).show()
}
}
companion object {
const val REQUEST = "request"
const val LOG = "log"
const val NOTIFY = "notify"
}
}

View File

@ -1,112 +0,0 @@
package com.topjohnwu.magisk.ui.surequest
import android.content.Intent
import android.content.pm.PackageManager
import com.skoumal.teanity.extensions.subscribeK
import com.topjohnwu.magisk.BuildConfig
import com.topjohnwu.magisk.Config
import com.topjohnwu.magisk.data.database.MagiskDB
import com.topjohnwu.magisk.model.entity.Policy
import com.topjohnwu.magisk.model.events.DieEvent
import com.topjohnwu.magisk.model.events.SuDialogEvent
import com.topjohnwu.magisk.ui.base.MagiskViewModel
import com.topjohnwu.magisk.utils.SuConnector
import com.topjohnwu.magisk.utils.SuLogger
import com.topjohnwu.magisk.utils.feature.WIP
import com.topjohnwu.magisk.utils.now
import io.reactivex.Single
import timber.log.Timber
import java.util.concurrent.TimeUnit.MILLISECONDS
import java.util.concurrent.TimeUnit.MINUTES
@WIP
class _SuRequestViewModel(
intent: Intent,
action: String,
private val packageManager: PackageManager,
private val database: MagiskDB
) : MagiskViewModel() {
private val connector: Single<SuConnector> = Single.fromCallable {
val socketName = intent.extras?.getString("socket") ?: let {
deny()
throw IllegalStateException("Socket is empty or null")
}
object : SuConnector(socketName) {
override fun onResponse() {
policy.subscribeK { out.writeInt(it.policy) } //this just might be incorrect, lol
}
} as SuConnector
}.cache()
private val policy: Single<Policy> = connector.map {
val bundle = it.readSocketInput() ?: throw IllegalStateException("Socket bundle is null")
val uid = bundle.getString("uid")?.toIntOrNull() ?: let {
deny()
throw IllegalStateException("UID is empty or null")
}
database.clearOutdated()
database.getPolicy(uid) ?: Policy(uid, packageManager)
}.cache()
init {
when (action) {
SuRequestActivity.LOG -> SuLogger.handleLogs(intent).also { die() }
SuRequestActivity.NOTIFY -> SuLogger.handleNotify(intent).also { die() }
SuRequestActivity.REQUEST -> process()
else -> back() // invalid action, should ignore
}
}
private fun process() {
policy.subscribeK(onError = ::deny) { process(it) }
}
private fun process(policy: Policy) {
if (policy.packageName == BuildConfig.APPLICATION_ID)
deny().also { return }
if (policy.policy != Policy.INTERACTIVE)
grant().also { return }
when (Config.get<Int>(Config.Key.SU_AUTO_RESPONSE)) {
Config.Value.SU_AUTO_DENY -> deny().also { return }
Config.Value.SU_AUTO_ALLOW -> grant().also { return }
}
requestDialog(policy)
}
fun deny(e: Throwable? = null) = updatePolicy(Policy.DENY, 0).also { Timber.e(e) }
fun grant(time: Long = 0) = updatePolicy(Policy.ALLOW, time)
private fun updatePolicy(action: Int, time: Long) {
fun finish(e: Throwable? = null) = die().also { Timber.e(e) }
policy
.map { it.policy = action; it }
.doOnSuccess {
if (time >= 0) {
it.until = if (time == 0L) {
0
} else {
MILLISECONDS.toSeconds(now) + MINUTES.toSeconds(time)
}
database.updatePolicy(it)
}
}
.flatMap { connector }
.subscribeK(onError = ::finish) {
it.response()
finish()
}
}
private fun requestDialog(policy: Policy) {
SuDialogEvent(policy).publish()
}
private fun die() = DieEvent().publish()
}