[Android] Migrate to library provided handler for Android Auto detection

Robert Stone git at videolan.org
Thu Feb 15 10:48:27 UTC 2024


vlc-android | branch: master | Robert Stone <rhstone at gmail.com> | Tue Feb 13 20:00:34 2024 -0800| [c6da5ed01cca6fa0daa97ee1944efd7912f5d78e] | committer: Nicolas Pomepuy

Migrate to library provided handler for Android Auto detection

> https://code.videolan.org/videolan/vlc-android/commit/c6da5ed01cca6fa0daa97ee1944efd7912f5d78e
---

 .../src/org/videolan/vlc/PlaybackService.kt        | 19 ++---
 .../org/videolan/vlc/util/CarConnectionHandler.kt  | 84 ----------------------
 2 files changed, 7 insertions(+), 96 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index d38c378ce6..4fc1338e1e 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -41,6 +41,7 @@ import android.widget.Toast
 import androidx.annotation.MainThread
 import androidx.annotation.RequiresApi
 import androidx.annotation.StringRes
+import androidx.car.app.connection.CarConnection
 import androidx.car.app.notification.CarPendingIntent
 import androidx.core.app.NotificationManagerCompat
 import androidx.core.app.ServiceCompat
@@ -147,7 +148,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
     private var popupManager: PopupManager? = null
 
     private val mediaFactory = FactoryManager.getFactory(IMediaFactory.factoryId) as IMediaFactory
-    private lateinit var carConnectionHandler:CarConnectionHandler
+    private lateinit var carConnection: CarConnection
 
     /**
      * Binds a [MediaBrowserCompat] to the service to allow receiving the
@@ -161,11 +162,6 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
             val action = intent.action ?: return
             val state = intent.getIntExtra("state", 0)
 
-            if (action == CarConnectionHandler.RECEIVER_ACTION) {
-                carConnectionHandler.query()
-                return
-            }
-
             // skip all headsets events if there is a call
             if ((context.getSystemService(AUDIO_SERVICE) as AudioManager).mode == AudioManager.MODE_IN_CALL) return
 
@@ -652,13 +648,12 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
             addAction(MiniPlayerAppWidgetProvider.ACTION_WIDGET_DISABLED)
             addAction(Intent.ACTION_HEADSET_PLUG)
             addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY)
-            if (CarConnectionHandler.preferCarConnectionHandler()) addAction(CarConnectionHandler.RECEIVER_ACTION)
             addAction(CUSTOM_ACTION)
         }
         registerReceiverCompat(receiver, filter, false)
-        if (CarConnectionHandler.preferCarConnectionHandler()) {
-            carConnectionHandler = CarConnectionHandler(contentResolver)
-            carConnectionHandler.connectionType.observeForever {
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+            carConnection = CarConnection(this)
+            carConnection.type.observeForever {
                 if (it != null) executeUpdate(true)
             }
         }
@@ -1849,8 +1844,8 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
     }
 
     fun isCarMode(): Boolean {
-        return if (CarConnectionHandler.preferCarConnectionHandler()) {
-            carConnectionHandler.connectionType.value?.let { it > CarConnectionHandler.CONNECTION_TYPE_NOT_CONNECTED } ?: false
+        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+            carConnection.type.value?.let { it > CarConnection.CONNECTION_TYPE_NOT_CONNECTED } ?: false
         } else {
             (getSystemService(Context.UI_MODE_SERVICE) as UiModeManager).currentModeType == Configuration.UI_MODE_TYPE_CAR
         }
diff --git a/application/vlc-android/src/org/videolan/vlc/util/CarConnectionHandler.kt b/application/vlc-android/src/org/videolan/vlc/util/CarConnectionHandler.kt
deleted file mode 100644
index cebfae49ff..0000000000
--- a/application/vlc-android/src/org/videolan/vlc/util/CarConnectionHandler.kt
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * ************************************************************************
- *  CarConnectionQueryHandler.java
- * *************************************************************************
- * Copyright © 2022 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.AsyncQueryHandler
-import android.content.ContentResolver
-import android.database.Cursor
-import android.net.Uri
-import android.os.Build
-import androidx.lifecycle.MutableLiveData
-
-private const val CAR_CONNECTION_STATE = "CarConnectionState"
-private const val CAR_CONNECTION_AUTHORITY = "androidx.car.app.connection"
-
-internal class CarConnectionHandler(resolver: ContentResolver?) : AsyncQueryHandler(resolver) {
-    val connectionType = MutableLiveData<Int?>(null)
-
-    init {
-        query()
-    }
-
-    fun query() {
-        startQuery(42, null, Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(CAR_CONNECTION_AUTHORITY).build(), arrayOf(CAR_CONNECTION_STATE), null, null, null)
-    }
-
-    override fun onQueryComplete(token: Int, cookie: Any?, cursor: Cursor?) {
-        if (cursor == null) {
-            connectionType.postValue(CONNECTION_TYPE_NOT_CONNECTED)
-            return
-        }
-        val carConnectionTypeColumn = cursor.getColumnIndex(CAR_CONNECTION_STATE)
-        if (carConnectionTypeColumn < 0) {
-            connectionType.postValue(CONNECTION_TYPE_NOT_CONNECTED)
-            return
-        }
-        if (!cursor.moveToNext()) {
-            connectionType.postValue(CONNECTION_TYPE_NOT_CONNECTED)
-            return
-        }
-        connectionType.postValue(cursor.getInt(carConnectionTypeColumn))
-    }
-
-     companion object {
-         const val RECEIVER_ACTION = "androidx.car.app.connection.action.CAR_CONNECTION_UPDATED"
-
-         /**
-          * Not connected to any car head unit.z
-          */
-         const val CONNECTION_TYPE_NOT_CONNECTED = 0
-
-         /**
-          * Natively running on a head unit (Android Automotive OS).
-          */
-         const val CONNECTION_TYPE_NATIVE = 1
-
-         /**
-          * Connected to a car head unit by projecting to it.
-          */
-         const val CONNECTION_TYPE_PROJECTION = 2
-
-         fun preferCarConnectionHandler() = Build.VERSION.SDK_INT >= 23
-     }
-}
\ No newline at end of file



More information about the Android mailing list