Skip to content
Open
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
45 changes: 40 additions & 5 deletions src/Plugins/Compilers/AndroidPluginCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,6 @@ protected function buildServiceEntry(array $service, Plugin $plugin): string
}
if (isset($service['foregroundServiceType'])) {
$type = $service['foregroundServiceType'];
// Support both array and string formats
if (is_array($type)) {
$type = implode('|', $type);
}
Expand All @@ -557,17 +556,53 @@ protected function buildServiceEntry(array $service, Plugin $plugin): string

$attrString = implode("\n ", $attrs);

// Support both snake_case and kebab-case
// Build nested content (intent-filters and meta-data)
$nestedContent = '';

// Support both snake_case and kebab-case for intent filters
$intentFilters = $service['intent_filters'] ?? $service['intent-filters'] ?? [];
if (! empty($intentFilters)) {
$filters = $this->buildIntentFilters($intentFilters);
if (!empty($intentFilters)) {
$nestedContent .= $this->buildIntentFilters($intentFilters);
}

// Add meta-data support at service level
$metaData = $service['meta_data'] ?? $service['meta-data'] ?? [];
if (!empty($metaData)) {
$nestedContent .= $this->buildComponentMetaData($metaData);
}

return "<service\n {$attrString}>\n{$filters} </service>";
if (!empty($nestedContent)) {
return "<service\n {$attrString}>\n{$nestedContent} </service>";
}

return "<service\n {$attrString} />";
}

/**
* Build meta-data XML entries for use inside manifest components
*/
protected function buildComponentMetaData(array $metaDataEntries): string
{
$xml = '';

foreach ($metaDataEntries as $metaData) {
$name = $metaData['name'];
$value = $metaData['value'] ?? null;
$resource = $metaData['resource'] ?? null;

if ($resource !== null) {
$xml .= " <meta-data android:name=\"{$name}\" android:resource=\"{$resource}\" />\n";
} elseif ($value !== null) {
if (is_bool($value)) {
$value = $value ? 'true' : 'false';
}
$xml .= " <meta-data android:name=\"{$name}\" android:value=\"{$value}\" />\n";
}
}

return $xml;
}

/**
* Build a receiver XML entry
*/
Expand Down
299 changes: 299 additions & 0 deletions tests/Feature/Plugins/AndroidCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,305 @@ public function it_handles_plugins_without_bridge_functions(): void
$this->assertFileExists($generatedPath);
}

/**
* @test
*
* Should add meta-data entries inside a service component using value attribute.
*/
public function it_adds_meta_data_with_value_inside_service(): void
{
$plugin = $this->createTestPlugin([
'android' => [
'permissions' => [],
'dependencies' => [],
'services' => [
[
'name' => 'com.example.MyService',
'exported' => false,
'meta_data' => [
['name' => 'com.example.API_KEY', 'value' => 'abc123'],
],
],
],
],
]);

$this->mockRegistry
->shouldReceive('all')
->andReturn(collect([$plugin]));

$this->compiler->compile();

$manifestPath = $this->testBasePath.'/android/app/src/main/AndroidManifest.xml';
$content = $this->files->get($manifestPath);

$this->assertStringContainsString('<service', $content);
$this->assertStringContainsString('<meta-data android:name="com.example.API_KEY" android:value="abc123" />', $content);
// Service should use closing tag (not self-closing) when it has nested content
$this->assertStringContainsString('</service>', $content);
}

/**
* @test
*
* Should add meta-data entries inside a service component using resource attribute.
*/
public function it_adds_meta_data_with_resource_inside_service(): void
{
$plugin = $this->createTestPlugin([
'android' => [
'permissions' => [],
'dependencies' => [],
'services' => [
[
'name' => 'com.example.MyService',
'exported' => false,
'meta_data' => [
['name' => 'com.example.ICON', 'resource' => '@drawable/icon'],
],
],
],
],
]);

$this->mockRegistry
->shouldReceive('all')
->andReturn(collect([$plugin]));

$this->compiler->compile();

$manifestPath = $this->testBasePath.'/android/app/src/main/AndroidManifest.xml';
$content = $this->files->get($manifestPath);

$this->assertStringContainsString('<meta-data android:name="com.example.ICON" android:resource="@drawable/icon" />', $content);
}

/**
* @test
*
* Should handle boolean values in service meta-data.
*/
public function it_handles_boolean_values_in_service_meta_data(): void
{
$plugin = $this->createTestPlugin([
'android' => [
'permissions' => [],
'dependencies' => [],
'services' => [
[
'name' => 'com.example.MyService',
'exported' => false,
'meta_data' => [
['name' => 'com.example.ENABLED', 'value' => true],
['name' => 'com.example.DEBUG', 'value' => false],
],
],
],
],
]);

$this->mockRegistry
->shouldReceive('all')
->andReturn(collect([$plugin]));

$this->compiler->compile();

$manifestPath = $this->testBasePath.'/android/app/src/main/AndroidManifest.xml';
$content = $this->files->get($manifestPath);

$this->assertStringContainsString('android:value="true"', $content);
$this->assertStringContainsString('android:value="false"', $content);
}

