Skip to content

Commit 071198b

Browse files
committed
feat: Delete APK on successful install
Closes #1000 Signed-off-by: LooKeR <iamlooker@proton.me>
1 parent afbbde1 commit 071198b

File tree

8 files changed

+43
-6
lines changed

8 files changed

+43
-6
lines changed

app/src/main/kotlin/com/looker/droidify/datastore/PreferenceSettingsRepository.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ class PreferenceSettingsRepository(
182182
return repoId in data.first().enabledRepoIds
183183
}
184184

185+
override suspend fun setDeleteApkOnInstall(enable: Boolean) =
186+
DELETE_APK_ON_INSTALL.update(enable)
187+
185188
private fun mapSettings(preferences: Preferences): Settings {
186189
val installerType =
187190
InstallerType.valueOf(preferences[INSTALLER_TYPE] ?: InstallerType.Default.name)
@@ -223,6 +226,7 @@ class PreferenceSettingsRepository(
223226
val homeScreenSwiping = preferences[HOME_SCREEN_SWIPING] ?: true
224227
val enabledRepoIds =
225228
preferences[ENABLED_REPO_IDS]?.mapNotNull { it.toIntOrNull() }?.toSet() ?: emptySet()
229+
val deleteApkOnInstall = preferences[DELETE_APK_ON_INSTALL] ?: false
226230

227231
return Settings(
228232
language = language,
@@ -245,6 +249,7 @@ class PreferenceSettingsRepository(
245249
favouriteApps = favouriteApps,
246250
homeScreenSwiping = homeScreenSwiping,
247251
enabledRepoIds = enabledRepoIds,
252+
deleteApkOnInstall = deleteApkOnInstall,
248253
)
249254
}
250255

@@ -270,6 +275,7 @@ class PreferenceSettingsRepository(
270275
val LAST_MODIFIED_DS = longPreferencesKey("key_last_modified_download_stats")
271276
val FAVOURITE_APPS = stringSetPreferencesKey("key_favourite_apps")
272277
val HOME_SCREEN_SWIPING = booleanPreferencesKey("key_home_swiping")
278+
val DELETE_APK_ON_INSTALL = booleanPreferencesKey("key_delete_apk_on_install")
273279
val LEGACY_INSTALLER_COMPONENT_CLASS =
274280
stringPreferencesKey("key_legacy_installer_component_class")
275281
val LEGACY_INSTALLER_COMPONENT_ACTIVITY =
@@ -334,6 +340,7 @@ class PreferenceSettingsRepository(
334340
set(FAVOURITE_APPS, settings.favouriteApps)
335341
set(HOME_SCREEN_SWIPING, settings.homeScreenSwiping)
336342
set(ENABLED_REPO_IDS, settings.enabledRepoIds.map { it.toString() }.toSet())
343+
set(DELETE_APK_ON_INSTALL, settings.deleteApkOnInstall)
337344
return this.toPreferences()
338345
}
339346
}

app/src/main/kotlin/com/looker/droidify/datastore/Settings.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ data class Settings(
4747
val favouriteApps: Set<String> = emptySet(),
4848
val homeScreenSwiping: Boolean = true,
4949
val enabledRepoIds: Set<Int> = emptySet(),
50+
val deleteApkOnInstall: Boolean = false,
5051
)
5152

5253
@OptIn(ExperimentalSerializationApi::class)

app/src/main/kotlin/com/looker/droidify/datastore/SettingsRepository.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ interface SettingsRepository {
7070
fun getEnabledRepoIds(): Flow<Set<Int>>
7171

7272
suspend fun isRepoEnabled(repoId: Int): Boolean
73+
74+
suspend fun setDeleteApkOnInstall(enable: Boolean)
7375
}
7476

7577
inline fun <T> SettingsRepository.get(crossinline block: suspend Settings.() -> T): Flow<T> {

app/src/main/kotlin/com/looker/droidify/installer/InstallManager.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.looker.droidify.installer.model.InstallItem
1515
import com.looker.droidify.installer.model.InstallState
1616
import com.looker.droidify.service.SyncService
1717
import com.looker.droidify.utility.common.Constants
18+
import com.looker.droidify.utility.common.cache.Cache
1819
import com.looker.droidify.utility.common.extension.addAndCompute
1920
import com.looker.droidify.utility.common.extension.filter
2021
import com.looker.droidify.utility.common.extension.notificationManager
@@ -55,6 +56,7 @@ class InstallManager(
5556
private val lock = Mutex()
5657
private val skipSignature = settingsRepository.get { ignoreSignature }
5758
private val installerPreference = settingsRepository.get { installerType }
59+
private val deleteApkPreference = settingsRepository.get { deleteApkOnInstall }
5860
private val notificationManager by lazy { context.notificationManager }
5961

6062
suspend operator fun invoke() = coroutineScope {
@@ -104,6 +106,12 @@ class InstallManager(
104106
)
105107
)
106108
val success = installer.use { it.install(item) }
109+
if (success == InstallState.Installed) {
110+
if (deleteApkPreference.first()) {
111+
val apkFile = Cache.getReleaseFile(context, item.installFileName)
112+
apkFile.delete()
113+
}
114+
}
107115
if (success == InstallState.Installed && SyncService.autoUpdating) {
108116
val updates = Database.ProductAdapter.getUpdates(skipSignature.first())
109117
when {

app/src/main/kotlin/com/looker/droidify/ui/settings/SettingsFragment.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ import com.looker.droidify.utility.common.extension.updateAsMutable
4949
import com.looker.droidify.utility.common.isIgnoreBatteryEnabled
5050
import com.looker.droidify.utility.common.requestBatteryFreedom
5151
import dagger.hilt.android.AndroidEntryPoint
52-
import kotlinx.coroutines.flow.Flow
53-
import kotlinx.coroutines.launch
54-
import java.util.Locale
52+
import java.util.*
5553
import kotlin.time.Duration
5654
import kotlin.time.Duration.Companion.days
5755
import kotlin.time.Duration.Companion.hours
56+
import kotlinx.coroutines.flow.Flow
57+
import kotlinx.coroutines.launch
5858
import com.google.android.material.R as MaterialR
5959

6060
@AndroidEntryPoint
@@ -283,6 +283,11 @@ class SettingsFragment : Fragment() {
283283
onClick = { viewModel.setLegacyInstallerComponentComponent(it) },
284284
)
285285
}
286+
deleteApkOnInstall.connect(
287+
titleText = getString(R.string.delete_apk_on_install),
288+
contentText = getString(R.string.delete_apk_on_install_summary),
289+
setting = viewModel.getInitialSetting { deleteApkOnInstall },
290+
)
286291
incompatibleUpdates.connect(
287292
titleText = getString(R.string.incompatible_versions),
288293
contentText = getString(R.string.incompatible_versions_summary),
@@ -413,6 +418,9 @@ class SettingsFragment : Fragment() {
413418
incompatibleUpdates.checked.setOnCheckedChangeListener { _, checked ->
414419
viewModel.setIncompatibleUpdates(checked)
415420
}
421+
deleteApkOnInstall.checked.setOnCheckedChangeListener { _, checked ->
422+
viewModel.setDeleteApkOnInstall(checked)
423+
}
416424
forceCleanUp.root.setOnClickListener {
417425
viewModel.forceCleanup(it.context)
418426
}

app/src/main/kotlin/com/looker/droidify/ui/settings/SettingsViewModel.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ import com.looker.droidify.installer.installers.isShizukuInstalled
2828
import com.looker.droidify.installer.installers.requestPermissionListener
2929
import com.looker.droidify.work.CleanUpWorker
3030
import dagger.hilt.android.lifecycle.HiltViewModel
31+
import java.util.*
32+
import javax.inject.Inject
33+
import kotlin.time.Duration
3134
import kotlinx.coroutines.flow.Flow
3235
import kotlinx.coroutines.flow.MutableSharedFlow
3336
import kotlinx.coroutines.flow.asSharedFlow
3437
import kotlinx.coroutines.flow.flow
3538
import kotlinx.coroutines.flow.map
3639
import kotlinx.coroutines.launch
37-
import java.util.Locale
38-
import javax.inject.Inject
39-
import kotlin.time.Duration
4040

4141
@HiltViewModel
4242
class SettingsViewModel
@@ -199,6 +199,12 @@ class SettingsViewModel
199199
}
200200
}
201201

202+
fun setDeleteApkOnInstall(enable: Boolean) {
203+
viewModelScope.launch {
204+
settingsRepository.setDeleteApkOnInstall(enable)
205+
}
206+
}
207+
202208
fun exportSettings(file: Uri) {
203209
viewModelScope.launch {
204210
settingsRepository.export(file)

app/src/main/res/layout/settings_page.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@
148148
<include
149149
android:id="@+id/legacy_installer_component"
150150
layout="@layout/enum_type" />
151+
<include
152+
android:id="@+id/delete_apk_on_install"
153+
layout="@layout/switch_type" />
151154

152155
<TextView
153156
android:layout_width="match_parent"

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@
105105
<string name="install_types">Installation</string>
106106
<string name="installer">Installer</string>
107107
<string name="legacyInstallerComponent">Legacy installer component</string>
108+
<string name="delete_apk_on_install">Delete APK on install</string>
109+
<string name="delete_apk_on_install_summary">Automatically delete APK files after successful installation</string>
108110
<string name="unspecified">Unspecified</string>
109111
<string name="insufficient_storage">Insufficient space</string>
110112
<string name="insufficient_storage_DESC">There isn\'t enough free space to install this app</string>

0 commit comments

Comments
 (0)