|
@@ -0,0 +1,233 @@
|
|
|
+package com.tenmove.client;
|
|
|
+
|
|
|
+import android.content.ComponentName;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.pm.PackageInfo;
|
|
|
+import android.content.pm.PackageManager;
|
|
|
+import android.content.pm.ResolveInfo;
|
|
|
+import android.net.Uri;
|
|
|
+import android.net.wifi.WifiInfo;
|
|
|
+import android.net.wifi.WifiManager;
|
|
|
+import android.os.Build;
|
|
|
+import android.os.Bundle;
|
|
|
+
|
|
|
+import com.unity3d.player.UnityPlayerActivity;
|
|
|
+import android.support.v4.content.FileProvider;
|
|
|
+import android.util.Log;
|
|
|
+import android.content.Intent;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+
|
|
|
+public class BaseActivity extends UnityPlayerActivity
|
|
|
+{
|
|
|
+ static
|
|
|
+ {
|
|
|
+ // Add jni-loader for libmono jni invoke and hot-fix -By Simon.King
|
|
|
+ System.loadLibrary("jniloader");
|
|
|
+ System.loadLibrary("hpatch");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onCreate(Bundle savedInstanceState)
|
|
|
+ {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Add restart application function for android logic hot-fix -By Simon.King
|
|
|
+ public void RestartApplication()
|
|
|
+ {
|
|
|
+ new Thread()
|
|
|
+ {
|
|
|
+ public void run()
|
|
|
+ {
|
|
|
+ Intent launch= getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
|
|
|
+ launch.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
+ startActivity(launch);
|
|
|
+ android.os.Process.killProcess(android.os.Process.myPid());
|
|
|
+ }
|
|
|
+ }.start();
|
|
|
+ finish();
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean CheckInstallAppStatus()
|
|
|
+ {
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
|
|
|
+ { // 7.0+以上版本
|
|
|
+ Context baseContext = getBaseContext();
|
|
|
+ if (null == baseContext)
|
|
|
+ {
|
|
|
+ Log.e("Unity", "CheckInstallAppStatus baseContext is null : ");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ File externalFilesDir = baseContext.getExternalFilesDir("");
|
|
|
+ return _CheckInstallAppStatus(baseContext, externalFilesDir);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean _CheckInstallAppStatus(Context baseContext, File file)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ Log.i("Unity", "CheckInstallAppStatus::Start with ApkFile path : " + file.getAbsolutePath());
|
|
|
+ String providerName = baseContext.getApplicationContext().getPackageName() + ".fileprovider";
|
|
|
+ Log.i("Unity", "CheckInstallAppStatus::Provider name: " + providerName);
|
|
|
+ Uri apkUri = FileProvider.getUriForFile(baseContext,providerName, file); //与manifest中定义的provider中的authorities="com.xinchuang.buynow.fileprovider"保持一致
|
|
|
+ }
|
|
|
+ catch (IllegalArgumentException e)
|
|
|
+ {
|
|
|
+ Log.e("Unity", "CheckInstallAppStatus::IllegalArumentException : " + e.toString());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ Log.e("Unity", "CheckInstallAppStatus::Exception : " + e.toString());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean IsAppInstalled(String packageName)
|
|
|
+ {
|
|
|
+ Log.i("Unity", "IsAppInstalled::Check App IsInstall with Name : "+ packageName);
|
|
|
+
|
|
|
+ Context context = getBaseContext();
|
|
|
+ if (null == context)
|
|
|
+ {
|
|
|
+ Log.e("Unity", "IsAppInstalled::baseContext is null");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ final PackageManager packageManager = context.getPackageManager();
|
|
|
+ List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
|
|
|
+ List<String> pName = new ArrayList<String>();
|
|
|
+ if (pinfo != null)
|
|
|
+ {
|
|
|
+ for (int i = 0; i < pinfo.size(); i++)
|
|
|
+ {
|
|
|
+ String pn = pinfo.get(i).packageName;
|
|
|
+ pName.add(pn);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ boolean isInstall = pName.contains(packageName);
|
|
|
+
|
|
|
+ Log.i("Unity", "IsAppInstalled::Check App IsInstall with Name : "+ packageName + ":"+isInstall);
|
|
|
+
|
|
|
+ return isInstall;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean OpenAppWithName(String packageNameToOpen)
|
|
|
+ {
|
|
|
+ Log.i("Unity", "OpenAppWithName::Start Find App with Name : "+ packageNameToOpen);
|
|
|
+ // 获取包管理器
|
|
|
+ PackageManager manager = getPackageManager();
|
|
|
+ // 指定入口,启动类型,包名
|
|
|
+ Intent intent = new Intent(Intent.ACTION_MAIN);//入口Main
|
|
|
+ intent.addCategory(Intent.CATEGORY_LAUNCHER);// 启动LAUNCHER,跟MainActivity里面的配置类似
|
|
|
+ intent.setPackage(packageNameToOpen);//包名
|
|
|
+ //查询要启动的Activity
|
|
|
+ List<ResolveInfo> apps = manager.queryIntentActivities(intent, 0);
|
|
|
+ if (apps.size() > 0)
|
|
|
+ {//如果包名存在
|
|
|
+ Log.i("Unity", "OpenAppWithName::Find App with Name : "+ packageNameToOpen);
|
|
|
+ ResolveInfo ri = apps.get(0);
|
|
|
+ // //获取包名
|
|
|
+ String packageName = ri.activityInfo.packageName;
|
|
|
+ //获取app启动类型
|
|
|
+ String className = ri.activityInfo.name;
|
|
|
+ //组装包名和类名
|
|
|
+ ComponentName cn = new ComponentName(packageName, className);
|
|
|
+ //设置给Intent
|
|
|
+ intent.setComponent(cn);
|
|
|
+ //根据包名类型打开Activity
|
|
|
+ startActivity(intent);
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean InstallPackageWithPath(String apkFilePath)
|
|
|
+ {
|
|
|
+ File apkfile = new File(apkFilePath);
|
|
|
+ if (!apkfile.exists())
|
|
|
+ {
|
|
|
+ Log.e("Unity", "InstallPackageWithPath::File not exit " + apkfile.getAbsolutePath());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ Context baseContext = getBaseContext();
|
|
|
+
|
|
|
+ if (null == baseContext)
|
|
|
+ {
|
|
|
+ Log.e("Unity", "InstallPackageWithPath::baseContext is null " + apkfile.getAbsolutePath());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ Intent i = new Intent(Intent.ACTION_VIEW);
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
|
|
|
+ { // 7.0+以上版本
|
|
|
+ Log.i("Unity", "InstallPackageWithPath::Android Version 7.0 + " + apkfile.getAbsolutePath());
|
|
|
+ String providerName = baseContext.getApplicationContext().getPackageName() + ".fileprovider";
|
|
|
+ Log.i("Unity", "CheckInstallAppStatus::Provider name: " + providerName);
|
|
|
+
|
|
|
+ Uri apkUri = FileProvider.getUriForFile(baseContext, providerName, apkfile); //与manifest中定义的provider中的authorities="com.xinchuang.buynow.fileprovider"保持一致
|
|
|
+ i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
+ i.setDataAndType(apkUri, "application/vnd.android.package-archive");
|
|
|
+ i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Log.i("Unity", "InstallPackageWithPath::Android Version 7.0- " + apkfile.getAbsolutePath());
|
|
|
+
|
|
|
+ i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");
|
|
|
+ i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ Log.e("Unity","InstallPackageWithPath::Install with Exception:" + e.toString());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ getBaseContext().startActivity(i);
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String GetMacAddress() {
|
|
|
+
|
|
|
+ String macAddress = null ;
|
|
|
+ WifiManager wifiManager =
|
|
|
+ (WifiManager)getBaseContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
|
+ WifiInfo info;
|
|
|
+ info = ( null == wifiManager ? null : wifiManager.getConnectionInfo());
|
|
|
+
|
|
|
+ if (!wifiManager.isWifiEnabled())
|
|
|
+ {
|
|
|
+ //必须先打开,才能获取到MAC地址
|
|
|
+ wifiManager.setWifiEnabled( true );
|
|
|
+ wifiManager.setWifiEnabled( false );
|
|
|
+ }
|
|
|
+ if ( null != info) {
|
|
|
+ macAddress = info.getMacAddress();
|
|
|
+ }
|
|
|
+ return macAddress;
|
|
|
+ }
|
|
|
+}
|