Skip to content
This repository
Browse code

[IOS] do not keep network alive in idle state (paused or stopped).

  • Loading branch information...
commit f4b1dbb7170c80fb1eec3fe2fc45bcd2eeaf6b6a 1 parent 165be20
ulion authored March 08, 2013
12  xbmc/osx/ios/XBMCController.h
@@ -28,6 +28,13 @@
28 28
 
29 29
 @class IOSEAGLView;
30 30
 
  31
+typedef enum
  32
+{
  33
+  IOS_PLAYBACK_STOPPED,
  34
+  IOS_PLAYBACK_PAUSED,
  35
+  IOS_PLAYBACK_PLAYING
  36
+} IOSPlaybackState;
  37
+
31 38
 @interface XBMCController : UIViewController <UIGestureRecognizerDelegate>
32 39
 {
33 40
   UIWindow *m_window;
@@ -46,6 +53,8 @@
46 53
   bool m_isPlayingBeforeInactive;
47 54
   bool m_isInterrupted;
48 55
   UIBackgroundTaskIdentifier m_bgTask;
  56
+  NSTimer *m_networkAutoSuspendTimer;
  57
+  IOSPlaybackState m_playbackState;
49 58
 }
50 59
 @property (readonly, nonatomic, getter=isAnimating) BOOL animating;
51 60
 @property CGPoint lastGesturePoint;
@@ -53,6 +62,7 @@
53 62
 @property bool touchBeginSignaled;
54 63
 @property int  m_screenIdx;
55 64
 @property CGSize screensize;
  65
+@property (nonatomic, retain) NSTimer *m_networkAutoSuspendTimer;
56 66
 
57 67
 // message from which our instance is obtained
58 68
 - (void) pauseAnimation;
@@ -79,7 +89,7 @@
79 89
 - (void) deactivateKeyboard:(UIView *)view;
80 90
 
81 91
 - (void) disableNetworkAutoSuspend;
82  
-- (void) enableNetworkAutoSuspend;
  92
+- (void) enableNetworkAutoSuspend:(id)obj;
83 93
 - (void) disableSystemSleep;
84 94
 - (void) enableSystemSleep;
85 95
 - (void) disableScreenSaver;
44  xbmc/osx/ios/XBMCController.mm
@@ -240,6 +240,7 @@ @implementation XBMCController
240 240
 @synthesize touchBeginSignaled;
241 241
 @synthesize m_screenIdx;
242 242
 @synthesize screensize;
  243
+@synthesize m_networkAutoSuspendTimer;
243 244
 //--------------------------------------------------------------
244 245
 -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
245 246
 {  
@@ -698,6 +699,7 @@ - (id)initWithFrame:(CGRect)frame withScreen:(UIScreen *)screen
698 699
   m_isPlayingBeforeInactive = NO;
699 700
   m_isInterrupted = NO;
700 701
   m_bgTask = UIBackgroundTaskInvalid;
  702
+  m_playbackState = IOS_PLAYBACK_STOPPED;
701 703
 
702 704
   m_window = [[UIWindow alloc] initWithFrame:frame];
703 705
   [m_window setRootViewController:self];  
@@ -745,7 +747,8 @@ -(void)viewDidLoad
745 747
 - (void)dealloc
746 748
 {
747 749
   // stop background task
748  
-  [self enableNetworkAutoSuspend];
  750
+  [m_networkAutoSuspendTimer invalidate];
  751
+  [self enableNetworkAutoSuspend:nil];
749 752
 
750 753
   AnnounceReceiver::dealloc();
751 754
   [m_glView stopAnimation];
@@ -867,10 +870,16 @@ - (void)disableNetworkAutoSuspend
867 870
   }
868 871
   // we have to alloc the background task for keep network working after screen lock and dark.
869 872
   UIBackgroundTaskIdentifier newTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
870  
-  m_bgTask = newTask;  
  873
+  m_bgTask = newTask;
  874
+
  875
+  if (m_networkAutoSuspendTimer)
  876
+  {
  877
+    [m_networkAutoSuspendTimer invalidate];
  878
+    self.m_networkAutoSuspendTimer = nil;
  879
+  }
871 880
 }
