Skip to content

Commit a4a96a1

Browse files
rajveermalviyagnprice
authored andcommitted
notif: Use PackageInfo to generate sound resource file URL
1 parent 40046c8 commit a4a96a1

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

lib/notifications/display.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,21 @@ class NotificationChannelManager {
6262
/// `android.resource://com.zulip.flutter/raw/chime3`
6363
///
6464
/// Based on: https://stackoverflow.com/a/38340580
65-
static Uri _resourceUrlFromName({
65+
static Future<String> _resourceUrlFromName({
6666
required String resourceTypeName,
6767
required String resourceEntryName,
68-
}) {
69-
const packageName = 'com.zulip.flutter'; // TODO(#407)
68+
}) async {
69+
final packageInfo = await ZulipBinding.instance.packageInfo;
7070

7171
// URL scheme for Android resource url.
7272
// See: https://developer.android.com/reference/android/content/ContentResolver#SCHEME_ANDROID_RESOURCE
7373
const schemeAndroidResource = 'android.resource';
7474

7575
return Uri(
7676
scheme: schemeAndroidResource,
77-
host: packageName,
77+
host: packageInfo!.packageName,
7878
pathSegments: <String>[resourceTypeName, resourceEntryName],
79-
);
79+
).toString();
8080
}
8181

8282
/// Prepare our notification sounds; return a URL for our default sound.
@@ -87,9 +87,9 @@ class NotificationChannelManager {
8787
/// Returns a URL for our default notification sound: either in shared storage
8888
/// if we successfully copied it there, or else as our internal resource file.
8989
static Future<String> _ensureInitNotificationSounds() async {
90-
String defaultSoundUrl = _resourceUrlFromName(
90+
String defaultSoundUrl = await _resourceUrlFromName(
9191
resourceTypeName: 'raw',
92-
resourceEntryName: kDefaultNotificationSound.resourceName).toString();
92+
resourceEntryName: kDefaultNotificationSound.resourceName);
9393

9494
final shouldUseResourceFile = switch (await ZulipBinding.instance.deviceInfo) {
9595
// Before Android 10 Q, we don't attempt to put the sounds in shared media storage.

test/notifications/display_test.dart

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ void main() {
210210
NotificationChannelManager.kDefaultNotificationSound.resourceName;
211211
String fakeStoredUrl(String resourceName) =>
212212
testBinding.androidNotificationHost.fakeStoredNotificationSoundUrl(resourceName);
213-
String fakeResourceUrl(String resourceName) =>
214-
'android.resource://com.zulip.flutter/raw/$resourceName';
213+
String fakeResourceUrl({required String resourceName, String? packageName}) =>
214+
'android.resource://${packageName ?? eg.packageInfo().packageName}/raw/$resourceName';
215215

216216
test('on Android 28 (and lower) resource file is used for notification sound', () async {
217217
addTearDown(testBinding.reset);
@@ -227,7 +227,30 @@ void main() {
227227
.isEmpty();
228228
check(androidNotificationHost.takeCreatedChannels())
229229
.single
230-
.soundUrl.equals(fakeResourceUrl(defaultSoundResourceName));
230+
.soundUrl.equals(fakeResourceUrl(resourceName: defaultSoundResourceName));
231+
});
232+
233+
test('generates resource file URL from app package name', () async {
234+
addTearDown(testBinding.reset);
235+
final androidNotificationHost = testBinding.androidNotificationHost;
236+
237+
testBinding.packageInfoResult = eg.packageInfo(packageName: 'com.example.test');
238+
239+
// Force the default sound URL to be the resource file URL, by forcing
240+
// the Android version to the one where we don't store sounds through the
241+
// media store.
242+
testBinding.deviceInfoResult =
243+
const AndroidDeviceInfo(sdkInt: 28, release: '9');
244+
245+
await NotificationChannelManager.ensureChannel();
246+
check(androidNotificationHost.takeCopySoundResourceToMediaStoreCalls())
247+
.isEmpty();
248+
check(androidNotificationHost.takeCreatedChannels())
249+
.single
250+
.soundUrl.equals(fakeResourceUrl(
251+
resourceName: defaultSoundResourceName,
252+
packageName: 'com.example.test',
253+
));
231254
});
232255

233256
test('notification sound resource files are being copied to the media store', () async {
@@ -315,7 +338,7 @@ void main() {
315338
.isEmpty();
316339
check(androidNotificationHost.takeCreatedChannels())
317340
.single
318-
.soundUrl.equals(fakeResourceUrl(defaultSoundResourceName));
341+
.soundUrl.equals(fakeResourceUrl(resourceName: defaultSoundResourceName));
319342
});
320343
});
321344

0 commit comments

Comments
 (0)