|
|
@@ -0,0 +1,480 @@
|
|
|
+package com.cocos.game;
|
|
|
+
|
|
|
+import android.Manifest;
|
|
|
+import android.app.Activity;
|
|
|
+import android.content.ClipData;
|
|
|
+import android.content.ClipboardManager;
|
|
|
+import android.content.Intent;
|
|
|
+import android.content.SharedPreferences;
|
|
|
+import android.content.pm.PackageManager;
|
|
|
+import android.content.res.Configuration;
|
|
|
+import android.net.Uri;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.support.annotation.NonNull;
|
|
|
+import android.support.v4.app.ActivityCompat;
|
|
|
+import android.util.Log;
|
|
|
+import android.view.LayoutInflater;
|
|
|
+import android.view.View;
|
|
|
+import android.view.WindowManager;
|
|
|
+import android.widget.RelativeLayout;
|
|
|
+
|
|
|
+import com.anythink.splashad.api.ATSplashAd;
|
|
|
+import com.aries.kxnly.mz.BuildConfig;
|
|
|
+import com.aries.kxnly.mz.R;
|
|
|
+import com.aries.kxnly.mz.ad.FullScreenAD;
|
|
|
+import com.aries.kxnly.mz.ad.InterstitalAD;
|
|
|
+import com.aries.kxnly.mz.ad.NativeExpressAD;
|
|
|
+import com.aries.kxnly.mz.ad.RewardVideoAD;
|
|
|
+import com.aries.kxnly.mz.ad.SplashADActivity;
|
|
|
+import com.aries.kxnly.mz.utils.CheckUtil;
|
|
|
+import com.aries.kxnly.mz.utils.DownloadUtil;
|
|
|
+import com.aries.kxnly.mz.utils.MiitHelper;
|
|
|
+import com.aries.kxnly.mz.utils.MobileInfoUtil;
|
|
|
+import com.aries.kxnly.mz.utils.UMUtil;
|
|
|
+import com.aries.kxnly.mz.utils.WechatUtil;
|
|
|
+import com.cocos.lib.CocosActivity;
|
|
|
+import com.cocos.lib.CocosHelper;
|
|
|
+import com.cocos.lib.CocosJavascriptJavaBridge;
|
|
|
+import com.cocos.service.SDKWrapper;
|
|
|
+import com.duoyou.task.openapi.DyAdApi;
|
|
|
+import com.umeng.analytics.MobclickAgent;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class MainActivity extends CocosActivity {
|
|
|
+
|
|
|
+ public static Activity app;
|
|
|
+
|
|
|
+ private static boolean isSupportOaid = true;
|
|
|
+
|
|
|
+ private static final int REQUEST_PERMISSIONS_CODE = 100;
|
|
|
+
|
|
|
+ private static int errorCode;
|
|
|
+
|
|
|
+ private static String OAID;
|
|
|
+
|
|
|
+ private static RewardVideoAD rewardVideoAD;
|
|
|
+ private static InterstitalAD interstitalAD;
|
|
|
+ private static FullScreenAD fullScreenAD;
|
|
|
+ // private static BannerAD bannerAD;
|
|
|
+ private static NativeExpressAD nativeExpressAD;
|
|
|
+
|
|
|
+ // private static ViewGroup viewGroup;
|
|
|
+ private static RelativeLayout relativeLayout;
|
|
|
+
|
|
|
+
|
|
|
+ private static MobileInfoUtil mobileInfoUtil;
|
|
|
+ private View splash;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ if (!isTaskRoot()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ SDKWrapper.shared().init(this);
|
|
|
+ getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//屏幕常亮
|
|
|
+
|
|
|
+ app = this;
|
|
|
+ firstStart();
|
|
|
+ new MiitHelper(appIdsUpdater).getDeviceIds(this);
|
|
|
+ splash = LayoutInflater.from(this).inflate(R.layout.splash, null);
|
|
|
+ mFrameLayout.addView(splash);
|
|
|
+ initLoadAD();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isSupportOaid() {
|
|
|
+ return isSupportOaid;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void setIsSupportOaid(boolean isSupportOaid) {
|
|
|
+ MainActivity.isSupportOaid = isSupportOaid;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void setIsSupportOaid(boolean isSupportOaid, int ErrorCode) {
|
|
|
+ MainActivity.isSupportOaid = isSupportOaid;
|
|
|
+ MainActivity.errorCode = ErrorCode;
|
|
|
+ }
|
|
|
+
|
|
|
+ private MiitHelper.AppIdsUpdater appIdsUpdater = ids -> MainActivity.OAID = ids;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化预加载广告
|
|
|
+ */
|
|
|
+ public static void initLoadAD() {
|
|
|
+ rewardVideoAD = new RewardVideoAD(app);
|
|
|
+ rewardVideoAD.loadRewardVideoAD();
|
|
|
+ interstitalAD = new InterstitalAD(app);
|
|
|
+ interstitalAD.loadInterstitalAD();
|
|
|
+ fullScreenAD = new FullScreenAD(app);
|
|
|
+ fullScreenAD.loadInterstitalAD();
|
|
|
+
|
|
|
+ //横幅广告设置布局文件
|
|
|
+// View layoutBanner = LayoutInflater.from(app).inflate(R.layout.banner_ad, null);
|
|
|
+// mFrameLayout.addView(layoutBanner);
|
|
|
+// viewGroup = app.findViewById(R.id.banner_container);
|
|
|
+// bannerAD = new BannerAD(app, viewGroup);
|
|
|
+// bannerAD.loadBannerAD();
|
|
|
+// viewGroup.setVisibility(View.GONE);
|
|
|
+
|
|
|
+ //原生广告设置布局文件
|
|
|
+ View layoutNative = LayoutInflater.from(app).inflate(R.layout.native_ad, null);
|
|
|
+ mFrameLayout.addView(layoutNative);
|
|
|
+ relativeLayout = app.findViewById(R.id.container);
|
|
|
+ nativeExpressAD = new NativeExpressAD(relativeLayout, app);
|
|
|
+ nativeExpressAD.initNativeAd();
|
|
|
+ nativeExpressAD.loadNativeAd();
|
|
|
+ relativeLayout.setVisibility(View.GONE);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onResume() {
|
|
|
+ super.onResume();
|
|
|
+ SDKWrapper.shared().onResume();
|
|
|
+ hideBottomUIMenu();
|
|
|
+ MobclickAgent.onResume(this);
|
|
|
+ /**
|
|
|
+ * 检测开屏数据
|
|
|
+ */
|
|
|
+ if (MainApplication.getInstance().splashAdInfo != null && !MainApplication.getInstance().splashAdInfo.equals("")) {
|
|
|
+ CocosHelper.runOnGameThread(() -> {
|
|
|
+ String splashAdInfo = MainApplication.getInstance().splashAdInfo + "";
|
|
|
+ CocosJavascriptJavaBridge.evalString(splashAdInfo);
|
|
|
+ MainApplication.getInstance().splashAdInfo = null;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onPause() {
|
|
|
+ super.onPause();
|
|
|
+ SDKWrapper.shared().onPause();
|
|
|
+ MobclickAgent.onPause(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onDestroy() {
|
|
|
+ super.onDestroy();
|
|
|
+ if (!isTaskRoot()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ SDKWrapper.shared().onDestroy();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
+ super.onActivityResult(requestCode, resultCode, data);
|
|
|
+ SDKWrapper.shared().onActivityResult(requestCode, resultCode, data);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onNewIntent(Intent intent) {
|
|
|
+ super.onNewIntent(intent);
|
|
|
+ SDKWrapper.shared().onNewIntent(intent);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onRestart() {
|
|
|
+ super.onRestart();
|
|
|
+ SDKWrapper.shared().onRestart();
|
|
|
+ hideBottomUIMenu();
|
|
|
+ SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
|
|
|
+ long lastTime = pref.getLong("time", 0);
|
|
|
+ if (lastTime != -1 && (System.currentTimeMillis() - lastTime) > 40000) {
|
|
|
+ jumpToSplashActivity();
|
|
|
+ }
|
|
|
+ if (DownloadUtil.apkList.size() > 0) {
|
|
|
+ DownloadUtil.installApk(this, DownloadUtil.apkList.get(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onStop() {
|
|
|
+ super.onStop();
|
|
|
+ SDKWrapper.shared().onStop();
|
|
|
+ SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
|
|
|
+ if (!CheckUtil.isOnForeground(this)) {
|
|
|
+ editor.putLong("time", System.currentTimeMillis());
|
|
|
+ } else {
|
|
|
+ editor.putLong("time", -1);
|
|
|
+ }
|
|
|
+ editor.commit();
|
|
|
+ if (!DownloadUtil.apkList.isEmpty()) {
|
|
|
+ DownloadUtil.apkList.remove(0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 跳转广告
|
|
|
+ */
|
|
|
+ public void jumpToSplashActivity() {
|
|
|
+ Intent intent = new Intent(this, SplashADActivity.class);
|
|
|
+ startActivity(intent);
|
|
|
+ overridePendingTransition(0, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void firstStart() {
|
|
|
+ boolean isFirstRun = hasPermissions();
|
|
|
+ if (isFirstRun) {
|
|
|
+ jumpToSplashActivity();//拉开屏广告
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onBackPressed() {
|
|
|
+ SDKWrapper.shared().onBackPressed();
|
|
|
+ super.onBackPressed();
|
|
|
+ hideBottomUIMenu();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onConfigurationChanged(Configuration newConfig) {
|
|
|
+ SDKWrapper.shared().onConfigurationChanged(newConfig);
|
|
|
+ super.onConfigurationChanged(newConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onRestoreInstanceState(Bundle savedInstanceState) {
|
|
|
+ SDKWrapper.shared().onRestoreInstanceState(savedInstanceState);
|
|
|
+ super.onRestoreInstanceState(savedInstanceState);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onSaveInstanceState(Bundle outState) {
|
|
|
+ SDKWrapper.shared().onSaveInstanceState(outState);
|
|
|
+ super.onSaveInstanceState(outState);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onStart() {
|
|
|
+ SDKWrapper.shared().onStart();
|
|
|
+ super.onStart();
|
|
|
+ hideBottomUIMenu();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onLowMemory() {
|
|
|
+ SDKWrapper.shared().onLowMemory();
|
|
|
+ super.onLowMemory();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查是否具有权限
|
|
|
+ */
|
|
|
+ public static boolean hasPermissions() {
|
|
|
+ if (PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(app, Manifest.permission.READ_PHONE_STATE)) {
|
|
|
+ return PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(app, Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查权限若不具有则申请
|
|
|
+ */
|
|
|
+ public static void checkCPermissions() {
|
|
|
+ List<String> listPermissions = new ArrayList<>();
|
|
|
+ if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(app, Manifest.permission.READ_PHONE_STATE)) {
|
|
|
+ listPermissions.add(Manifest.permission.READ_PHONE_STATE);
|
|
|
+ }
|
|
|
+ if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(app, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
|
|
|
+ listPermissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
|
|
+ }
|
|
|
+ //权限申请
|
|
|
+ if (listPermissions.size() != 0) {
|
|
|
+ String[] temp = new String[listPermissions.size()];
|
|
|
+ listPermissions.toArray(temp);
|
|
|
+ ActivityCompat.requestPermissions(app, temp, REQUEST_PERMISSIONS_CODE);//请求权限
|
|
|
+ } else {
|
|
|
+ mobileInfoUtil = new MobileInfoUtil();
|
|
|
+ setDeviceInfo();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
+ if (requestCode == REQUEST_PERMISSIONS_CODE) {
|
|
|
+ if (!hasPermissions()) {
|
|
|
+ finish();
|
|
|
+ } else {
|
|
|
+ mobileInfoUtil = new MobileInfoUtil();
|
|
|
+ setDeviceInfo();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 游戏运行成功请求
|
|
|
+ */
|
|
|
+ public static void setDeviceInfo() {
|
|
|
+ if (!hasPermissions()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ CheckUtil.checkVersion(app);
|
|
|
+ CocosHelper.runOnGameThread(() -> CocosJavascriptJavaBridge.evalString("g.deviceData.imei='" + mobileInfoUtil.getIMEI(app) + "';g.deviceData.oaid='" + OAID + "';g.deviceData.androId='" + mobileInfoUtil.getAndroidID(app) + "';g.deviceData.mac='" + mobileInfoUtil.getMac(app) + "';"));
|
|
|
+ //多玩游戏列表接入
|
|
|
+ DyAdApi.getDyAdApi().init(app, "dy_59635583", "4a8733e0dc51df641f52091759b0342d", BuildConfig.TF_CHANNEL, false);
|
|
|
+ DyAdApi.getDyAdApi().setOAID(OAID);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 虚拟按键遮挡问题
|
|
|
+ */
|
|
|
+ protected void hideBottomUIMenu() {
|
|
|
+ View decorView = getWindow().getDecorView();
|
|
|
+ int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
|
|
+ | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|
|
|
+ | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
|
|
+ | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
|
|
|
+ | View.SYSTEM_UI_FLAG_FULLSCREEN; // hide status bar
|
|
|
+
|
|
|
+ if (android.os.Build.VERSION.SDK_INT >= 19) {
|
|
|
+ uiFlags |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; //View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY: hide navigation bars - compatibility: building API level is lower thatn 19, use magic number directly for higher API target level
|
|
|
+ } else {
|
|
|
+ uiFlags |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
|
|
|
+ }
|
|
|
+ decorView.setSystemUiVisibility(uiFlags);
|
|
|
+ //处理bar弹出生成后无法隐藏
|
|
|
+ getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(arg0 -> {
|
|
|
+ hideBottomUIMenu();
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载APK
|
|
|
+ */
|
|
|
+ public static void downloadAPK(String url, String apkName, boolean visibility) {
|
|
|
+ app.runOnUiThread(() -> DownloadUtil.downloadApk(app, url, apkName, visibility));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 展示激励视频
|
|
|
+ */
|
|
|
+ public static void showRewardVideoAD() {
|
|
|
+ rewardVideoAD.showRewardVideoAD();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 展示插屏广告
|
|
|
+ */
|
|
|
+ public static void showInterstutalAD() {
|
|
|
+ interstitalAD.showInterstitalAD();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 展示全屏广告
|
|
|
+ */
|
|
|
+ public static void showFullScreenAD() {
|
|
|
+ fullScreenAD.showInterstitalAD();
|
|
|
+ }
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 展示横幅广告
|
|
|
+// */
|
|
|
+// public static void showBannerAD() {
|
|
|
+// app.runOnUiThread(() -> {
|
|
|
+// viewGroup.setVisibility(View.VISIBLE);
|
|
|
+// });
|
|
|
+// }
|
|
|
+//
|
|
|
+// public static void removeBannerAD() {
|
|
|
+// app.runOnUiThread(() -> {
|
|
|
+// viewGroup.setVisibility(View.GONE);
|
|
|
+// });
|
|
|
+// }
|
|
|
+
|
|
|
+ public static void showNativeAD() {
|
|
|
+ app.runOnUiThread(() -> {
|
|
|
+ relativeLayout.setVisibility(View.VISIBLE);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void removeNativeAD() {
|
|
|
+ app.runOnUiThread(() -> {
|
|
|
+ relativeLayout.setVisibility(View.GONE);
|
|
|
+ nativeExpressAD.changeAD();
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void removeSplash() {
|
|
|
+ app.runOnUiThread(() -> {
|
|
|
+ mFrameLayout.removeView(((MainActivity) app).splash);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 微信登录
|
|
|
+ */
|
|
|
+ public static void wxLogin() {
|
|
|
+ WechatUtil.wxLogin(app);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 微信分享
|
|
|
+ */
|
|
|
+ public static void wxShare(String url, String title, String descroption) {
|
|
|
+ WechatUtil.shareLinkToWX(0, app, url, title, descroption);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 友盟埋点统计
|
|
|
+ * 1、dayLogin 每日登录视频点击
|
|
|
+ * 2、turntable 转盘抽奖视频点击
|
|
|
+ * 3、dailyReward 每日奖励观看
|
|
|
+ * 4、freeRewardAD 主界面可领取视频观看
|
|
|
+ * 5、growVegetables 种菜点击
|
|
|
+ * 6、videoExpansion 视频扩充土地
|
|
|
+ * 7、lvReward 等级奖励点击
|
|
|
+ */
|
|
|
+ public static void umBuriedPoint(String eventID) {
|
|
|
+ //埋点主线程上报
|
|
|
+ app.runOnUiThread(() -> {
|
|
|
+ HashMap<String, String> map = new HashMap<String, String>();
|
|
|
+ map.put("channel", BuildConfig.TF_CHANNEL);
|
|
|
+ UMUtil.getInstance()._UMSDKEventMap(app, eventID, map);
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 打开URL
|
|
|
+ */
|
|
|
+ public static void openUrl(String url) {
|
|
|
+ Uri uri = Uri.parse(url);
|
|
|
+ Intent intent = new Intent(Intent.ACTION_VIEW, uri);
|
|
|
+ app.startActivity(intent);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 打开微信小程序
|
|
|
+ */
|
|
|
+ public static void openWXApplets(String userName, String path) {
|
|
|
+ WechatUtil.openWXApplets(app, userName, path);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 进入游戏列表
|
|
|
+ */
|
|
|
+ public static void intoGameList(String userId) {
|
|
|
+ DyAdApi.getDyAdApi().jumpAdList(app, userId, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 拷贝
|
|
|
+ */
|
|
|
+ public static void copyText(String text) {
|
|
|
+ //获取剪贴板管理器:
|
|
|
+ ClipboardManager cm = (ClipboardManager) app.getSystemService(app.CLIPBOARD_SERVICE);
|
|
|
+ // 创建普通字符型ClipData
|
|
|
+ ClipData mClipData = ClipData.newPlainText("Label", text);
|
|
|
+ // 将ClipData内容放到系统剪贴板里。
|
|
|
+ cm.setPrimaryClip(mClipData);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|