Skip to content

Commit eaa067d

Browse files
authored
Merge pull request #20 from noxasch/feat/android-widget-provider-diff-name
FEAT(android): support provider with different package name
2 parents b434e08 + 62c3b91 commit eaa067d

File tree

21 files changed

+477
-168
lines changed

21 files changed

+477
-168
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ plugin.
3232
in `app_widget/example/integration_test/app_widget_test.dart`
3333

3434
```sh
35+
cd app_widget/example
3536
# this will require a connected device for android
3637
flutter test integration_test/app_widget_test.dart
3738
```
38-

app_widget/.fvm/fvm_config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"flutterSdkVersion": "3.0.5",
2+
"flutterSdkVersion": "3.13.8",
33
"flavors": {}
44
}

app_widget/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.3.0
2+
* feat(android): support widget provider with diff androidPackageName
3+
* test: update widget test
4+
* test(android): update integration test
5+
16
## 0.2.2
27

38
* fix(android): `reloadWidgets` to use initialized `androidPackageName`

app_widget/README.md

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -465,25 +465,27 @@ void main() {
465465
466466
const MethodChannel channel = MethodChannel(AppWidgetPlatform.channel);
467467
final List<MethodCall> log = <MethodCall>[];
468-
469-
channel.setMockMethodCallHandler((methodCall) async {
470-
log.add(methodCall);
471-
switch (methodCall.method) {
472-
case 'configureWidget':
473-
return true;
474-
case 'cancelConfigureWidget':
475-
return true;
476-
case 'getWidgetIds':
477-
return [42];
478-
case 'reloadWidgets':
479-
return true;
480-
case 'updateWidget':
481-
return true;
482-
case 'widgetExist':
483-
return true;
484-
default:
485-
}
486-
});
468+
469+
470+
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
471+
.setMockMethodCallHandler(channel, (methodCall) async {
472+
log.add(methodCall);
473+
switch (methodCall.method) {
474+
case 'getPlatformVersion':
475+
return '42';
476+
case 'configureWidget':
477+
return true;
478+
case 'cancelConfigureWidget':
479+
return true;
480+
case 'getWidgetIds':
481+
return [];
482+
case 'reloadWidgets':
483+
return true;
484+
case 'widgetExist':
485+
return true;
486+
default:
487+
return null;
488+
}
487489
});
488490
489491
setUp(() {

app_widget/example/android/app/src/main/AndroidManifest.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@
3737
<meta-data android:name="android.appwidget.provider"
3838
android:resource="@xml/app_widget_example_info" />
3939
</receiver>
40+
<receiver android:exported="true" android:name="tech.noxasch.diff_name.AppWidgetExampleDiffProvider">
41+
<intent-filter>
42+
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
43+
<action android:name="android.appwidget.action.APPWIDGET_DELETED"/>
44+
</intent-filter>
45+
<meta-data android:name="android.appwidget.provider"
46+
android:resource="@xml/app_widget_example_info" />
47+
</receiver>
4048
<!-- Don't delete the meta-data below.
4149
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
4250
<meta-data
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package tech.noxasch.diff_name
2+
3+
import android.appwidget.AppWidgetManager
4+
import android.appwidget.AppWidgetProvider
5+
import android.content.Context
6+
import android.content.Intent
7+
import android.util.Log
8+
import android.widget.RemoteViews
9+
import androidx.core.content.ContextCompat.startActivity
10+
import androidx.core.view.accessibility.AccessibilityEventCompat.setAction
11+
import io.flutter.embedding.engine.FlutterEngine
12+
import io.flutter.plugin.common.MethodChannel
13+
import tech.noxasch.app_widget.AppWidgetPlugin
14+
15+
class AppWidgetExampleDiffProvider : AppWidgetProvider() {
16+
override fun onUpdate(
17+
context: Context?,
18+
appWidgetManager: AppWidgetManager?,
19+
appWidgetIds: IntArray?
20+
) {
21+
super.onUpdate(context, appWidgetManager, appWidgetIds)
22+
Log.d("APP_WIDGET_PLUGIN", "ON_UPDATE")
23+
if (appWidgetIds != null) {
24+
for (widgetId in appWidgetIds) {
25+
Log.d("APP_WIDGET_PLUGIN", "WIDGET_ID: $widgetId")
26+
}
27+
}
28+
29+
// check if widgetId store sharedPreferences
30+
// fetch data from sharedPreferences
31+
// then update
32+
// for (widgetId in appWidgetIds!!) {
33+
// val remoteViews = RemoteViews(context!!.packageName, R.layout.example_layout).apply() {
34+
// setTextViewText(R.id.widget_title, "Widget Title")
35+
// setTextViewText(R.id.widget_message, "This is my message")
36+
// }
37+
//
38+
// appWidgetManager!!.partiallyUpdateAppWidget(widgetId, remoteViews)
39+
// }
40+
}
41+
}

app_widget/example/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ subprojects {
2626
project.evaluationDependsOn(':app')
2727
}
2828

29-
task clean(type: Delete) {
29+
tasks.register("clean", Delete) {
3030
delete rootProject.buildDir
3131
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import 'package:app_widget/app_widget.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:integration_test/integration_test.dart';
4+
5+
// there is no way to test callback as it need to interact with actual widgets
6+
void main() {
7+
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
8+
9+
group('with androidPackageName', () {
10+
final AppWidgetPlugin appWidgetPlugin = AppWidgetPlugin(
11+
androidPackageName: 'tech.noxasch.app_widget_example',
12+
);
13+
14+
testWidgets('configureWidget', (tester) async {
15+
final res = await appWidgetPlugin.configureWidget(
16+
androidPackageName: 'tech.noxasch.diff_name',
17+
widgetId: 1,
18+
widgetLayout: 'example_layout',
19+
payload: '{"itemId": 1, "stringUid": "uid"}',
20+
url: 'https://google.come',
21+
);
22+
expect(res, isTrue);
23+
});
24+
25+
testWidgets('cancelConfigureWidget', (tester) async {
26+
final res = await appWidgetPlugin.cancelConfigureWidget();
27+
28+
expect(res, isTrue);
29+
});
30+
31+
testWidgets('updateWidget', (tester) async {
32+
final res = await appWidgetPlugin.updateWidget(
33+
androidPackageName: 'tech.noxasch.diff_name',
34+
widgetId: 1,
35+
widgetLayout: 'example_layout',
36+
payload: '{"itemId": 1, "stringUid": "uid"}',
37+
url: 'https://google.come',
38+
textViews: {'widget_title': 'my title'},
39+
);
40+
41+
expect(res, isTrue);
42+
});
43+
44+
testWidgets('getWidgetIds', (tester) async {
45+
final res = await appWidgetPlugin.getWidgetIds(
46+
androidPackageName: 'tech.noxasch.diff_name',
47+
androidProviderName: 'AppWidgetExampleDiffProvider',
48+
);
49+
50+
expect(res, []);
51+
});
52+
53+
testWidgets('reloadWidgets', (tester) async {
54+
final res = await appWidgetPlugin.reloadWidgets(
55+
androidPackageName: 'tech.noxasch.diff_name',
56+
androidProviderName: 'AppWidgetExampleDiffProvider',
57+
);
58+
59+
expect(res, isTrue);
60+
});
61+
62+
testWidgets('widgetExist', (tester) async {
63+
final res = await appWidgetPlugin.widgetExist(12);
64+
65+
expect(res, isFalse);
66+
});
67+
});
68+
}

0 commit comments

Comments
 (0)