[Android] Add a network monitor object
Nicolas Pomepuy
git at videolan.org
Wed May 15 13:56:42 UTC 2024
vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Mon Jan 8 14:28:53 2024 +0100| [014e7c4993a290abc4f5efbed3d828cd97734b0b] | committer: Nicolas Pomepuy
Add a network monitor object
> https://code.videolan.org/videolan/vlc-android/commit/014e7c4993a290abc4f5efbed3d828cd97734b0b
---
.../org/videolan/mobile/app/AppSetupDelegate.kt | 4 ++
application/vlc-android/AndroidManifest.xml | 1 +
.../videolan/vlc/util/NetworkConnectionManager.kt | 70 ++++++++++++++++++++++
3 files changed, 75 insertions(+)
diff --git a/application/app/src/main/java/org/videolan/mobile/app/AppSetupDelegate.kt b/application/app/src/main/java/org/videolan/mobile/app/AppSetupDelegate.kt
index f3043bda0f..38697d50bc 100644
--- a/application/app/src/main/java/org/videolan/mobile/app/AppSetupDelegate.kt
+++ b/application/app/src/main/java/org/videolan/mobile/app/AppSetupDelegate.kt
@@ -50,6 +50,7 @@ import org.videolan.vlc.BuildConfig
import org.videolan.vlc.gui.SendCrashActivity
import org.videolan.vlc.gui.helpers.NotificationHelper
import org.videolan.vlc.util.DialogDelegate
+import org.videolan.vlc.util.NetworkConnectionManager
import org.videolan.vlc.util.VersionMigration
import org.videolan.vlc.widget.MiniPlayerAppWidgetProvider
@@ -105,6 +106,9 @@ class AppSetupDelegate : AppDelegate,
if (!AndroidDevices.isAndroidTv) sendBroadcast(Intent(MiniPlayerAppWidgetProvider.ACTION_WIDGET_INIT).apply {
component = ComponentName(appContextProvider.appContext, MiniPlayerAppWidgetProvider::class.java)
})
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+ NetworkConnectionManager.start(AppContextProvider.appContext)
+ }
}
}
diff --git a/application/vlc-android/AndroidManifest.xml b/application/vlc-android/AndroidManifest.xml
index bf9a8672a2..26ab499712 100644
--- a/application/vlc-android/AndroidManifest.xml
+++ b/application/vlc-android/AndroidManifest.xml
@@ -20,6 +20,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/> <!-- normal -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
+ <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
diff --git a/application/vlc-android/src/org/videolan/vlc/util/NetworkConnectionManager.kt b/application/vlc-android/src/org/videolan/vlc/util/NetworkConnectionManager.kt
new file mode 100644
index 0000000000..a610bd175e
--- /dev/null
+++ b/application/vlc-android/src/org/videolan/vlc/util/NetworkConnectionManager.kt
@@ -0,0 +1,70 @@
+/*
+ * ************************************************************************
+ * NetworkConnectionManager.kt
+ * *************************************************************************
+ * Copyright © 2024 VLC authors and VideoLAN
+ * Author: Nicolas POMEPUY
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * **************************************************************************
+ *
+ *
+ */
+
+package org.videolan.vlc.util
+
+import android.content.Context
+import android.net.ConnectivityManager
+import android.net.Network
+import android.net.NetworkCapabilities
+import android.net.NetworkRequest
+import android.os.Build
+import androidx.annotation.RequiresApi
+import androidx.lifecycle.MutableLiveData
+
+ at RequiresApi(Build.VERSION_CODES.LOLLIPOP)
+object NetworkConnectionManager {
+ val isMetered = MutableLiveData(true)
+ val hasConnection = MutableLiveData(false)
+
+ private val networkCallback = object : ConnectivityManager.NetworkCallback() {
+ override fun onAvailable(network: Network) {
+ super.onAvailable(network)
+ if (hasConnection.value == false) hasConnection.postValue(true)
+
+ }
+
+ // Network capabilities have changed for the network
+ override fun onCapabilitiesChanged(
+ network: Network,
+ networkCapabilities: NetworkCapabilities
+ ) {
+ super.onCapabilitiesChanged(network, networkCapabilities)
+ val metered = !networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)
+ if (metered != isMetered.value) isMetered.postValue(metered)
+ }
+
+ override fun onLost(network: Network) {
+ super.onLost(network)
+ if (hasConnection.value == true) hasConnection.postValue(false)
+ }
+ }
+ fun start(context: Context) {
+ val networkRequest = NetworkRequest.Builder()
+ .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
+ .build()
+ val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
+ connectivityManager.requestNetwork(networkRequest, networkCallback)
+ }
+}
\ No newline at end of file
More information about the Android
mailing list