Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to specify PROPERTY_SPECIAL_USE_FGS_SUBTYPE via Service Attribute #8729

Closed
tipa opened this issue Feb 16, 2024 · 1 comment · Fixed by #9016
Closed

Unable to specify PROPERTY_SPECIAL_USE_FGS_SUBTYPE via Service Attribute #8729

tipa opened this issue Feb 16, 2024 · 1 comment · Fixed by #9016
Assignees
Labels
Area: App+Library Build Issues when building Library projects or Application projects. Area: Mono.Android Issues with the Android API binding (Mono.Android.dll). enhancement Proposed change to current functionality.
Milestone

Comments

@tipa
Copy link

tipa commented Feb 16, 2024

Android application type

.NET Android (net7.0-android, net8.0-android, etc.)

Affected platform version

VS2022, NET8

Description

The goal is to specify a FOREGROUND_SERVICE_SPECIAL_USE permission using a Service attribute.
This issue is a continuation of this issue: #8409, which was closed after fixing a problem that prevented the foregroundServiceType to be filled in the AndroidManifest.xml.
The issue that remains is that is isn't possible to declare the PROPERTY_SPECIAL_USE_FGS_SUBTYPE for "special use" via an attribute: https://developer.android.com/about/versions/14/changes/fgs-types-required#special-use

Did you find any workaround?

I was able to workaround the problem by manually adding the service tag in the AndroidManifex.xml and giving my Service an explicit name

<service android:foregroundServiceType="specialUse" android:name="my.app.MyService">
   <property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
             android:value="Explanation on why I am using this service type"/>
</service>
[Service(Name = "my.app.MyService")]
sealed class FocusSessionService : Service
{ 
}
@tipa tipa added Area: App+Library Build Issues when building Library projects or Application projects. needs-triage Issues that need to be assigned. labels Feb 16, 2024
@dellis1972 dellis1972 removed the needs-triage Issues that need to be assigned. label Feb 16, 2024
@dellis1972 dellis1972 added this to the .NET 9 milestone Feb 16, 2024
@jonpryor
Copy link
Member

Related: https://developer.android.com/guide/topics/manifest/property-element

We should probably introduce a PropertyAttribute, which emits <property/> within the appropriate scope:

[AttributeUsage (AttributeTargets.Class, AllowMultiple=true)]
public partial class PropertyAttribute : Attribute {
    public PropertyAttribute (string name);
    public string Resource {get; set;}
    public string Value {get; set;}
}

which would allow:

[Service]
[Property ("android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE", Value="Explanation")]
partial class FocusSessionService : Service {
}

Open design question: from the <property/> docs:

Note: A <property> tag should not contain both android:value and android:resource attributes. If that happens, android:resource is preferred.

Given this, it might instead be better to have PropertyResourceAttribute and PropertyValueAttribute instead of a single PropertyAttribute.

@jpobst jpobst added the enhancement Proposed change to current functionality. label Apr 17, 2024
@jpobst jpobst assigned jpobst and unassigned dellis1972 and jonathanpeppers Apr 23, 2024
@jpobst jpobst added the Area: Mono.Android Issues with the Android API binding (Mono.Android.dll). label Apr 23, 2024
jonpryor pushed a commit that referenced this issue Jun 7, 2024
…8781)

Fixes: #8272

Context: #8235
Context: #8729
Context: e790874

Previously, we did not have an established process for detecting new
XML elements and attributes allowed in `AndroidManifest.xml` and
surfacing them to users via our manifest attributes like
`[ActivityAttribute]`.  This leads to users having to use manual
workarounds until our attributes can be updated.

Additionally, whenever we do add new properties to these attributes,
it requires manually updating multiple files by hand that must remain
in sync, eg:

  * [src/Mono.Android/Android.App/IntentFilterAttribute.cs](https://github.com/xamarin/xamarin-android/blob/180dd5205ab270bb74bb853754665db9cb5d65f1/src/Mono.Android/Android.App/IntentFilterAttribute.cs#L9)
  * [src/Xamarin.Android.Build.Tasks/Mono.Android/IntentFilterAttribute.Partial.cs](https://github.com/xamarin/xamarin-android/blob/180dd5205ab270bb74bb853754665db9cb5d65f1/src/Xamarin.Android.Build.Tasks/Mono.Android/IntentFilterAttribute.Partial.cs#L14)

The `build-tools/manifest-attribute-codegen` utility (e790874) has
support to parse Android SDK `attrs_manifest.xml` files, which
specifies what elements and attributes are valid within
`AndroidManifest.xml`.

Update `manifest-attribute-codegen` to do what it's name already
implied: generate code!  It now reads a `metadata.xml` file which
controls which custom attributes to emit, where to emit them, and
what members those custom attributes should have (among other things).
This makes it easier to ensure that code shared by `src/Mono.Android`
and `src/Xamarin.Android.Build.Tasks` are consistent, meaking it
easier to correctly add support for new attributes and/or
attribute members.

Generated file semantics and naming conventions: consider the C# type
`Android.App.ActivityAttribute`.

  * `src\Xamarin.Android.NamingCustomAttributes\Android.App\ActivityAttribute.cs`
    contains the C# `partial` class declaration that can be shared
    by both `src\Mono.Android` and `src\Xamarin.Android.Build.Tasks`.
    This file also contains a `#if XABT_MANIFEST_EXTENSIONS` block
    which is only used by `src\Xamarin.Android.Build.Tasks`.

  * `src/Xamarin.Android.Build.Tasks/Mono.Android/ActivityAttribute.Partial.cs`
    contains the C# `partial` class declaration with code specific
    to `Xamarin.Android.Build.Tasks.dll`.

  * `src/Xamarin.Android.NamingCustomAttributes/Android.App/ActivityAttribute.Partial.cs`
    contains the C# `partial` class declaration with code specific
    to `Mono.Android.dll`.

`metadata.xml` contents and the update process is documented in
`build-tools/manifest-attribute-codegen/README.md`.

Also removed the `ANDROID_*` values from `$(DefineConstants)` for
`Xamarin.Android.Build.Tasks.csproj` as we no longer build separate
assemblies for old Android API levels.

Note this commit does not change any existing manifest attributes or
the properties they expose.  It merely generates what we expose today.
We will determine additional properties to expose in a future commit.
@github-actions github-actions bot locked and limited conversation to collaborators Aug 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Area: App+Library Build Issues when building Library projects or Application projects. Area: Mono.Android Issues with the Android API binding (Mono.Android.dll). enhancement Proposed change to current functionality.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants