client_Hale
2018-09-04 fff1f19a381e6ad295cb3e7bd41cce48e74fbeea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
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;
    }
}