黄学彪
2020-08-11 26d1cdf2f5246c1ad7890e6740444c4a6a80e6fb
ZigbeeApp/GateWay.Droid/Application.cs
@@ -26,13 +26,13 @@
    {
        public void OnReq(BaseReq p0)
        {
        }
        protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
            Intent = intent;
            com.hdl.home.Application.api.HandleIntent(intent, this);
            com.hdl.home.Application.api?.HandleIntent(intent, this);
            //Finish();
        }
        public static Action<string> RespAction;
@@ -73,32 +73,59 @@
            // 将应用的appId注册到微信
            //api.RegisterApp("wx2ec8f53f6fa36e82");
          com.hdl.home.Application.api.HandleIntent(Intent, this);
          com.hdl.home.Application.api?.HandleIntent(Intent, this);
        }
    }
    /// <summary>
    /// 设定为默认启动的Application
    /// </summary>
    [Android.App.Application]
    public class Application : Android.App.Application
    {
        public Application(IntPtr handle, Android.Runtime.JniHandleOwnership ownerShip) : base(handle, ownerShip) { }

        public override void OnCreate()
        {
            //安卓配网初始
            //var result = Com.Mediatek.Elian.ElianNative.LoadLib();
            //if (!result)
            //{
            //    System.Console.WriteLine("Error,can't load elianjni lib");
            //}
            initAll();
            base.OnCreate();
    /// <summary>
    /// 设定为默认启动的Application
    /// </summary>
    [Android.App.Application()]
    public class Application : Android.App.Application
    {
        public Application(IntPtr handle, Android.Runtime.JniHandleOwnership ownerShip) : base(handle, ownerShip) { }

        public override void OnCreate()
        {
            //如果不是App的主进程,则不需要处理
            if (this.IsCurrentAppProcess == false)
            {
                base.OnCreate();
                return;
            }
            try
            {
                //这个东西好像可能会出异常
                this.initAll();
            }
            catch (Exception ex)
            {
                //调试:记录系统异常
                Shared.Phone.UserCenter.HdlLogLogic.Current.WriteOtherText(Shared.Phone.UserCenter.DirNameResourse.SystemLogFile, ex.Message + "\r\n" + ex.StackTrace, true, false);
            }
            base.OnCreate();
            // 通过WXAPIFactory工厂,获取IWXAPI的实例
            api = WXAPIFactory.CreateWXAPI(this, "wx2ec8f53f6fa36e82", true);
            //api = WXAPIFactory.CreateWXAPI(this, "wx2ec8f53f6fa36e82", true);
            // 将应用的appId注册到微信
            api.RegisterApp("wx2ec8f53f6fa36e82");
            api?.RegisterApp("wx2ec8f53f6fa36e82");
        }
        /// <summary>
        /// 判断当前进程是不是App自己的主进程
        /// </summary>
        private bool IsCurrentAppProcess
        {
            get
            {
                var activityManager = (ActivityManager)GetSystemService(ActivityService);
                var listProcess = activityManager.RunningAppProcesses;
                foreach (var process in listProcess)
                {
                    if (process.ProcessName == "com.evoyo.home" && process.Pid == Android.OS.Process.MyPid())
                    {
                        return true;
                    }
                }
                return false;
            }
        }
       internal static IWXAPI api;
        public static void WXLogin() {
            var req = new SendAuth.Req { Scope = "snsapi_userinfo", State = "ZigbeeApp" };
            api.SendReq(req);
            api?.SendReq(req);
        }
        /// <summary>
        /// 所有初始化全部在这个方法实现
        /// </summary>
        void initAll()
        {
            Shared.Application.IsGpsEnable = false;
        /// <summary>
        /// 所有初始化全部在这个方法实现
        /// </summary>
        void initAll()
        {
            Shared.Application.IsGpsEnable = System.IO.File.Exists(Shared.Phone.UserCenter.DirNameResourse.OpenGbsFile);
            //设置极光调试模式,为false时只打印警告信息
            // System.Console.WriteLine($"AAA : {System.DateTime.Now.ToString()}");
@@ -159,11 +186,17 @@
                Shared.Application.FontSize = 12;
                AppCenter.Start("4802834a-e7e9-4dd8-93f1-c2f88f0bd464", typeof(Analytics), typeof(Crashes));
#if Release
                //保存获取的极光服务器上的注册ID到本地文件
                var registrationId = JPushInterface.GetRegistrationID(activity);
                System.Console.WriteLine("registrationId-极光id=" + registrationId);
                if (!string.IsNullOrEmpty(registrationId))
                {
                    Shared.Common.Config.Instance.RegistrationID = registrationId;
                    Shared.Common.Config.Instance.Save();
                }
                //调试:记录极光ID
                Shared.Phone.UserCenter.HdlLogLogic.Current.WriteOtherText(Shared.Phone.UserCenter.DirNameResourse.JiguangFile, "receive1:" + registrationId, true, true);
#endif
            };

           
            BaseActivity.RefreshUIAction += (activity) => {
                Shared.Language.CurrentLanguage = "Chinese";
                Shared.Common.CommonPage.Instance.Show();
             };

            BaseActivity.NetworkStateChanged += (v) =>
            {
            };
            BaseActivity.OnDestroyAction += (activity) => {
            //socket停止连接
            //ZigBee.Device.ZbGateway.FindGateWaySocket.Stop();
            };

            HDLUtils.SetAuthoritiesName("com.hdl.home.fileProvider");
         }

        public static bool IsShowTip = true;
        JpushNotificationReceiver myReceiver = new JpushNotificationReceiver { };

        /// <summary>
        /// 项目启动时网络查询
        /// </summary>
        void checkSomeInfo()
        {
            var status = isNetworkAvailable(this);
            string internetStatus = "Available";
            if (!status)
            {
                internetStatus = "UnaVailable";
            }
            else
            {
                var isWifi = isWifiConnected(this);
                if (isWifi)
                {
                    internetStatus = "WiFiConnect";
                }
                else
                {
                    internetStatus = "MobileConnect";
                }
            }

            int connectState = 0;
            if (internetStatus == "UnaVailable")
            {
                connectState = 0;
            }
            else if (internetStatus == "WiFiConnect")
            {
                connectState = 2;
            }
            else
            {
                connectState = 1;
            }
            //ZigBee.Device.ZbGateway.CheckConnection(connectState);
         }

        /// <summary>
        /// 网络是否可用
        /// </summary>
        /// <returns><c>true</c>, if network available was ised, <c>false</c> otherwise.</returns>
        /// <param name="context">Context.</param>
        bool isNetworkAvailable(Context context)
        {
            var cm = (ConnectivityManager)context.GetSystemService(ConnectivityService);

            if (cm == null || cm.ActiveNetworkInfo == null)
            {
                //当前网络不可用
                return false;
            }
            else
            {
                return cm.ActiveNetworkInfo.IsAvailable;
            }
        }

        /// <summary>
        /// 网络是否连接
        /// </summary>
        /// <returns><c>true</c>, if wifi connected was ised, <c>false</c> otherwise.</returns>
        /// <param name="context">Context.</param>
        bool isWifiConnected(Context context)
        {
            if (context != null)
            {
                var cm = (ConnectivityManager)context.GetSystemService(ConnectivityService);

                var mWiFiNetworkInfo = cm.GetNetworkInfo(ConnectivityType.Wifi);
                if (Android.Net.NetworkInfo.State.Connected == mWiFiNetworkInfo.GetState())
                {
                    //当前网络是Wi-Fi连接
                    return true;
                }

                var moWiFiNetworkInfo = cm.GetNetworkInfo(ConnectivityType.Mobile);
                if (Android.Net.NetworkInfo.State.Connected == moWiFiNetworkInfo.GetState())
                {
                    //当前网络是Mobile连接
                    return false;
                }
            }
            return false;
        }
                if (!string.IsNullOrEmpty(registrationId))
                {
                    Shared.Common.Config.Instance.RegistrationID = registrationId;
                    Shared.Common.Config.Instance.Save();
                }
                //调试:记录极光ID
                Shared.Phone.UserCenter.HdlLogLogic.Current.WriteOtherText(Shared.Phone.UserCenter.DirNameResourse.JiguangFile, "receive1:" + registrationId, true, true);
#endif
            };

           
            BaseActivity.RefreshUIAction += (activity) => {
                Shared.Language.CurrentLanguage = "Chinese";
                Shared.Common.CommonPage.Instance.Show();
             };

            BaseActivity.NetworkStateChanged += (v) =>
            {
            };
            BaseActivity.OnDestroyAction += (activity) => {
            //socket停止连接
            //ZigBee.Device.ZbGateway.FindGateWaySocket.Stop();
            };

            HDLUtils.SetAuthoritiesName("com.evoyo.home.fileProvider");
         }

        public static bool IsShowTip = true;
        JpushNotificationReceiver myReceiver = new JpushNotificationReceiver { };

        /// <summary>
        /// 项目启动时网络查询
        /// </summary>
        void checkSomeInfo()
        {
            var status = isNetworkAvailable(this);
            string internetStatus = "Available";
            if (!status)
            {
                internetStatus = "UnaVailable";
            }
            else
            {
                var isWifi = isWifiConnected(this);
                if (isWifi)
                {
                    internetStatus = "WiFiConnect";
                }
                else
                {
                    internetStatus = "MobileConnect";
                }
            }

            int connectState = 0;
            if (internetStatus == "UnaVailable")
            {
                connectState = 0;
            }
            else if (internetStatus == "WiFiConnect")
            {
                connectState = 2;
            }
            else
            {
                connectState = 1;
            }
            //ZigBee.Device.ZbGateway.CheckConnection(connectState);
         }

        /// <summary>
        /// 网络是否可用
        /// </summary>
        /// <returns><c>true</c>, if network available was ised, <c>false</c> otherwise.</returns>
        /// <param name="context">Context.</param>
        bool isNetworkAvailable(Context context)
        {
            var cm = (ConnectivityManager)context.GetSystemService(ConnectivityService);

            if (cm == null || cm.ActiveNetworkInfo == null)
            {
                //当前网络不可用
                return false;
            }
            else
            {
                return cm.ActiveNetworkInfo.IsAvailable;
            }
        }

        /// <summary>
        /// 网络是否连接
        /// </summary>
        /// <returns><c>true</c>, if wifi connected was ised, <c>false</c> otherwise.</returns>
        /// <param name="context">Context.</param>
        bool isWifiConnected(Context context)
        {
            if (context != null)
            {
                var cm = (ConnectivityManager)context.GetSystemService(ConnectivityService);

                var mWiFiNetworkInfo = cm.GetNetworkInfo(ConnectivityType.Wifi);
                if (Android.Net.NetworkInfo.State.Connected == mWiFiNetworkInfo.GetState())
                {
                    //当前网络是Wi-Fi连接
                    return true;
                }

                var moWiFiNetworkInfo = cm.GetNetworkInfo(ConnectivityType.Mobile);
                if (Android.Net.NetworkInfo.State.Connected == moWiFiNetworkInfo.GetState())
                {
                    //当前网络是Mobile连接
                    return false;
                }
            }
            return false;
        }
       
    }
    }
    [BroadcastReceiver(Name = "com.evoyo.home.JpushNotificationReceiver", Exported = false, Enabled = true)]
    [IntentFilter(new string[] { "cn.jpush.android.intent.REGISTRATION",
               "cn.jpush.android.intent.MESSAGE_RECEIVED",
                "cn.jpush.android.intent.NOTIFICATION_RECEIVED",
                "cn.jpush.android.intent.NOTIFICATION_OPENED",
                "cn.jpush.android.intent.CONNECTION"  }, Categories = new string[] { "com.evoyo.home" })]
    public class JpushNotificationReceiver : BroadcastReceiver
    {
        private static string TAG = "JpushNotificationReceiver";
@@ -215,8 +248,8 @@
                //接收到推送下来的自定义消息
                else if (JPushInterface.ActionMessageReceived == intent.Action)
                {
                    bundle.GetString(JPushInterface.ExtraMessage);
                 }
                    bundle.GetString(JPushInterface.ExtraMessage);
                }
                else
                {
                }
