diff --git a/app_widget/example/lib/main.dart b/app_widget/example/lib/main.dart index 80b4c0f..ba0e31e 100644 --- a/app_widget/example/lib/main.dart +++ b/app_widget/example/lib/main.dart @@ -26,6 +26,7 @@ class _MyAppState extends State { late final AppWidgetPlugin _appWidgetPlugin; late final TextEditingController _controller; int? _widgetId; + int? _layoutId; @override void initState() { @@ -44,8 +45,11 @@ class _MyAppState extends State { super.dispose(); } - void onConfigureWidget(int widgetId) { - setState(() => _widgetId = widgetId); + void onConfigureWidget(int widgetId, int layoutId) { + setState(() { + _widgetId = widgetId; + _layoutId = layoutId; + }); // do something } @@ -73,7 +77,9 @@ class _MyAppState extends State { height: 10, ), ConfigureButton( - widgetId: _widgetId, appWidgetPlugin: _appWidgetPlugin), + widgetId: _widgetId, + layoutId: _layoutId, + appWidgetPlugin: _appWidgetPlugin), const SizedBox( height: 10, ), @@ -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 @@ -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' diff --git a/app_widget/lib/src/app_widget_plugin.dart b/app_widget/lib/src/app_widget_plugin.dart index ee9065c..9b2f975 100644 --- a/app_widget/lib/src/app_widget_plugin.dart +++ b/app_widget/lib/src/app_widget_plugin.dart @@ -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!; @@ -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, @@ -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 @@ -100,7 +100,7 @@ class AppWidgetPlugin { /// Future configureWidget({ int? widgetId, - String? widgetLayout, + int? layoutId, Map? textViews = const {}, String? payload, String? url, @@ -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, @@ -177,6 +177,7 @@ class AppWidgetPlugin { /// Future updateWidget({ int? widgetId, + int? layoutId, String? widgetLayout, Map? textViews = const {}, String? payload, @@ -186,6 +187,7 @@ class AppWidgetPlugin { return AppWidgetPlatform.instance.updateWidget( androidPackageName: androidPackageName ?? _androidPackageName, widgetId: widgetId, + layoutId: layoutId, widgetLayout: widgetLayout, textViews: textViews, payload: payload, diff --git a/app_widget/pubspec.yaml b/app_widget/pubspec.yaml index a3b6399..b17edb5 100644 --- a/app_widget/pubspec.yaml +++ b/app_widget/pubspec.yaml @@ -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 diff --git a/app_widget_android/android/src/main/kotlin/tech/noxasch/app_widget/AppWidgetMethodCallHandler.kt b/app_widget_android/android/src/main/kotlin/tech/noxasch/app_widget/AppWidgetMethodCallHandler.kt index 4aef26a..7a664af 100644 --- a/app_widget_android/android/src/main/kotlin/tech/noxasch/app_widget/AppWidgetMethodCallHandler.kt +++ b/app_widget_android/android/src/main/kotlin/tech/noxasch/app_widget/AppWidgetMethodCallHandler.kt @@ -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 } @@ -108,10 +116,8 @@ class AppWidgetMethodCallHandler(private val context: Context, ) ?: context.packageName val widgetId = call.argument("widgetId") ?: return result.error("-1", "widgetId is required!", null) - val widgetLayout = call.argument("widgetLayout") - ?: return result.error("-1", "widgetLayout is required!", null) - - val widgetLayoutId: Int = context.resources.getIdentifier(widgetLayout, "layout", context.packageName) + val layoutId = call.argument("layoutId") + ?: return result.error("-1", "layoutId is required!", null) val payload = call.argument("payload") val url = call.argument("url") val activityClass = Class.forName("${context.packageName}.MainActivity") @@ -120,7 +126,7 @@ class AppWidgetMethodCallHandler(private val context: Context, ) val textViewsMap = call.argument>("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) diff --git a/app_widget_android/android/src/main/kotlin/tech/noxasch/app_widget/AppWidgetPlugin.kt b/app_widget_android/android/src/main/kotlin/tech/noxasch/app_widget/AppWidgetPlugin.kt index 637603c..e1f10ff 100644 --- a/app_widget_android/android/src/main/kotlin/tech/noxasch/app_widget/AppWidgetPlugin.kt +++ b/app_widget_android/android/src/main/kotlin/tech/noxasch/app_widget/AppWidgetPlugin.kt @@ -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) } } diff --git a/app_widget_android/lib/src/app_widget_android_plugin.dart b/app_widget_android/lib/src/app_widget_android_plugin.dart index a5f3dc1..a775e25 100644 --- a/app_widget_android/lib/src/app_widget_android_plugin.dart +++ b/app_widget_android/lib/src/app_widget_android_plugin.dart @@ -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 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: @@ -42,6 +43,7 @@ class AppWidgetAndroidPlugin extends AppWidgetAndroid { Future configureWidget({ String? androidPackageName, int? widgetId, + int? layoutId, String? widgetLayout, Map? textViews = const {}, String? payload, @@ -52,7 +54,8 @@ class AppWidgetAndroidPlugin extends AppWidgetAndroid { return _methodChannel.invokeMethod('configureWidget', { 'androidPackageName': androidPackageName, - 'widgetLayout': widgetLayout, + // 'widgetLayout': widgetLayout, + 'layoutId': layoutId, 'widgetId': widgetId, 'textViews': textViews, 'payload': payload, @@ -106,6 +109,7 @@ class AppWidgetAndroidPlugin extends AppWidgetAndroid { Future updateWidget({ String? androidPackageName, int? widgetId, + int? layoutId, String? widgetLayout, Map? textViews = const {}, String? payload, @@ -120,6 +124,7 @@ class AppWidgetAndroidPlugin extends AppWidgetAndroid { }, 'widgetLayout': widgetLayout, 'widgetId': widgetId, + 'layoutId': layoutId, 'textViews': textViews, 'payload': payload, 'url': url, diff --git a/app_widget_android/test/app_widget_android_test.dart b/app_widget_android/test/app_widget_android_test.dart index 62c2256..8fcc219 100644 --- a/app_widget_android/test/app_widget_android_test.dart +++ b/app_widget_android/test/app_widget_android_test.dart @@ -76,6 +76,7 @@ class MockAppWidgetAndroidPlatform Future configureWidget({ String? androidPackageName, int? widgetId, + int? layoutId, String? widgetLayout, String? widgetContainerName, Map? textViews, @@ -97,6 +98,7 @@ class MockAppWidgetAndroidPlatform Future updateWidget({ String? androidPackageName, int? widgetId, + int? layoutId, String? widgetLayout, Map? textViews, String? payload, diff --git a/app_widget_platform_interface/lib/src/app_widget_platform.dart b/app_widget_platform_interface/lib/src/app_widget_platform.dart index 255606f..4accda8 100644 --- a/app_widget_platform_interface/lib/src/app_widget_platform.dart +++ b/app_widget_platform_interface/lib/src/app_widget_platform.dart @@ -34,6 +34,7 @@ abstract class AppWidgetPlatform extends PlatformInterface { Future configureWidget({ String? androidPackageName, int? widgetId, + int? layoutId, String? widgetLayout, Map? textViews, String? payload, @@ -52,6 +53,7 @@ abstract class AppWidgetPlatform extends PlatformInterface { Future updateWidget({ String? androidPackageName, int? widgetId, + int? layoutId, String? widgetLayout, Map? textViews, String? payload, diff --git a/app_widget_platform_interface/pubspec.yaml b/app_widget_platform_interface/pubspec.yaml index b101be4..63f2243 100644 --- a/app_widget_platform_interface/pubspec.yaml +++ b/app_widget_platform_interface/pubspec.yaml @@ -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