protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); try { starting = true; //verifyStoragePermissions(); Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; //RequestWindowFeature(WindowFeatures.ActionBar); //Window.AddFlags(WindowManagerFlags.Fullscreen); //ActionBar.SetDisplayShowHomeEnabled(true); ActionBar.SetDisplayShowTitleEnabled(false); ActionBar.SetDisplayShowHomeEnabled(false); ActionBar.NavigationMode = ActionBarNavigationMode.Tabs; /*var setHasEmbeddedTabsMethod = ActionBar.Class * .GetDeclaredMethod("setHasEmbeddedTabs", Java.Lang.Boolean.Type); * setHasEmbeddedTabsMethod.Accessible=true; * setHasEmbeddedTabsMethod.Invoke(ActionBar, true);*/ //ActionBar.MenuVisibility = true; //ActionBar.DisplayOptions=ActionBarDisplayOptions SetContentView(Resource.Layout.Main); gridView1 = FindViewById <GridView>(Resource.Id.gridView1); ladapter = new CustomAdapter(this, gridView1); gridView1.Adapter = ladapter; parser = new Parser(this, ladapter); statusText = FindViewById <TextView>(Resource.Id.StatusText); /*statusTimer = new System.Timers.Timer(10 * 1000); * statusTimer.Elapsed += timer_Elapsed; * statusTimer.AutoReset = false;*/ statusText.Visibility = Android.Views.ViewStates.Invisible; statusText.Text = ""; // initalize menu menuIcon = FindViewById <ImageView>(Resource.Id.menuIcon); PopupMenu menu = new PopupMenu(this, menuIcon); menu.Inflate(Resource.Menu.OptionsMenu); //menu.Menu.FindItem(Resource.Id.selectionMode).SetCheckable(true); menuIcon.Click += (s, arg) => { menu.Show(); }; menuIcon.Clickable = true; menu.MenuItemClick += Menu_MenuItemClick; prefIcon = FindViewById <ImageView>(Resource.Id.prefIcon); PopupMenu prefMenu = new PopupMenu(this, prefIcon); prefMenu.Inflate(Resource.Menu.Wrench); //menu.Menu.FindItem(Resource.Id.selectionMode).SetCheckable(true); prefIcon.Click += (s, arg) => { prefMenu.Show(); }; prefIcon.Clickable = true; prefMenu.MenuItemClick += Menu_MenuItemClick; // context menu RegisterForContextMenu(gridView1); bluetoothHandler = new BluetoothHandler(this, parser); tabs = new List <Tab>(); prefs = PreferenceManager.GetDefaultSharedPreferences(this); if (prefs.Contains("tabs")) { // read saved stuff LoadTabs(); } else { CreateDefaultTabs(); } convertToImperial = prefs.GetBoolean("convertToImperial", false); prefMenu.Menu.FindItem(Resource.Id.metric) .SetChecked(!convertToImperial); gridView1.Clickable = true; gridView1.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) => { int packetId = ladapter[e.Position].packetId; if (!ladapter[e.Position].selected) { sel = 1; } else { sel++; } if (sel > 3) { sel = 0; } if (sel == 0 || sel == 1) { ladapter[e.Position].selected = sel == 1; ladapter.Touch(ladapter[e.Position]); } else { foreach (var item in ladapter.items.Where(x => x.packetId == packetId)) { item.selected = sel == 2; ladapter.Touch(item); } } //ladapter.NotifyDataSetChanged(); }; var VersionName = PackageManager.GetPackageInfo(PackageName, 0).VersionName; //Toast.MakeText(this, "Version " + VersionName, ToastLength.Long).Show(); prefMenu.Menu.Add("Version " + VersionName); var StoredVersion = prefs.GetString("version", ""); if (StoredVersion != VersionName) { AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.SetTitle("Welcome to Scan My Tesla version " + VersionName + "!"); if (StoredVersion == "" && prefs.All.Keys.Any()) // if there are any prefs in here, must mean we are upgrading { alert.SetMessage( // but version recording started in 1.3.0, so upgrading from earlier than that "This version has some new and redesigned tabs.\n" + "Use 'Factory reset all tabs' from the left menu if you want the new layout.\n" + "Or, you can select 'New tab' from the right menu to add a single tab from the templates."); } alert.SetCancelable(true); alert.SetPositiveButton("Changelog", (senderAlert, args) => { var uri = Android.Net.Uri.Parse("https://sites.google.com/view/scanmytesla/changelog"); var intent = new Intent(Intent.ActionView, uri); StartActivity(intent); }); alert.SetNegativeButton("Close", (senderAlert, args) => { }); Dialog dialog = alert.Create(); dialog.Show(); PutPref("version", VersionName); } #if !disablebluetooth var storedDevice = prefs.GetString("device", ""); if (storedDevice == "") { StartActivityForResult(typeof(DeviceListActivity), 1); } else { Intent intent = new Intent(this, typeof(MainActivity)); intent.PutExtra("device_address", storedDevice); OnActivityResult(1, Result.Ok, intent); } var t = tabs.Where(x => x.name == prefs.GetString("currentTab", "")).FirstOrDefault(); if (t != null) { ActionBar.SelectTab(t.ActionBarTab); } //Finish(); #else var t = tabs.Where(x => x.name == prefs.GetString("currentTab", "")).FirstOrDefault(); if (t != null) { ActionBar.SelectTab(t.ActionBarTab); } bluetoothHandler.Initialize(null); Window.SetFlags(WindowManagerFlags.KeepScreenOn, WindowManagerFlags.KeepScreenOn); //bluetoothHandler.Start(); #endif starting = false; verifyStoragePermissions(); /*if (LogTimer == null) * LogTimer = new Timer(LogTimerCallback, null, 2000, 200);*/ //SupportFunctions.Version = context.PackageManager.GetPackageInfo(context.PackageName, 0).VersionName; //prefs.GetInt("") } catch (Exception e) { //set alert for executing the task AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.SetTitle("Error"); alert.SetMessage(e.ToString()); alert.SetCancelable(false); alert.SetPositiveButton("OK", (senderAlert, args) => { System.Environment.Exit(1); }); Dialog dialog = alert.Create(); dialog.Show(); } }
private void ParsePacket(string raw, int id, byte[] bytes) { if (packets.ContainsKey(id)) { packets[id].Update(bytes); numUpdates++; if (id == 0x6F2) { if (bytes[0] >= 24) { var values = items.Where(x => x.Value.unit == "zCC"); double min = values.Min(x => x.Value.GetValue(false)); double max = values.Max(x => x.Value.GetValue(false)); double avg = values.Average(x => x.Value.GetValue(false)); UpdateItem("Cell temp min", "c", "bcz", 1000, min, 0x6F2); UpdateItem("Cell temp avg", "c", "bcpz", 1001, avg, 0x6F2); UpdateItem("Cell temp max", "c", "bcz", 1002, max, 0x6F2); UpdateItem("Cell temp diff", "Cd", "bcz", 1003, max - min, 0x6F2); } else { var values = items.Where(x => x.Value.unit == "zVC"); double min = values.Min(x => x.Value.GetValue(false)); double max = values.Max(x => x.Value.GetValue(false)); double avg = values.Average(x => x.Value.GetValue(false)); UpdateItem("Cell min", "Vc", "bz", 1000, min, 0x6F2); UpdateItem("Cell avg", "Vc", "bpz", 1001, avg, 0x6F2); UpdateItem("Cell max", "Vc", "bz", 1002, max, 0x6F2); UpdateItem("Cell diff", "Vcd", "bz", 1003, max - min, 0x6F2); } } if (time < SystemClock.ElapsedRealtime()) { UpdateItem("Packets per second", "xp", "", 0, numUpdates, 0xFFF); numUpdates = 0; time = SystemClock.ElapsedRealtime() + 1000; foreach (var item in items.Where(x => x.Value.LimitsChanged()).Select(x => x.Value)) { adapter.Touch(item); } } if (resetGaugeTime < SystemClock.ElapsedRealtime()) { adapter.limits.Remove("zCC"); adapter.limits.Remove("zVC"); foreach (var item in items.Where(x => x.Value.unit == "zCC" || x.Value.unit == "zVC").Select(x => x.Value)) { item.UpdateLimits(item.GetValue(false)); } resetGaugeTime = SystemClock.ElapsedRealtime() + 1000 * 60; } if (fastLogEnabled) { string s = ""; int pos = 0, lastPos = 0; bool anythingToLog = false; foreach (var logItem in fastLogItems) { pos++; /*if (logItem.name == "Raw") * s += raw;*/ if (logItem.packetId != null && logItem.packetId.Contains(id)) { lastPos = pos; if (items.ContainsKey(logItem.name)) // in the case of Front RPM, the next line throws exception on an RWD car (because front RPM is not in items) { s += items[logItem.name].GetValue(((MainActivity)adapter.GetContext()).convertToImperial) .ToString(System.Globalization.CultureInfo.InvariantCulture); anythingToLog = true; } } s += separator; } if (anythingToLog) { fastLogStream.WriteLine(logTimer.ElapsedMilliseconds.ToString() + separator + s); } } } }