872 881
 //--------------------------------------------------------------
873  
-- (void)enableNetworkAutoSuspend
  882
+- (void)enableNetworkAutoSuspend:(id)obj
874 883
 {
875 884
   PRINT_SIGNATURE();
876 885
   if (m_bgTask != UIBackgroundTaskInvalid)
@@ -923,6 +932,7 @@ - (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
923 932
   LOG(@"%s: type %d, subtype: %d", __PRETTY_FUNCTION__, receivedEvent.type, receivedEvent.subtype);
924 933
   if (receivedEvent.type == UIEventTypeRemoteControl)
925 934
   {
  935
+    [self disableNetworkAutoSuspend];
926 936
     switch (receivedEvent.subtype)
927 937
     {
928 938
       case UIEventSubtypeRemoteControlTogglePlayPause:
@@ -945,6 +955,7 @@ - (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
945 955
         LOG(@"unhandled subtype: %d", receivedEvent.subtype);
946 956
         break;
947 957
     }
  958
+    [self rescheduleNetworkAutoSuspend];
948 959
   }
949 960
 }
950 961
 //--------------------------------------------------------------
@@ -980,8 +991,8 @@ - (void)becomeInactive
980 991
     m_isPlayingBeforeInactive = YES;
981 992
     CApplicationMessenger::Get().MediaPauseIfPlaying();
982 993
   }
983  
-  // TODO: only disable network auto-suspend when app is playing
984  
-  [self disableNetworkAutoSuspend];
  994
+  // check whether we need disable network auto suspend.
  995
+  [self rescheduleNetworkAutoSuspend];
985 996
 }
986 997
 
987 998
 - (void)beginInterruption
@@ -1089,11 +1100,16 @@ - (void)onPlay:(NSDictionary *)item
1089 1100
     [[NowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];
1090 1101
     [dict release];
1091 1102
   }
  1103
+  m_playbackState = IOS_PLAYBACK_PLAYING;
  1104
+  [self disableNetworkAutoSuspend];
1092 1105
 }
1093 1106
 //--------------------------------------------------------------
1094 1107
 - (void)onPause:(NSDictionary *)item
1095 1108
 {
1096 1109
   PRINT_SIGNATURE();
  1110
+  m_playbackState = IOS_PLAYBACK_PAUSED;
  1111
+  // schedule set network auto suspend state for save power if idle.
  1112
+  [self rescheduleNetworkAutoSuspend];
1097 1113
 }
1098 1114
 //--------------------------------------------------------------
1099 1115
 - (void)onStop:(NSDictionary *)item
@@ -1102,6 +1118,24 @@ - (void)onStop:(NSDictionary *)item
1102 1118
   Class NowPlayingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
1103 1119
   if (NowPlayingInfoCenter)
1104 1120
     [[NowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nil];
  1121
+  m_playbackState = IOS_PLAYBACK_STOPPED;
  1122
+  // delay set network auto suspend state in case we are switching playing item.
  1123
+  [self rescheduleNetworkAutoSuspend];
  1124
+}
  1125
+//--------------------------------------------------------------
  1126
+- (void)rescheduleNetworkAutoSuspend
  1127
+{
  1128
+  LOG(@"%s: playback state: %d", __PRETTY_FUNCTION__,  m_playbackState);
  1129
+  if (m_playbackState == IOS_PLAYBACK_PLAYING)
  1130
+  {
  1131
+    [self disableNetworkAutoSuspend];
  1132
+    return;
  1133
+  }
  1134
+  if (m_networkAutoSuspendTimer)
  1135
+    [m_networkAutoSuspendTimer invalidate];
  1136
+
  1137
+  int delay = m_playbackState == IOS_PLAYBACK_PAUSED ? 60 : 30;  // wait longer if paused than stopped
  1138
+  self.m_networkAutoSuspendTimer = [NSTimer scheduledTimerWithTimeInterval:delay target:self selector:@selector(enableNetworkAutoSuspend:) userInfo:nil repeats:NO];
1105 1139
 }
1106 1140
 
1107 1141
 #pragma mark -

0 notes on commit f4b1dbb

Please sign in to comment.
Something went wrong with that request. Please try again.