PushSoundViewController.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2013-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "PushSoundViewController.h"
  21. #import "PushSounds.h"
  22. #import "UserSettings.h"
  23. #import "BundleUtil.h"
  24. #import <AudioToolbox/AudioToolbox.h>
  25. @interface PushSoundViewController ()
  26. @end
  27. @implementation PushSoundViewController {
  28. NSArray *pushSounds;
  29. NSIndexPath *selectedIndexPath;
  30. }
  31. @synthesize group;
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. pushSounds = [PushSounds getPushSounds];
  35. }
  36. - (void)viewWillAppear:(BOOL)animated {
  37. [super viewWillAppear:animated];
  38. [self.tableView reloadData];
  39. }
  40. static void soundCompletionCallback(SystemSoundID soundId, void *arg) {
  41. AudioServicesRemoveSystemSoundCompletion(soundId);
  42. AudioServicesDisposeSystemSoundID(soundId);
  43. }
  44. - (void)playPushSound:(NSString*)pushSoundName {
  45. if ([pushSoundName isEqualToString:@"none"])
  46. return;
  47. if ([pushSoundName isEqualToString:@"default"]) {
  48. AudioServicesPlayAlertSound(1007);
  49. return;
  50. }
  51. NSString *soundPath = [BundleUtil pathForResource:pushSoundName ofType:@"caf"];
  52. CFURLRef soundUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:soundPath];
  53. SystemSoundID soundId;
  54. AudioServicesCreateSystemSoundID(soundUrl, &soundId);
  55. AudioServicesAddSystemSoundCompletion(soundId, NULL, NULL, soundCompletionCallback, NULL);
  56. AudioServicesPlayAlertSound(soundId);
  57. }
  58. - (NSString*)currentPushSound {
  59. if (group)
  60. return [UserSettings sharedUserSettings].pushGroupSound;
  61. else
  62. return [UserSettings sharedUserSettings].pushSound;
  63. }
  64. #pragma mark - Table view data source
  65. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  66. return 1;
  67. }
  68. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  69. return pushSounds.count;
  70. }
  71. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  72. UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"SoundCell"];
  73. NSString *soundName = pushSounds[indexPath.row];
  74. NSString *soundNameLoc = [NSString stringWithFormat:@"sound_%@", soundName];
  75. cell.textLabel.text = NSLocalizedString(soundNameLoc, nil);
  76. if ([self.currentPushSound isEqualToString:soundName]) {
  77. selectedIndexPath = indexPath;
  78. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  79. } else {
  80. cell.accessoryType = UITableViewCellAccessoryNone;
  81. }
  82. return cell;
  83. }
  84. #pragma mark - Table view delegate
  85. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  86. {
  87. if (group)
  88. [UserSettings sharedUserSettings].pushGroupSound = pushSounds[indexPath.row];
  89. else
  90. [UserSettings sharedUserSettings].pushSound = pushSounds[indexPath.row];
  91. [self playPushSound:pushSounds[indexPath.row]];
  92. if (selectedIndexPath != nil)
  93. [self.tableView cellForRowAtIndexPath:selectedIndexPath].accessoryType = UITableViewCellAccessoryNone;
  94. [self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
  95. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  96. selectedIndexPath = indexPath;
  97. }
  98. @end