@@ -251,36 +284,83 @@
        /// <param name="bundle">Bundle.</param>
        private void OpenNotification(Context context, Bundle bundle)
        {
            Intent i = new Intent(context, typeof(BaseActivity));//Intent intent=new Intent( 起始组件对象 , 目标 Service.class);
            i.PutExtras(bundle);
            i.SetFlags(ActivityFlags.NewTask);
            context.StartActivity(i);
            String extras = bundle.GetString(JPushInterface.ExtraExtra);
            String myValue = "";
            //String myValue = "";
            try
            {
                JSONObject extrasJson = new JSONObject(extras);
                myValue = extrasJson.OptString("myKey");
                VideoMethod(extras);
                //JSONObject extrasJson = new JSONObject(extras);
                //myValue = extrasJson.OptString("myKey");
            }
            catch (Exception e)
            {
                System.Console.WriteLine(TAG, "Unexpected: extras is not a valid json", e);
                return;
            }
            //if (TYPE_THIS.equals(myValue))
            //{
            //Intent mIntent = new Intent(context, ThisActivity.class);
            //mIntent.putExtras(bundle);
            //mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //context.startActivity(mIntent);
            //}
            //else if (TYPE_ANOTHER.equals(myValue))
            //{
            //Intent mIntent = new Intent(context, AnotherActivity.class);
            //mIntent.putExtras(bundle);
            //mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //context.startActivity(mIntent);
            //}
        }
        /// <summary>
        /// android通知栏点击打开可视对讲的方法
        /// </summary>
        /// <param name="hiddenJson">云端推过来的数据</param>
        private void VideoMethod(string hiddenJson)
        {
            var hiddanJson = Newtonsoft.Json.Linq.JObject.Parse(hiddenJson);
            if (hiddanJson == null)
            {
                return;
            }
            Intent i = new Intent(context, typeof(BaseActivity));//Intent intent=new Intent( 起始组件对象 , 目标 Service.class);
            i.PutExtras(bundle);
            i.SetFlags(ActivityFlags.NewTask);
            context.StartActivity(i);
            //if (TYPE_THIS.equals(myValue))
            //{
            //Intent mIntent = new Intent(context, ThisActivity.class);
            //mIntent.putExtras(bundle);
            //mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //context.startActivity(mIntent);
            //}
            //else if (TYPE_ANOTHER.equals(myValue))
            //{
            //Intent mIntent = new Intent(context, AnotherActivity.class);
            //mIntent.putExtras(bundle);
            //mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //context.startActivity(mIntent);
            //}
            var json = Newtonsoft.Json.Linq.JObject.Parse(hiddanJson["HiddanJson"].ToString());
            if (json == null)
            {
                return;
            }
            Shared.Phone.VideoPhone.ESVideoInfo eSVideoInfo = new Shared.Phone.VideoPhone.ESVideoInfo();
            if (json["uuid"].ToString().Contains(","))
            {
                var uuid = json["uuid"].ToString().Split(',');
                eSVideoInfo.ESVideoUUID = uuid[0];
            }
            else
            {
                eSVideoInfo.ESVideoUUID = json["uuid"].ToString();
            }
            eSVideoInfo.DeviceName = Language.StringByID(Shared.R.MyInternationalizationString.theOutdoorUnit);
            eSVideoInfo.ESRoomID = int.Parse(json["roomno"].ToString());
            if (!string.IsNullOrEmpty(json["unitno"].ToString()))
            {
                string str = json["unitno"].ToString();
                string str1 = str.Substring(0, 2).TrimStart('0');
                string str2 = str.Substring(2, 2).TrimStart('0');
                eSVideoInfo.RoomName = str1 + Language.StringByID(Shared.R.MyInternationalizationString.dong) + str2 + Language.StringByID(Shared.R.MyInternationalizationString.unit);
            }
            Shared.Phone.VideoPhone.ESVideo.ShowESvideoVideoIntercom(eSVideoInfo);
        }
    }
}