package com.mugua.mgvideo.util;
|
|
import android.app.Activity;
|
import android.app.Dialog;
|
import android.content.Context;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.widget.FrameLayout;
|
|
import com.lcjian.library.util.SystemCommon;
|
import com.mugua.mgvideo.R;
|
import com.qq.e.ads.cfg.VideoOption;
|
import com.qq.e.ads.nativ.ADSize;
|
import com.qq.e.ads.nativ.NativeExpressAD;
|
import com.qq.e.ads.nativ.NativeExpressADView;
|
import com.qq.e.comm.util.AdError;
|
import com.yeshi.base.entity.ad.AdPositionEnum;
|
import com.yeshi.base.utils.ad.AdUtil;
|
import com.yeshi.base.utils.ad.GDTConstant;
|
|
import java.util.List;
|
|
/**
|
* Created by weikou2015 on 2017/2/28.
|
*/
|
|
public class ExitDialog extends Dialog {
|
public ExitDialog(Context context) {
|
super(context);
|
this.setCancelable(false);
|
}
|
|
public ExitDialog(Context context, int theme) {
|
super(context, theme);
|
this.setCancelable(false);
|
}
|
|
public static class Builder {
|
private Activity context;
|
private String positiveButtonText;
|
private String negativeButtonText;
|
private OnClickListener positiveButtonClickListener;
|
private OnClickListener negativeButtonClickListener;
|
|
public Builder(Activity context) {
|
this.context = context;
|
}
|
|
|
/**
|
* Set the positive button resource and it's listener
|
*
|
* @param positiveButtonText
|
* @return
|
*/
|
public Builder setPositiveButton(int positiveButtonText,
|
OnClickListener listener) {
|
this.positiveButtonText = (String) context
|
.getText(positiveButtonText);
|
this.positiveButtonClickListener = listener;
|
return this;
|
}
|
|
public Builder setPositiveButton(String positiveButtonText,
|
OnClickListener listener) {
|
this.positiveButtonText = positiveButtonText;
|
this.positiveButtonClickListener = listener;
|
return this;
|
}
|
|
public Builder setNegativeButton(int negativeButtonText,
|
OnClickListener listener) {
|
this.negativeButtonText = (String) context
|
.getText(negativeButtonText);
|
this.negativeButtonClickListener = listener;
|
return this;
|
}
|
|
public Builder setNegativeButton(String negativeButtonText,
|
OnClickListener listener) {
|
this.negativeButtonText = negativeButtonText;
|
this.negativeButtonClickListener = listener;
|
return this;
|
}
|
|
FrameLayout fl_advertisement;
|
|
public ExitDialog create() {
|
LayoutInflater inflater = (LayoutInflater) context
|
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
// instantiate the dialog with the custom Theme
|
final ExitDialog dialog = new ExitDialog(context, R.style.Dialog);
|
View layout = inflater.inflate(R.layout.item_exit, null);
|
fl_advertisement = layout.findViewById(R.id.fl_advertisement);
|
nativeAd();
|
dialog.addContentView(layout, new FrameLayout.LayoutParams(
|
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
|
// set the confirm button
|
layout.findViewById(R.id.tv_login)
|
.setOnClickListener(new View.OnClickListener() {
|
public void onClick(final View v) {
|
if (dialog.isShowing())
|
dialog.dismiss();
|
}
|
});
|
layout.findViewById(R.id.tv_cancle)
|
.setOnClickListener(new View.OnClickListener() {
|
public void onClick(View v) {
|
dialog.dismiss();
|
context.finish();
|
}
|
});
|
dialog.setContentView(layout);
|
|
android.view.WindowManager.LayoutParams params = dialog.getWindow()
|
.getAttributes();
|
params.width = (int) ((SystemCommon.getScreenWidth(context) * 3) / 4);
|
params.height = android.view.WindowManager.LayoutParams.WRAP_CONTENT;
|
dialog.getWindow().setAttributes(params);
|
dialog.setCanceledOnTouchOutside(false);
|
return dialog;
|
}
|
|
// 1.加载广告,先设置加载上下文环境和条件
|
private void nativeAd() {
|
if (AdUtil.getAdType(context, AdPositionEnum.exitApp) == null)
|
return;
|
NativeExpressAD nativeExpressAD = new NativeExpressAD(context, new ADSize(ADSize.FULL_WIDTH, ADSize.AUTO_HEIGHT),
|
GDTConstant.GDT_EXIT_DIALOG, new NativeExpressAD.NativeExpressADListener() {
|
@Override
|
public void onADLoaded(final List<NativeExpressADView> list) {
|
fl_advertisement.post(new Runnable() {
|
@Override
|
public void run() {
|
if (null != list && list.size() > 0) {
|
fl_advertisement.removeAllViews();
|
NativeExpressADView adView = list.get(0);
|
adView.render();
|
FrameLayout parent = (FrameLayout) adView.getParent();
|
if (parent != null) {
|
parent.removeAllViews();
|
}
|
fl_advertisement.addView(adView);
|
}
|
}
|
});
|
|
}
|
|
@Override
|
public void onRenderFail(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onRenderSuccess(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onADExposure(NativeExpressADView nativeExpressADView) {
|
}
|
|
@Override
|
public void onADClicked(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onADClosed(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onADLeftApplication(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onADOpenOverlay(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onADCloseOverlay(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onNoAD(AdError adError) {
|
}
|
}); // 传入Activity
|
// 注意:如果您在联盟平台上新建原生模板广告位时,选择了支持视频,那么可以进行个性化设置(可选)
|
nativeExpressAD.setVideoOption(new VideoOption.Builder()
|
.setAutoPlayPolicy(VideoOption.AutoPlayPolicy.WIFI) // WIFI 环境下可以自动播放视频
|
.setAutoPlayMuted(true) // 自动播放时为静音
|
.build()); //
|
nativeExpressAD.loadAD(2);
|
}
|
|
}
|
}
|