[rhythmbox] improve android device identification



commit 873e500635d4bf85481fc239e4cfe034b66085e8
Author: Jonathan Matthew <jonathan d14n org>
Date:   Sat Sep 5 22:55:42 2015 +1000

    improve android device identification
    
    Sadly it's not as easy as just looking for 'Android' in the device model string.
    Anything with 'Android' or 'Nexus' in the model string, or 'motorola' or 'OnePlus'
    in the vendor string is pretty safe to assume to be an Android device.
    This covers most of the major vendors except Sony.  Most likely, more will be added
    later.
    
    Since the checks are more complicated now, they've been moved to the removable media
    manager so both the android and mtp plugins can call them.

 plugins/android/rb-android-plugin.c |   47 +++++++-------------------
 plugins/mtpdevice/rb-mtp-plugin.c   |    2 +-
 shell/rb-removable-media-manager.c  |   61 +++++++++++++++++++++++++++++++++++
 shell/rb-removable-media-manager.h  |    3 ++
 4 files changed, 78 insertions(+), 35 deletions(-)
---
diff --git a/plugins/android/rb-android-plugin.c b/plugins/android/rb-android-plugin.c
index 92b0646..97c0b13 100644
--- a/plugins/android/rb-android-plugin.c
+++ b/plugins/android/rb-android-plugin.c
@@ -91,34 +91,6 @@ source_deleted_cb (RBAndroidSource *source, RBAndroidPlugin *plugin)
        plugin->sources = g_list_remove (plugin->sources, source);
 }
 
