Skip to content

Commit

Permalink
fix: visibility checker problem due to transparent navigation bar #475
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinPostindustria committed Jun 8, 2022
1 parent 96319d4 commit 85d245b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,24 @@ protected void setAdViewManagerValues() throws AdException {
}

protected InternalFriendlyObstruction[] formInterstitialObstructionsArray() {
InternalFriendlyObstruction[] obstructionArray = new InternalFriendlyObstruction[3];
InternalFriendlyObstruction[] obstructionArray = new InternalFriendlyObstruction[5];

View closeInterstitial = findViewById(R.id.iv_close_interstitial);
View skipInterstitial = findViewById(R.id.iv_skip);
View countDownTimer = findViewById(R.id.rl_count_down);
View actionButton = findViewById(R.id.tv_learn_more);

obstructionArray[0] = new InternalFriendlyObstruction(closeInterstitial, InternalFriendlyObstruction.Purpose.CLOSE_AD, null);
obstructionArray[1] = new InternalFriendlyObstruction(countDownTimer, InternalFriendlyObstruction.Purpose.OTHER, "CountDownTimer");
obstructionArray[2] = new InternalFriendlyObstruction(actionButton, InternalFriendlyObstruction.Purpose.OTHER, "Action button");
obstructionArray[1] = new InternalFriendlyObstruction(skipInterstitial, InternalFriendlyObstruction.Purpose.CLOSE_AD, null);
obstructionArray[2] = new InternalFriendlyObstruction(countDownTimer, InternalFriendlyObstruction.Purpose.OTHER, "CountDownTimer");
obstructionArray[3] = new InternalFriendlyObstruction(actionButton, InternalFriendlyObstruction.Purpose.OTHER, "Action button");

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
View bottomNavigation = findViewById(android.R.id.navigationBarBackground);
obstructionArray[4] = new InternalFriendlyObstruction(bottomNavigation, InternalFriendlyObstruction.Purpose.OTHER, "Bottom navigation bar");
} else {
obstructionArray[4] = null;
}

return obstructionArray;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,21 @@ private List<Rect> buildObstructionsRectList() {
return pickedObstructionList;
}

private boolean visitParent(ViewGroup parentView, View childView) {
/**
* Checks whether the parent view is visible
* and whether children are not covered by any obstruction.
*/
private boolean visitParent(
ViewGroup parentView,
View childView
) {
if (parentView.getVisibility() != View.VISIBLE || isViewTransparent(parentView)) {
return false;
}

boolean clip = isClippedToBounds(parentView);
boolean childrenAreClippedToParent = isClippedToBounds(parentView);

if (clip) {
if (childrenAreClippedToParent) {
Rect bounds = new Rect();
parentView.getDrawingRect(bounds);
Rect convertRect = convertRect(bounds, parentView, testedViewWeakReference.get());
Expand Down Expand Up @@ -152,8 +159,13 @@ private boolean visitParent(ViewGroup parentView, View childView) {

// don't test child if it is viewGroup and transparent
private boolean isFriendlyObstruction(View child) {
return (child instanceof ImageView && child.getId() == R.id.iv_close_interstitial)
|| child.getId() == R.id.rl_count_down;
boolean result = (child instanceof ImageView && child.getId() == R.id.iv_close_interstitial)
|| (child instanceof ImageView && child.getId() == R.id.iv_skip)
|| child.getId() == R.id.rl_count_down;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
result = result || child.getId() == android.R.id.navigationBarBackground;
}
return result;
}

private void collectObstructionsFrom(View child) {
Expand Down Expand Up @@ -291,7 +303,8 @@ private void fragmentize(Rect valueRect, Rect aroundRect, List<Rect> destList) {
new Rect(trimmedRect.right,
valueRect.top,
valueRect.right,
valueRect.top + valueRect.height())
valueRect.top + valueRect.height()
)
};

for (Rect rect : subRectArray) {
Expand All @@ -301,14 +314,23 @@ private void fragmentize(Rect valueRect, Rect aroundRect, List<Rect> destList) {
}
}

/**
* Returns whether ViewGroup's children are clipped to their bounds.
* <p>
* {@link ViewGroup#getClipChildren()}
*/
private boolean isClippedToBounds(ViewGroup viewGroup) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
return viewGroup.getClipChildren();
}
return false;
}

private Rect convertRect(Rect fromRect, View fromView, View toView) {
private Rect convertRect(
Rect fromRect,
View fromView,
View toView
) {
if (fromRect == null || fromView == null || toView == null) {
LogUtil.debug(TAG, "convertRect: Failed. One of the provided param is null. Returning empty rect.");
return new Rect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package org.prebid.mobile.rendering.views;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -334,7 +336,9 @@ public void addObstructions(InternalFriendlyObstruction... friendlyObstructions)
}

for (InternalFriendlyObstruction friendlyObstruction : friendlyObstructions) {
currentCreative.addOmFriendlyObstruction(friendlyObstruction);
if (friendlyObstruction != null) {
currentCreative.addOmFriendlyObstruction(friendlyObstruction);
}
}
}

Expand Down Expand Up @@ -444,7 +448,22 @@ private void addHtmlInterstitialObstructions(ViewGroup rootViewGroup) {
return;
}
View closeButtonView = rootViewGroup.findViewById(R.id.iv_close_interstitial);
addObstructions(new InternalFriendlyObstruction(closeButtonView, InternalFriendlyObstruction.Purpose.CLOSE_AD, null));

InternalFriendlyObstruction[] obstructionArray = new InternalFriendlyObstruction[2];
obstructionArray[0] = new InternalFriendlyObstruction(closeButtonView, InternalFriendlyObstruction.Purpose.CLOSE_AD, null);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Context context = closeButtonView.getContext();
Activity activity = (Activity) context;
ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
View view = decorView.findViewById(android.R.id.navigationBarBackground);
InternalFriendlyObstruction obstruction = new InternalFriendlyObstruction(view, InternalFriendlyObstruction.Purpose.OTHER, "Bottom navigation");
obstructionArray[1] = obstruction;
} else {
obstructionArray[1] = null;
}

addObstructions(obstructionArray);
}

private void processTransaction(Transaction transaction) {
Expand Down

0 comments on commit 85d245b

Please sign in to comment.