Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

150 řádky
5.0 KiB

  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. static BOOL IsOperatingSystemAtLeastVersion(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 FullSafeWidthLandscape(void) {
  19. CGRect screenBounds = [UIScreen mainScreen].bounds;
  20. if (IsOperatingSystemAtLeastVersion(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. + (GADAdSize)safeAdSizeForAdSize:(GADAdSize)adSize {
  37. if (IsOperatingSystemAtLeastVersion(11) &&
  38. GADAdSizeEqualToSize(kGADAdSizeSmartBannerLandscape, adSize)) {
  39. CGSize usualSize = CGSizeFromGADAdSize(kGADAdSizeSmartBannerLandscape);
  40. CGSize bannerSize = CGSizeMake(FullSafeWidthLandscape(), usualSize.height);
  41. return GADAdSizeFromCGSize(bannerSize);
  42. } else {
  43. return adSize;
  44. }
  45. }
  46. + (UIViewController *)unityGLViewController {
  47. return ((UnityAppController *)[UIApplication sharedApplication].delegate).rootViewController;
  48. }
  49. + (void)positionView:(UIView *)view
  50. inParentView:(UIView *)parentView
  51. adPosition:(GADAdPosition)adPosition {
  52. CGRect parentBounds = parentView.bounds;
  53. if (IsOperatingSystemAtLeastVersion(11)) {
  54. CGRect safeAreaFrame = parentView.safeAreaLayoutGuide.layoutFrame;
  55. if (!CGSizeEqualToSize(CGSizeZero, safeAreaFrame.size)) {
  56. parentBounds = safeAreaFrame;
  57. }
  58. }
  59. CGFloat top = CGRectGetMinY(parentBounds) + CGRectGetMidY(view.bounds);
  60. CGFloat left = CGRectGetMinX(parentBounds) + CGRectGetMidX(view.bounds);
  61. CGFloat bottom = CGRectGetMaxY(parentBounds) - CGRectGetMidY(view.bounds);
  62. CGFloat right = CGRectGetMaxX(parentBounds) - CGRectGetMidX(view.bounds);
  63. CGFloat centerX = CGRectGetMidX(parentBounds);
  64. CGFloat centerY = CGRectGetMidY(parentBounds);
  65. // If this view is of greater or equal width to the parent view, do not offset
  66. // to edge of safe area. Eg for smart banners that are still full screen
  67. // width.
  68. if (CGRectGetWidth(view.bounds) >= CGRectGetWidth(parentView.bounds)) {
  69. left = CGRectGetMidX(parentView.bounds);
  70. }
  71. // Similarly for height, if view is of custom size which is full screen
  72. // height, do not offset.
  73. if (CGRectGetHeight(view.bounds) >= CGRectGetHeight(parentView.bounds)) {
  74. top = CGRectGetMidY(parentView.bounds);
  75. }
  76. CGPoint center = CGPointMake(centerX, top);
  77. switch (adPosition) {
  78. case kGADAdPositionTopOfScreen:
  79. center = CGPointMake(centerX, top);
  80. break;
  81. case kGADAdPositionBottomOfScreen:
  82. center = CGPointMake(centerX, bottom);
  83. break;
  84. case kGADAdPositionTopLeftOfScreen:
  85. center = CGPointMake(left, top);
  86. break;
  87. case kGADAdPositionTopRightOfScreen:
  88. center = CGPointMake(right, top);
  89. break;
  90. case kGADAdPositionBottomLeftOfScreen:
  91. center = CGPointMake(left, bottom);
  92. break;
  93. case kGADAdPositionBottomRightOfScreen:
  94. center = CGPointMake(right, bottom);
  95. break;
  96. case kGADAdPositionCenterOfScreen:
  97. center = CGPointMake(centerX, centerY);
  98. break;
  99. default:
  100. break;
  101. }
  102. view.center = center;
  103. }
  104. + (void)positionView:(UIView *)view
  105. inParentView:(UIView *)parentView
  106. customPosition:(CGPoint)adPosition {
  107. CGPoint origin = parentView.bounds.origin;
  108. if (IsOperatingSystemAtLeastVersion(11)) {
  109. CGRect safeAreaFrame = parentView.safeAreaLayoutGuide.layoutFrame;
  110. if (!CGSizeEqualToSize(CGSizeZero, safeAreaFrame.size)) {
  111. origin = safeAreaFrame.origin;
  112. }
  113. }
  114. CGPoint center = CGPointMake(origin.x + adPosition.x + CGRectGetMidX(view.bounds),
  115. origin.y + adPosition.y + CGRectGetMidY(view.bounds));
  116. view.center = center;
  117. }
  118. + (GADAdSize)adSizeForWidth:(CGFloat)width height:(CGFloat)height {
  119. UIDeviceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
  120. if (width == kGADUAdSizeUseFullWidth && UIInterfaceOrientationIsPortrait(currentOrientation)) {
  121. return GADAdSizeFullWidthPortraitWithHeight(height);
  122. } else if ((width == kGADUAdSizeUseFullWidth &&
  123. UIInterfaceOrientationIsLandscape(currentOrientation))) {
  124. return GADAdSizeFromCGSize(CGSizeMake(FullSafeWidthLandscape(), height));
  125. }
  126. return GADAdSizeFromCGSize(CGSizeMake(width, height));
  127. }
  128. @end