using System;
using System.Net;
using System.Text;
using Android;
using Android.Content;
using Android.Net;
using Android.OS;
using CN.Jpush.Android.Api;
using CN.Jpush.Android.Service;
using Java.Util;
using Newtonsoft.Json.Linq;
using Shared;
using Shared.SimpleControl;
using SmartHome;
namespace com.hdl.on
{
///
/// 设定为默认启动的Application
///
[Android.App.Application]
public class Application : Android.App.Application
{
static void reStartApp (Android.App.Application application)
{
var intent = new Intent (application, typeof (BaseActivity));
intent.AddFlags (ActivityFlags.NewTask);
application.StartActivity (intent);
Android.OS.Process.KillProcess (Android.OS.Process.MyPid ());
}
public Application (IntPtr handle, Android.Runtime.JniHandleOwnership ownerShip) : base (handle, ownerShip) { }
public override void OnCreate ()
{
if (Shared.Application.Activity != null) {
reStartApp (this);
return;
}
base.OnCreate ();
initAll ();
com.freeview.global.Video.Start ();
}
///
/// 所有初始化全部在这个方法实现
///
void initAll ()
{
#if wallon
BaseActivity.TopMargin = -30;
BaseActivity.IsEnnableGPS = false;
BaseActivity.VerifyDateTime = DateTime.MaxValue;
BaseActivity.KeepScreenON = false;
#endif
Locale locale1 = Locale.Default;
var localeList = Resources.Configuration.Locale;
//if(localeList.Language == "zh") {
//Language.CurrentLanguage = "Chinese";
//}
string [] permissions = new string [] { Manifest.Permission.WriteExternalStorage, Manifest.Permission.RecordAudio,
Manifest.Permission.Camera, Manifest.Permission.ProcessOutgoingCalls, Manifest.Permission.GetAccounts};
var version = Build.VERSION.SdkInt + "";
Console.WriteLine ("Build.VERSION.SdkInt : " + version);
//Android.Support.V4.App.ActivityCompat.RequestPermissions (BaseActivity., permissions, 321);
BaseActivity.BackKeyAction = () => {
if (Shared.SimpleControl.MainPage.Loading != null) {
Console.WriteLine (MainPage.Loading.CurStatus);
if (Shared.SimpleControl.MainPage.Loading.CurStatus) {
//Shared.SimpleControl.MainPage.Loading.Hide ();
return;
}
}
try {
var lastView = Shared.Application.MainPage.GetChildren (Shared.Application.MainPage.ChildrenCount - 1);
//Console.WriteLine ("lastView " + lastView);
if (lastView != null && lastView.Tag.ToString () == "Dialog") {
if (SharedMethod.SharedMethod.CurPageLayout == null) {
BaseActivity.VerifyDateTime = DateTime.MaxValue;
return;
} else {
//Console.WriteLine ("Remove Dialog!!!!");
var lastView2 = Shared.Application.MainPage.GetChildren (Shared.Application.MainPage.ChildrenCount - 2);
lastView.RemoveFromParent ();
lastView2.RemoveFromParent ();
//Console.WriteLine ("Remove Dialog End!!!!");
BaseActivity.VerifyDateTime = DateTime.MinValue;
return;
}
}
} catch (System.Exception ezx) {
Console.WriteLine (ezx.Message);
}
if (SharedMethod.SharedMethod.CurPageLayout != null) {
if (SharedMethod.SharedMethod.CurPageLayout.ChildrenCount > 1) {
//Console.WriteLine ("Remove PageLayout Children!!!!");
SharedMethod.SharedMethod.CurPageLayout.GetChildren (SharedMethod.SharedMethod.CurPageLayout.ChildrenCount - 1).RemoveFromParent ();
//Console.WriteLine ("Remove PageLayout Children End!!!!");
BaseActivity.VerifyDateTime = DateTime.MinValue;
} else {
//Console.WriteLine ("BaseActivity.VerifyDateTime = DateTime.MaxValue");
BaseActivity.VerifyDateTime = DateTime.MaxValue;
}
}
};
BaseActivity.IsHideVirualButtons = true;
ZXing.Mobile.MobileBarcodeScanner.Initialize (this);
init ();
BaseActivity.OnCreateActoin += (activity, application) => {
Microsoft.AppCenter.AppCenter.Start ("7e0ba985-ae3b-4299-9483-b10f4041f8b2", typeof (Microsoft.AppCenter.Analytics.Analytics), typeof (Microsoft.AppCenter.Crashes.Crashes));
if (!string.IsNullOrEmpty (UserConfig.Instance.SetLanguage)) {
Language.CurrentLanguage = UserConfig.Instance.SetLanguage;
}
//保存获取的极光服务器上的注册ID到本地文件
#if wallon
#else
var tokenID = JPushInterface.GetRegistrationID (activity);
Console.WriteLine ("RegistrationID: " + tokenID);
if (!string.IsNullOrEmpty (tokenID) && UserConfig.Instance.tokenID != tokenID) {
UserConfig.Instance.tokenID = tokenID;
UserConfig.Instance.SaveUserConfig ();
}
int loadTokenCount = 0;
System.Threading.Tasks.Task.Run (() => {
while (string.IsNullOrEmpty (tokenID)) {
System.Threading.Thread.Sleep (1000);
tokenID = JPushInterface.GetRegistrationID (activity);
Console.WriteLine ("Sleep.tokenID + " + JPushInterface.GetRegistrationID (activity));
loadTokenCount++;
if (loadTokenCount > 30) {
break;
}
}
Console.WriteLine ("RegistrationID: " + tokenID);
if (!string.IsNullOrEmpty (tokenID) && UserConfig.Instance.tokenID != tokenID) {
UserConfig.Instance.tokenID = tokenID;
UserConfig.Instance.SaveUserConfig ();
}
});
#endif
BaseActivity.NetworkStateChanged += (int obj) => {
MqttCommon.DisConnectRemoteMqttClient ("NetworkStateChanged");
Shared.SimpleControl.EquipmentPublicClass.CheckLinkRemote (obj);
};
};
BaseActivity.RefreshUIAction += (activity) => {
MainPage.Show ();
checkSomeInfo ();
};
BaseActivity.OnResumeAction += (activity) => {
//new System.Threading.Thread (() => {
// while (!MainPage.Showed) {
// System.Threading.Thread.Sleep (50);
// }
MqttCommon.DisConnectRemoteMqttClient ();
checkSomeInfo ();
Console.WriteLine ("Close OnResumeAction!!!");
WiFiSet.refreshView?.Invoke ();
};
BaseActivity.OnDestroyAction += (activity) => {
Console.WriteLine ("OnDestroyAction");
BusSocket.Stop ();
};
}
public static bool IsShowTip = true;
void init ()
{
Shared.Application.Skin = UserConfig.Instance.CurrentSkinName;
SkinStyle.Current.ChangeColor ();
UserConfig.Instance.phoneName = Android.OS.Build.Manufacturer;
//设置极光调试模式,为false时只打印警告信息
JPushInterface.SetDebugMode (false);
JPushInterface.Init (this);//保存手机名称到本地文件
//获取经纬度
MainPage.AirQuality = new AirQuality ();
Shared.Application.LocationAction = (arg1, arg2) => {
Shared.Application.LocationAction = null;
System.Threading.Tasks.Task.Run (() => {
while (true) {
System.Console.WriteLine ("Get Location GetAirQuality");
//获取天气
var webClient = new WebClient ();
string url = $"https://developer.hdlcontrol.com/Weather/Weather/FindCity/?lon={arg1}&lat={arg2}";
string responseString = null;
try {
responseString = Encoding.UTF8.GetString (webClient.DownloadData (url));
} catch (Exception ex) {
Console.WriteLine (ex.Message);
}
if (responseString != null) {
try {
var revertObj = Newtonsoft.Json.JsonConvert.DeserializeObject (responseString);
JObject jt = Newtonsoft.Json.JsonConvert.DeserializeObject (revertObj.ResponseData.ToString ());
MainPage.AirQuality.city = jt ["City"].ToString ();
var cityId = jt ["Cid"].ToString ();
url = $"https://developer.hdlcontrol.com/Weather/Weather/GetAirQualityAndWeather/?cid={cityId}";
responseString = null;
responseString = Encoding.UTF8.GetString (webClient.DownloadData (url));
revertObj = Newtonsoft.Json.JsonConvert.DeserializeObject (responseString);
jt = Newtonsoft.Json.JsonConvert.DeserializeObject (revertObj.ResponseData.ToString ());
MainPage.AirQuality.airQTemp = jt ["Temperature"].ToString ();
MainPage.AirQuality.airQHumidity = jt ["Humidity"].ToString ();
MainPage.AirQuality.airQAirPM25 = jt ["PM25"].ToString ();
MainPage.AirQuality.airQAirWeather = jt ["Weather"].ToString ();
break;
} catch (Exception ex) {
Console.WriteLine (ex.ToString ());
}
}
System.Threading.Thread.Sleep (1000);
}
Shared.Application.RunOnMainThread (() => {
CommonPage.RefreshAir?.Invoke ();
});
});
};
}
void checkSomeInfo ()
{
try {
RemoteInfo.Current.ReadMsgList (IsShowTip);
JPushInterface.ClearAllNotifications (this);
IsShowTip = true;
var status = isNetworkAvailable (this);
int internetStatus = 0;
if (!status) {
internetStatus = 0;
}
var isWifi = isWifiConnected (this);
if (isWifi) {
internetStatus = 2;
} else {
internetStatus = 1;
}
UserConfig.Instance.internetStatus = internetStatus;
Shared.SimpleControl.EquipmentPublicClass.CheckLinkRemote (internetStatus);
} catch (Exception ex) {
Console.WriteLine ($"android check some info erorr : {ex.Message}");
}
}
bool isNetworkAvailable (Context context)
{
var cm = (ConnectivityManager)context.GetSystemService (ConnectivityService);
if (cm == null || cm.ActiveNetworkInfo == null) {
return false;
} else {
return cm.ActiveNetworkInfo.IsAvailable;
}
}
bool isWifiConnected (Context context)
{
if (context != null) {
var mConnectivityManager = (ConnectivityManager)context.GetSystemService
(ConnectivityService);
var mWiFiNetworkInfo = mConnectivityManager.GetNetworkInfo (ConnectivityType.Wifi);
if (mWiFiNetworkInfo != null) {
return mWiFiNetworkInfo.IsAvailable;
}
}
return false;
}
}
[BroadcastReceiver]
[Android.App.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.hdl.in" })
]
public class JpushNotificationReceiver : BroadcastReceiver
{
static string ACTION = "android.intent.action.BOOT_COMPLETED";
public override void OnReceive (Context context, Intent intent)
{
try {
Console.WriteLine ("JpushNotificationReceiver" + intent.Action);
Bundle bundle = intent.Extras;
if (intent.Action == ACTION) {
//Console.WriteLine ("开机自动服务自动启动,PushService是要启动的服务 ");
var service = new Intent (context, typeof (PushService));
context.StartService (service);
}
//接收Registration Id
else if (JPushInterface.ActionRegistrationId == intent.Action) {
string regId = bundle.GetString (JPushInterface.ExtraRegistrationId);
//System.Console.WriteLine ("Registration ID:" + regId);
} else if (JPushInterface.ActionNotificationOpened == intent.Action) {
Console.WriteLine ("用户点击打开了通知");
Application.IsShowTip = false;
if (Shared.Application.Activity == null) {
var tempIntent = new Intent (context, typeof (Shared.BaseActivity));//Intent intent=new Intent( 起始组件对象 , 目标 Service.class);
tempIntent.SetFlags (ActivityFlags.BroughtToFront);
context.StartActivity (tempIntent);
} else {
(Shared.Application.Activity as BaseActivity).MoveToFront ();
RemoteInfo.Current.ReadMsgList (false);
}
} else {
Console.WriteLine ("极光推送!!!");
RemoteInfo.Current.ReadMsgList (true);
}
} catch (System.Exception e) {
Console.WriteLine ("极光推送出错:" + e.Message);
}
}
}
}