Bug 12656 - BroadcastReceiver doesn't support boolean resources for the Enabled attribute
Summary: BroadcastReceiver doesn't support boolean resources for the Enabled attribute
Status: RESOLVED NOT_ON_ROADMAP
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.6.x
Hardware: Macintosh Mac OS
: Low enhancement
Target Milestone: ---
Assignee: Atsushi Eno
URL:
: 14164 ()
Depends on:
Blocks:
 
Reported: 2013-06-12 10:56 UTC by hwrdprkns
Modified: 2013-08-26 12:48 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NOT_ON_ROADMAP

Description hwrdprkns 2013-06-12 10:56:59 UTC
[BroadcastReceiver(Enabled="@bool/is_enabled")] isn't supported at compile-time in Xamarin.Android even though it is supported in the AndroidManifest.xml.
Comment 1 hwrdprkns 2013-06-12 11:00:33 UTC
I think this is just a lapse in the documentation. For example, http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList doesn't mention the support of boolean resources in StateListDrawables, however https://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html mentions it for every state. 

I believe that, its always mentioned in the JavaDoc, however in the "guide" resources it isn't. Either way, Android supports it in the BroadcastReceiver case and Xamarin.Android doesn't. :(
Comment 2 Atsushi Eno 2013-06-17 06:19:37 UTC
Could you provide a reproducible Java Android project or link to any of such a project? I searched for "is_enabled" on developer.android.com but it didn't find anything.
Comment 3 hwrdprkns 2013-06-17 13:47:41 UTC
Thanks for responding so quickly!

Take a look at this AndroidManifest.xml: https://github.com/hwrdprkns/BroadcastReceiverTest/blob/master/BroadcastReceiverTest/src/main/AndroidManifest.xml

It is possible to specify a boolean resource for the enabled attribute of the broadcast receiver just in case you don't want it to run on older platforms. In Xamarin.Android, if I try to do something like this there is a compilation error:

[BroadcastReceiver(Enabled="@bool/some_boolean_resource_from_bools_xml")]
public class MyWidgetReceiver : BroadcastReceiver {
    ....
}

The BroadcastReceiver attribute should support boolean resources.
Comment 4 Jonathan Pryor 2013-06-18 17:51:53 UTC
There's the little question of whether this is supposed to be supported by Android. No Android documentation explicitly mentions that this is supported.

For example, //activity/@android:name states that both string literals and resource references are valid:

http://developer.android.com/guide/topics/manifest/activity-element.html#label
> The label should be set as a reference to a string resource, so that it can be
> localized like other strings in the user interface. However, as a convenience
> while you're developing the application, it can also be set as a raw string.

Compare to //activity/@android:enabled, which just mentions true or false:

http://developer.android.com/guide/topics/manifest/activity-element.html#enabled
> Whether or not the activity can be instantiated by the system — "true" if it can 
> be, and "false" if not. The default value is "true".

---

That said, there are apparent places where references are used instead of string literals. Comment #3 mentions one, as does stackoverflow and the android-developers mailing list:

http://stackoverflow.com/questions/14399292/is-selectively-enabling-an-activity-alias-in-the-manifest-with-androidenabled
http://osdir.com/ml/Android-Developers/2012-08/msg03252.html

---

So what should be done? Unfortunately, "supporting" this would ~double the number of properties on the custom attribute, one taking bool/etc. and an "overload" being a string:

    partial class ActivityAttribute {
        public bool   Enabled {get; set;}
        public string EnabledReference {get; set;}
    }

This is ugly, and a lot of complexity for a small corner case.
Comment 5 Jonathan Pryor 2013-06-18 17:57:00 UTC
I do not foresee us implementing this support. The use-case is too small to worry about, especially since there's a workaround: provide your own XML.

1: Write code as normal:

    namespace Example {
        [BroadcastReceiver]
        public partial class MyWidgetReceiver : BroadcastReceiver {
        }
    }

2. Build it into a package.

3. Read obj\Debug\android\AndroidManifest.xml. Copy the <receiver/> element into Properties\AndroidManifest.xml:

    <receiver android:name="example.MyWidgetReceiver" />

4. Modify the Properties\AndroidManifest.xml as you see fit:

    <receiver android:name="example.MyWidgetReceiver"
            android:enabled="@bool/whatevs"
    />

Note that the @android:name attribute _must_ be fully qualified so that the AndoridManifest.xml generation step "sees" it. Presence of e.g. //receiver[@name='example.MyWidgetReceiver'] in Properties\AndroidManifest.xml will inhibit generation by the build process, ensuring that your version is used instead of the generated version.
Comment 6 Jonathan Pryor 2013-08-26 12:48:32 UTC
*** Bug 14164 has been marked as a duplicate of this bug. ***