Skip to content

Commit 9ee9e56

Browse files
committed
feat: add configuration for widget provider package name
1 parent 36b2b90 commit 9ee9e56

3 files changed

Lines changed: 62 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- `packageName` option for defining custom package name for widget
13+
1014
## [0.16.1] - 2025-04-13
1115

1216
### Fixed

app.plugin.ts

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ export interface Widget {
5252
* Minimum is 1.800.000 (30 minutes == 30 * 60 * 1000).
5353
*/
5454
updatePeriodMillis?: number;
55+
/**
56+
* Custom Java package name for the widget's AppWidgetProvider.
57+
*
58+
* It must start with the actual apps package name.
59+
*
60+
* Can be used if introducing react-native-android-widget in existing app
61+
* that already has widgets in a different package that the default one.
62+
*
63+
* Must not end with "."
64+
*
65+
* @default "<app-package-name>.widget"
66+
*/
67+
packageName?: string;
5568
}
5669

5770
export interface WithAndroidWidgetsParams {
@@ -213,15 +226,17 @@ function withWidgetReceiver(
213226
): void {
214227
mainApplication.receiver = mainApplication.receiver ?? [];
215228

229+
const { receiverName } = getReceiverInfo(config, widget);
230+
216231
const alreadyAdded = mainApplication.receiver.some(
217-
(service) => service.$['android:name'] === `.widget.${widget.name}`
232+
(service) => service.$['android:name'] === receiverName
218233
);
219234

220235
if (alreadyAdded) return;
221236

222237
mainApplication.receiver?.push({
223238
'$': {
224-
'android:name': `.widget.${widget.name}`,
239+
'android:name': receiverName,
225240
'android:exported': 'false',
226241
'android:label': `${widget.label ?? widget.name}`,
227242
} as any,
@@ -336,16 +351,16 @@ function withWidgetProviderClass(
336351
projectPaths: ProjectPaths,
337352
widget: Widget
338353
) {
354+
const { receiverPackage } = getReceiverInfo(config, widget);
355+
339356
const widgetPackagePath = path.join(
340357
projectPaths.platformProjectRoot,
341-
'android/app/src/main/java/' +
342-
config.android?.package?.split('.').join('/') +
343-
'/widget'
358+
'android/app/src/main/java/' + receiverPackage.split('.').join('/')
344359
);
345360

346361
const javaFilePath = path.join(widgetPackagePath, `/${widget.name}.java`);
347362

348-
const data = `package ${config.android?.package}.widget;
363+
const data = `package ${receiverPackage};
349364
350365
import com.reactnativeandroidwidget.RNWidgetProvider;
351366
@@ -454,3 +469,27 @@ function withWidgetPreview(projectPaths: ProjectPaths, widget: Widget) {
454469
fs.copyFileSync(previewAssetPath, output);
455470
}
456471
}
472+
473+
function getReceiverInfo(config: ExpoConfig, widget: Widget) {
474+
if (!config.android?.package) {
475+
throw new Error('Must provide a package for the app');
476+
}
477+
478+
if (
479+
widget.packageName &&
480+
!widget.packageName.startsWith(config.android.package)
481+
) {
482+
throw new Error(
483+
`Package name for widget with name ${widget.name} must start with ${config.android.package}`
484+
);
485+
}
486+
487+
const receiverPackage =
488+
widget.packageName ?? `${config.android.package}.widget`;
489+
const receiverName = `${receiverPackage.replace(
490+
config.android.package,
491+
''
492+
)}.${widget.name}`;
493+
494+
return { receiverPackage, receiverName };
495+
}

src/config-plugin.type.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ export interface Widget {
3939
* Minimum is 1.800.000 (30 minutes == 30 * 60 * 1000).
4040
*/
4141
updatePeriodMillis?: number;
42+
/**
43+
* Custom Java package name for the widget's AppWidgetProvider.
44+
*
45+
* It must start with the actual apps package name.
46+
*
47+
* Can be used if introducing react-native-android-widget in existing app
48+
* that already has widgets in a different package that the default one.
49+
*
50+
* Must not end with "."
51+
*
52+
* @default "<app-package-name>.widget"
53+
*/
54+
packageName?: string;
4255
}
4356

4457
export interface WithAndroidWidgetsParams {

0 commit comments

Comments
 (0)