| New file | 
|  |  |  | 
|---|
|  |  |  | 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.allugame.freesdk.port.FreePlatform; | 
|---|
|  |  |  | 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"; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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: | 
|---|
|  |  |  | 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: | 
|---|
|  |  |  | FreePlatformUtil.getInstace().init(_activity); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case CodeU2A.FreePlatformLogin: | 
|---|
|  |  |  | FreePlatformUtil.getInstace().login(_activity); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case CodeU2A.FreePlatformSwitchAccount: | 
|---|
|  |  |  | FreePlatformUtil.getInstace().switchAccount(_activity); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case CodeU2A.FreePlatformLogout: | 
|---|
|  |  |  | FreePlatformUtil.getInstace().logout(); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case CodeU2A.FreePlatformPay: | 
|---|
|  |  |  | FreePlatformUtil.getInstace().pay(_activity, | 
|---|
|  |  |  | _json.getString("title"), | 
|---|
|  |  |  | _json.getString("orderId"), | 
|---|
|  |  |  | (float) _json.getDouble("mount"), | 
|---|
|  |  |  | _json.getString("cpInfo")); | 
|---|
|  |  |  | FreePlatformUtil.getInstace().payProcessing = true; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case CodeU2A.PayFinished: | 
|---|
|  |  |  | FreePlatformUtil.getInstace().payProcessing = false; | 
|---|
|  |  |  | 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: | 
|---|
|  |  |  | FreePlatformUtil.getInstance().SendRegisterEvent(); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } catch (JSONException e) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 本地推送 | 
|---|
|  |  |  | public static void addLocalNotification(Activity activity, int id, String title, String content, | 
|---|
|  |  |  | long fireTime) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | 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) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // ------------------------------- 自由SDK ------------------------------- | 
|---|
|  |  |  | FreePlatformUtil.getInstace().init(activity); | 
|---|
|  |  |  | // ------------------------------- 极光推送 ------------------------------- | 
|---|
|  |  |  | JPushInterface.setDebugMode(true); | 
|---|
|  |  |  | JPushInterface.init(activity); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | final long _waitingTime = System.currentTimeMillis(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | while (true) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | // 等待获取极光registrationID | 
|---|
|  |  |  | if (!JPushInterface.getRegistrationID(activity).equals("")) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | 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", JPushInterface.getRegistrationID(activity)); | 
|---|
|  |  |  | UniversalUtil.sendMessageToUnity(_msgStruct); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | _msgStruct.clear(); | 
|---|
|  |  |  | _msgStruct.put("code", CodeA2U.SdkInitComplete); | 
|---|
|  |  |  | UniversalUtil.sendMessageToUnity(_msgStruct); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | LogUtil.i(TAG, "初始化执行完毕"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | }).start(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void onCreate(Activity activity, Bundle savedInstanceState) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | FreePlatform.getInstance().onCreate(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void onWindowFocusChanged(boolean b) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void onNewIntent(final Activity activity, final Intent intent) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void onActivityResult(int requestCode, | 
|---|
|  |  |  | int resultCode, | 
|---|
|  |  |  | final Intent data, | 
|---|
|  |  |  | final Activity activity) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void onConfigurationChanged(final Configuration newConfig) {} | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void onRestart(final Activity activity) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void onStart(final Activity activity) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | FreePlatform.getInstance().onStart(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void onPause(final Activity activity) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | FreePlatform.getInstance().onPause(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void onResume(final Activity activity) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | FreePlatform.getInstance().onResume(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(FreePlatformUtil.getInstace().payProcessing) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | Map<String, Object> _msgStruct = new HashMap<>(); | 
|---|
|  |  |  | _msgStruct.put("code", CodeA2U.FreePlatformPayCancel); | 
|---|
|  |  |  | UniversalUtil.sendMessageToUnity(_msgStruct); | 
|---|
|  |  |  | FreePlatformUtil.getInstace().payProcessing = false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void onStop(final Activity activity) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | FreePlatform.getInstance().onStop(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void onDestroy(final Activity activity) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | FreePlatform.getInstance().onDestory(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|