Skip to content

Commit

Permalink
feat(android): forward layout id and name
Browse files Browse the repository at this point in the history
  • Loading branch information
noxasch committed Apr 25, 2024
1 parent eaa067d commit fbe6d2b
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 21 deletions.
17 changes: 13 additions & 4 deletions app_widget/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class _MyAppState extends State<MyApp> {
late final AppWidgetPlugin _appWidgetPlugin;
late final TextEditingController _controller;
int? _widgetId;
int? _layoutId;

@override
void initState() {
Expand All @@ -44,8 +45,11 @@ class _MyAppState extends State<MyApp> {
super.dispose();
}

void onConfigureWidget(int widgetId) {
setState(() => _widgetId = widgetId);
void onConfigureWidget(int widgetId, int layoutId) {
setState(() {
_widgetId = widgetId;
_layoutId = layoutId;
});
// do something
}

Expand Down Expand Up @@ -73,7 +77,9 @@ class _MyAppState extends State<MyApp> {
height: 10,
),
ConfigureButton(
widgetId: _widgetId, appWidgetPlugin: _appWidgetPlugin),
widgetId: _widgetId,
layoutId: _layoutId,
appWidgetPlugin: _appWidgetPlugin),
const SizedBox(
height: 10,
),
Expand Down Expand Up @@ -240,12 +246,15 @@ class ConfigureButton extends StatelessWidget {
const ConfigureButton({
Key? key,
required int? widgetId,
required int? layoutId,
required AppWidgetPlugin appWidgetPlugin,
}) : _widgetId = widgetId,
_layoutId = layoutId,
_appWidgetPlugin = appWidgetPlugin,
super(key: key);

final int? _widgetId;
final int? _layoutId;
final AppWidgetPlugin _appWidgetPlugin;

@override
Expand All @@ -259,7 +268,7 @@ class ConfigureButton extends StatelessWidget {
// send configure
await _appWidgetPlugin.configureWidget(
widgetId: _widgetId!,
widgetLayout: 'example_layout',
layoutId: _layoutId!,
textViews: {
'widget_title': 'App Widget',
'widget_message': 'Configured in flutter'
Expand Down
12 changes: 7 additions & 5 deletions app_widget/lib/src/app_widget_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AppWidgetPlugin {
String? androidPackageName,

/// callback function when the widget is first created
void Function(int widgetId)? onConfigureWidget,
void Function(int widgetId, int layoutId)? onConfigureWidget,
void Function(String? payload)? onClickWidget,
}) {
if (instance != null) return instance!;
Expand All @@ -39,7 +39,7 @@ class AppWidgetPlugin {

AppWidgetPlugin._({
required String? androidPackageName,
required void Function(int widgetId)? onConfigureWidget,
required void Function(int widgetId, int layoutId)? onConfigureWidget,
required void Function(String? payload)? onClickWidget,
}) : _onConfigureWidget = onConfigureWidget,
_onClickWidget = onClickWidget,
Expand All @@ -62,7 +62,7 @@ class AppWidgetPlugin {
final String? _androidPackageName;

/// callback function when the widget is first created
final void Function(int widgetId)? _onConfigureWidget;
final void Function(int widgetId, int layoutId)? _onConfigureWidget;

/// payload keys:
/// - itemId
Expand Down Expand Up @@ -100,7 +100,7 @@ class AppWidgetPlugin {
///
Future<bool?> configureWidget({
int? widgetId,
String? widgetLayout,
int? layoutId,
Map<String, String>? textViews = const {},
String? payload,
String? url,
Expand All @@ -109,7 +109,7 @@ class AppWidgetPlugin {
return AppWidgetPlatform.instance.configureWidget(
androidPackageName: androidPackageName ?? _androidPackageName,
widgetId: widgetId,
widgetLayout: widgetLayout,
layoutId: layoutId,
textViews: textViews,
payload: payload,
url: url,
Expand Down Expand Up @@ -177,6 +177,7 @@ class AppWidgetPlugin {
///
Future<bool?> updateWidget({
int? widgetId,
int? layoutId,
String? widgetLayout,
Map<String, String>? textViews = const {},
String? payload,
Expand All @@ -186,6 +187,7 @@ class AppWidgetPlugin {
return AppWidgetPlatform.instance.updateWidget(
androidPackageName: androidPackageName ?? _androidPackageName,
widgetId: widgetId,
layoutId: layoutId,
widgetLayout: widgetLayout,
textViews: textViews,
payload: payload,
Expand Down
2 changes: 1 addition & 1 deletion app_widget/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: app_widget
description: Flutter plugin to manage app widget / home screen widget from within flutter app.
version: 0.3.0
version: 0.4.0
homepage: https://noxasch.tech/
repository: https://github.com/noxasch/flutter_app_widget/tree/master/app_widget
issue_tracker: https://github.com/noxasch/flutter_app_widget/issues
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ class AppWidgetMethodCallHandler(private val context: Context, )

fun handleConfigureIntent(intent: Intent): Boolean {
val widgetId = intent.extras!!.getInt("widgetId")
channel!!.invokeMethod(AppWidgetPlugin.ON_CONFIGURE_WIDGET_CALLBACK, mapOf("widgetId" to widgetId))
val layoutId = intent.extras!!.getInt("layoutId")
val layoutName = intent.extras!!.getString("layoutName")
channel!!.invokeMethod(AppWidgetPlugin.ON_CONFIGURE_WIDGET_CALLBACK,
mapOf(
"widgetId" to widgetId,
"layoutId" to layoutId,
"layoutName" to layoutName
)
)
return true
}

Expand Down Expand Up @@ -108,10 +116,8 @@ class AppWidgetMethodCallHandler(private val context: Context, )
?: context.packageName
val widgetId = call.argument<Int>("widgetId")
?: return result.error("-1", "widgetId is required!", null)
val widgetLayout = call.argument<String>("widgetLayout")
?: return result.error("-1", "widgetLayout is required!", null)

val widgetLayoutId: Int = context.resources.getIdentifier(widgetLayout, "layout", context.packageName)
val layoutId = call.argument<Int>("layoutId")
?: return result.error("-1", "layoutId is required!", null)
val payload = call.argument<String>("payload")
val url = call.argument<String>("url")
val activityClass = Class.forName("${context.packageName}.MainActivity")
Expand All @@ -120,7 +126,7 @@ class AppWidgetMethodCallHandler(private val context: Context, )
val textViewsMap = call.argument<Map<String, String>>("textViews")

if (textViewsMap != null) {
val views : RemoteViews = RemoteViews(context.packageName, widgetLayoutId).apply {
val views : RemoteViews = RemoteViews(context.packageName, layoutId).apply {
for ((key, value) in textViewsMap) {
val textViewId: Int =
context.resources.getIdentifier(key, "id", context.packageName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,16 @@ class AppWidgetPlugin: FlutterPlugin, ActivityAware,
val widgetId: Int = extras?.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID) ?: return
if (widgetId == 0) return

val widgetManager = AppWidgetManager.getInstance(context)
val appWidgetInfo = widgetManager.getAppWidgetInfo(widgetId)
val layoutId = appWidgetInfo.initialLayout
val layoutName = context.resources.getResourceName(layoutId)

val configIntent = Intent(context, context.javaClass)
configIntent.action = CONFIGURE_WIDGET_ACTION_CALLBACK
configIntent.putExtra("widgetId", widgetId)
configIntent.putExtra("layoutId", layoutId)
configIntent.putExtra("layoutName", layoutName)
context.startActivity(configIntent)
}
}
Expand Down
13 changes: 9 additions & 4 deletions app_widget_android/lib/src/app_widget_android_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ const MethodChannel _methodChannel = MethodChannel(AppWidgetPlatform.channel);

class AppWidgetAndroidPlugin extends AppWidgetAndroid {
AppWidgetAndroidPlugin({
void Function(int widgetId)? onConfigureWidget,
void Function(int widgetId, int layoutId)? onConfigureWidget,
void Function(String? payload)? onClickWidget,
}) : _onConfigureWidget = onConfigureWidget,
_onClickWidget = onClickWidget {
_methodChannel.setMethodCallHandler(handleMethod);
}

final void Function(int widgetId)? _onConfigureWidget;
final void Function(int widgetId, int layoutId)? _onConfigureWidget;
final void Function(String? payload)? _onClickWidget;

Future<dynamic> handleMethod(MethodCall call) async {
switch (call.method) {
case onConfigureWidgetCallback:
final widgetId = call.arguments['widgetId'] as int;
return _onConfigureWidget?.call(widgetId);
final layoutId = call.arguments['layoutId'] as int;
return _onConfigureWidget?.call(widgetId, layoutId);
case onClickWidgetCallback:
return _onClickWidget?.call(call.arguments['payload'] as String);
default:
Expand All @@ -42,6 +43,7 @@ class AppWidgetAndroidPlugin extends AppWidgetAndroid {
Future<bool?> configureWidget({
String? androidPackageName,
int? widgetId,
int? layoutId,
String? widgetLayout,
Map<String, String>? textViews = const {},
String? payload,
Expand All @@ -52,7 +54,8 @@ class AppWidgetAndroidPlugin extends AppWidgetAndroid {

return _methodChannel.invokeMethod<bool>('configureWidget', {
'androidPackageName': androidPackageName,
'widgetLayout': widgetLayout,
// 'widgetLayout': widgetLayout,
'layoutId': layoutId,
'widgetId': widgetId,
'textViews': textViews,
'payload': payload,
Expand Down Expand Up @@ -106,6 +109,7 @@ class AppWidgetAndroidPlugin extends AppWidgetAndroid {
Future<bool?> updateWidget({
String? androidPackageName,
int? widgetId,
int? layoutId,
String? widgetLayout,
Map<String, String>? textViews = const {},
String? payload,
Expand All @@ -120,6 +124,7 @@ class AppWidgetAndroidPlugin extends AppWidgetAndroid {
},
'widgetLayout': widgetLayout,
'widgetId': widgetId,
'layoutId': layoutId,
'textViews': textViews,
'payload': payload,
'url': url,
Expand Down
2 changes: 2 additions & 0 deletions app_widget_android/test/app_widget_android_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class MockAppWidgetAndroidPlatform
Future<bool?> configureWidget({
String? androidPackageName,
int? widgetId,
int? layoutId,
String? widgetLayout,
String? widgetContainerName,
Map<String, String>? textViews,
Expand All @@ -97,6 +98,7 @@ class MockAppWidgetAndroidPlatform
Future<bool?> updateWidget({
String? androidPackageName,
int? widgetId,
int? layoutId,
String? widgetLayout,
Map<String, String>? textViews,
String? payload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ abstract class AppWidgetPlatform extends PlatformInterface {
Future<bool?> configureWidget({
String? androidPackageName,
int? widgetId,
int? layoutId,
String? widgetLayout,
Map<String, String>? textViews,
String? payload,
Expand All @@ -52,6 +53,7 @@ abstract class AppWidgetPlatform extends PlatformInterface {
Future<bool?> updateWidget({
String? androidPackageName,
int? widgetId,
int? layoutId,
String? widgetLayout,
Map<String, String>? textViews,
String? payload,
Expand Down
2 changes: 1 addition & 1 deletion app_widget_platform_interface/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: app_widget_platform_interface
description: Common platform interface for app_widget plugin.
version: 0.3.1
version: 0.4.0
homepage: https://noxasch.tech/
repository: https://github.com/noxasch/flutter_app_widget/tree/master/app_widget_platform_interface
issue_tracker: https://github.com/noxasch/flutter_app_widget/issues
Expand Down

0 comments on commit fbe6d2b

Please sign in to comment.