Skip to content

Commit

Permalink
feat(controls): add skip button area and position fields #388
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinPostindustria committed Apr 7, 2022
1 parent 9c72950 commit 19cb1af
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,22 @@ public void setSkipDelay(int secondsDelay) {
adUnitConfig.setSkipDelay(secondsDelay);
}

/**
* Sets skip button percentage size in range from 0.05 to 1.
* If value less than 0.05, size will be default.
*/
public void setSkipButtonArea(@FloatRange(from = 0, to = 1.0) double buttonArea) {
adUnitConfig.setSkipButtonArea(buttonArea);
}

/**
* Sets skip button position on the screen. Suitable values TOP_LEFT and TOP_RIGHT.
* Default value TOP_RIGHT.
*/
public void setSkipButtonPosition(Position skipButtonPosition) {
adUnitConfig.setSkipButtonPosition(skipButtonPosition);
}

public void setIsMuted(boolean isMuted) {
adUnitConfig.setIsMuted(isMuted);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ protected void addSkipView() {
return;
}

mSkipView = Utils.createSkipView(mContextReference.get());
InterstitialDisplayPropertiesInternal properties = mInterstitialManager.getInterstitialDisplayProperties();
mSkipView = Utils.createSkipView(mContextReference.get(), properties);

if (mSkipView == null) {
LogUtil.error(TAG, "Unable to add skip button. Skip view is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@ public static void configureDisplayProperties(AdUnitConfiguration adConfiguratio
displayProperties.closeButtonArea = adConfiguration.getCloseButtonArea();
displayProperties.closeButtonPosition = adConfiguration.getCloseButtonPosition();
displayProperties.skipDelay = adConfiguration.getSkipDelay();
displayProperties.skipButtonArea = adConfiguration.getSkipButtonArea();
displayProperties.skipButtonPosition = adConfiguration.getSkipButtonPosition();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ public class InterstitialDisplayPropertiesInternal extends InterstitialDisplayPr

public int expandWidth;
public int expandHeight;
public double closeButtonArea = 0;
public int orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
public int skipDelay = 0;
public double closeButtonArea = 0;
public double skipButtonArea = 0;

public boolean isSoundButtonVisible = false;
public boolean isMuted = false;
public boolean isRotationEnabled = false;

public Position closeButtonPosition = Position.TOP_RIGHT;
public Position skipButtonPosition = Position.TOP_RIGHT;

public void resetExpandValues() {
expandHeight = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import android.view.*;
import android.webkit.MimeTypeMap;
import android.widget.FrameLayout;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -431,61 +433,71 @@ public static Map<String, String> getQueryMap(String query) {
return map;
}

public static View createSkipView(Context context) {
if (context == null) {
LogUtil.error(TAG, "Unable to create view. Context is null");
return null;
}

View view = LayoutInflater.from(context).inflate(R.layout.lyt_skip, null);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
public static View createSkipView(
Context context,
@Nullable InterstitialDisplayPropertiesInternal properties
) {
return createButtonToNextPage(context,
R.layout.lyt_skip,
properties != null ? properties.skipButtonArea : 0,
properties != null ? properties.skipButtonPosition : Position.TOP_RIGHT
);
params.gravity = Gravity.END | Gravity.TOP;
view.setLayoutParams(params);
return view;
}

private static final int MIN_BUTTON_SIZE_DP = 30;

public static View createCloseView(Context context) {
return createCloseView(context, null);
}

public static View createCloseView(
Context context,
InterstitialDisplayPropertiesInternal properties
@Nullable InterstitialDisplayPropertiesInternal properties
) {
return createButtonToNextPage(context,
R.layout.lyt_close,
properties != null ? properties.closeButtonArea : 0,
properties != null ? properties.closeButtonPosition : Position.TOP_RIGHT
);
}

private static View createButtonToNextPage(
@Nullable Context context,
@LayoutRes int layoutResId,
double buttonArea,
@NonNull Position buttonPosition
) {
if (context == null) {
LogUtil.error(TAG, "Unable to create close view. Context is null");
return null;
}

View closeView = LayoutInflater.from(context).inflate(R.layout.lyt_close, null);
FrameLayout.LayoutParams params = calculateButtonSize(closeView, properties);
View view = LayoutInflater.from(context).inflate(layoutResId, null);

FrameLayout.LayoutParams params;
params = calculateButtonSize(view, buttonArea);
params.gravity = Gravity.END | Gravity.TOP;
if (properties != null && properties.closeButtonPosition == Position.TOP_LEFT) {
if (buttonPosition == Position.TOP_LEFT) {
params.gravity = Gravity.START | Gravity.TOP;
}

closeView.setLayoutParams(params);
return closeView;
view.setLayoutParams(params);
return view;
}

private static final int MIN_BUTTON_SIZE_DP = 30;

private static FrameLayout.LayoutParams calculateButtonSize(
View view,
InterstitialDisplayPropertiesInternal properties
double closeButtonArea
) {
Context context = view.getContext();
if (properties == null || properties.closeButtonArea < 0.05 || properties.closeButtonArea > 1) {
if (closeButtonArea < 0.05 || closeButtonArea > 1) {
return new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
);
}

int screenSize = getSmallestScreenSideSize(context);
int buttonSize = (int) (screenSize * properties.closeButtonArea);
int buttonSize = (int) (screenSize * closeButtonArea);
if (convertPxToDp(buttonSize, context) < MIN_BUTTON_SIZE_DP) {
buttonSize = convertDpToPx(MIN_BUTTON_SIZE_DP, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class AdUnitConfiguration {
private final int broadcastId = Utils.generateRandomInt();
private float videoInitialVolume = ExoPlayerView.DEFAULT_INITIAL_VIDEO_VOLUME;
private double closeButtonArea = 0;
private double skipButtonArea = 0;

private int maxVideoDuration = 3600;

Expand All @@ -38,6 +39,7 @@ public class AdUnitConfiguration {
private String interstitialSize;

private Position closeButtonPosition = Position.TOP_RIGHT;
private Position skipButtonPosition = Position.TOP_RIGHT;
private AdSize minSizePercentage;
private PlacementType placementType;
private AdPosition adPosition;
Expand Down Expand Up @@ -290,6 +292,25 @@ public int getSkipDelay() {
return skipDelay;
}

public double getSkipButtonArea() {
return skipButtonArea;
}

public void setSkipButtonArea(double skipButtonArea) {
this.skipButtonArea = skipButtonArea;
}

@NonNull
public Position getSkipButtonPosition() {
return skipButtonPosition;
}

public void setSkipButtonPosition(@Nullable Position skipButtonPosition) {
if (skipButtonPosition != null) {
this.skipButtonPosition = skipButtonPosition;
}
}

@NonNull
public EnumSet<AdFormat> getAdFormats() {
return adFormats;
Expand Down Expand Up @@ -339,9 +360,7 @@ public String getInterstitialSize() {

public void setCloseButtonPosition(@Nullable Position closeButtonPosition) {
if (closeButtonPosition != null) {
if (closeButtonPosition == Position.TOP_LEFT || closeButtonPosition == Position.TOP_RIGHT) {
this.closeButtonPosition = closeButtonPosition;
}
this.closeButtonPosition = closeButtonPosition;
}
}

Expand Down

0 comments on commit 19cb1af

Please sign in to comment.