Skip to content

Commit

Permalink
Root install
Browse files Browse the repository at this point in the history
  • Loading branch information
rumboalla committed Aug 3, 2023
1 parent b5d56c7 commit 1e68029
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The 3.x branch is a full rewrite using modern technologies like **Jetpack Compos
* Supports **Android TV**.
* **Material Design 3** with **Dark**, **Light** and **Dynamic** theme support.
* **Direct install** of updates for sources that support it.
* **Root install** of updates. TODO
* **Root install** of updates.
* **Languages**: English, Spanish, Dutch, German.

# Download
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ dependencies {
implementation 'com.github.rumboalla.KryptoPrefs:kryptoprefs:0.4.3'
implementation 'com.github.rumboalla.KryptoPrefs:kryptoprefs-gson:0.4.3'
implementation 'org.jsoup:jsoup:1.16.1'
implementation 'eu.chainfire:libsuperuser:1.1.1'

testImplementation 'junit:junit:4.13.2'

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/apkupdater/di/MainModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ val mainModule = module {

viewModel { MainViewModel() }

viewModel { parameters -> UpdatesViewModel(parameters.get(), get(), get(), get()) }
viewModel { parameters -> UpdatesViewModel(parameters.get(), get(), get(), get(), get()) }

viewModel { SettingsViewModel(get(), get(), WorkManager.getInstance(get())) }

viewModel { parameters -> SearchViewModel(parameters.get(), get(), get(), get()) }
viewModel { parameters -> SearchViewModel(parameters.get(), get(), get(), get(), get()) }

}
1 change: 1 addition & 0 deletions app/src/main/java/com/apkupdater/prefs/Prefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ class Prefs(
val alarmHour = int("alarmHour", defValue = 12, backed = true)
val alarmFrequency = int("alarmFrequency", 0, backed = true)
val androidTvUi = boolean("androidTvUi", defValue = isAndroidTv, backed = true)
val rootInstall = boolean("rootInstall", defValue = false, backed = true)
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/apkupdater/ui/component/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ fun SwitchSetting(
Switch(
checked = value,
onCheckedChange = {
value = it
setValue(it)
value = getValue()
},
modifier = Modifier.align(CenterEnd)
)
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/apkupdater/ui/screen/SettingsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ fun SettingsScreen(viewModel: SettingsViewModel = koinViewModel()) = Column {

item {
LargeTitle(stringResource(R.string.settings_options), Modifier.padding(start = 16.dp, top = 16.dp))
SwitchSetting(
{ viewModel.getRootInstall() },
{ viewModel.setRootInstall(it) },
stringResource(R.string.root_install),
R.drawable.ic_beta
)
SwitchSetting(
{ viewModel.getIgnoreAlpha() },
{ viewModel.setIgnoreAlpha(it) },
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/com/apkupdater/util/SessionInstaller.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import androidx.core.content.ContextCompat.startActivity
import com.apkupdater.BuildConfig
import com.apkupdater.data.ui.AppUpdate
import com.apkupdater.ui.activity.MainActivity
import eu.chainfire.libsuperuser.Shell
import java.io.File
import java.io.InputStream
import java.util.UUID
import java.util.concurrent.atomic.AtomicBoolean


Expand Down Expand Up @@ -58,6 +59,12 @@ class SessionInstaller(private val context: Context) {
session.close()
}

fun rootInstall(file: File): Boolean {
val res = Shell.Pool.SU.run("pm install -r ${file.absolutePath}") == 0
file.delete()
return res
}

fun finish() = installMutex.unlock()

fun checkPermission(): Boolean {
Expand Down
23 changes: 21 additions & 2 deletions app/src/main/java/com/apkupdater/viewmodel/SearchViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.apkupdater.data.ui.ApkMirrorSource
import com.apkupdater.data.ui.AppUpdate
import com.apkupdater.data.ui.SearchUiState
import com.apkupdater.data.ui.indexOf
import com.apkupdater.prefs.Prefs
import com.apkupdater.repository.SearchRepository
import com.apkupdater.util.Downloader
import com.apkupdater.util.SessionInstaller
Expand All @@ -22,7 +23,8 @@ class SearchViewModel(
private val mainViewModel: MainViewModel,
private val searchRepository: SearchRepository,
private val downloader: Downloader,
private val installer: SessionInstaller
private val installer: SessionInstaller,
private val prefs: Prefs
) : ViewModel() {

private val mutex = Mutex()
Expand Down Expand Up @@ -55,7 +57,13 @@ class SearchViewModel(
fun install(update: AppUpdate, uriHandler: UriHandler) {
when (update.source) {
ApkMirrorSource -> uriHandler.openUri(update.link)
else -> downloadAndInstall(update)
else -> {
if (prefs.rootInstall.get()) {
downloadAndRootInstall(update)
} else {
downloadAndInstall(update)
}
}
}
}

Expand All @@ -82,6 +90,17 @@ class SearchViewModel(
}
}

private fun downloadAndRootInstall(update: AppUpdate) = viewModelScope.launch(Dispatchers.IO) {
state.value = SearchUiState.Success(setIsInstalling(update.id, true))
val file = downloader.download(update.link)
val res = installer.rootInstall(file)
if (res) {
finishInstall(update.id)
} else {
cancelInstall(update.id)
}
}

private fun downloadAndInstall(update: AppUpdate) = viewModelScope.launch(Dispatchers.IO) {
if(installer.checkPermission()) {
state.value = SearchUiState.Success(setIsInstalling(update.id, true))
Expand Down
12 changes: 11 additions & 1 deletion app/src/main/java/com/apkupdater/viewmodel/SettingsViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import androidx.work.WorkManager
import com.apkupdater.prefs.Prefs
import com.apkupdater.util.UpdatesNotification
import com.apkupdater.worker.UpdatesWorker
import eu.chainfire.libsuperuser.Shell


class SettingsViewModel(
private val prefs: Prefs,
Expand All @@ -32,9 +34,18 @@ class SettingsViewModel(
fun getAndroidTvUi() = prefs.androidTvUi.get()
fun setAndroidTvUi(b: Boolean) = prefs.androidTvUi.put(b)
fun getEnableAlarm() = prefs.enableAlarm.get()
fun getRootInstall() = prefs.rootInstall.get()
fun getAlarmHour() = prefs.alarmHour.get()
fun getAlarmFrequency() = prefs.alarmFrequency.get()

fun setRootInstall(b: Boolean) {
if (b && Shell.SU.available()) {
prefs.rootInstall.put(true)
} else {
prefs.rootInstall.put(false)
}
}

fun setAlarmFrequency(frequency: Int) {
prefs.alarmFrequency.put(frequency)
if (getEnableAlarm()) UpdatesWorker.launch(workManager) else UpdatesWorker.cancel(workManager)
Expand All @@ -44,7 +55,6 @@ class SettingsViewModel(
prefs.enableAlarm.put(b)
if (b) {
notification.checkNotificationPermission(launcher)
notification.showUpdateNotification(11)
UpdatesWorker.launch(workManager)
} else {
UpdatesWorker.cancel(workManager)
Expand Down
25 changes: 22 additions & 3 deletions app/src/main/java/com/apkupdater/viewmodel/UpdatesViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import androidx.lifecycle.viewModelScope
import com.apkupdater.data.ui.ApkMirrorSource
import com.apkupdater.data.ui.AppUpdate
import com.apkupdater.data.ui.UpdatesUiState
import com.apkupdater.data.ui.indexOf
import com.apkupdater.prefs.Prefs
import com.apkupdater.repository.UpdatesRepository
import com.apkupdater.util.Downloader
import com.apkupdater.util.SessionInstaller
Expand All @@ -15,14 +17,14 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import com.apkupdater.data.ui.indexOf


class UpdatesViewModel(
private val mainViewModel: MainViewModel,
private val updatesRepository: UpdatesRepository,
private val downloader: Downloader,
private val installer: SessionInstaller
private val installer: SessionInstaller,
private val prefs: Prefs
) : ViewModel() {

private val mutex = Mutex()
Expand All @@ -44,7 +46,13 @@ class UpdatesViewModel(
fun install(update: AppUpdate, uriHandler: UriHandler) {
when (update.source) {
ApkMirrorSource -> uriHandler.openUri(update.link)
else -> downloadAndInstall(update)
else -> {
if (prefs.rootInstall.get()) {
downloadAndRootInstall(update)
} else {
downloadAndInstall(update)
}
}
}
}

Expand All @@ -71,6 +79,17 @@ class UpdatesViewModel(
}
}

private fun downloadAndRootInstall(update: AppUpdate) = viewModelScope.launch(Dispatchers.IO) {
state.value = UpdatesUiState.Success(setIsInstalling(update.id, true))
val file = downloader.download(update.link)
val res = installer.rootInstall(file)
if (res) {
finishInstall(update.id)
} else {
cancelInstall(update.id)
}
}

private fun downloadAndInstall(update: AppUpdate) = viewModelScope.launch(Dispatchers.IO) {
if(installer.checkPermission()) {
state.value = UpdatesUiState.Success(setIsInstalling(update.id, true))
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<string name="settings_android_tv_ui" translatable="false">Android TV UI</string>
<string name="ignore_alpha">Ignore Alpha</string>
<string name="ignore_beta">Ignore Beta</string>
<string name="root_install">Root Install</string>
<string name="source_apkmirror" translatable="false">ApkMirror</string>
<string name="source_fdroid" translatable="false">F-Droid</string>
<string name="source_aptoide" translatable="false">Aptoide</string>
Expand Down

0 comments on commit 1e68029

Please sign in to comment.