-static GUdevDevice *
-get_gudev_device (GMount *mount)
-{
-       GVolume *volume;
-       GUdevClient *client;
-       GUdevDevice *udevice = NULL;
-       char *devpath;
-       char *subsystems[] = { "usb", NULL };
-
-       volume = g_mount_get_volume (mount);
-       if (volume == NULL) {
-               return FALSE;
-       }
-       devpath = g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
-       g_clear_object (&volume);
-
-       if (devpath == NULL) {
-               return FALSE;
-       }
-
-       client = g_udev_client_new ((const char * const *)subsystems);
-       if (client != NULL)
-               udevice = g_udev_client_query_by_device_file (client, devpath);
-
-       g_clear_object (&client);
-       return udevice;
-}
-
 static RBSource *
 create_source_cb (RBRemovableMediaManager *rmm, GMount *mount, MPIDDevice *device_info, RBAndroidPlugin 
*plugin)
 {
@@ -128,31 +100,37 @@ create_source_cb (RBRemovableMediaManager *rmm, GMount *mount, MPIDDevice *devic
        RhythmDBEntryType *entry_type;
        RhythmDBEntryType *error_type;
        RhythmDBEntryType *ignore_type;
+       GObject *dev;
        GUdevDevice *gudev_device;
        GtkBuilder *builder;
        GMenu *toolbar;
        GVolume *volume;
        GSettings *settings;
        GFile *root;
-       const char *model;
        const char *device_serial;
        char *uri_prefix;
        char *name;
        char *path;
 
-       gudev_device = get_gudev_device (mount);
-       if (gudev_device == NULL)
+       volume = g_mount_get_volume (mount);
+       if (volume == NULL)
+               return NULL;
+
+       dev = rb_removable_media_manager_get_gudev_device (rmm, volume);
+       if (dev == NULL) {
+               g_object_unref (volume);
                return NULL;
+       }
+       gudev_device = G_UDEV_DEVICE (dev);
 
-       model = g_udev_device_get_property (gudev_device, "ID_MODEL");
-       if (g_strcmp0 (model, "Android") != 0) {
+       if (rb_removable_media_manager_device_is_android (rmm, G_OBJECT (gudev_device)) == FALSE) {
                g_object_unref (gudev_device);
+               g_object_unref (volume);
                return NULL;
        }
 
        device_info = mpid_device_new_from_mpi_file (rb_find_plugin_data_file (G_OBJECT (plugin), 
"android.mpi"));
 
-       volume = g_mount_get_volume (mount);
        path = g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
 
        g_object_get (plugin, "object", &shell, NULL);
@@ -238,6 +216,7 @@ create_source_cb (RBRemovableMediaManager *rmm, GMount *mount, MPIDDevice *devic
                                 plugin, 0);
 
        g_object_unref (shell);
+       g_object_unref (volume);
        return source;
 }
 
diff --git a/plugins/mtpdevice/rb-mtp-plugin.c b/plugins/mtpdevice/rb-mtp-plugin.c
index 5408658..52ecb08 100644
--- a/plugins/mtpdevice/rb-mtp-plugin.c
+++ b/plugins/mtpdevice/rb-mtp-plugin.c
@@ -262,7 +262,7 @@ create_source_device_cb (RBRemovableMediaManager *rmm, GObject *device_obj, RBMt
        }
 
        /* check that it's not an android device */
-       if (g_strcmp0 (g_udev_device_get_property (device, "ID_MODEL"), "Android") == 0) {
+       if (rb_removable_media_manager_device_is_android (rmm, device_obj)) {
                rb_debug ("device %s is android based, android plugin should handle it",
                          g_udev_device_get_name (device));
                return NULL;
diff --git a/shell/rb-removable-media-manager.c b/shell/rb-removable-media-manager.c
index db08a8d..0e7c3cd 100644
--- a/shell/rb-removable-media-manager.c
+++ b/shell/rb-removable-media-manager.c
@@ -860,3 +860,64 @@ rb_removable_media_manager_scan (RBRemovableMediaManager *manager)
        g_list_free (list);
 #endif
 }
+
+GObject *
+rb_removable_media_manager_get_gudev_device (RBRemovableMediaManager *manager, GVolume *volume)
+{
+#if defined(HAVE_GUDEV)
+       RBRemovableMediaManagerPrivate *priv = GET_PRIVATE (manager);
+       char *devpath;
+       GUdevDevice *udevice = NULL;
+
+       devpath = g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
+       if (devpath != NULL)
+               udevice = g_udev_client_query_by_device_file (priv->gudev_client, devpath);
+
+       g_free (devpath);
+       return G_OBJECT (udevice);
+#else
+       return NULL;
+#endif
+}
+
+gboolean
+rb_removable_media_manager_device_is_android (RBRemovableMediaManager *manager, GObject *dev)
+{
+#if defined(HAVE_GUDEV)
+       gboolean match;
+       const char *model;
+       const char *vendor;
+       int i;
+
+       const char *androids[] = {
+               "Android",
+               "Nexus"
+       };
+       const char *android_vendors[] = {
+               "motorola",
+               "OnePlus"
+       };
+
+       match = FALSE;
+
+       model = g_udev_device_get_property (G_UDEV_DEVICE (dev), "ID_MODEL");
+       if (model != NULL) {
+               for (i = 0; i < G_N_ELEMENTS (androids); i++) {
+                       if (strstr (model, androids[i]))
+                               match = TRUE;
+               }
+       }
+
+       vendor = g_udev_device_get_property (G_UDEV_DEVICE (dev), "ID_VENDOR");
+       if (vendor != NULL) {
+               for (i = 0; i < G_N_ELEMENTS (android_vendors); i++) {
+                       if (strstr (vendor, android_vendors[i]))
+                               match = TRUE;
+               }
+       }
+
+       return match;
+#else
+       return FALSE;
+#endif
+}
diff --git a/shell/rb-removable-media-manager.h b/shell/rb-removable-media-manager.h
index 98ccf34..2cb16d7 100644
--- a/shell/rb-removable-media-manager.h
+++ b/shell/rb-removable-media-manager.h
@@ -73,6 +73,9 @@ GType                 rb_removable_media_manager_get_type     (void);
 
 void                   rb_removable_media_manager_scan (RBRemovableMediaManager *manager);
 
+GObject *              rb_removable_media_manager_get_gudev_device (RBRemovableMediaManager *manager, 
GVolume *volume);
+gboolean               rb_removable_media_manager_device_is_android (RBRemovableMediaManager *manager, 
GObject *device);
+
 G_END_DECLS
 
 #endif /* __RB_REMOVABLE_MEDIA_MANAGER_H */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]