GADUPluginUtil.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2016 Google Inc. All Rights Reserved.
  2. #import "GADUPluginUtil.h"
  3. #import "UnityAppController.h"
  4. @interface UIView (unityStub)
  5. @property UILayoutGuide *safeAreaLayoutGuide;
  6. @end
  7. BOOL GADUIsOperatingSystemAtLeastVersion(NSInteger majorVersion) {
  8. NSProcessInfo *processInfo = NSProcessInfo.processInfo;
  9. if ([processInfo respondsToSelector:@selector(isOperatingSystemAtLeastVersion:)]) {
  10. // iOS 8+.
  11. NSOperatingSystemVersion version = {majorVersion};
  12. return [processInfo isOperatingSystemAtLeastVersion:version];
  13. } else {
  14. // pre-iOS 8. App supports iOS 7+, so this process must be running on iOS 7.
  15. return majorVersion >= 7;
  16. }
  17. }
  18. static CGFloat GADUSafeWidthLandscape(void) {
  19. CGRect screenBounds = [UIScreen mainScreen].bounds;
  20. if (GADUIsOperatingSystemAtLeastVersion(11)) {
  21. CGRect safeFrame = [UIApplication sharedApplication].keyWindow.safeAreaLayoutGuide.layoutFrame;
  22. if (!CGSizeEqualToSize(safeFrame.size, CGSizeZero)) {
  23. screenBounds = safeFrame;
  24. }
  25. }
  26. return MAX(CGRectGetWidth(screenBounds), CGRectGetHeight(screenBounds));
  27. }
  28. @implementation GADUPluginUtil
  29. static BOOL _pauseOnBackground = NO;
  30. + (BOOL)pauseOnBackground {
  31. return _pauseOnBackground;
  32. }
  33. + (void)setPauseOnBackground:(BOOL)pause {
  34. _pauseOnBackground = pause;
  35. }
  36. + (NSString *)GADUStringFromUTF8String:(const char *)bytes {
  37. return bytes ? @(bytes) : nil;
  38. }
  39. + (GADAdSize)safeAdSizeForAdSize:(GADAdSize)adSize {
  40. if (GADUIsOperatingSystemAtLeastVersion(11) &&
  41. GADAdSizeEqualToSize(kGADAdSizeSmartBannerLandscape, adSize)) {
  42. CGSize usualSize = CGSizeFromGADAdSize(kGADAdSizeSmartBannerLandscape);
  43. CGSize bannerSize = CGSizeMake(GADUSafeWidthLandscape(), usualSize.height);
  44. return GADAdSizeFromCGSize(bannerSize);
  45. } else {
  46. return adSize;
  47. }
  48. }
  49. + (UIViewController *)unityGLViewController {
  50. return ((UnityAppController *)[UIApplication sharedApplication].delegate).rootViewController;
  51. }
  52. + (void)positionView:(UIView *)view
  53. inParentView:(UIView *)parentView
  54. adPosition:(GADAdPosition)adPosition {
  55. CGRect parentBounds = parentView.bounds;
  56. if (GADUIsOperatingSystemAtLeastVersion(11)) {
  57. CGRect safeAreaFrame = parentView.safeAreaLayoutGuide.layoutFrame;
  58. if (!CGSizeEqualToSize(CGSizeZero, safeAreaFrame.size)) {
  59. parentBounds = safeAreaFrame;
  60. }
  61. }
  62. CGFloat top = CGRectGetMinY(parentBounds) + CGRectGetMidY(view.bounds);
  63. CGFloat left = CGRectGetMinX(parentBounds) + CGRectGetMidX(view.bounds);
  64. CGFloat bottom = CGRectGetMaxY(parentBounds) - CGRectGetMidY(view.bounds);
  65. CGFloat right = CGRectGetMaxX(parentBounds) - CGRectGetMidX(view.bounds);
  66. CGFloat centerX = CGRectGetMidX(parentBounds);
  67. CGFloat centerY = CGRectGetMidY(parentBounds);
  68. // If this view is of greater or equal width to the parent view, do not offset
  69. // to edge of safe area. Eg for smart banners that are still full screen
  70. // width.
  71. if (CGRectGetWidth(view.bounds) >= CGRectGetWidth(parentView.bounds)) {
  72. left = CGRectGetMidX(parentView.bounds);
  73. }
  74. // Similarly for height, if view is of custom size which is full screen
  75. // height, do not offset.
  76. if (CGRectGetHeight(view.bounds) >= CGRectGetHeight(parentView.bounds)) {
  77. top = CGRectGetMidY(parentView.bounds);
  78. }
  79. CGPoint center = CGPointMake(centerX, top);
  80. switch (adPosition) {
  81. case kGADAdPositionTopOfScreen:
  82. center = CGPointMake(centerX, top);
  83. break;
  84. case kGADAdPositionBottomOfScreen:
  85. center = CGPointMake(centerX, bottom);
  86. break;
  87. case kGADAdPositionTopLeftOfScreen:
  88. center = CGPointMake(left, top);
  89. break;
  90. case kGADAdPositionTopRightOfScreen:
  91. center = CGPointMake(right, top);
  92. break;
  93. case kGADAdPositionBottomLeftOfScreen:
  94. center = CGPointMake(left, bottom);
  95. break;
  96. case kGADAdPositionBottomRightOfScreen:
  97. center = CGPointMake(right, bottom);
  98. break;
  99. case kGADAdPositionCenterOfScreen:
  100. center = CGPointMake(centerX, centerY);
  101. break;
  102. default:
  103. break;
  104. }
  105. view.center = center;
  106. }
  107. + (void)positionView:(UIView *)view
  108. inParentView:(UIView *)parentView
  109. customPosition:(CGPoint)adPosition {
  110. CGPoint origin = parentView.bounds.origin;
  111. if (GADUIsOperatingSystemAtLeastVersion(11)) {
  112. CGRect safeAreaFrame = parentView.safeAreaLayoutGuide.layoutFrame;
  113. if (!CGSizeEqualToSize(CGSizeZero, safeAreaFrame.size)) {
  114. origin = safeAreaFrame.origin;
  115. }
  116. }
  117. CGPoint center = CGPointMake(origin.x + adPosition.x + CGRectGetMidX(view.bounds),
  118. origin.y + adPosition.y + CGRectGetMidY(view.bounds));
  119. view.center = center;
  120. }
  121. + (GADAdSize)adSizeForWidth:(CGFloat)width height:(CGFloat)height {
  122. UIInterfaceOrientation currentOrientation =
  123. [UIApplication sharedApplication].statusBarOrientation;
  124. if (width == kGADUAdSizeUseFullWidth && UIInterfaceOrientationIsPortrait(currentOrientation)) {
  125. return GADAdSizeFullWidthPortraitWithHeight(height);
  126. } else if ((width == kGADUAdSizeUseFullWidth &&
  127. UIInterfaceOrientationIsLandscape(currentOrientation))) {
  128. return GADAdSizeFromCGSize(CGSizeMake(GADUSafeWidthLandscape(), height));
  129. }
  130. return GADAdSizeFromCGSize(CGSizeMake(width, height));
  131. }
  132. + (GADAdSize)adaptiveAdSizeForWidth:(CGFloat)width orientation:(GADUBannerOrientation)orientation {
  133. if (width == kGADUAdSizeUseFullWidth) {
  134. width = GADUDeviceSafeWidth();
  135. }
  136. switch (orientation) {
  137. case kGADUBannerOrientationCurrent:
  138. return GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(width);
  139. case kGADUBannerOrientationLandscape:
  140. return GADLandscapeAnchoredAdaptiveBannerAdSizeWithWidth(width);
  141. case kGADUBannerOrientationPortrait:
  142. return GADPortraitAnchoredAdaptiveBannerAdSizeWithWidth(width);
  143. }
  144. }
  145. @end