forked from seedvault-app/seedvault
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
198 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
app/src/main/java/com/stevesoltys/seedvault/ui/check/FileCheckFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 The Calyx Institute | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.stevesoltys.seedvault.ui.check | ||
|
||
import android.os.Bundle | ||
import android.text.format.Formatter | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.Button | ||
import android.widget.ScrollView | ||
import android.widget.TextView | ||
import androidx.fragment.app.Fragment | ||
import com.google.android.material.slider.LabelFormatter | ||
import com.google.android.material.slider.Slider | ||
import com.stevesoltys.seedvault.R | ||
import com.stevesoltys.seedvault.settings.SettingsViewModel | ||
import org.koin.androidx.viewmodel.ext.android.activityViewModel | ||
|
||
class FileCheckFragment : Fragment() { | ||
|
||
private val viewModel: SettingsViewModel by activityViewModel() | ||
private lateinit var sliderLabel: TextView | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle?, | ||
): View { | ||
val v = inflater.inflate(R.layout.fragment_app_check, container, false) as ScrollView | ||
|
||
v.requireViewById<TextView>(R.id.titleView).setText(R.string.settings_file_check_title) | ||
v.requireViewById<TextView>(R.id.descriptionView).setText(R.string.settings_file_check_text) | ||
v.requireViewById<TextView>(R.id.introView).setText(R.string.settings_file_check_text2) | ||
|
||
val slider = v.requireViewById<Slider>(R.id.slider) | ||
sliderLabel = v.requireViewById(R.id.sliderLabel) | ||
|
||
// label not scrolling will be fixed in material-components 1.12.0 (next update) | ||
slider.setLabelFormatter { value -> | ||
viewModel.filesBackupSize.value?.let { | ||
Formatter.formatShortFileSize(context, (it * value / 100).toLong()) | ||
} ?: "${value.toInt()}%" | ||
} | ||
slider.addOnChangeListener { _, value, _ -> | ||
onSliderChanged(value) | ||
} | ||
|
||
viewModel.filesBackupSize.observe(viewLifecycleOwner) { | ||
if (it != null) { | ||
slider.labelBehavior = LabelFormatter.LABEL_VISIBLE | ||
slider.invalidate() | ||
onSliderChanged(slider.value) | ||
} | ||
// we can stop observing as the loaded size won't change again | ||
viewModel.filesBackupSize.removeObservers(viewLifecycleOwner) | ||
} | ||
|
||
v.requireViewById<Button>(R.id.startButton).setOnClickListener { | ||
viewModel.checkFileBackups(slider.value.toInt()) | ||
requireActivity().onBackPressedDispatcher.onBackPressed() | ||
} | ||
return v | ||
} | ||
|
||
override fun onStart() { | ||
super.onStart() | ||
viewModel.loadFileBackupSize() | ||
} | ||
|
||
private fun onSliderChanged(value: Float) { | ||
val size = viewModel.filesBackupSize.value | ||
// when size is unknown, we show warning based on percent | ||
val showWarning = if (size == null) { | ||
value > WARN_PERCENT | ||
} else { | ||
size * value / 100 > WARN_BYTES | ||
} | ||
// only update label visibility when different from before | ||
val newVisibility = if (showWarning) View.VISIBLE else View.GONE | ||
if (sliderLabel.visibility != newVisibility) { | ||
sliderLabel.visibility = newVisibility | ||
} | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
app/src/main/java/com/stevesoltys/seedvault/worker/FileCheckerWorker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 The Calyx Institute | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.stevesoltys.seedvault.worker | ||
|
||
import android.content.Context | ||
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC | ||
import android.util.Log | ||
import androidx.work.BackoffPolicy.EXPONENTIAL | ||
import androidx.work.CoroutineWorker | ||
import androidx.work.Data | ||
import androidx.work.ExistingWorkPolicy.REPLACE | ||
import androidx.work.ForegroundInfo | ||
import androidx.work.OneTimeWorkRequestBuilder | ||
import androidx.work.OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST | ||
import androidx.work.WorkManager | ||
import androidx.work.WorkerParameters | ||
import com.stevesoltys.seedvault.BackupStateManager | ||
import com.stevesoltys.seedvault.ui.notification.BackupNotificationManager | ||
import com.stevesoltys.seedvault.ui.notification.NOTIFICATION_ID_CHECKING | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import kotlinx.coroutines.flow.first | ||
import org.calyxos.backup.storage.api.StorageBackup | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
import java.time.Duration | ||
|
||
class FileCheckerWorker( | ||
appContext: Context, | ||
workerParams: WorkerParameters, | ||
) : CoroutineWorker(appContext, workerParams), KoinComponent { | ||
|
||
companion object { | ||
private val TAG = FileCheckerWorker::class.simpleName | ||
private const val PERCENT = "percent" | ||
internal const val UNIQUE_WORK_NAME = "com.stevesoltys.seedvault.FILE_BACKUP_CHECK" | ||
|
||
fun scheduleNow(context: Context, percent: Int) { | ||
check(percent in 0..100) { "Percent $percent out of bounds." } | ||
val data = Data.Builder().putInt(PERCENT, percent).build() | ||
val workRequest = OneTimeWorkRequestBuilder<FileCheckerWorker>() | ||
.setExpedited(RUN_AS_NON_EXPEDITED_WORK_REQUEST) | ||
.setBackoffCriteria(EXPONENTIAL, Duration.ofSeconds(10)) | ||
.setInputData(data) | ||
.build() | ||
val workManager = WorkManager.getInstance(context) | ||
Log.i(TAG, "Asking to check $percent% of file backups now...") | ||
workManager.enqueueUniqueWork(UNIQUE_WORK_NAME, REPLACE, workRequest) | ||
} | ||
} | ||
|
||
private val log = KotlinLogging.logger {} | ||
private val backupStateManager: BackupStateManager by inject() | ||
private val storageBackup: StorageBackup by inject() | ||
private val nm: BackupNotificationManager by inject() | ||
|
||
override suspend fun doWork(): Result { | ||
log.info { "Start worker $this ($id)" } | ||
if (backupStateManager.isBackupRunning.first()) { | ||
Log.i(TAG, "isBackupRunning was true, so retrying later...") | ||
return Result.retry() | ||
} | ||
// try { | ||
// setForeground(createForegroundInfo()) | ||
// } catch (e: Exception) { | ||
// log.error(e) { "Error while running setForeground: " } | ||
// } | ||
val percent = inputData.getInt(PERCENT, -1) | ||
check(percent in 0..100) { "Percent $percent out of bounds." } | ||
|
||
storageBackup.checkBackups(percent, null) | ||
return Result.success() | ||
} | ||
|
||
// TODO | ||
private fun createForegroundInfo() = ForegroundInfo( | ||
NOTIFICATION_ID_CHECKING, | ||
nm.getCheckNotification().build(), | ||
FOREGROUND_SERVICE_TYPE_DATA_SYNC, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters