package com.secondworld.universalsdk; import android.Manifest; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.content.res.Configuration; import android.net.Uri; import android.os.Bundle; import android.os.Process; import android.provider.Settings; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.PermissionChecker; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.widget.FrameLayout; import android.widget.RelativeLayout; import android.widget.Toast; import com.quicksdk.QuickSDK; import com.quicksdk.Sdk; import com.secondworld.univeralsdk.R; import com.unity3d.player.UnityPlayer; import com.unity3d.player.UnityPlayerActivity; public class MainActivity extends UnityPlayerActivity { private static final String TAG = "MainActivity"; public static boolean isForeground = false; // 启用6.0以上权限回调code private final int REQUEST_RECORD_PERMISSION_SETTING = 999; public static final int SHOW_TENCENT_LOGIN = 0; public static final int HIDE_TENCENT_LOGIN = 1; private RelativeLayout m_MainContainer; private int mType; private boolean isNotch = false; @Override protected void onCreate(Bundle savedInstanceState) { // sInstance = this; super.onCreate(savedInstanceState); // LogUtil.i(TAG, "onCreate"); LogUtil.init(this, true); setContentView(R.layout.activity_main); m_MainContainer = (RelativeLayout) findViewById(R.id.main_container); String _brand = NotchPhoneUtil.getDeviceBrand(); if (_brand.toUpperCase().contains("VIVO")) { isNotch = NotchPhoneUtil.HasNotchVivo(MainActivity.this); mType = 1; } else if (_brand.toUpperCase().contains("HUAWEI") || _brand.toUpperCase().contains("HONOR")) { // isNotch = NotchPhoneUtil.hasNotchAtHuawei(this);; // mType = 2; } else if (_brand.toUpperCase().contains("OPPO")) { isNotch = NotchPhoneUtil.HasNotchOPPO(MainActivity.this); mType = 3; } else if (_brand.toUpperCase().contains("XIAOMI")) { isNotch = NotchPhoneUtil.HasNotchXiaoMi(); mType = 4; } NotchPhoneUtil.onConfigurationChanged(this, isNotch, mType, m_MainContainer); FrameLayout _frameLayout = (FrameLayout) findViewById(R.id.unity_view); View unityView = mUnityPlayer.getView(); _frameLayout.addView(unityView); H2EngineSDK.onCreate(this, savedInstanceState); } @Override public void onWindowFocusChanged(boolean b) { //LogUtil.i(TAG, "onWindowFocusChanged: " + b); super.onWindowFocusChanged(b); H2EngineSDK.onWindowFocusChanged(b); } @Override protected void onNewIntent(Intent intent) { //LogUtil.i(TAG, "onNewIntent"); super.onNewIntent(intent); H2EngineSDK.onNewIntent(this, intent); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { //LogUtil.i(TAG, "onActivityResult"); H2EngineSDK.onActivityResult(requestCode, resultCode, data, this); super.onActivityResult(requestCode, resultCode, data); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (grantResults != null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // 申请成功 QuickPlatformUtil.getInstance().onCreate(); if (!QuickPlatformUtil.getInstance().isInited) { Sdk.getInstance().init(this, "88049844578484520615487574815873", "82414864"); } } else { // 失败 这里逻辑以游戏为准 这里只是模拟申请失败 退出游戏 cp方可改为继续申请 或者其他逻辑 Log.e("Unity", "onRequestPermissionsResult Fail"); if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) && ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_PHONE_STATE) && ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.RECORD_AUDIO)) { Log.d("Unity", "ActivityCompat shouldShowRequestPermissionRationale true"); } else { Log.e("Unity", "ActivityCompat shouldShowRequestPermissionRationale false"); final AlertDialog.Builder normalDialog = new AlertDialog.Builder(this); normalDialog.setTitle("权限设置"); normalDialog.setMessage("请在设置中打开权限"); normalDialog.setPositiveButton("前往应用设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // ...To-do Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); Uri uri = Uri.fromParts("package", getPackageName(), null); intent.setData(uri); startActivityForResult(intent, REQUEST_RECORD_PERMISSION_SETTING); QuickPlatformUtil.getInstance().onCreate(); Sdk.getInstance().init(UnityPlayer.currentActivity, "88049844578484520615487574815873", "82414864"); } }); normalDialog.setNegativeButton("关闭", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(UnityPlayer.currentActivity, "权限被拒绝", Toast.LENGTH_SHORT).show(); QuickPlatformUtil.getInstance().onCreate(); Sdk.getInstance().init(UnityPlayer.currentActivity, "88049844578484520615487574815873", "82414864"); } }); // 显示 normalDialog.show(); } } } @Override public void onConfigurationChanged(Configuration newConfig) { //LogUtil.i(TAG, "onConfigurationChanged"); NotchPhoneUtil.onConfigurationChanged(this, isNotch, mType, m_MainContainer); H2EngineSDK.onConfigurationChanged(newConfig); super.onConfigurationChanged(newConfig); } @Override protected void onStart() { LogUtil.i(TAG, "onStart"); H2EngineSDK.onStart(this); super.onStart(); } @Override protected void onStop() { //LogUtil.i(TAG, "onStop"); isForeground = false; H2EngineSDK.onStop(this); super.onStop(); } @Override protected void onResume() { //LogUtil.i(TAG, "onResume"); isForeground = true; H2EngineSDK.onResume(this); super.onResume(); // 检测本地存储权限是否有, 没有的话要提示用户 if (PermissionChecker.checkPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE, Process.myPid(), Process.myUid(), getPackageName()) != PackageManager.PERMISSION_GRANTED || PermissionChecker.checkPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE, Process.myPid(), Process.myUid(), getPackageName()) != PackageManager.PERMISSION_GRANTED) { new AlertDialog.Builder(this) .setMessage("应用没有存储读取权限,点击确定至设置中开启,否则无法继续游戏.") .setCancelable(false) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { UniversalUtil.hasGoToSetting = true; Intent intent = new Intent( Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromParts("package", getPackageName(), null); intent.setData(uri); startActivity(intent); } }) .setNegativeButton("拒绝", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { finish(); } }) .show(); } } @Override protected void onPause() { //LogUtil.i(TAG, "onPause"); H2EngineSDK.onPause(this); super.onPause(); } @Override protected void onDestroy() { //LogUtil.i(TAG, "onDestroy"); H2EngineSDK.onDestroy(this); super.onDestroy(); } @Override protected void onRestart() { //LogUtil.i(TAG, "onRestart"); H2EngineSDK.onRestart(this); super.onRestart(); } @Override public boolean onKeyDown(int i, KeyEvent keyEvent) { if (i == KeyEvent.KEYCODE_BACK) { if(QuickSDK.getInstance().isShowExitDialog()){ Sdk.getInstance().exit(this); }else{ // 游戏调用自身的退出对话框,点击确定后,调用quick的exit接口 new AlertDialog.Builder(MainActivity.this).setTitle("退出").setMessage("是否退出游戏?").setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { Sdk.getInstance().exit(MainActivity.this); } }).setNegativeButton("取消", null).show(); } } return super.onKeyDown(i, keyEvent); } }