Skip to content

Commit

Permalink
replace WifiManager with ConnectivityManager, send to android wifi se…
Browse files Browse the repository at this point in the history
…ttings, update permissions
  • Loading branch information
pazos committed Feb 5, 2020
1 parent 8ccfef5 commit b0cbf26
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 58 deletions.
3 changes: 1 addition & 2 deletions app/manifest/base.xml
Expand Up @@ -4,8 +4,7 @@
package="org.koreader.launcher" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
Expand Down
117 changes: 79 additions & 38 deletions app/src/org/koreader/launcher/BaseActivity.kt
Expand Up @@ -10,9 +10,11 @@ import android.app.DownloadManager
import android.app.NativeActivity
import android.content.*
import android.content.pm.PackageManager
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.net.Uri
import android.net.wifi.WifiManager
import android.os.*
import android.provider.Settings
import android.text.format.Formatter
import android.view.Gravity
import android.view.SurfaceHolder
Expand All @@ -38,6 +40,13 @@ abstract class BaseActivity : NativeActivity(), JNILuaInterface,

companion object {
private const val TAG = "BaseActivity"
private const val ACTIVE_NETWORK_NONE = 0
private const val ACTIVE_NETWORK_WIFI = 1
private const val ACTIVE_NETWORK_MOBILE = 2
private const val ACTIVE_NETWORK_ETHERNET = 3
private const val ACTIVE_NETWORK_BLUETOOTH = 4
private const val ACTIVE_NETWORK_VPN = 5

private val BATTERY_FILTER = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
private val PRODUCT = DeviceInfo.PRODUCT
private val RUNTIME_VERSION = Build.VERSION.RELEASE
Expand Down Expand Up @@ -320,31 +329,78 @@ abstract class BaseActivity : NativeActivity(), JNILuaInterface,
}
}

@Suppress("DEPRECATION")
override fun getNetworkInfo(): String {
val wifi: WifiManager? = applicationContext.getSystemService(
Context.WIFI_SERVICE) as? WifiManager
return if (wifi != null) {
val wi = wifi.connectionInfo
val dhcp = wifi.dhcpInfo
val ip = wi.ipAddress
val gw = dhcp.gateway
val ipAddress = formatIp(ip)
val gwAddress = formatIp(gw)
String.format(Locale.US, "%s;%s;%s", wi.ssid, ipAddress, gwAddress)
} else ""
}

override fun isWifiEnabled(): Int {
val wifi: WifiManager? = applicationContext.getSystemService(
Context.WIFI_SERVICE) as? WifiManager
return if (wifi?.isWifiEnabled == true) 1 else 0
val connectivityManager = this.getSystemService(Context.CONNECTIVITY_SERVICE)
as ConnectivityManager

var connectionType: Int = ACTIVE_NETWORK_NONE
val connected: Boolean =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
connectivityManager.activeNetwork?.let { net ->
connectivityManager.getNetworkCapabilities(net)?.let {
when {
it.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> {
connectionType = ACTIVE_NETWORK_WIFI
true
}
it.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> {
connectionType = ACTIVE_NETWORK_MOBILE
true
}
it.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> {
connectionType = ACTIVE_NETWORK_ETHERNET
true
}
it.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH) -> {
connectionType = ACTIVE_NETWORK_BLUETOOTH
true
}
it.hasTransport(NetworkCapabilities.TRANSPORT_VPN) -> {
connectionType = ACTIVE_NETWORK_VPN
true
}
else -> false
}
} ?: false
} ?: false
} else {
connectivityManager.run {
connectivityManager.activeNetworkInfo?.run {
when (type) {
ConnectivityManager.TYPE_WIFI -> {
connectionType = ACTIVE_NETWORK_WIFI
true
}
ConnectivityManager.TYPE_MOBILE -> {
connectionType = ACTIVE_NETWORK_MOBILE
true
}
ConnectivityManager.TYPE_ETHERNET -> {
connectionType = ACTIVE_NETWORK_ETHERNET
true
}
ConnectivityManager.TYPE_BLUETOOTH -> {
connectionType = ACTIVE_NETWORK_BLUETOOTH
true
}
ConnectivityManager.TYPE_VPN -> {
connectionType = ACTIVE_NETWORK_VPN
true
}
else -> false
}
}
} ?: false
}
return String.format(Locale.US, "%d;%d", if (connected) 1 else 0, connectionType)
}

