VLCActivityManager.m 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*****************************************************************************
  2. * VLCActivityManager.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCActivityManager.h"
  13. @interface VLCActivityManager ()
  14. {
  15. int _idleCounter;
  16. int _networkActivityCounter;
  17. }
  18. @end
  19. @implementation VLCActivityManager
  20. + (instancetype)defaultManager
  21. {
  22. static VLCActivityManager *sharedInstance = nil;
  23. static dispatch_once_t pred;
  24. dispatch_once(&pred, ^{
  25. sharedInstance = [VLCActivityManager new];
  26. });
  27. return sharedInstance;
  28. }
  29. - (void)activateIdleTimer
  30. {
  31. if (![NSThread isMainThread]) {
  32. [self performSelectorOnMainThread:@selector(activateIdleTimer) withObject:nil waitUntilDone:NO];
  33. return;
  34. }
  35. _idleCounter--;
  36. if (_idleCounter < 1)
  37. [UIApplication sharedApplication].idleTimerDisabled = NO;
  38. }
  39. - (void)disableIdleTimer
  40. {
  41. if (![NSThread isMainThread]) {
  42. [self performSelectorOnMainThread:@selector(disableIdleTimer) withObject:nil waitUntilDone:NO];
  43. return;
  44. }
  45. _idleCounter++;
  46. if ([UIApplication sharedApplication].idleTimerDisabled == NO)
  47. [UIApplication sharedApplication].idleTimerDisabled = YES;
  48. }
  49. - (void)networkActivityStarted
  50. {
  51. if (![NSThread isMainThread]) {
  52. [self performSelectorOnMainThread:@selector(networkActivityStarted) withObject:nil waitUntilDone:NO];
  53. return;
  54. }
  55. _networkActivityCounter++;
  56. #if TARGET_OS_IOS
  57. if ([UIApplication sharedApplication].networkActivityIndicatorVisible == NO)
  58. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  59. #endif
  60. }
  61. - (BOOL)haveNetworkActivity
  62. {
  63. return _networkActivityCounter >= 1;
  64. }
  65. - (void)networkActivityStopped
  66. {
  67. if (![NSThread isMainThread]) {
  68. [self performSelectorOnMainThread:@selector(networkActivityStopped) withObject:nil waitUntilDone:NO];
  69. return;
  70. }
  71. _networkActivityCounter--;
  72. #if TARGET_OS_IOS
  73. if (_networkActivityCounter < 1)
  74. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  75. #endif
  76. }
  77. @end