/**
* @test
*
* Should support kebab-case meta-data key in service definitions.
*/
public function it_supports_kebab_case_meta_data_key_in_service(): void
{
$plugin = $this->createTestPlugin([
'android' => [
'permissions' => [],
'dependencies' => [],
'services' => [
[
'name' => 'com.example.MyService',
'exported' => false,
'meta-data' => [
['name' => 'com.example.SETTING', 'value' => 'kebab-works'],
],
],
],
],
]);

$this->mockRegistry
->shouldReceive('all')
->andReturn(collect([$plugin]));

$this->compiler->compile();

$manifestPath = $this->testBasePath.'/android/app/src/main/AndroidManifest.xml';
$content = $this->files->get($manifestPath);

$this->assertStringContainsString('<meta-data android:name="com.example.SETTING" android:value="kebab-works" />', $content);
}

/**
* @test
*
* Should support both intent-filters and meta-data nested inside a service.
*/
public function it_supports_both_intent_filters_and_meta_data_in_service(): void
{
$plugin = $this->createTestPlugin([
'android' => [
'permissions' => [],
'dependencies' => [],
'services' => [
[
'name' => 'com.example.MyService',
'exported' => true,
'intent_filters' => [
[
'action' => 'com.example.ACTION',
'category' => 'android.intent.category.DEFAULT',
],
],
'meta_data' => [
['name' => 'com.example.API_KEY', 'value' => 'abc123'],
],
],
],
],
]);

$this->mockRegistry
->shouldReceive('all')
->andReturn(collect([$plugin]));

$this->compiler->compile();

$manifestPath = $this->testBasePath.'/android/app/src/main/AndroidManifest.xml';
$content = $this->files->get($manifestPath);

$this->assertStringContainsString('<intent-filter>', $content);
$this->assertStringContainsString('<meta-data android:name="com.example.API_KEY" android:value="abc123" />', $content);
$this->assertStringContainsString('</service>', $content);
}

/**
* @test
*
* Service without meta-data or intent-filters should remain self-closing.
*/
public function it_keeps_service_self_closing_without_nested_content(): void
{
$plugin = $this->createTestPlugin([
'android' => [
'permissions' => [],
'dependencies' => [],
'services' => [
[
'name' => 'com.example.SimpleService',
'exported' => false,
],
],
],
]);

$this->mockRegistry
->shouldReceive('all')
->andReturn(collect([$plugin]));

$this->compiler->compile();

$manifestPath = $this->testBasePath.'/android/app/src/main/AndroidManifest.xml';
$content = $this->files->get($manifestPath);

$this->assertStringContainsString('com.example.SimpleService', $content);
// Self-closing service tag
$this->assertMatchesRegularExpression('/<service[^>]*\/>/', $content);
$this->assertStringNotContainsString('</service>', $content);
}

/**
* @test
*
* Resource attribute should take precedence over value in service meta-data.
*/
public function it_prefers_resource_over_value_in_service_meta_data(): void
{
$plugin = $this->createTestPlugin([
'android' => [
'permissions' => [],
'dependencies' => [],
'services' => [
[
'name' => 'com.example.MyService',
'exported' => false,
'meta_data' => [
['name' => 'com.example.ICON', 'resource' => '@drawable/icon', 'value' => 'ignored'],
],
],
],
],
]);

$this->mockRegistry
->shouldReceive('all')
->andReturn(collect([$plugin]));

$this->compiler->compile();

$manifestPath = $this->testBasePath.'/android/app/src/main/AndroidManifest.xml';
$content = $this->files->get($manifestPath);

// Resource should be used, not value
$this->assertStringContainsString('android:resource="@drawable/icon"', $content);
$this->assertStringNotContainsString('android:value="ignored"', $content);
}

/**
* @test
*
* Should handle multiple meta-data entries inside a single service.
*/
public function it_handles_multiple_meta_data_entries_in_service(): void
{
$plugin = $this->createTestPlugin([
'android' => [
'permissions' => [],
'dependencies' => [],
'services' => [
[
'name' => 'com.example.MyService',
'exported' => false,
'meta_data' => [
['name' => 'com.example.KEY_ONE', 'value' => 'first'],
['name' => 'com.example.KEY_TWO', 'value' => 'second'],
['name' => 'com.example.KEY_THREE', 'resource' => '@string/third'],
],
],
],
],
]);

$this->mockRegistry
->shouldReceive('all')
->andReturn(collect([$plugin]));

$this->compiler->compile();

$manifestPath = $this->testBasePath.'/android/app/src/main/AndroidManifest.xml';
$content = $this->files->get($manifestPath);

$this->assertStringContainsString('android:name="com.example.KEY_ONE" android:value="first"', $content);
$this->assertStringContainsString('android:name="com.example.KEY_TWO" android:value="second"', $content);
$this->assertStringContainsString('android:name="com.example.KEY_THREE" android:resource="@string/third"', $content);
}

/**
* Helper method to create a test Plugin instance.
*/
Expand Down