@Suppress("DEPRECATION")
override fun setWifiEnabled(enabled: Boolean) {
val wifi: WifiManager? = applicationContext.getSystemService(
Context.WIFI_SERVICE) as? WifiManager
wifi?.isWifiEnabled = enabled
override fun openWifiSettings() {
val intent = Intent().apply {
action = Settings.ACTION_WIFI_SETTINGS
}
startActivityIfSafe(intent)
}

/* package manager */
Expand Down Expand Up @@ -475,21 +531,6 @@ abstract class BaseActivity : NativeActivity(), JNILuaInterface,
}
}

/* network */

/* Formatter.formatIpAddress is deprecated in API15 because
it does not support IPv6. Is still handy to format ip and
gateway addresses given by most LAN routers */

@Suppress("DEPRECATION")
private fun formatIp(number: Int): String {
return if (number > 0) {
Formatter.formatIpAddress(number)
} else {
number.toString()
}
}

/* power */
private fun getBatteryState(isPercent: Boolean): Int {
val intent = applicationContext.registerReceiver(null, BATTERY_FILTER)
Expand Down
3 changes: 1 addition & 2 deletions app/src/org/koreader/launcher/JNILuaInterface.kt
Expand Up @@ -38,9 +38,9 @@ internal interface JNILuaInterface {
fun isFullscreen(): Int
fun isPackageEnabled(pkg: String): Int
fun isPathInsideSandbox(path: String?): Int
fun isWifiEnabled(): Int
fun needsWakelocks(): Int
fun openLink(url: String): Int
fun openWifiSettings()
fun performHapticFeedback(constant: Int)
fun requestWriteSystemSettings()
fun sendText(text: String?)
Expand All @@ -49,7 +49,6 @@ internal interface JNILuaInterface {
fun setHapticOverride(enabled: Boolean)
fun setScreenBrightness(brightness: Int)
fun setScreenOffTimeout(timeout: Int)
fun setWifiEnabled(enabled: Boolean)
fun startEPDTestActivity()
fun showToast(message: String)
fun showToast(message: String, longTimeout: Boolean)
Expand Down
30 changes: 14 additions & 16 deletions assets/android.lua
Expand Up @@ -334,6 +334,15 @@ enum {
AHAPTIC_TEXT_HANDLE_MOVE = 9,
};

enum {
ANETWORK_NONE = 0,
ANETWORK_WIFI = 1,
ANETWORK_MOBILE = 2,
ANETWORK_ETHERNET = 3,
ANETWORK_BLUETOOTH = 4,
ANETWORK_VPN = 5,
};

int32_t AInputEvent_getType(const AInputEvent* event);
int32_t AInputEvent_getDeviceId(const AInputEvent* event);
int32_t AInputEvent_getSource(const AInputEvent* event);
Expand Down Expand Up @@ -1863,28 +1872,17 @@ local function run(android_app_state)
"getNetworkInfo",
"()Ljava/lang/String;"
)
return string.match(JNI:to_string(network_info), "(.*);(.*);(.*)")
return string.match(JNI:to_string(network_info), "(.*);(.*)")
end)
end
android.isWifiEnabled = function()
return JNI:context(android.app.activity.vm, function(JNI)
return JNI:callIntMethod(
android.app.activity.clazz,
"isWifiEnabled",
"()I"
) == 1
end)
end
android.setWifiEnabled = function(wifiEnabled)
android.DEBUG("setting wifi to: " .. tostring(wifiEnabled))
android.openWifiSettings = function()
android.DEBUG("open android settings")
JNI:context(android.app.activity.vm, function(JNI)
JNI:callVoidMethod(
android.app.activity.clazz,
"setWifiEnabled",
"(Z)V",
ffi.new("bool", wifiEnabled)
"openWifiSettings",
"()V"
)
end)
end
Expand Down

0 comments on commit b0cbf26

Please sign in to comment.