MainActivity.java 19.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
package com.quzizi.fbbubble;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import androidx.appcompat.app.AppCompatActivity;

import com.adControler.AdControler;
import com.adListener.RewardedVideoListener;
import com.framework.pay.GooglePay;
import com.framework.pay.OnPayListener;
import com.plugin.analytics.AnalyticsControl;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, OnPayListener {
    private String TAG = MainActivity.class.getSimpleName();
    private TextView mSetUserId, mGetUserId;
    private TextView mUserProperty, mMultipleProperty;
    private TextView mUserAddProperty, mUserAddMultipleProperty;
    private TextView mUserOnceProperty, mUserMultipleOnceProperty;
    private TextView mEvent, mMarketEvent;
    private TextView mShowBanner, mHideBanner, mShowInterstitial, mShowRewardVideo, mShowOpen, mShowInGame, mHideInGame;
    private Activity mActivity;
    private TextView mPayConfig, mGoogleProduct, mOwnedProducts, mPay, mConsumePurchase;
    private JSONArray mInAppProductPayArray;
    private ArrayList<String> mInAppProductIdList;
    private JSONObject mPayConfigObject;
    private String mPaySuccess;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setListener();
        mActivity = this;
        //url="https://collect.playdayy.com/"
        AnalyticsControl.activeThinkingData(mActivity, getString(R.string.thinkingDataKey),getString(R.string.url),true);
//        AdControler.useTestServer();
        AdControler.setTestDevice(getString(R.string.facebookDeviceId), "C943FE291012713B9FCAD54152C4032F");//上线前注释
        final RelativeLayout layout = new RelativeLayout(this);
        //layout全屏的方式添加到当前界面中
        addContentView(layout, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        AdControler.init(mActivity, layout, false);//初始化 在确保在主进程中
        AdControler.start();//开始拉取广告配置,自动加载广告id
        //是否需要下发订单信息
        GooglePay.initPaySDKWithNewApi(mActivity,true);
        GooglePay.useCnServer();  //测试服务器
        try{
            GooglePay.setPayListener(this);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void initView() {
        mSetUserId = findViewById(R.id.setId);//设置用户属性
        mGetUserId = findViewById(R.id.getId);//获取用户属性

        mUserProperty = findViewById(R.id.property);//设置用户属性
        mMultipleProperty = findViewById(R.id.multipleProperty);//设置多参用户属性

        mUserAddProperty = findViewById(R.id.addProperty);//用户累计属性
        mUserAddMultipleProperty = findViewById(R.id.addMultipleProperty);//多元用户累加属性

        mUserOnceProperty = findViewById(R.id.onceProperty);//设置一次性用户属性
        mUserMultipleOnceProperty = findViewById(R.id.multipleOnceProperty);//多元用户属性

        mEvent = findViewById(R.id.event);//事件打点
        mMarketEvent = findViewById(R.id.marketEvent);//投放打点

        mShowBanner = findViewById(R.id.showBanner);//展示Banner
        mHideBanner = findViewById(R.id.hideBanner);//隐藏插屏
        mShowInterstitial = findViewById(R.id.showInterstitial);//展示插屏
        mShowRewardVideo = findViewById(R.id.showRewardVideo);//展示激励视频
        mShowOpen = findViewById(R.id.showOpenAd);//展示开屏广告
        mShowInGame = findViewById(R.id.showIngame);//展示Ingame
        mHideInGame = findViewById(R.id.hideIngame);//隐藏Ingame

        //
        mPayConfig = findViewById(R.id.payConfig);
        mGoogleProduct = findViewById(R.id.googleProduct);//隐藏Ingame
        mOwnedProducts = findViewById(R.id.ownedProducts);//隐藏Ingame
        mPay = findViewById(R.id.pay);//隐藏Ingame
        mConsumePurchase = findViewById(R.id.consumePurchase);//消耗商品
    }

    /**
     *
     */
    private void setListener() {
        mSetUserId.setOnClickListener(this);
        mGetUserId.setOnClickListener(this);
        mUserProperty.setOnClickListener(this);
        mMultipleProperty.setOnClickListener(this);
        mUserAddProperty.setOnClickListener(this);
        mUserAddMultipleProperty.setOnClickListener(this);
        mUserOnceProperty.setOnClickListener(this);
        mUserMultipleOnceProperty.setOnClickListener(this);
        mEvent.setOnClickListener(this);
        mMarketEvent.setOnClickListener(this);
        //广告
        mShowBanner.setOnClickListener(this);
        mHideBanner.setOnClickListener(this);
        mShowInterstitial.setOnClickListener(this);
        mShowRewardVideo.setOnClickListener(this);
        mShowOpen.setOnClickListener(this);
        mShowInGame.setOnClickListener(this);
        mHideInGame.setOnClickListener(this);
        //支付
        mPayConfig.setOnClickListener(this);
        mGoogleProduct.setOnClickListener(this);
        mOwnedProducts.setOnClickListener(this);
        mPay.setOnClickListener(this);
        mConsumePurchase.setOnClickListener(this);
        //设置激励视频监听
        AdControler.setRewardedAdListener(new RewardedVideoListener() {
            @Override
            public void rewaredVideoReady() {
                //激励视频加载好了
                Toast.makeText(mActivity, R.string.rewardVideoReady, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void rewaredVideoCompleted() {
                //激励视频领取奖励
                Log.i(TAG, "MainActivity>>>rewaredVideoCompleted: ==");
                Toast.makeText(mActivity, R.string.obtainReward, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void rewardVideoFailed() {
                //激励视频播放失败
                Log.i(TAG, "MainActivity>>>rewardVideoFailed: ==");
            }
        });

    }

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

    @Override
    protected void onPause() {
        super.onPause();
        AnalyticsControl.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AnalyticsControl.onDestroy();
        GooglePay.destroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        GooglePay.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.setId:
                AnalyticsControl.setUserId(mActivity, "2020mmddhhss");
                break;
            case R.id.getId:
                String usrId = AnalyticsControl.getUserId(mActivity);//该id 通过setUserId设置的数据
                Log.i(TAG, "MainActivity>>>onClick: 获取到的用户id==" + usrId);
                String distinctId = AnalyticsControl.getUserDistinctId(mActivity); //改id 是thinkingdata 自动生成的
                Log.i(TAG, "MainActivity>>>onClick: 获取到的访客id==" + distinctId);
                break;
            case R.id.property:
                AnalyticsControl.setUserProperty(mActivity, "demoKey", "demoValue"); //设置用户属性
                break;
            case R.id.multipleProperty:
                JSONObject object = new JSONObject();
                try {
                    object.put("demoKeyString", "demoValue");
                    object.put("demoKeyInt", 2020);
                    object.put("demoKeyDouble", 2020.8);
                    object.put("demoKeyBoolean", true);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                AnalyticsControl.setUserProperties(mActivity, object);
                break;
            case R.id.addProperty:
                //每触发一次属性demoKeyAddInt的值就增加1
                AnalyticsControl.setAddedProperty(mActivity, "demoKeyAddInt", 1);
                break;
            case R.id.addMultipleProperty:
                JSONObject addObject = new JSONObject();
                try {
                    addObject.put("demoKeyAddInt", 2);
                    addObject.put("demoKeyDouble", 2020.8);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //每触发一次demoKeyAddInt属性值增加2,demoKeyDouble属性值增加2020.8
                AnalyticsControl.setAddedProperties(mActivity, addObject);
                break;
            case R.id.onceProperty:
                Date currentTime = new Date();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //无论触发多少次,demoRegisterTime的属性值永远是安装后第一次设置的时间
                AnalyticsControl.setOnceProperty(mActivity, "demoRegisterTime", formatter.format(currentTime));//

                break;
            case R.id.multipleOnceProperty:
                try {
                    JSONObject objectOnnce = new JSONObject();
                    objectOnnce.put("demoRegisterVersion", "1.0.0");
                    objectOnnce.put("demoRegisterData", new Date());
                    AnalyticsControl.setOnceProperties(mActivity, objectOnnce);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.event:
                try {
                    JSONObject eventObject = new JSONObject();
                    eventObject.put("demoTime", new Date());
                    eventObject.put("demoNum", 1);
                    AnalyticsControl.sendEvent("demoEventName", eventObject.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            case R.id.marketEvent:
                try {

                    Bundle bundle = new Bundle();
                    bundle.putString("demoKey", "demoValue");
                    AnalyticsControl.sendFacebookEvent("demoEventName", bundle);
                    AnalyticsControl.sendFirebaseEvent("tf_demoEventName", bundle);
                    Map<String, String> map = new TreeMap<>();
                    map.put("demoKey", "demoValue");
                    AnalyticsControl.sendAppsFlyerEvent(mActivity, "demoEventName", map);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            case R.id.showBanner:
                if (!AdControler.isBannerReady()) {
                    Toast.makeText(mActivity, R.string.bannerNoFill, Toast.LENGTH_SHORT).show();
                }
                AdControler.showBottomADBannar(AdControler.ADPOS_BOTTOM);


                break;
            case R.id.hideBanner:
                AdControler.hiddenBannarWithStack();

                break;
            case R.id.showInterstitial:
                if (AdControler.isInterstitialReady()) {
                    AdControler.showInterstitialAD();
                } else {
                    Toast.makeText(mActivity, R.string.interstitialNoFill, Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.showRewardVideo:
                if (AdControler.isRewardVideoReady()) {
                    AdControler.showRewardVideo();
                } else {
                    Toast.makeText(mActivity, R.string.rewardVideoNoFill, Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.showOpenAd:
                if (AdControler.isOpenAdReady()) {
                    AdControler.showOpenAd();
                } else {
                    Toast.makeText(mActivity, R.string.openAdNoFill, Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.showIngame:
                JSONObject params = new JSONObject();
                try {
                    JSONObject objectParams = new JSONObject();
                    objectParams.put("w", 800);
                    objectParams.put("h", 800);
                    objectParams.put("top", 50);
                    objectParams.put("centerH", 10);
                    params.put("view", objectParams);
                    objectParams = new JSONObject();
                    objectParams.put("w", 150);
                    objectParams.put("h", 150);
                    objectParams.put("top", 10);
                    objectParams.put("left", 10);
                    params.put("icon", objectParams);
                    objectParams = new JSONObject();
                    objectParams.put("w", 500);
                    objectParams.put("h", 150);
                    objectParams.put("top", 20);
                    objectParams.put("left", 170);
                    objectParams.put("color", "#000000");
                    objectParams.put("size", "24");
                    params.put("title", objectParams);
                    objectParams = new JSONObject();
                    objectParams.put("w", 700);
                    objectParams.put("h", 100);
                    objectParams.put("top", 170);
                    objectParams.put("left", 50);
                    objectParams.put("color", "#000000");
                    objectParams.put("size", "16");
                    params.put("body", objectParams);
                    objectParams = new JSONObject();
                    objectParams.put("w", 800);
                    objectParams.put("h", 400);
                    objectParams.put("top", 300);
                    objectParams.put("left", 0);
                    params.put("media", objectParams);
                    objectParams = new JSONObject();
                    objectParams.put("w", 500);
                    objectParams.put("h", 80);
                    objectParams.put("color", "#0000ff");
                    objectParams.put("bottom", 10);
                    objectParams.put("centerH", 20);
                    params.put("button", objectParams);
                    objectParams = new JSONObject();
                    objectParams.put("color", "#ff0000");
                    objectParams.put("size", "20");
                    params.put("action", objectParams);
                } catch (Exception e) {
                    Log.i(TAG, "MainActivity>>>onClick: ==" + e.toString());
                }
                if (!AdControler.isInGameReady()) {
                    Toast.makeText(mActivity, R.string.inGameNoFill, Toast.LENGTH_SHORT).show();
                }

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Log.i(TAG, "MainActivity>>>run: ==" + params.toString());
                        AdControler.showInGameAD(params.toString());
                    }
                }).start();
                break;
            case R.id.hideIngame:
                AdControler.hiddenInGameAD();
                break;
            //支付系统
            case R.id.payConfig:
                String payMessage = GooglePay.getPayConfig();
                Log.i(TAG, "MainActivity>>>onClick: =支付数据=" + payMessage);
                try {
                    mPayConfigObject = new JSONObject(payMessage);
                    //内购订单
                    mInAppProductPayArray = mPayConfigObject.getJSONArray("inAppProductInfo");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.googleProduct:
                mInAppProductIdList = new ArrayList<>();
                try {
                    if (mInAppProductPayArray == null) {
                        Toast.makeText(mActivity, R.string.inspectionProcess, Toast.LENGTH_SHORT).show();
                        return;
                    }
                    for (int i = 0; i < mInAppProductPayArray.length(); i++) {
                        JSONObject inAppProductMessage = mInAppProductPayArray.getJSONObject(i);
                        String id = inAppProductMessage.getString("payId");
                        Log.i(TAG, "MainActivity>>>onClick: ==" + id);
                        mInAppProductIdList.add(id);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                GooglePay.requestProducts(mInAppProductIdList, null);
                break;
            case R.id.ownedProducts:
                ArrayList<String> ownedProductList = GooglePay.getOwnedProducts();
                Log.i(TAG, "MainActivity>>>onClick: ==" + ownedProductList);
                break;
            case R.id.pay:
                if (mInAppProductIdList == null || mInAppProductPayArray == null || mPayConfigObject == null || mInAppProductIdList.size() == 0 ) {
                    Toast.makeText(mActivity,  R.string.inspectionProcess, Toast.LENGTH_SHORT).show();
                    return;
                }
                try {
                    JSONObject payObject = new JSONObject();
                    payObject.put("productId", mInAppProductIdList.get(0));//这里随机支付第一个商品的id
                    payObject.put("time", System.currentTimeMillis() / 1000);
                    payObject.put("name", mInAppProductPayArray.getJSONObject(0).get("name"));
                    payObject.put("group", mPayConfigObject.get("groupIndex"));
                    payObject.put("consumable", mPayConfigObject.get("consumable"));
                    payObject.put("shopItemId", mPayConfigObject.get("shopItemId"));
                    GooglePay.payForProduct(payObject.toString(), false,"scene","USD",0,0);//这个是内购商品,不是订阅商品
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            case R.id.consumePurchase:
                if (mPaySuccess != null) {
                    GooglePay.consumePurchase(mPaySuccess, false);//此处支付的不是订阅商品,
                } else {
                    Toast.makeText(mActivity, "没有拿到支付成功数据", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    public void onPaySuccess(String s) {
        //支付成功 的数据s 消耗时使用
        Log.i(TAG, "MainActivity>>>onPaySuccess: ==" + s);
        mPaySuccess = s;
    }

    @Override
    public void onPayFailed(String s) {
        Log.i(TAG, "MainActivity>>>onPayFailed: 支付失败==" + s);
    }

    @Override
    public void onPayCancel() {
        Log.i(TAG, "MainActivity>>>onPayCancel: ==");
    }

    @Override
    public void onRequestProducts(ArrayList arrayList) {
        Log.i(TAG, "MainActivity>>>onRequestProducts: ==" + arrayList.toString());
    }

    @Override
    public void onQuerySubsProducts(ArrayList arrayList) {
        Log.i(TAG, "MainActivity>>>onQuerySubsProducts: ==");
    }

    @Override
    public void onConfigData(String s) {
        Log.i(TAG, "MainActivity>>>onConfigData: ==" + s);
    }

    @Override
    public void onConfigDataFailed(String s) {
        Log.i(TAG, "MainActivity>>>onConfigDataFailed: ==" + s);
    }

    @Override
    public void onOrderUploaded() {
        Log.i(TAG, "MainActivity>>>onOrderUploaded: ==");
    }

    @Override
    public void onOrderUploadedFailed() {
        Log.i(TAG, "MainActivity>>>onOrderUploadedFailed: ==");
    }
}