[Android] Handle extensions icon specific for menu

Geoffrey Métais git at videolan.org
Mon Jan 25 15:17:13 CET 2016


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Jan 25 15:16:18 2016 +0100| [7f75a39a614955798cc79abd1dce7b630c48f727] | committer: Geoffrey Métais

Handle extensions icon specific for menu

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

 .../videolan/vlc/extensions/ExtensionListing.java  | 18 ++++++++++++++++
 .../vlc/extensions/ExtensionManagerService.java    | 22 +++++++++++---------
 .../src/org/videolan/vlc/gui/MainActivity.java     | 24 +++++++++++++++++-----
 3 files changed, 49 insertions(+), 15 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/extensions/ExtensionListing.java b/vlc-android/src/org/videolan/vlc/extensions/ExtensionListing.java
index 315b62f..eab8464 100644
--- a/vlc-android/src/org/videolan/vlc/extensions/ExtensionListing.java
+++ b/vlc-android/src/org/videolan/vlc/extensions/ExtensionListing.java
@@ -34,6 +34,7 @@ public class ExtensionListing implements Parcelable {
     private ComponentName mComponentName;
     private int mProtocolVersion;
     private boolean mCompatible;
+    private int menuIcon = 0;
     private String mTitle;
     private String mDescription;
     private ComponentName mSettingsActivity;
@@ -71,6 +72,21 @@ public class ExtensionListing implements Parcelable {
     }
 
     /**
+     * Sets the resId of the icon displayed in VLC menu for this extension.
+     */
+    public ExtensionListing menuIcon(int menuIcon) {
+        this.menuIcon = menuIcon;
+        return this;
+    }
+
+    /**
+     * Returns the resId of the icon displayed in VLC menu for this extension.
+     */
+    public int menuIcon() {
+        return menuIcon;
+    }
+
+    /**
      * Sets the version of the {@link org.videolan.vlc.extensions.api.VLCExtensionService}
      * protocol used by the extension.
      */
@@ -176,6 +192,7 @@ public class ExtensionListing implements Parcelable {
             mTitle = in.readString();
             mDescription = in.readString();
             boolean hasSettings = in.readInt() == 1;
+            menuIcon = in.readInt();
             if (hasSettings) {
                 mSettingsActivity = ComponentName.readFromParcel(in);
             }
@@ -197,6 +214,7 @@ public class ExtensionListing implements Parcelable {
         parcel.writeString(mTitle);
         parcel.writeString(mDescription);
         parcel.writeInt(mSettingsActivity != null ? 1 : 0);
+        parcel.writeInt(menuIcon);
         if (mSettingsActivity != null) {
             mSettingsActivity.writeToParcel(parcel, 0);
         }
diff --git a/vlc-android/src/org/videolan/vlc/extensions/ExtensionManagerService.java b/vlc-android/src/org/videolan/vlc/extensions/ExtensionManagerService.java
index d3bb57c..aaaf884 100644
--- a/vlc-android/src/org/videolan/vlc/extensions/ExtensionManagerService.java
+++ b/vlc-android/src/org/videolan/vlc/extensions/ExtensionManagerService.java
@@ -41,11 +41,11 @@ import android.text.TextUtils;
 import android.util.Log;
 
 import org.videolan.vlc.VLCApplication;
-import org.videolan.vlc.media.MediaUtils;
-import org.videolan.vlc.media.MediaWrapper;
 import org.videolan.vlc.extensions.api.IExtensionHost;
 import org.videolan.vlc.extensions.api.IExtensionService;
 import org.videolan.vlc.extensions.api.VLCExtensionItem;
+import org.videolan.vlc.media.MediaUtils;
+import org.videolan.vlc.media.MediaWrapper;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -57,6 +57,7 @@ public class ExtensionManagerService extends Service {
     private static final String KEY_PROTOCOL_VERSION = "protocolVersion";
     private static final String KEY_LISTING_TITLE = "title";
     private static final String KEY_DESCRIPTION = "description";
+    private static final String KEY_MENU_ICON = "menuicon";
     private static final String KEY_SETTINGS_ACTIVITY = "settingsActivity";
 
     public static final String ACTION_EXTENSION = "org.videolan.vlc.Extension";
@@ -113,23 +114,24 @@ public class ExtensionManagerService extends Service {
         ArrayList<ExtensionListing> extensions = new ArrayList<>();
 
         for (ResolveInfo resolveInfo : resolveInfos) {
-            ExtensionListing info = new ExtensionListing();
-            info.componentName(new ComponentName(resolveInfo.serviceInfo.packageName,
+            ExtensionListing extension = new ExtensionListing();
+            extension.componentName(new ComponentName(resolveInfo.serviceInfo.packageName,
                     resolveInfo.serviceInfo.name));
             Bundle metaData = resolveInfo.serviceInfo.metaData;
             if (metaData != null) {
-                info.compatible(metaData.getInt(KEY_PROTOCOL_VERSION) == PROTOCOLE_VERSION);
-                if (!info.compatible())
+                extension.compatible(metaData.getInt(KEY_PROTOCOL_VERSION) == PROTOCOLE_VERSION);
+                if (!extension.compatible())
                     continue;
                 String title = metaData.getString(KEY_LISTING_TITLE);
-                info.title(title != null ? title : resolveInfo.loadLabel(pm).toString());
-                info.description(metaData.getString(KEY_DESCRIPTION));
+                extension.title(title != null ? title : resolveInfo.loadLabel(pm).toString());
+                extension.description(metaData.getString(KEY_DESCRIPTION));
                 String settingsActivity = metaData.getString(KEY_SETTINGS_ACTIVITY);
                 if (!TextUtils.isEmpty(settingsActivity)) {
-                    info.settingsActivity(ComponentName.unflattenFromString(
+                    extension.settingsActivity(ComponentName.unflattenFromString(
                             resolveInfo.serviceInfo.packageName + "/" + settingsActivity));
                 }
-                extensions.add(info);
+                extension.menuIcon(metaData.getInt(KEY_MENU_ICON, 0));
+                extensions.add(extension);
             }
         }
         synchronized (mExtensions) {
diff --git a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index 3c387a9..69f14e6 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -29,7 +29,9 @@ import android.content.ServiceConnection;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.content.pm.PackageManager;
+import android.content.res.Resources;
 import android.database.Cursor;
+import android.graphics.drawable.Drawable;
 import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
@@ -308,12 +310,24 @@ public class MainActivity extends AudioPlayerContainerActivity implements Search
             SubMenu subMenu = navMenu.addSubMenu(PLUGIN_NAVIGATION_GROUP, PLUGIN_NAVIGATION_GROUP,
                PLUGIN_NAVIGATION_GROUP, R.string.plugins);
         for (int i = 0 ; i < plugins.size() ; ++i) {
-            MenuItem item = subMenu.add(PLUGIN_NAVIGATION_GROUP, i, 0, plugins.get(i).title());
-            try {
-                item.setIcon(pm.getApplicationIcon(plugins.get(i).componentName().getPackageName()));
-            } catch (PackageManager.NameNotFoundException e) {
-                item.setIcon(R.drawable.icon);
+            ExtensionListing extension = plugins.get(i);
+            MenuItem item = subMenu.add(PLUGIN_NAVIGATION_GROUP, i, 0, extension.title());
+            int iconRes = extension.menuIcon();
+            Drawable extensionIcon = null;
+            if (iconRes != 0) {
+                try {
+                    Resources res = VLCApplication.getAppContext().getPackageManager().getResourcesForApplication(extension.componentName().getPackageName());
+                    extensionIcon = res.getDrawable(extension.menuIcon());
+                } catch (PackageManager.NameNotFoundException e) {}
             }
+            if (extensionIcon != null)
+                item.setIcon(extensionIcon);
+            else
+                try {
+                    item.setIcon(pm.getApplicationIcon(plugins.get(i).componentName().getPackageName()));
+                } catch (PackageManager.NameNotFoundException e) {
+                    item.setIcon(R.drawable.icon);
+                }
         }
         mNavigationView.invalidate();
     }



More information about the Android mailing list