BaseNotifyActivity.java 11.4 KB
package com.lotus.town.notify;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ad.lib.AdInfo;
import com.ad.lib.AdManager;
import com.ad.lib.IAdCallback;
import com.ad.lib.RequestInfo;
import com.ad.lib.tt.TTInterstitialActivity;
import com.bumptech.glide.Glide;
import com.lotus.town.R;
import com.lotus.town.utils.NotificationUtils;
import com.manager.AdCache;
import com.sdk.EventBus;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public abstract class BaseNotifyActivity extends Activity implements View.OnClickListener , EventBus.Listener{

    protected TextView title;
    protected Context mContext;
    protected ImageView close;
    protected RelativeLayout delete_icon;
    protected int bigRetryTimes = 0;
    private int smallRetryTimes = 0;
    protected ImageView adCloseButton;

//    private Handler handler = new Handler(){
//        @Override
//        public void handleMessage(Message msg) {
//            initBigAd();
//            handler.sendEmptyMessageDelayed(0,8000);
//        }
//    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NotificationUtils.clearNotification(this);
        EventBus.getInstance().register(EventBus.PHONE_COMING,this);
        AdCache.getInstance().pushActivity(getBigAdNotifyType(),this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
        initLayout();
        mContext = this;
        setFinishOnTouchOutside(false);
        initView();
        initInfo();
        initSmallView();
        initBigView();
        initBigAd();
//        handler.sendEmptyMessageDelayed(0,8000);
//        initSmallAd();
    }

    protected void initLayout(){
//        setContentView(R.layout.notify_layout);notify_layout
    }

    abstract protected String getEventName();
    abstract protected String getEventClickName();
    abstract protected String getEventPageName();

    protected abstract RequestInfo getSmallPlacementId();
    protected abstract RequestInfo getBigPlacementId();

    @Override
    protected void onResume() {
        super.onResume();

        MobclickAgent.onEvent(this,getEventPageName());
        MobclickAgent.onPageStart(this.getLocalClassName()); //手动统计页面("SplashScreen"为页面名称,可自定义)
        MobclickAgent.onResume(this); //统计时长
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getLocalClassName()); //手动统计页面("SplashScreen"为页面名称,可自定义),必须保证 onPageEnd 在 onPause 之前调用,因为SDK会在 onPause 中保存onPageEnd统计到的页面数据。
        MobclickAgent.onPause(this);
    }

    protected abstract void initInfo();

    protected void initView(){
        close = findViewById(R.id.close);
        delete_icon = findViewById(R.id.delete_icon);
        close.setOnClickListener(this);
        adCloseButton = (ImageView)findViewById(R.id.close_ad);
    }

    ImageView smallAdFromLogo;
    ImageView smallAdImage;
    TextView smallTitle;
    TextView smallSubtTitle;
    Button smallButton;
    RelativeLayout smallLayout;

    protected void initSmallView(){
        smallAdFromLogo = (ImageView) findViewById(R.id.small_ad_from_logo);
        smallAdImage = (ImageView) findViewById(R.id.small_ad_image);
        smallTitle = (TextView) findViewById(R.id.small_title);
        smallSubtTitle = (TextView) findViewById(R.id.small_subtitle);
        smallButton = (Button)findViewById(R.id.small_btn);
        smallLayout = (RelativeLayout)findViewById(R.id.small_layout);
    }

    ImageView bigAdFromLogo;
    ImageView bigAdImage;
    ImageView bigAdIcon;
    TextView bigTitle;
    TextView bigSubtTitle;
    Button bigButton;
    LinearLayout bigLayout;

    protected void initBigView(){
        bigLayout = (LinearLayout) findViewById(R.id.big_layout);
        bigAdFromLogo = (ImageView) findViewById(R.id.big_ad_from_logo);
        bigAdImage = (ImageView) findViewById(R.id.big_ad_image);
        bigAdIcon = (ImageView) findViewById(R.id.big_ad_icon);
        bigTitle = (TextView) findViewById(R.id.big_ad_title);
        bigSubtTitle = (TextView) findViewById(R.id.big_ad_subtitle);
        bigButton = (Button)findViewById(R.id.big_btn);
    }

    protected abstract int getSmallAdType();
    protected abstract int getBitAdType();
    protected abstract int getBigAdNotifyType();
    protected void initSmallAd() {

    }
    protected void initBigAd() {
        RequestInfo rInfo = getBigPlacementId();
        final AdInfo info = AdCache.getInstance().getCacheAdInfo(getBigAdNotifyType());
        if(info == null){
            AdManager.getInstance().getAdController(this,getBitAdType()).loadNativeAd(rInfo, new IAdCallback() {
                @Override
                public void onADLoaded(final AdInfo info) {

                    if(info.getView() != null){
                        bigLayout.removeAllViews();
                        bigLayout.addView(info.getView());
                        info.getReporter().render();
                        return;
                    }
                    if(info.getAdIcon() != null) {
                        bigAdFromLogo.setImageBitmap(info.getAdIcon());
                    }
                    if(info.getImageList() != null && info.getImageList().size()>0){
                        Glide.with(mContext).load(info.getImageList().get(0)).into(bigAdImage);
                    } else {
                        Glide.with(mContext).load(info.getIconUrl()).into(bigAdImage);
                    }
                    Glide.with(mContext).load(info.getIconUrl()).into(bigAdIcon);
                    bigTitle.setText(info.getTitle());
                    bigSubtTitle.setText(info.getSubtitle());
                    bigButton.setText(info.getButtonLabel());
                    if(getBitAdType() == 0){
                        List<View> clickViewList = new ArrayList<>();
                        clickViewList.add(bigLayout);
                        clickViewList.add(bigButton);
                        info.getReporter().impress(bigLayout,clickViewList,getEventName(),getEventClickName());

                    } else if(getBitAdType() == 1 || getBitAdType() == 2){
                        info.getReporter().impress(bigLayout,getEventName());
                        info.getReporter().impress(bigButton,getEventName());
                        bigLayout.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                info.getReporter().click(view,getEventClickName()); // 点击接口
                            }
                        });
                        bigButton.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                info.getReporter().click(view,getEventClickName()); // 点击接口
                            }
                        });
                    }
                }

                @Override
                public void onADError() {
                    if (bigRetryTimes++ < 1){
                        initBigAd();
                    }
                }

                @Override
                public void onClicked() {
                    finish();
                }
            });
            return;
        }
        if(info != null && info.getReporter() != null) {
            info.getReporter().bindDislikeView(this, adCloseButton);
        }
        adCloseButton.setVisibility(View.VISIBLE);
        if (info.getView() != null) {
            bigLayout.removeAllViews();
            bigLayout.addView(info.getView());
            if(info != null && info.getReporter() != null) {
                info.getReporter().render();
            }
            return;
        }
        if (info.getAdIcon() != null) {
            bigAdFromLogo.setImageBitmap(info.getAdIcon());
        }
        if (info.getImageList() != null && info.getImageList().size() > 0) {
            Glide.with(mContext).load(info.getImageList().get(0)).into(bigAdImage);
        } else {
            Glide.with(mContext).load(info.getIconUrl()).into(bigAdImage);
        }
        Glide.with(mContext).load(info.getIconUrl()).into(bigAdIcon);
        bigTitle.setText(info.getTitle());
        bigSubtTitle.setText(info.getSubtitle());
        bigButton.setText(info.getButtonLabel());
        if (getBitAdType() == 0) {
            List<View> clickViewList = new ArrayList<>();
            clickViewList.add(bigLayout);
            clickViewList.add(bigButton);
            clickViewList.add(delete_icon);
            if(info != null && info.getReporter() != null) {
                info.getReporter().impress(bigLayout, clickViewList, getEventName(), getEventClickName());
            }
        } else if (getBitAdType() == 1 || getBitAdType() == 2) {
            info.getReporter().impress(bigLayout, getEventName());
            info.getReporter().impress(bigButton, getEventName());
            bigLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    info.getReporter().click(view, getEventClickName()); // 点击接口
                }
            });
            bigButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    info.getReporter().click(view, getEventClickName()); // 点击接口
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        if(v == close){
            int i = new Random().nextInt(100);
            Intent intent = new Intent();
            intent.setClass(this, TTInterstitialActivity.class);
            this.startActivity(intent);
            finish();
        }
    }
    private volatile boolean isBackPressed = false;
    @Override
    public void onBackPressed() {
        if(isBackPressed){
            return;
        }
        isBackPressed = true;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                int i = new Random().nextInt(100);
                Intent intent = new Intent();
                intent.setClass(BaseNotifyActivity.this, TTInterstitialActivity.class);
                BaseNotifyActivity.this.startActivity(intent);
                finish();
                isBackPressed = false;
            }
        },1500);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        if (handler.hasMessages(0)) {
//            handler.removeMessages(0);
//        }
        EventBus.getInstance().unregister(EventBus.PHONE_COMING,this);
        AdCache.getInstance().popActivity(getBigAdNotifyType());
    }

    @Override
    public void onEvent(int notify) {
        if(notify == EventBus.PHONE_COMING){
            finish();
        }
    }
}