Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
<a name='roundIcon' api-level='25' />
<a name='visibleToInstantApps' api-level='26' />
<a name='splitName' api-level='26' />
<a name='foregroundServiceType' api-level='29' />
</e>
<e name='receiver'>
<parent>application</parent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@

using Xamarin.Android.Manifest;

using Android.Content.PM;

namespace Android.App {

partial class ServiceAttribute {

bool _IsolatedProcess;
string _RoundIcon;
ForegroundService _ForegroundServiceType;

static ManifestDocumentElement<ServiceAttribute> mapping = new ManifestDocumentElement<ServiceAttribute> ("service") {
{
Expand All @@ -31,6 +34,11 @@ partial class ServiceAttribute {
"exported",
self => self.Exported,
(self, value) => self.Exported = (bool) value
}, {
"ForegroundServiceType",
"foregroundServiceType",
self => self._ForegroundServiceType,
(self, value) => self._ForegroundServiceType = (ForegroundService) value
}, {
"Icon",
"icon",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -735,5 +735,44 @@ class TestActivity : Activity { }"
Assert.AreEqual (expectedOutput, string.Join (" ", e.Attributes ()));
}
}

[Test]
public void AllServiceAttributeProperties ()
{
const string expectedOutput = "android:directBootAware=\"true\" android:enabled=\"true\" android:exported=\"true\" android:foregroundServiceType=\"connectedDevice\" android:icon=\"@drawable/icon\" android:isolatedProcess=\"true\" android:label=\"TestActivity\" android:name=\"com.contoso.TestActivity\" android:permission=\"com.contoso.permission.TEST_ACTIVITY\" android:process=\"com.contoso.process.testactivity_process\" android:roundIcon=\"@drawable/icon\"";

var proj = new XamarinAndroidApplicationProject ();

proj.Sources.Add (new BuildItem.Source ("TestActivity.cs") {
TextContent = () => @"using Android.App;
using Android.Content.PM;
using Android.Views;
[Service (
DirectBootAware = true,
Enabled = true,
Exported = true,
ForegroundServiceType = ForegroundService.TypeConnectedDevice,
Icon = ""@drawable/icon"",
IsolatedProcess = true,
Label = ""TestActivity"",
Name = ""com.contoso.TestActivity"",
Permission = ""com.contoso.permission.TEST_ACTIVITY"",
Process = ""com.contoso.process.testactivity_process"",
RoundIcon = ""@drawable/icon"")]
class TestService : Service { public override Android.OS.IBinder OnBind (Android.Content.Intent intent) { return null; } }"
});

using (ProjectBuilder builder = CreateDllBuilder (Path.Combine ("temp", TestName))) {
Assert.IsTrue (builder.Build (proj), "Build should have succeeded");

string manifest = builder.Output.GetIntermediaryAsText (Path.Combine ("android", "AndroidManifest.xml"));
var doc = XDocument.Parse (manifest);
var ns = XNamespace.Get ("http://schemas.android.com/apk/res/android");
IEnumerable<XElement> activities = doc.Element ("manifest")?.Element ("application")?.Elements ("service");
XElement e = activities.FirstOrDefault (x => x.Attribute (ns.GetName ("label"))?.Value == "TestActivity");
Assert.IsNotNull (e, "Manifest should contain an activity labeled TestActivity");
Assert.AreEqual (expectedOutput, string.Join (" ", e.Attributes ()));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ string ToAttributeValue (string name, T value, ICustomAttributeProvider provider
{ typeof (ActivityPersistableMode), (value, p, r, v) => ToString ((ActivityPersistableMode) value) },
{ typeof (ConfigChanges), (value, p, r, v) => ToString ((ConfigChanges) value) },
{ typeof (DocumentLaunchMode), (value, p, r, v) => ToString ((DocumentLaunchMode) value) },
{ typeof (ForegroundService), (value, p, r, v) => ToString ((ForegroundService) value) },
{ typeof (LaunchMode), (value, p, r, v) => ToString ((LaunchMode) value) },
{ typeof (Protection), (value, p, r, v) => ToString ((Protection) value) },
{ typeof (ScreenOrientation), (value, p, r, v) => ToString ((ScreenOrientation) value, v) },
Expand Down Expand Up @@ -365,6 +366,19 @@ static string ToString (string value, ICustomAttributeProvider provider, IAssemb
return ToString (typeDef);
}

static string ToString (ForegroundService value)
{
switch (value) {
case ForegroundService.TypeConnectedDevice: return "connectedDevice";
case ForegroundService.TypeDataSync: return "dataSync";
case ForegroundService.TypeLocation: return "location";
case ForegroundService.TypeMediaPlayback: return "mediaPlayback";
case ForegroundService.TypeMediaProjection: return "mediaProjection";
case ForegroundService.TypePhoneCall: return "phoneCall";
default:
throw new ArgumentException ($"Unsupported ForegroundServiceType value '{value}'.", nameof (value));
}
}
IEnumerator<string> IEnumerable<string>.GetEnumerator ()
{
return Mappings.Keys.GetEnumerator ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@
<_MonoAndroidEnum Include="$(AndroidGeneratedClassDirectory)\Android.Content.PM.LaunchMode.cs" />
<_MonoAndroidEnum Include="$(AndroidGeneratedClassDirectory)\Android.Content.PM.ScreenOrientation.cs" />
<_MonoAndroidEnum Include="$(AndroidGeneratedClassDirectory)\Android.Content.PM.ConfigChanges.cs" />
<_MonoAndroidEnum Include="$(AndroidGeneratedClassDirectory)\Android.Content.PM.ForegroundService.cs" />
<_MonoAndroidEnum Include="$(AndroidGeneratedClassDirectory)\Android.Content.PM.UiOptions.cs" />
<_MonoAndroidEnum Include="$(AndroidGeneratedClassDirectory)\Android.Views.SoftInput.cs" />
<_MonoAndroidEnum Include="$(AndroidGeneratedClassDirectory)\Android.Content.PM.Protection.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
namespace Android.App {

[Serializable]
[AttributeUsage (AttributeTargets.Class,
AllowMultiple=false,
[AttributeUsage (AttributeTargets.Class,
AllowMultiple=false,
Inherited=false)]
public sealed partial class ServiceAttribute : Attribute, Java.Interop.IJniNameProviderAttribute {

Expand All @@ -23,6 +23,9 @@ public ServiceAttribute ()
#endif
public bool Enabled {get; set;}
public bool Exported {get; set;}
#if ANDROID_29
public ForegroundService ForegroundServiceType {get; set;}
#endif
[Category ("@drawable;@mipmap")]
public string Icon {get; set;}
#if ANDROID_16
Expand Down