| package com.secondworld.univeralsdk; | 
|   | 
| import android.Manifest; | 
| 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.ApplicationInfo; | 
| import android.content.pm.PackageInfo; | 
| import android.content.pm.PackageManager; | 
| import android.content.pm.PermissionInfo; | 
| import android.net.ConnectivityManager; | 
| import android.net.NetworkInfo; | 
| import android.net.Uri; | 
| import android.net.wifi.WifiInfo; | 
| import android.net.wifi.WifiManager; | 
| import android.os.Process; | 
| import android.provider.Settings; | 
| import android.support.v4.content.PermissionChecker; | 
| import android.widget.Toast; | 
|   | 
| import com.unity3d.player.UnityPlayer; | 
|   | 
| import org.json.JSONObject; | 
|   | 
| import java.io.BufferedReader; | 
| import java.io.File; | 
| import java.io.FileNotFoundException; | 
| import java.io.FileReader; | 
| import java.io.IOException; | 
| import java.io.InputStreamReader; | 
| import java.net.Inet4Address; | 
| import java.net.InetAddress; | 
| import java.net.NetworkInterface; | 
| import java.net.SocketException; | 
| import java.net.URL; | 
| import java.net.URLConnection; | 
| import java.util.Enumeration; | 
| import java.util.Map; | 
|   | 
| /** | 
|  * Created by Administrator on 2018/6/15 0015. | 
|  */ | 
|   | 
| public class UniversalUtil | 
| { | 
|     private final static String TAG = "UniversalUtil"; | 
|   | 
|     /** | 
|      * 获得应用的版本号 | 
|      */ | 
|     public static String GetVersionName(Context context) | 
|     { | 
|         PackageManager _packageManager = context.getPackageManager(); | 
|         PackageInfo _packageInfo = null; | 
|   | 
|         try | 
|         { | 
|             _packageInfo = _packageManager.getPackageInfo(context.getPackageName(), 0); | 
|         } catch (Exception e) | 
|         { | 
|             e.printStackTrace(); | 
|         } | 
|   | 
|         if (_packageInfo != null) | 
|         { | 
|             return _packageInfo.versionName; | 
|         } | 
|         return null; | 
|     } | 
|   | 
|     public static void sendMessageToUnity(Map<String, Object> jsonMap) | 
|     { | 
|         if (jsonMap == null || jsonMap.isEmpty()) | 
|         { | 
|             return; | 
|         } | 
|   | 
|         JSONObject _jsonObject = new JSONObject(jsonMap); | 
|   | 
|         UnityPlayer.UnitySendMessage(StaticDefine.UnityGameObjectName, | 
|                                      StaticDefine.UnityHandleFuncName, | 
|                                      _jsonObject.toString()); | 
|     } | 
|   | 
|     public static void InstallAPK(Context context, final String path) | 
|     { | 
|         File _file = new File(path); | 
|   | 
|         if (_file == null) | 
|         { | 
|             Toast.makeText(context, "给定的地址[" + path + "]找不到要安装的应用文件", Toast.LENGTH_SHORT).show(); | 
|             return; | 
|         } | 
|   | 
|         try | 
|         { | 
|             Intent _intent = new Intent(Intent.ACTION_VIEW); | 
|   | 
| //            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) | 
| //            { | 
| //                _intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); | 
| //                Uri _contentUri = FileProvider.getUriForFile(context, | 
| //                                                             context.getPackageName() + ".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); | 
| //            } | 
|   | 
|             context.startActivity(_intent); | 
|   | 
|         } catch (Exception e) | 
|         { | 
|             e.printStackTrace(); | 
|             Toast.makeText(context, "找不到打开此类型文件的程序", Toast.LENGTH_SHORT).show(); | 
|         } | 
|     } | 
|   | 
|     public static void CopyTextToClipboard(final Activity activity, final String text) | 
|     { | 
|         activity.runOnUiThread(new Runnable() | 
|         { | 
|             @Override | 
|             public void run() | 
|             { | 
|                 ClipboardManager _mgr = (ClipboardManager) activity.getSystemService( | 
|                         Context.CLIPBOARD_SERVICE); | 
|                 ClipData _data = ClipData.newPlainText("playerId", text); | 
|                 _mgr.setPrimaryClip(_data); | 
|             } | 
|         }); | 
|     } | 
|   | 
|     public static long getMemTotal() | 
|     { | 
|         try | 
|         { | 
|             FileReader fileReader = new FileReader("/proc/meminfo"); | 
|             BufferedReader bufferedReader = new BufferedReader(fileReader, 4 * 1024); | 
|             String str; | 
|             while ((str = bufferedReader.readLine()) != null) | 
|             { | 
|                 if (str.contains("MemTotal")) | 
|                 { | 
|                     break; | 
|                 } | 
|             } | 
|             bufferedReader.close(); | 
|             fileReader.close(); | 
|             String[] array = str.split("\\s+"); | 
|             // 获得系统总内存,单位是KB | 
|             return Integer.valueOf(array[1]).intValue(); | 
|         } catch (FileNotFoundException e) | 
|         { | 
|             e.printStackTrace(); | 
|         } catch (IOException e) | 
|         { | 
|             e.printStackTrace(); | 
|         } | 
|         return 0; | 
|     } | 
|   | 
|     public static String getPublicIp(boolean useHttps) | 
|     { | 
|         String _ip; | 
|         try | 
|         { | 
|             URL ipify = useHttps ? new URL("https://api.ipify.org") : new URL( | 
|                     "http://api.ipify.org"); | 
|             URLConnection conn = ipify.openConnection(); | 
|             BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); | 
|             _ip = in.readLine(); | 
|             in.close(); | 
|         } catch (IOException e) | 
|         { | 
|             e.printStackTrace(); | 
|         } | 
|         return ""; | 
|     } | 
|   | 
|     public static String getIP(Context context) | 
|     { | 
|         NetworkInfo info = ((ConnectivityManager) context | 
|                 .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); | 
|         if (info != null && info.isConnected()) | 
|         { | 
|             if (info.getType() == ConnectivityManager.TYPE_MOBILE) | 
|             {//当前使用2G/3G/4G网络 | 
|                 try | 
|                 { | 
|                     for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) | 
|                     { | 
|                         NetworkInterface intf = en.nextElement(); | 
|                         for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) | 
|                         { | 
|                             InetAddress inetAddress = enumIpAddr.nextElement(); | 
|                             if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) | 
|                             { | 
|                                 return inetAddress.getHostAddress(); | 
|                             } | 
|                         } | 
|                     } | 
|                 } catch (SocketException e) | 
|                 { | 
|                     e.printStackTrace(); | 
|                 } | 
|   | 
|             } | 
|             else if (info.getType() == ConnectivityManager.TYPE_WIFI) | 
|             {//当前使用无线网络 | 
|                 WifiManager wifiManager = (WifiManager) context.getSystemService( | 
|                         Context.WIFI_SERVICE); | 
|                 WifiInfo wifiInfo = wifiManager.getConnectionInfo(); | 
|                 return intIP2StringIP(wifiInfo.getIpAddress()); | 
|             } | 
|         } | 
|         else | 
|         { | 
|             //当前无网络连接,请在设置中打开网络 | 
|         } | 
|         return null; | 
|     } | 
|   | 
|     private static String intIP2StringIP(int ip) | 
|     { | 
|         return (ip & 0xFF) + "." + | 
|                 ((ip >> 8) & 0xFF) + "." + | 
|                 ((ip >> 16) & 0xFF) + "." + | 
|                 (ip >> 24 & 0xFF); | 
|     } | 
|   | 
|     public static boolean hasRWP = false; | 
|     public static boolean hasGoToSetting = false; | 
|   | 
|     public static void CheckWriteAndReadExternalStorage(final Activity activity) | 
|     { | 
|         //Log.i("MainActivity", "CheckWriteAndReadExternalStorage 开始 hasGoToSetting: " + hasGoToSetting); | 
|   | 
|         if (hasRWP && !hasGoToSetting) | 
|         { | 
|             //Log.i("MainActivity", "1111 有权限"); | 
|             return; | 
|         } | 
|   | 
|         if (PermissionChecker.checkPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE, | 
|                                               Process.myPid(), Process.myUid(), | 
|                                               activity.getPackageName()) == PackageManager.PERMISSION_GRANTED) | 
|         { | 
|             //Log.i("MainActivity", "有权限"); | 
|             hasRWP = true; | 
|             if (hasGoToSetting) | 
|             { | 
|                 //Log.i("MainActivity", "去过设置界面, 这里重启"); | 
|                 activity.runOnUiThread(new Runnable() | 
|                 { | 
|                     @Override | 
|                     public void run() | 
|                     { | 
|                         new Thread() | 
|                         { | 
|                             public void run() | 
|                             { | 
|                                 String _packageName = activity.getPackageName(); | 
|                                 Intent _launch = activity.getBaseContext().getPackageManager().getLaunchIntentForPackage( | 
|                                         _packageName); | 
|                                 _launch.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | 
|                                 activity.startActivity(_launch); | 
|                                 Process.killProcess(Process.myPid()); | 
|                             } | 
|                         }.start(); | 
|                         activity.finish(); | 
|                     } | 
|                 }); | 
|             } | 
|             return; | 
|         } | 
|   | 
|         if (!hasRWP) | 
|         { | 
|             new AlertDialog.Builder(activity) | 
|                     .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", | 
|                                                                        activity.getPackageName(), | 
|                                                                        null); | 
|                                                intent.setData(uri); | 
|                                                activity.startActivity(intent); | 
|                                            } | 
|                                        }) | 
|                     .setNegativeButton("拒绝", | 
|                                        new DialogInterface.OnClickListener() | 
|                                        { | 
|                                            @Override | 
|                                            public void onClick(DialogInterface dialogInterface, | 
|                                                                int i) | 
|                                            { | 
|                                                activity.finish(); | 
|                                            } | 
|                                        }) | 
|                     .show(); | 
|         } | 
|     } | 
|   | 
|     public static boolean checkPermission22(Activity activity, String permission) | 
|     { | 
|         PackageManager _pkgMgr = activity.getPackageManager(); | 
|         LogUtil.i(TAG, "开始检测权限: " + permission); | 
|         try | 
|         { | 
|             PermissionInfo _info = _pkgMgr.getPermissionInfo(permission, | 
|                                                              PackageManager.GET_META_DATA); | 
|   | 
|             LogUtil.i(TAG, "  |-- 保护级别: " + _info.protectionLevel); | 
|             if (_info.protectionLevel != PermissionInfo.PROTECTION_DANGEROUS && _info.protectionLevel != 4097) | 
|             { | 
|                 LogUtil.i(TAG, "  |-- 不是危险权限."); | 
|                 return false; | 
|             } | 
|   | 
|         } catch (PackageManager.NameNotFoundException e) | 
|         { | 
|             e.printStackTrace(); | 
|         } | 
|   | 
|         int _result = PermissionChecker.checkPermission(activity, permission, | 
|                                                         Process.myPid(), Process.myUid(), | 
|                                                         activity.getPackageName()); | 
|   | 
|         LogUtil.i(TAG, "  |-- 获取类型: " + _result); | 
|   | 
|         if (_result == PackageManager.PERMISSION_GRANTED) | 
|         { | 
|             LogUtil.i(TAG, permission + " 有这个权限."); | 
|             return false; | 
|         } | 
|   | 
|         return true; | 
|     } | 
|   | 
|     public static String getMetaString(Activity activity, String key) | 
|     { | 
|         PackageManager _pkgMgr = activity.getPackageManager(); | 
|         ApplicationInfo _appInfo; | 
|         try | 
|         { | 
|             _appInfo = _pkgMgr.getApplicationInfo(activity.getPackageName(), | 
|                                                   PackageManager.GET_META_DATA); | 
|             return _appInfo.metaData.getString(key); | 
|         } catch (PackageManager.NameNotFoundException e) | 
|         { | 
|             e.printStackTrace(); | 
|         } | 
|         return null; | 
|     } | 
|   | 
|     public static int getMetaInt(Activity activity, String key) | 
|     { | 
|         LogUtil.i(TAG, "将要获取的MetaKey: " + key); | 
|         PackageManager _pkgMgr = activity.getPackageManager(); | 
|         ApplicationInfo _appInfo; | 
|         try | 
|         { | 
|             _appInfo = _pkgMgr.getApplicationInfo(activity.getPackageName(), | 
|                                                   PackageManager.GET_META_DATA); | 
|             return _appInfo.metaData.getInt(key); | 
|         } catch (PackageManager.NameNotFoundException e) | 
|         { | 
|             e.printStackTrace(); | 
|         } | 
|         return -1; | 
|     } | 
|   | 
|     public static boolean isEmpty(String s) { | 
|         if (null == s) | 
|             return true; | 
|         if (s.length() == 0) | 
|             return true; | 
|         if (s.trim().length() == 0) | 
|             return true; | 
|         return false; | 
|     } | 
| } |