diff options
author | Torsten Grote <t@grobox.de> | 2020-07-02 16:09:07 -0300 |
---|---|---|
committer | Torsten Grote <t@grobox.de> | 2020-07-02 16:09:07 -0300 |
commit | 9400d765f07f57adb775518c5b66d8001893a7c4 (patch) | |
tree | 83aeea29a62c374fa15619702ce9d4ab78b2a90f /wallet | |
parent | 2b6cfa75123e70ee90aed744d21240da6c944555 (diff) | |
download | taler-android-9400d765f07f57adb775518c5b66d8001893a7c4.tar.gz taler-android-9400d765f07f57adb775518c5b66d8001893a7c4.tar.bz2 taler-android-9400d765f07f57adb775518c5b66d8001893a7c4.zip |
[wallet] Try to connect to open Wi-Fi
if included in fragment of scanned/opened taler:// URI
Diffstat (limited to 'wallet')
-rw-r--r-- | wallet/src/main/AndroidManifest.xml | 3 | ||||
-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 | ||||
-rw-r--r-- | wallet/src/main/res/values/strings.xml | 2 |
4 files changed, 74 insertions, 0 deletions
diff --git a/wallet/src/main/AndroidManifest.xml b/wallet/src/main/AndroidManifest.xml index 5d86a77..e2a79da 100644 --- a/wallet/src/main/AndroidManifest.xml +++ b/wallet/src/main/AndroidManifest.xml @@ -18,6 +18,9 @@ xmlns:tools="http://schemas.android.com/tools" package="net.taler.wallet"> + <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> + <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> + <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.NFC" /> <uses-feature 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('/') diff --git a/wallet/src/main/res/values/strings.xml b/wallet/src/main/res/values/strings.xml index c856345..fec5948 100644 --- a/wallet/src/main/res/values/strings.xml +++ b/wallet/src/main/res/values/strings.xml @@ -155,5 +155,7 @@ GNU Taler is immune against many types of fraud, such as phishing of credit card <string name="refund_success">Refund received</string> <string name="tip_title">Tip</string> + <string name="wifi_disabled_error">Turn on Wi-Fi to get free Wi-Fi</string> + <string name="wifi_connect_error">Could not connect to free Wi-Fi: %s</string> </resources> |