Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Activity and/or Application Logo Programmatically from Manifest

I am trying to load the logo associated with the current activity and/or its parent application from the definition in the manifest. This technique has already worked successfully on loading the title and icon associated with both, but the logo is always returning null or 0.

Here is the relevant info from the manifest I am working with:

<application
    android:icon="@drawable/ic_launcher"
    android:logo="@drawable/ad_logo"
    android:label="@string/app_name"
    android:hardwareAccelerated="true">

    <activity
        android:name=".MainActivity" 
        android:label="@string/app_name"
        android:logo="@drawable/ad_logo">

With activity being an instance of the above defined activity, I have tried the following (results annotated inline):

PackageManager pm = activity.getPackageManager();
ComponentName cn = activity.getComponentName();
ApplicationInfo ai = activity.getApplicationInfo();

//ACTIVITY LABEL: Works
pm.getActivityInfo(cn, PackageManager.GET_ACTIVITIES).loadLabel(pm);

//APPLICATION LABEL: Works
ai.loadLabel(pm);
//APPLICATION LABEL: Works
pm.getApplicationLabel(ai);

//ACTIVITY ICON: Works
pm.getActivityInfo(cn, PackageManager.GET_ACTIVITIES).loadIcon(pm);
//ACTIVITTY ICON: Works
pm.getActivityIcon(cn);

//APPLICATION ICON: Works
pm.getApplicationIcon(ai);
//APPLICATION ICON: Works
ai.loadIcon(pm);

//ACTIVITY LOGO: Does not work
pm.getActivityInfo(cn, PackageManager.GET_ACTIVITIES).loadLogo(pm);
//ACTIVITY LOGO: Does not work
pm.getActivityInfo(cn, PackageManager.GET_ACTIVITIES).logo
//ACTIVITY LOGO: Does not work
pm.getActivityLogo(cn);

//APPLICATION LOGO: Does not work
pm.getApplicationLogo(ai);
//APPLICATION LOGO: Does not work
pm.getApplicationLogo(activity.getApplication().getPackageName());
//APPLICATION LOGO: Does not work
ai.loadLogo(pm);

//APPLICATION LOGO: Does not work
pm.getApplicationInfo(ai.packageName, 0).loadLogo(pm);
//APPLICATION LOGO: Does not work
ai.logo;

Does anyone know how to properly load the logo for either or both of these targets?

edit: Logo support requires API level 9. The devices I am testing on are emulators and physical devices all runing API level 10. This code will never be executed on 3.0+.

like image 284
Jake Wharton Avatar asked May 24 '11 03:05

Jake Wharton


People also ask

How do I declare an activity in manifest xml?

There are several other attributes that you can include in this element, to define properties such as the label for the activity, an icon for the activity, or a theme to style the activity's UI. The android:name attribute is the only required attribute—it specifies the class name of the activity.

What is AndroidManifest XML file and why do you need this?

Every app project must have an AndroidManifest. xml file (with precisely that name) at the root of the project source set. The manifest file describes essential information about your app to the Android build tools, the Android operating system, and Google Play.

Where can I find AndroidManifest XML file?

xml file is created in the app's corresponding dist folder. The file is located at WorkspaceName>/temp/<AppName>/build/luaandroid/dist. The manifest file provides essential information about your app to the Android operating system, and Google Play store.


2 Answers

From my tests, your code works properly running on a Honeycomb device, but not on a Gingerbread or below device. That seems to imply that the android:logo functionality, although present in API Level 8, is not implemented correctly in the underlying platform. That is, the same application apk on a Level 8-10 device is not retaining the logo attribute, but a Level 11 device does. That means the problem is not with the code or the AndroidManifest.xml, but with the platform.

like image 123
Joe Avatar answered Sep 20 '22 18:09

Joe


If you take a look at the code in android.app.ContextImpl.ApplicationPackageManager (here), you will find that eventually it uses the method

public Drawable getDrawable(String packageName,int resid,ApplicationInfo appInfo)

(lines 2131 to 2173 in 2.3).

There are several Log.w calls there that you can use to follow what is going on through adb (i.e., Failure retrieving resources for...)

like image 42
Aleadam Avatar answered Sep 17 '22 18:09

Aleadam