VoIPSoundViewController.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "VoIPSoundViewController.h"
  21. #import "VoIPSounds.h"
  22. #import "UserSettings.h"
  23. #import "BundleUtil.h"
  24. #import <AVFoundation/AVFoundation.h>
  25. @interface VoIPSoundViewController ()
  26. @end
  27. @implementation VoIPSoundViewController {
  28. NSArray *voIPSounds;
  29. NSIndexPath *selectedIndexPath;
  30. AVAudioPlayer *_audioPlayer;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. voIPSounds = [VoIPSounds getVoIPSounds];
  35. }
  36. - (void)viewWillAppear:(BOOL)animated {
  37. [super viewWillAppear:animated];
  38. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
  39. [self.tableView reloadData];
  40. }
  41. - (void)viewWillDisappear:(BOOL)animated {
  42. [super viewWillDisappear:animated];
  43. [_audioPlayer stop];
  44. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
  45. }
  46. - (void)playVoIPSound:(NSString*)voIPSoundName {
  47. if ([voIPSoundName isEqualToString:@"default"]) {
  48. return;
  49. }
  50. NSString *soundPath = [BundleUtil pathForResource:voIPSoundName ofType:@"caf"];
  51. NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
  52. if (_audioPlayer) {
  53. if ([_audioPlayer.url isEqual:soundUrl]) {
  54. if (_audioPlayer.isPlaying) {
  55. [_audioPlayer stop];
  56. } else {
  57. _audioPlayer.currentTime = 0;
  58. [_audioPlayer play];
  59. }
  60. return;
  61. }
  62. [_audioPlayer stop];
  63. _audioPlayer = nil;
  64. }
  65. _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
  66. _audioPlayer.numberOfLoops = 2;
  67. [_audioPlayer play];
  68. }
  69. - (NSString*)currentVoIPSound {
  70. return [UserSettings sharedUserSettings].voIPSound;
  71. }
  72. #pragma mark - Table view data source
  73. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  74. return 1;
  75. }
  76. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  77. return voIPSounds.count;
  78. }
  79. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  80. UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"SoundCell"];
  81. NSString *soundName = voIPSounds[indexPath.row];
  82. NSString *soundNameLoc = [NSString stringWithFormat:@"sound_%@", soundName];
  83. cell.textLabel.text = NSLocalizedString(soundNameLoc, nil);
  84. if ([self.currentVoIPSound isEqualToString:soundName]) {
  85. selectedIndexPath = indexPath;
  86. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  87. } else {
  88. cell.accessoryType = UITableViewCellAccessoryNone;
  89. }
  90. return cell;
  91. }
  92. #pragma mark - Table view delegate
  93. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  94. {
  95. [UserSettings sharedUserSettings].voIPSound = voIPSounds[indexPath.row];
  96. // [[CallManager sharedInstance] createNewProvider];
  97. [self playVoIPSound:voIPSounds[indexPath.row]];
  98. if (selectedIndexPath != nil)
  99. [self.tableView cellForRowAtIndexPath:selectedIndexPath].accessoryType = UITableViewCellAccessoryNone;
  100. [self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
  101. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  102. selectedIndexPath = indexPath;
  103. }
  104. #pragma mark - Notifications
  105. - (void)applicationDidEnterBackground:(UIApplication *)application
  106. {
  107. if (_audioPlayer) {
  108. [_audioPlayer stop];
  109. };
  110. }
  111. @end