Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.os.BatteryManager
import android.os.Build
import io.ionic.android_js_engine.capacitor_api.DeviceAPI
import org.json.JSONObject

class Device (context: android.content.Context) : DeviceAPI {
private val context: android.content.Context
class Device (context: Context) : DeviceAPI {
private val context: Context
private val connectivityManager: ConnectivityManager

init {
this.context = context
this.connectivityManager = this.context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager;
this.connectivityManager = this.context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
}

override fun getBatteryStatus(): String {
Expand All @@ -30,25 +29,20 @@ class Device (context: android.content.Context) : DeviceAPI {

override fun getNetworkStatus(): String {
var status = NetworkStatus()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val activeNetwork: Network? = this.connectivityManager.activeNetwork
val capabilities: NetworkCapabilities? =
this.connectivityManager.getNetworkCapabilities(this.connectivityManager.activeNetwork)
if (activeNetwork != null && capabilities != null) {
status.connected =
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) &&
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
status.connectionType = NetworkStatus.ConnectionType.WIFI
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
status.connectionType = NetworkStatus.ConnectionType.CELLULAR
} else {
status.connectionType = NetworkStatus.ConnectionType.UNKNOWN
}
val activeNetwork: Network? = this.connectivityManager.activeNetwork
val capabilities: NetworkCapabilities? =
this.connectivityManager.getNetworkCapabilities(this.connectivityManager.activeNetwork)
if (activeNetwork != null && capabilities != null) {
status.connected =
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) &&
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
status.connectionType = NetworkStatus.ConnectionType.WIFI
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
status.connectionType = NetworkStatus.ConnectionType.CELLULAR
} else {
status.connectionType = NetworkStatus.ConnectionType.UNKNOWN
}
} else {
status = getAndParseNetworkInfo()
}

val networkInfo = JSONObject()
Expand Down Expand Up @@ -79,20 +73,4 @@ class Device (context: android.content.Context) : DeviceAPI {
}
return false
}

@Suppress("DEPRECATION")
private fun getAndParseNetworkInfo(): NetworkStatus {
val networkStatus = NetworkStatus()
val networkInfo = connectivityManager.activeNetworkInfo
if (networkInfo != null) {
networkStatus.connected = networkInfo.isConnected
val typeName = networkInfo.typeName
if (typeName == "WIFI") {
networkStatus.connectionType = NetworkStatus.ConnectionType.WIFI
} else if (typeName == "MOBILE") {
networkStatus.connectionType = NetworkStatus.ConnectionType.CELLULAR
}
}
return networkStatus
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,42 +125,38 @@ class Notifications(context: Context) : NotificationsAPI {
}

private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name: CharSequence = "Default"
val description = "Default"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(defaultNotificationChannelID, name, importance)
channel.description = description
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
val soundUri = this.getDefaultSoundUrl(context)
if (soundUri != null) {
channel.setSound(soundUri, audioAttributes)
}

val notificationManager = context.getSystemService(
NotificationManager::class.java
)
notificationManager.createNotificationChannel(channel)
val name: CharSequence = "Default"
val description = "Default"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(defaultNotificationChannelID, name, importance)
channel.description = description
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
val soundUri = this.getDefaultSoundUrl(context)
if (soundUri != null) {
channel.setSound(soundUri, audioAttributes)
}

val notificationManager = context.getSystemService(
NotificationManager::class.java
)
notificationManager.createNotificationChannel(channel)
}

private fun createBadgeNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name: CharSequence = "Badge"
val description = "Badge"
val importance = NotificationManager.IMPORTANCE_LOW
val channel = NotificationChannel(defaultBadgeNotificationChannelID, name, importance)
channel.description = description
channel.setShowBadge(true)

val notificationManager = context.getSystemService(
NotificationManager::class.java
)
notificationManager.createNotificationChannel(channel)
}
val name: CharSequence = "Badge"
val description = "Badge"
val importance = NotificationManager.IMPORTANCE_LOW
val channel = NotificationChannel(defaultBadgeNotificationChannelID, name, importance)
channel.description = description
channel.setShowBadge(true)

val notificationManager = context.getSystemService(
NotificationManager::class.java
)
notificationManager.createNotificationChannel(channel)
}

private fun getDefaultSoundUrl(context: Context): Uri? {
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,7 @@ class CapacitorNotifications: NSObject, CapacitorNotificationsExports {
let badgeOptions = SetBadgeOption(from: jsonDict)

DispatchQueue.main.async {
if #available(iOS 16.0, *) {
UNUserNotificationCenter.current().setBadgeCount(badgeOptions.count)
} else {
UIApplication.shared.applicationIconBadgeNumber = badgeOptions.count
}
UNUserNotificationCenter.current().setBadgeCount(badgeOptions.count)
}
} catch {
JSContext.current().exception = JSValue(newErrorFromMessage: "\(error)", in: JSContext.current())
Expand All @@ -207,11 +203,7 @@ class CapacitorNotifications: NSObject, CapacitorNotificationsExports {
}

DispatchQueue.main.async {
if #available(iOS 16.0, *) {
UNUserNotificationCenter.current().setBadgeCount(0)
} else {
UIApplication.shared.applicationIconBadgeNumber = 0
}
UNUserNotificationCenter.current().setBadgeCount(0)
}
} catch {
JSContext.current().exception = JSValue(newErrorFromMessage: "\(error)", in: JSContext.current())
Expand Down
Loading