diff options
Diffstat (limited to 'wallet/src/main/java/net/taler')
-rw-r--r-- | wallet/src/main/java/net/taler/wallet/MainActivity.kt | 6 | ||||
-rw-r--r-- | wallet/src/main/java/net/taler/wallet/Utils.kt | 63 |
2 files changed, 69 insertions, 0 deletions
diff --git a/wallet/src/main/java/net/taler/wallet/MainActivity.kt b/wallet/src/main/java/net/taler/wallet/MainActivity.kt index 2eeb4fd..786e40e 100644 --- a/wallet/src/main/java/net/taler/wallet/MainActivity.kt +++ b/wallet/src/main/java/net/taler/wallet/MainActivity.kt @@ -22,6 +22,7 @@ import android.content.Context import android.content.Intent import android.content.Intent.ACTION_VIEW import android.content.IntentFilter +import android.net.Uri import android.os.Bundle import android.util.Log import android.view.MenuItem @@ -48,6 +49,7 @@ import com.google.zxing.integration.android.IntentIntegrator import com.google.zxing.integration.android.IntentIntegrator.parseActivityResult import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.app_bar_main.* +import net.taler.common.isOnline import net.taler.wallet.BuildConfig.VERSION_CODE import net.taler.wallet.BuildConfig.VERSION_NAME import net.taler.wallet.HostCardEmulatorService.Companion.HTTP_TUNNEL_RESPONSE @@ -144,6 +146,10 @@ class MainActivity : AppCompatActivity(), OnNavigationItemSelectedListener, } private fun handleTalerUri(url: String, from: String) { + val uri = Uri.parse(url) + if (uri.fragment != null && !isOnline()) { + connectToWifi(this, uri.fragment!!) + } when { url.toLowerCase(ROOT).startsWith("taler://pay/") -> { Log.v(TAG, "navigating!") diff --git a/wallet/src/main/java/net/taler/wallet/Utils.kt b/wallet/src/main/java/net/taler/wallet/Utils.kt index b41a202..e299245 100644 --- a/wallet/src/main/java/net/taler/wallet/Utils.kt +++ b/wallet/src/main/java/net/taler/wallet/Utils.kt @@ -17,6 +17,18 @@ package net.taler.wallet import android.app.Activity +import android.content.Context +import android.net.ConnectivityManager +import android.net.ConnectivityManager.NetworkCallback +import android.net.NetworkCapabilities +import android.net.NetworkRequest +import android.net.wifi.WifiConfiguration +import android.net.wifi.WifiManager +import android.net.wifi.WifiNetworkSpecifier +import android.os.Build.VERSION.SDK_INT +import android.widget.Toast +import android.widget.Toast.LENGTH_LONG +import androidx.annotation.RequiresApi import com.google.zxing.integration.android.IntentIntegrator fun scanQrCode(activity: Activity) { @@ -27,6 +39,57 @@ fun scanQrCode(activity: Activity) { }.initiateScan(listOf(IntentIntegrator.QR_CODE)) } +fun connectToWifi(context: Context, ssid: String) { + if (SDK_INT >= 29) { + connectToWifi29(context, ssid) + } else { + connectToWifiDeprecated(context, ssid) + } +} + +@RequiresApi(29) +private fun connectToWifi29(context: Context, ssid: String) { + val wifiManager = context.getSystemService(WifiManager::class.java) + if (wifiManager?.isWifiEnabled == false) { + // we are not allowed to enable the WiFi anymore, so show at least a hint about it + Toast.makeText(context, R.string.wifi_disabled_error, LENGTH_LONG).show() + } + + val specifier = WifiNetworkSpecifier.Builder() + .setSsid(ssid) + .build() + val request = NetworkRequest.Builder() + .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) + .setNetworkSpecifier(specifier) + .build() + val connectivityManager = context.getSystemService(ConnectivityManager::class.java) + connectivityManager?.requestNetwork(request, NetworkCallback()) +} + +@Suppress("DEPRECATION") +private fun connectToWifiDeprecated(context: Context, ssid: String) { + context.getSystemService(WifiManager::class.java)?.apply { + if (!isWifiEnabled) { + val enabledResult = setWifiEnabled(true) + while (enabledResult && !isWifiEnabled) Thread.sleep(25) + } + val wifiConfig = WifiConfiguration().apply { + SSID = "\"$ssid\"" + allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE) + } + addNetwork(wifiConfig).let { netId -> + if (netId == -1) { + val str = context.getString(R.string.wifi_connect_error, ssid) + Toast.makeText(context, str, LENGTH_LONG).show() + } else { + disconnect() + enableNetwork(netId, true) + reconnect() + } + } + } +} + fun cleanExchange(exchange: String) = exchange.let { if (it.startsWith("https://")) it.substring(8) else it }.trimEnd('/') |