Skip to content

Commit

Permalink
Merge pull request #20 from noxasch/feat/android-widget-provider-diff…
Browse files Browse the repository at this point in the history
…-name

FEAT(android): support provider with different package name
  • Loading branch information
noxasch authored Dec 13, 2023
2 parents b434e08 + 62c3b91 commit eaa067d
Show file tree
Hide file tree
Showing 21 changed files with 477 additions and 168 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ plugin.
in `app_widget/example/integration_test/app_widget_test.dart`

```sh
cd app_widget/example
# this will require a connected device for android
flutter test integration_test/app_widget_test.dart
```

2 changes: 1 addition & 1 deletion app_widget/.fvm/fvm_config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"flutterSdkVersion": "3.0.5",
"flutterSdkVersion": "3.13.8",
"flavors": {}
}
5 changes: 5 additions & 0 deletions app_widget/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.3.0
* feat(android): support widget provider with diff androidPackageName
* test: update widget test
* test(android): update integration test

## 0.2.2

* fix(android): `reloadWidgets` to use initialized `androidPackageName`
Expand Down
40 changes: 21 additions & 19 deletions app_widget/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,25 +465,27 @@ void main() {
const MethodChannel channel = MethodChannel(AppWidgetPlatform.channel);
final List<MethodCall> log = <MethodCall>[];
channel.setMockMethodCallHandler((methodCall) async {
log.add(methodCall);
switch (methodCall.method) {
case 'configureWidget':
return true;
case 'cancelConfigureWidget':
return true;
case 'getWidgetIds':
return [42];
case 'reloadWidgets':
return true;
case 'updateWidget':
return true;
case 'widgetExist':
return true;
default:
}
});
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (methodCall) async {
log.add(methodCall);
switch (methodCall.method) {
case 'getPlatformVersion':
return '42';
case 'configureWidget':
return true;
case 'cancelConfigureWidget':
return true;
case 'getWidgetIds':
return [];
case 'reloadWidgets':
return true;
case 'widgetExist':
return true;
default:
return null;
}
});
setUp(() {
Expand Down
8 changes: 8 additions & 0 deletions app_widget/example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/app_widget_example_info" />
</receiver>
<receiver android:exported="true" android:name="tech.noxasch.diff_name.AppWidgetExampleDiffProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<action android:name="android.appwidget.action.APPWIDGET_DELETED"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/app_widget_example_info" />
</receiver>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package tech.noxasch.diff_name

import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.RemoteViews
import androidx.core.content.ContextCompat.startActivity
import androidx.core.view.accessibility.AccessibilityEventCompat.setAction
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import tech.noxasch.app_widget.AppWidgetPlugin

class AppWidgetExampleDiffProvider : AppWidgetProvider() {
override fun onUpdate(
context: Context?,
appWidgetManager: AppWidgetManager?,
appWidgetIds: IntArray?
) {
super.onUpdate(context, appWidgetManager, appWidgetIds)
Log.d("APP_WIDGET_PLUGIN", "ON_UPDATE")
if (appWidgetIds != null) {
for (widgetId in appWidgetIds) {
Log.d("APP_WIDGET_PLUGIN", "WIDGET_ID: $widgetId")
}
}

// check if widgetId store sharedPreferences
// fetch data from sharedPreferences
// then update
// for (widgetId in appWidgetIds!!) {
// val remoteViews = RemoteViews(context!!.packageName, R.layout.example_layout).apply() {
// setTextViewText(R.id.widget_title, "Widget Title")
// setTextViewText(R.id.widget_message, "This is my message")
// }
//
// appWidgetManager!!.partiallyUpdateAppWidget(widgetId, remoteViews)
// }
}
}
2 changes: 1 addition & 1 deletion app_widget/example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
68 changes: 68 additions & 0 deletions app_widget/example/integration_test/android_test_diff_package.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import 'package:app_widget/app_widget.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';

// there is no way to test callback as it need to interact with actual widgets
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

group('with androidPackageName', () {
final AppWidgetPlugin appWidgetPlugin = AppWidgetPlugin(
androidPackageName: 'tech.noxasch.app_widget_example',
);

testWidgets('configureWidget', (tester) async {
final res = await appWidgetPlugin.configureWidget(
androidPackageName: 'tech.noxasch.diff_name',
widgetId: 1,
widgetLayout: 'example_layout',
payload: '{"itemId": 1, "stringUid": "uid"}',
url: 'https://google.come',
);
expect(res, isTrue);
});

testWidgets('cancelConfigureWidget', (tester) async {
final res = await appWidgetPlugin.cancelConfigureWidget();

expect(res, isTrue);
});

testWidgets('updateWidget', (tester) async {
final res = await appWidgetPlugin.updateWidget(
androidPackageName: 'tech.noxasch.diff_name',
widgetId: 1,
widgetLayout: 'example_layout',
payload: '{"itemId": 1, "stringUid": "uid"}',
url: 'https://google.come',
textViews: {'widget_title': 'my title'},
);

expect(res, isTrue);
});

testWidgets('getWidgetIds', (tester) async {
final res = await appWidgetPlugin.getWidgetIds(
androidPackageName: 'tech.noxasch.diff_name',
androidProviderName: 'AppWidgetExampleDiffProvider',
);

expect(res, []);
});

testWidgets('reloadWidgets', (tester) async {
final res = await appWidgetPlugin.reloadWidgets(
androidPackageName: 'tech.noxasch.diff_name',
androidProviderName: 'AppWidgetExampleDiffProvider',
);

expect(res, isTrue);
});

testWidgets('widgetExist', (tester) async {
final res = await appWidgetPlugin.widgetExist(12);

expect(res, isFalse);
});
});
}
Loading

0 comments on commit eaa067d

Please sign in to comment.