Skip to content

Commit

Permalink
feat: network warning dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
rushiiMachine committed Feb 7, 2024
1 parent 15464fa commit c28009c
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.aliucord.manager.ui.components.dialogs

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Warning
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import com.aliucord.manager.R

@Composable
fun NetworkWarningDialog(
onConfirm: () -> Unit,
onDismiss: () -> Unit,
) {
AlertDialog(
onDismissRequest = onDismiss,
confirmButton = {
FilledTonalButton(
onClick = onConfirm,
colors = ButtonDefaults.filledTonalButtonColors(
containerColor = MaterialTheme.colorScheme.error,
contentColor = MaterialTheme.colorScheme.onError,
),
) {
Text(stringResource(R.string.action_confirm))
}
},
dismissButton = {
TextButton(
onClick = onDismiss,
colors = ButtonDefaults.textButtonColors(
contentColor = MaterialTheme.colorScheme.onErrorContainer
),
) {
Text(stringResource(R.string.action_cancel))
}
},
title = { Text(stringResource(R.string.network_warning_title)) },
text = { Text(stringResource(R.string.network_warning_body)) },
icon = { Icon(Icons.Filled.Warning, contentDescription = null) },
containerColor = MaterialTheme.colorScheme.errorContainer,
iconContentColor = MaterialTheme.colorScheme.onErrorContainer,
titleContentColor = MaterialTheme.colorScheme.onErrorContainer,
textContentColor = MaterialTheme.colorScheme.onErrorContainer,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package com.aliucord.manager.ui.screens.home
import android.app.Application
import android.content.Intent
import android.content.pm.PackageManager
import android.net.ConnectivityManager
import android.provider.Settings
import android.telephony.TelephonyManager
import android.util.Log
import android.widget.Toast
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.core.content.getSystemService
import androidx.core.graphics.drawable.toBitmap
import androidx.core.net.toUri
import cafe.adriel.voyager.core.model.ScreenModel
Expand Down Expand Up @@ -55,6 +59,41 @@ class HomeModel(
application.startActivity(launchIntent)
}

fun showMultiInstallToast() {
Toast.makeText(application, "Multi-install support is coming soon!", Toast.LENGTH_SHORT).show()
}

/**
* Check whether the device is connected on a metered WIFI connection or through any type of mobile data,
* to avoid unknowingly downloading a lot of stuff through a potentially metered network.
*/
@Suppress("DEPRECATION")
fun isNetworkDangerous(): Boolean {
val connectivity = application.getSystemService<ConnectivityManager>()
?: error("Unable to get system connectivity service")

if (connectivity.isActiveNetworkMetered) return true

when (val info = connectivity.activeNetworkInfo) {
null -> return false
else -> {
if (info.isRoaming) return true
if (info.type == ConnectivityManager.TYPE_WIFI) return false
}
}

val telephony = application.getSystemService<TelephonyManager>()
?: error("Unable to get system telephony service")

val dangerousMobileDataStates = arrayOf(
/* TelephonyManager.DATA_DISCONNECTING */ 4,
TelephonyManager.DATA_CONNECTED,
TelephonyManager.DATA_CONNECTING,
)

return dangerousMobileDataStates.contains(telephony.dataState)
}

private fun fetchInstallations() = screenModelScope.launchBlock {
try {
val packageManager = application.packageManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import cafe.adriel.voyager.navigator.currentOrThrow
import com.aliucord.manager.R
import com.aliucord.manager.ui.components.ProjectHeader
import com.aliucord.manager.ui.components.dialogs.InstallerDialog
import com.aliucord.manager.ui.components.dialogs.NetworkWarningDialog
import com.aliucord.manager.ui.screens.home.components.*
import com.aliucord.manager.ui.screens.install.InstallScreen
import com.aliucord.manager.ui.screens.plugins.PluginsScreen
Expand All @@ -42,7 +43,33 @@ class HomeScreen : Screen {
val navigator = LocalNavigator.currentOrThrow
val model = getScreenModel<HomeModel>()

var showNetworkWarningDialog by remember { mutableStateOf(false) }
var showInstallerDialog by remember { mutableStateOf(false) }

val onClickInstall: () -> Unit = remember {
{
if ((model.installations as? InstallsState.Fetched)?.data?.isNotEmpty() == false) {
model.showMultiInstallToast()
} else if (model.isNetworkDangerous()) {
showNetworkWarningDialog = true
} else {
showInstallerDialog = true
}
}
}

if (showNetworkWarningDialog) {
NetworkWarningDialog(
onConfirm = {
showNetworkWarningDialog = false
showInstallerDialog = true
},
onDismiss = {
showNetworkWarningDialog = false
},
)
}

if (showInstallerDialog) {
InstallerDialog(
onDismiss = { showInstallerDialog = false },
Expand Down Expand Up @@ -72,9 +99,7 @@ class HomeScreen : Screen {

item(key = "ADD_INSTALL_BUTTON") {
InstallButton(
// TODO: install options screen to configure pkg name
enabled = (model.installations as? InstallsState.Fetched)?.data?.isEmpty() ?: false,
onClick = { showInstallerDialog = true },
onClick = onClickInstall,
modifier = Modifier
.fillMaxWidth()
.padding(top = 10.dp)
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
<string name="contributors_commit_title">§name§%1$s§ §middle§authored§ §commit§%2$s§</string>

<string name="network_load_fail">Failed to load</string>
<string name="network_warning_title">Metered network</string>
<string name="network_warning_body">It appears you are connected to mobile data or a potentially metered network. In order to continue with the installation, several hundred megabytes could be downloaded. Are you sure you want to continue?</string>

<string name="label_pkg_name">Package:</string>
<string name="label_version_discord">Discord version:</string>
Expand Down

0 comments on commit c28009c

Please sign in to comment.