Skip to content

Commit

Permalink
Add close button area and position properties (#411)
Browse files Browse the repository at this point in the history
* feat(controls): add close button area property #388

* feat(controls): add unit tests #388

* feat(controls): add internal example #388

* feat(controls): fix tests #388

* feat(controls): create close button position property #389

Make example in Internal test app. Write unit tests.
  • Loading branch information
ValentinPostindustria authored Apr 6, 2022
1 parent f6f4692 commit b4e2d70
Show file tree
Hide file tree
Showing 14 changed files with 315 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.prebid.mobile.renderingtestapp.plugplay.bidding.ppm

import org.prebid.mobile.rendering.bidding.enums.AdUnitFormat
import org.prebid.mobile.rendering.bidding.parallel.InterstitialAdUnit
import org.prebid.mobile.renderingtestapp.plugplay.bidding.base.BaseBidInterstitialFragment
import org.prebid.mobile.units.configuration.Position
import java.util.*

class PpmInterstitialCloseButtonAreaFragment : BaseBidInterstitialFragment() {

override fun initInterstitialAd(
adUnitFormat: AdUnitFormat,
adUnitId: String?,
configId: String?,
width: Int,
height: Int
) {
interstitialAdUnit = if (adUnitFormat == AdUnitFormat.VIDEO) {
InterstitialAdUnit(requireContext(), configId, EnumSet.of(adUnitFormat))
} else {
InterstitialAdUnit(requireContext(), configId)
}
interstitialAdUnit?.setInterstitialAdUnitListener(this)
interstitialAdUnit?.setCloseButtonArea(0.40)
interstitialAdUnit?.setCloseButtonPosition(Position.TOP_LEFT)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class DemoItemProvider private constructor() {

private const val ppmBannerAction = R.id.action_header_bidding_to_in_app_banner
private const val ppmInterstitialAction = R.id.action_header_bidding_to_in_app_interstitial
private const val ppmInterstitialCloseButtonAreaAction =
R.id.action_header_bidding_to_in_app_interstitial_close_button_area
private const val ppmInterstitialSoundButtonAction =
R.id.action_header_bidding_to_in_app_interstitial_sound_button
private const val ppmRewardedAction = R.id.action_header_bidding_to_in_app_video_rewarded
Expand Down Expand Up @@ -425,6 +427,20 @@ class DemoItemProvider private constructor() {
)
)
)
demoList.add(
DemoItem(
getString(R.string.demo_bidding_in_app_interstitial_video_320_480_end_card_close_button_area),
ppmInterstitialCloseButtonAreaAction,
ppmVideoTagList,
createBannerBundle(
R.string.imp_prebid_id_video_interstitial_320_480_with_end_card,
null,
MIN_WIDTH_PERC,
MIN_HEIGHT_PERC,
R.string.response_prebid_video_interstitial_320_480_with_end_card
)
)
)
demoList.add(
DemoItem(
getString(R.string.demo_bidding_in_app_interstitial_video_320_480_end_card_sound_button),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@
app:popExitAnim="@anim/nav_default_pop_exit_anim"
app:popUpTo="@+id/headerBiddingFragment" />

<action
android:id="@+id/action_header_bidding_to_in_app_interstitial_close_button_area"
app:destination="@id/navigation_in_app_interstitial_close_button_area"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim"
app:popUpTo="@+id/headerBiddingFragment" />

<action
android:id="@+id/action_header_bidding_to_in_app_interstitial_sound_button"
app:destination="@id/navigation_in_app_interstitial_sound_button"
Expand Down Expand Up @@ -407,6 +416,11 @@
android:name="org.prebid.mobile.renderingtestapp.plugplay.bidding.ppm.PpmInterstitialFragment"
android:label="In-App Interstitial" />

<fragment
android:id="@+id/navigation_in_app_interstitial_close_button_area"
android:name="org.prebid.mobile.renderingtestapp.plugplay.bidding.ppm.PpmInterstitialCloseButtonAreaFragment"
android:label="In-App Interstitial Close Button Area" />

<fragment
android:id="@+id/navigation_in_app_interstitial_sound_button"
android:name="org.prebid.mobile.renderingtestapp.plugplay.bidding.ppm.PpmInterstitialSoundButtonFragment"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<string name="demo_bidding_in_app_interstitial_video_320_480_deeplink">Video Interstitial 320x480 Deeplink+ (In-App)</string>
<string name="demo_bidding_in_app_interstitial_video_320_480_skipoffset">Video Interstitial 320x480 SkipOffset (In-App)</string>
<string name="demo_bidding_in_app_interstitial_video_320_480_end_card">Video Interstitial 320x480 With End Card (In-App)</string>
<string name="demo_bidding_in_app_interstitial_video_320_480_end_card_close_button_area">Video Interstitial 320x480 With End Card and Close Button Area (In-App)</string>
<string name="demo_bidding_in_app_interstitial_video_320_480_end_card_sound_button">Video Interstitial 320x480 With End Card and Sound Button (In-App)</string>
<string name="demo_bidding_in_app_interstitial_video_320_480_mraid_end_card">Video Interstitial 320x480 with MRAID End Card (In-App)</string>
<string name="demo_bidding_in_app_interstitial_video_320_480_no_bids">Video Interstitial 320x480 (In-App) [noBids]</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.prebid.mobile.rendering.bidding.parallel;

import android.content.Context;
import androidx.annotation.FloatRange;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import org.prebid.mobile.ContentObject;
Expand All @@ -31,6 +32,7 @@
import org.prebid.mobile.rendering.errors.AdException;
import org.prebid.mobile.rendering.models.AdPosition;
import org.prebid.mobile.units.configuration.AdUnitConfiguration;
import org.prebid.mobile.units.configuration.Position;

import java.lang.ref.WeakReference;
import java.util.Map;
Expand Down Expand Up @@ -182,6 +184,22 @@ public void setMaxVideoDuration(int seconds) {
adUnitConfig.setMaxVideoDuration(seconds);
}

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

/**
* Sets close button position on the screen. Suitable values TOP_LEFT and TOP_RIGHT.
* Default value TOP_RIGHT.
*/
public void setCloseButtonPosition(@Nullable Position closeButtonPosition) {
adUnitConfig.setCloseButtonPosition(closeButtonPosition);
}

/**
* Cleans up resources when destroyed.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,13 @@ protected Activity getActivity() {
}

protected void addCloseView() {
if (mAdViewContainer == null) {
if (mAdViewContainer == null || mInterstitialManager == null) {
LogUtil.error(TAG, "Unable to add close button. Container is null");
return;
}

mCloseView = Utils.createCloseView(mContextReference.get());
InterstitialDisplayPropertiesInternal properties = mInterstitialManager.getInterstitialDisplayProperties();
mCloseView = Utils.createCloseView(mContextReference.get(), properties);

if (mCloseView == null) {
LogUtil.error(TAG, "Unable to add close button. Close view is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public static void configureDisplayProperties(AdUnitConfiguration adConfiguratio
displayProperties.isRotationEnabled = true;
}

displayProperties.closeButtonArea = adConfiguration.getCloseButtonArea();
displayProperties.isSoundButtonVisible = adConfiguration.isSoundButtonVisible();
displayProperties.isMuted = adConfiguration.isMuted();
displayProperties.closeButtonPosition = adConfiguration.getCloseButtonPosition();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,21 @@
package org.prebid.mobile.rendering.models;

import android.content.pm.ActivityInfo;
import org.prebid.mobile.units.configuration.Position;

public class InterstitialDisplayPropertiesInternal extends InterstitialDisplayPropertiesPublic {

public int expandWidth;
public int expandHeight;
public double closeButtonArea = 0;
public int orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
public boolean isSoundButtonVisible = false;
public boolean isMuted = false;

public boolean isRotationEnabled = false;

public Position closeButtonPosition = Position.TOP_RIGHT;

public void resetExpandValues() {
expandHeight = 0;
expandWidth = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.prebid.mobile.rendering.utils.helpers;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
Expand All @@ -28,17 +29,18 @@
import android.view.*;
import android.webkit.MimeTypeMap;
import android.widget.FrameLayout;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.prebid.mobile.LogUtil;
import org.prebid.mobile.core.R;
import org.prebid.mobile.rendering.models.InterstitialDisplayPropertiesInternal;
import org.prebid.mobile.rendering.networking.BaseNetworkTask;
import org.prebid.mobile.rendering.parser.AdResponseParserVast;
import org.prebid.mobile.rendering.video.vast.VAST;
import org.prebid.mobile.units.configuration.Position;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
Expand Down Expand Up @@ -429,41 +431,108 @@ public static Map<String, String> getQueryMap(String query) {
return map;
}

private static final int MIN_BUTTON_SIZE_DP = 30;

public static View createCloseView(Context context) {
return createView(context, R.layout.lyt_close, Gravity.END | Gravity.TOP);
return createCloseView(context, null);
}

public static View createSoundView(Context context) {
return createView(context, R.layout.lyt_sound, Gravity.END | Gravity.BOTTOM);
public static View createCloseView(
Context context,
InterstitialDisplayPropertiesInternal properties
) {
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);

params.gravity = Gravity.END | Gravity.TOP;
if (properties != null && properties.closeButtonPosition == Position.TOP_LEFT) {
params.gravity = Gravity.START | Gravity.TOP;
}

closeView.setLayoutParams(params);
return closeView;
}

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

int screenSize = getSmallestScreenSideSize(context);
int buttonSize = (int) (screenSize * properties.closeButtonArea);
if (convertPxToDp(buttonSize, context) < MIN_BUTTON_SIZE_DP) {
buttonSize = convertDpToPx(MIN_BUTTON_SIZE_DP, context);
}
int padding = (int) (buttonSize * 0.2);
view.setPadding(padding, padding, padding, padding);
return new FrameLayout.LayoutParams(buttonSize, buttonSize);
}

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

View view = LayoutInflater.from(context).inflate(layoutId, null);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
View view = LayoutInflater.from(context).inflate(R.layout.lyt_sound, null);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
params.gravity = gravity;
params.gravity = Gravity.END | Gravity.BOTTOM;
view.setLayoutParams(params);
return view;
}

private static int getSmallestScreenSideSize(Context context) {
try {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int result = Math.min(displayMetrics.widthPixels, displayMetrics.heightPixels);
if (result > 0) {
return result;
}
} catch (Exception exception) {}
DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
return Math.min(displayMetrics.heightPixels, displayMetrics.widthPixels);
}

public static int convertPxToDp(
int px,
Context context
) {
return (int) (px / ((float) context.getResources()
.getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT));
}

public static int convertDpToPx(
int dp,
Context context
) {
return (int) (dp * ((float) context.getResources()
.getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT));
}

public static View createWatchAgainView(Context context) {
if (context == null) {
LogUtil.error(TAG, "Unable to create watch again view. Context is null");
return null;
}
View watchAgainView = LayoutInflater.from(context).inflate(R.layout.lyt_watch_again, null);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
params.gravity = Gravity.CENTER;
watchAgainView.setLayoutParams(params);
return watchAgainView;
Expand Down
Loading

0 comments on commit b4e2d70

Please sign in to comment.