| package com.secondworld.univeralsdk; | 
|   | 
| import android.app.Activity; | 
| import android.app.AlertDialog; | 
| import android.content.ClipData; | 
| import android.content.ClipboardManager; | 
| import android.content.Context; | 
| 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.Environment; | 
| import android.provider.Settings; | 
| import android.widget.Toast; | 
|   | 
| import com.linglei.sdk.openapi.LLSDK; | 
| import com.unity3d.player.UnityPlayer; | 
|   | 
| import org.json.JSONException; | 
| import org.json.JSONObject; | 
|   | 
| import java.io.File; | 
| import java.util.HashMap; | 
| import java.util.Map; | 
|   | 
| import cn.jpush.android.api.JPushInterface; | 
| import cn.jpush.android.data.JPushLocalNotification; | 
|   | 
| public class H2EngineSDK | 
| { | 
|     private static final String TAG = "H2EngineSDK"; | 
|     private static String APP_ID = ""; | 
|   | 
|     private static boolean PushEnable = true; | 
|   | 
|     public static void HandleUnityMessage(String json) | 
|     { | 
|         LogUtil.i(TAG, "收到Unity发来的信息: " + json); | 
|         Activity _activity = UnityPlayer.currentActivity; | 
|         try | 
|         { | 
|             JSONObject _json = new JSONObject(json); | 
|   | 
|             int _code = _json.getInt("code"); | 
|   | 
|             switch (_code) | 
|             { | 
|                 case CodeU2A.Init: | 
|                     APP_ID = _json.getString("appID"); | 
|                     init(_activity); | 
|                     break; | 
|                 case CodeU2A.AssetCopy: | 
|                     FileUtil.copyAssets(_activity); | 
|                     break; | 
|                 case CodeU2A.CopyOneAsset: | 
|                     FileUtil.copy(_activity, _json.getString("fileName")); | 
|                     break; | 
|                 case CodeU2A.BatteryListenStart: | 
|                     BatteryUtil.getInstance().start(_activity); | 
|                     break; | 
|                 case CodeU2A.BatteryListenStop: | 
|                     BatteryUtil.getInstance().stop(_activity); | 
|                     break; | 
|                 case CodeU2A.UniqueID: | 
|                     break; | 
|                 case CodeU2A.CopyContent: | 
|                     CopyContent(_activity, _json.getString("content")); | 
|                     break; | 
|                 case CodeU2A.OpenWebView: | 
|                     WebViewUtil.OpenWebView(_activity, _json.getString("url")); | 
|                     break; | 
|                 case CodeU2A.RestartApp: | 
|                     RestartApp(_activity); | 
|                     break; | 
|                 case CodeU2A.InstallAPK: | 
|                     InstallApp(_activity, _json.getString("path")); | 
|                     break; | 
|                 case CodeU2A.ExteneralStorage: | 
|                     GetExternalStorage(); | 
|                     break; | 
|                 case CodeU2A.RequestPermission: | 
|                     break; | 
|                 case CodeU2A.RequestManifestPermissions: | 
|                     break; | 
|                 case CodeU2A.FreePlatformInit: | 
|                     JSPlatformUtil.getInstance().init(_activity); | 
|                     break; | 
|                 case CodeU2A.FreePlatformLogin: | 
|                     JSPlatformUtil.getInstance().login(_activity); | 
|                     break; | 
|                 case CodeU2A.FreePlatformSwitchAccount: | 
|                     break; | 
|                 case CodeU2A.FreePlatformLogout: | 
|                     JSPlatformUtil.getInstance().logout(_activity); | 
|                     break; | 
|                 case CodeU2A.FreePlatformPay: | 
|   | 
|                     JSONObject _extraData = new JSONObject(); | 
|                     _extraData.put("appid", APP_ID); | 
|                     _extraData.put("cpinfo", _json.getString("cpInfo")); | 
|                     _extraData.put("cporderid", _json.getString("orderId")); | 
|   | 
|                     JSPlatformUtil.getInstance().pay(_activity, | 
|                                                      _json.getString("orderId"), | 
|                                                      _json.getInt("mount") * 100, | 
|                                                      _json.getString("title"), | 
|                                                      _extraData.toString()); | 
|   | 
|                     JSPlatformUtil.getInstance().payProcessing = true; | 
|                     break; | 
|                 case CodeU2A.PayFinished: | 
|                     JSPlatformUtil.getInstance().payProcessing = false; | 
|                     break; | 
|                 case CodeU2A.CreateRole: | 
|                     break; | 
|                 case CodeU2A.RoleLogin: | 
|                     JSPlatformUtil.getInstance().enterWorld(_activity, | 
|                                                             _json.getString("roleID"), | 
|                                                             _json.getString("sid"), | 
|                                                             _json.getString( | 
|                                                                     "roleName").trim().replace(" ", | 
|                                                                                                ""), | 
|                                                             _json.getString("level"), | 
|                                                             _json.getString("vipLevel"), | 
|                                                             _json.getString("money")); | 
|                     break; | 
|                 case CodeU2A.ClientPackage: | 
|                     break; | 
|                 case CodeU2A.JPushAddLocalMessage: | 
|                     addLocalNotification(_activity, | 
|                                          _json.getInt("id"), | 
|                                          _json.getString("title"), | 
|                                          _json.getString("content"), | 
|                                          _json.getLong("fireTime")); | 
|                     break; | 
|                 case CodeU2A.JPushRemoveLocalMessage: | 
|                     removeNotification(_activity, _json.getInt("id")); | 
|                     break; | 
|                 case CodeU2A.SendRegistEvent: | 
|                     JSPlatformUtil.getInstance().SendRegisterEvent(); | 
|                     break; | 
|             } | 
|   | 
|         } catch (JSONException e) | 
|         { | 
|             e.printStackTrace(); | 
|         } | 
|     } | 
|   | 
|     // 本地推送 | 
|     public static void addLocalNotification(Activity activity, int id, String title, String content, | 
|                                             long fireTime) | 
|     { | 
|         if (PushEnable) | 
|         { | 
|             try | 
|             { | 
|                 JPushLocalNotification ln = new JPushLocalNotification(); | 
|                 ln.setBuilderId(0);// 设置样式 | 
|                 ln.setNotificationId(id);// id | 
|                 ln.setTitle(title);// 标题 | 
|   | 
|                 ln.setContent(content);// 内容 | 
|                 ln.setBroadcastTime(fireTime);// 等待时间 | 
|                 JPushInterface.addLocalNotification(activity, ln); | 
|             } catch (Exception e) | 
|             { | 
|                 e.printStackTrace(); | 
|             } | 
|         } | 
|     } | 
|   | 
|     public static void removeNotification(Activity activity, int id) | 
|     { | 
|         if (PushEnable) | 
|         { | 
|             JPushInterface.removeLocalNotification(activity, id); | 
|         } | 
|     } | 
|   | 
|     public static void GetExternalStorage() | 
|     { | 
|         Map<String, Object> _msg = new HashMap<>(); | 
|         _msg.put("code", CodeA2U.ExternalStorage); | 
|         _msg.put("path", Environment.getExternalStorageDirectory().getAbsolutePath()); | 
|         UniversalUtil.sendMessageToUnity(_msg); | 
|     } | 
|   | 
|     public static void InstallApp(Activity activity, String path) | 
|     { | 
|         File _file = new File(path); | 
|   | 
|         if (_file == null) | 
|         { | 
|             LogUtil.i(TAG, "找不到给定地址的apk: " + path); | 
|             return; | 
|         } | 
|   | 
|         try | 
|         { | 
|             Intent _intent = new Intent(Intent.ACTION_VIEW); | 
|   | 
| //            if (Build.VERSION.SDK_INT >= 23) | 
| //            { | 
| //                _intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); | 
| //                Uri _contentUri = FileProvider.getUriForFile(activity, | 
| //                                                             "com.shandangceshi.snxxz.fileProvider", | 
| //                                                             _file); | 
| //                _intent.setDataAndType(_contentUri, "application/vnd.android.package-archive"); | 
| // | 
| //            } | 
| //            else | 
| //            { | 
|             _intent.setDataAndType(Uri.fromFile(_file), | 
|                                    "application/vnd.android.package-archive"); | 
|             _intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | 
|   | 
| //            } | 
|   | 
|             activity.startActivity(_intent); | 
|   | 
|         } catch (Exception e) | 
|         { | 
|             e.printStackTrace(); | 
|             Toast.makeText(activity, "没有找到打开此类文件的程序", Toast.LENGTH_SHORT).show(); | 
|         } | 
|     } | 
|   | 
|     public static void CopyContent(final Activity activity, final String content) | 
|     { | 
|         activity.runOnUiThread(new Runnable() | 
|         { | 
|             @Override | 
|             public void run() | 
|             { | 
|                 ClipboardManager _mgr = (ClipboardManager) activity.getSystemService( | 
|                         Context.CLIPBOARD_SERVICE); | 
|                 ClipData _data = ClipData.newPlainText("playerId", content); | 
|                 _mgr.setPrimaryClip(_data); | 
|             } | 
|         }); | 
|     } | 
|   | 
|     public static void RestartApp(final Activity activity) | 
|     { | 
|         activity.runOnUiThread(new Runnable() | 
|         { | 
|             @Override | 
|             public void run() | 
|             { | 
|                 new Thread() | 
|                 { | 
|                     public void run() | 
|                     { | 
|                         String _pn = activity.getPackageName(); | 
|                         PackageManager _pm = activity.getPackageManager(); | 
|                         Intent _l = _pm.getLaunchIntentForPackage(_pn); | 
|                         _l.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | 
|                         activity.startActivity(_l); | 
|                         android.os.Process.killProcess(android.os.Process.myPid()); | 
|                     } | 
|                 }.start(); | 
|                 activity.finish(); | 
|             } | 
|         }); | 
|     } | 
|   | 
|     public static void init(final Activity activity) | 
|     { | 
|         final int _memoryTotal = (int) (UniversalUtil.getMemTotal() / 1024); | 
|         if (_memoryTotal < 1024) | 
|         { | 
|             LogUtil.i(TAG, "检测设备内存不满足运行程序标准"); | 
|             AlertDialog.Builder _builder = new AlertDialog.Builder(activity); | 
|             _builder.setIcon(R.drawable.app_icon); | 
|             _builder.setTitle("警告"); | 
|             _builder.setMessage("您的手机内存不足,无法正常运行游戏"); | 
|             _builder.setPositiveButton("确定", new DialogInterface.OnClickListener() | 
|             { | 
|                 @Override | 
|                 public void onClick(DialogInterface dialogInterface, int i) | 
|                 { | 
|                     android.os.Process.killProcess(android.os.Process.myPid()); | 
|                 } | 
|             }); | 
|             _builder.show(); | 
|             return; | 
|         } | 
|   | 
|         PushEnable = activity.getPackageName().equals("com.lingleigame.shaonianqixiachuan"); | 
|   | 
|         new Thread(new Runnable() | 
|         { | 
|             @Override | 
|             public void run() | 
|             { | 
|                 LogUtil.i(TAG, "开始执行初始化"); | 
|   | 
|                 GetExternalStorage(); | 
|   | 
|                 // ------------------------------- 设备信息 ------------------------------- | 
|                 Map<String, Object> _msgStruct = new HashMap<>(); | 
|                 _msgStruct.put("code", CodeA2U.DeviceInfo); | 
|                 _msgStruct.put("userAgent", System.getProperty("http.agent")); | 
|                 _msgStruct.put("mac", UniqueID.getLocalMac(activity)); | 
|                 _msgStruct.put("imei", UniqueID.getDeviceId(activity)); | 
|                 _msgStruct.put("android_id", | 
|                                Settings.System.getString(activity.getContentResolver(), | 
|                                                          Settings.System.ANDROID_ID)); | 
|                 _msgStruct.put("unique_id", UniqueID.get(activity)); | 
|                 _msgStruct.put("memoryTotal", _memoryTotal); | 
|                 UniversalUtil.sendMessageToUnity(_msgStruct); | 
|   | 
|                 // ------------------------------- 极光推送 ------------------------------- | 
|                 String _registrationID = "0"; | 
|                 if (PushEnable) | 
|                 { | 
|                     JPushInterface.setDebugMode(true); | 
|                     JPushInterface.init(activity); | 
|   | 
|                     final long _waitingTime = System.currentTimeMillis(); | 
|   | 
|                     while (true) | 
|                     { | 
|                         // 等待获取极光registrationID | 
|                         if (!JPushInterface.getRegistrationID(activity).equals("")) | 
|                         { | 
|                             _registrationID = JPushInterface.getRegistrationID(activity); | 
|                             break; | 
|                         } | 
|   | 
|                         long _escapeTime = System.currentTimeMillis() - _waitingTime; | 
|   | 
|                         if (_escapeTime > 3000) | 
|                         { | 
|                             LogUtil.w(TAG, "等待获取极光推送registrationID超时: 3秒"); | 
|                             break; | 
|                         } | 
|                     } | 
|                 } | 
|   | 
|                 _msgStruct.clear(); | 
|                 _msgStruct.put("code", CodeA2U.PushClientID); | 
|                 _msgStruct.put("clientID", _registrationID); | 
|                 UniversalUtil.sendMessageToUnity(_msgStruct); | 
|   | 
|                 _msgStruct.clear(); | 
|                 _msgStruct.put("code", CodeA2U.SdkInitComplete); | 
|                 _msgStruct.put("channelPlatform", "js"); | 
|                 UniversalUtil.sendMessageToUnity(_msgStruct); | 
|   | 
|                 LogUtil.i(TAG, "初始化执行完毕"); | 
|             } | 
|   | 
|         }).start(); | 
|     } | 
|   | 
|     public static void onCreate(Activity activity, Bundle savedInstanceState) | 
|     { | 
|         // ------------------------------- 猫耳SDK ------------------------------- | 
|         JSPlatformUtil.getInstance().init(activity); | 
|     } | 
|   | 
|     private static boolean m_IsFocus = true; | 
|   | 
|     public static void onWindowFocusChanged(boolean b) | 
|     { | 
|         if (JSPlatformUtil.getInstance().payProcessing) | 
|         { | 
|             if (b) | 
|             { | 
|                 Map<String, Object> _msgStruct = new HashMap<>(); | 
|                 _msgStruct.put("code", CodeA2U.FreePlatformPayCancel); | 
|                 UniversalUtil.sendMessageToUnity(_msgStruct); | 
|                 JSPlatformUtil.getInstance().payProcessing = false; | 
|             } | 
|         } | 
|     } | 
|   | 
|     public static void onNewIntent(final Activity activity, final Intent intent) | 
|     { | 
|         LLSDK.getInstance().onNewIntent(intent); | 
|     } | 
|   | 
|     public static void onActivityResult(int requestCode, | 
|                                         int resultCode, | 
|                                         final Intent data, | 
|                                         final Activity activity) | 
|     { | 
|         LLSDK.getInstance().onActivityResult(requestCode, resultCode, data); | 
|     } | 
|   | 
|     public static void onConfigurationChanged(final Configuration newConfig) | 
|     { | 
|         LLSDK.getInstance().onConfigurationChanged(newConfig); | 
|     } | 
|   | 
|     public static void onRestart(final Activity activity) | 
|     { | 
|         LLSDK.getInstance().onRestart(); | 
|     } | 
|   | 
|     public static void onStart(final Activity activity) | 
|     { | 
|         LLSDK.getInstance().onStart(); | 
|     } | 
|   | 
|     public static void onPause(final Activity activity) | 
|     { | 
|         LLSDK.getInstance().onPause(); | 
|     } | 
|   | 
|     public static void onResume(final Activity activity) | 
|     { | 
|         LLSDK.getInstance().onResume(); | 
|   | 
|         if (JSPlatformUtil.getInstance().payProcessing) | 
|         { | 
|             Map<String, Object> _msgStruct = new HashMap<>(); | 
|             _msgStruct.put("code", CodeA2U.FreePlatformPayCancel); | 
|             UniversalUtil.sendMessageToUnity(_msgStruct); | 
|             JSPlatformUtil.getInstance().payProcessing = false; | 
|         } | 
|     } | 
|   | 
|     public static void onStop(final Activity activity) | 
|     { | 
|         LLSDK.getInstance().onStop(); | 
|     } | 
|   | 
|     public static void onDestroy(final Activity activity) | 
|     { | 
|         LLSDK.getInstance().onDestory(); | 
|     } | 
| } |