using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Management;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Android_Samfrp
{
    public partial class Form1 : Form

    {
        private const string pidPattern = "PID_([0-9A-F]{4})";
        private const string vidPattern = "VID_([0-9A-F]{4})";
        private readonly string DoFunction;
        private object cm;
        private string SVER;
        private string IMEI;
        private string SN;

        public string modelNumber { get; private set; }
        public string regionCode { get; private set; }

        public Form1()
        {
            InitializeComponent();
            base.Load += new EventHandler(this.Form1_Load);
           
            this.IMEI = "";
            this.SN = "";
            this.modelNumber = "";
            this.regionCode = "";
            this.SVER = "";
        }
        private bool ATSend(SerialPort ATPort, string command)
        {
            if (!ATPort.IsOpen)
            {
                return false;
            }
            ATPort.WriteLine(command);
            string str = "";
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            do
            {
                Thread.Sleep(50);
                if (!ATPort.IsOpen)
                {
                    break;
                }
                str = ATPort.ReadExisting();
            }
            while ((!str.Contains("\r\nOK\r\n") && !str.Contains("\r\nERROR\r\n")) && (stopwatch.ElapsedMilliseconds <= 0xbb8L));
            Console.WriteLine(str);
            return str.Contains("\r\nOK\r\n");
        }


        private void butEnable_Click(object sender, EventArgs e)
        {


           
            ManagementObjectCollection instances = new ManagementClass("Win32_SerialPort").GetInstances();
            using (ManagementObjectCollection.ManagementObjectEnumerator enumerator = instances.GetEnumerator())
            {
                while (true)
                {
                    if (!enumerator.MoveNext())
                    {
                        break;
                    }
                    ManagementBaseObject current = enumerator.Current;
                    ManagementObject obj3 = (ManagementObject)current;
                    if (obj3["name"].ToString().Contains("SAMSUNG Mobile USB Modem"))
                    {
                        this.SerialPort1 = new SerialPort();
                        this.SerialPort1.PortName = obj3["deviceid"].ToString();
                        this.SerialPort1.DataBits = 8;
                        this.SerialPort1.BaudRate = 0x1c200;
                        this.SerialPort1.Parity = Parity.None;
                        this.SerialPort1.Open();
                    }
                }
            }
            if (!this.SerialPort1.IsOpen)
            {
                this.log.Clear();
                this.Delay(0.5);
                this.addlog("Device not detect ", Color.Black, true, false);
            }
            else
            {
               
                this.log.Clear();
                this.Delay(0.5);
                this.addlog("Device ", Color.Black, true, false);
                this.addlog(this.Comport.Text, Color.BlueViolet, true, false);
                this.Delay(0.5);
                this.SerialPort1.WriteLine("AT+DEVCONINFO\r");
                Thread.Sleep(0x7d0);

                string input = this.SerialPort1.ReadExisting();
                this.modelNumber = Regex.Match(input, @"MN\((.+?)\)").Groups[1].Value;
                this.regionCode = Regex.Match(input, @"PRD\((.+?)\)").Groups[1].Value;
                this.SVER = Regex.Match(input, @"VER\((.+?)\)").Groups[1].Value;
                this.IMEI = Regex.Match(input, @"IMEI\((.+?)\)").Groups[1].Value;
                this.SN = Regex.Match(input, @"SN\((.+?)\)").Groups[1].Value;
                string str2 = Regex.Match(input, @"UN\((.+?)\)").Groups[1].Value;
                string str3 = Regex.Match(input, @"LOCK\((.+?)\)").Groups[1].Value;
                string str4 = str3;
                this.SN = str3;
                string str5 = str4;
                Thread.Sleep(0x7d0);

                {

                    string Message = (" Go to emergency dialer enter *#0*#, click OK when done");
                    string title = (" << Introduction >>");
                    MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                    DialogResult result = MessageBox.Show(Message, title, buttons);
                    if (result == DialogResult.Yes)
                    {

                    }
                    else
                    {

                    }
                    
                    this.Delay(0.1);
                    this.addlog("\r\n Process Enable  ", Color.Black, true, false);
                    this.Delay(0.5);
                    this.addlog("\r\n Writing Data 1", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);

                    if (!this.ATSend(SerialPort1, "AT+KSTRINGB=0,3"))
                    {
                        this.addlog("FAIL ", Color.Red, false, true);
                    }
                    else
                    {
                        this.addlog("OK", Color.Green, true, false);
                    }
                    
                    Thread.Sleep(0xbb8);
                    this.Delay(0.5);
                    this.addlog("\r\n Writing Data 2", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);

                    if (!this.ATSend(SerialPort1, "AT+DUMPCTRL=1,0\r\n"))
                    {
                        this.addlog("FAIL", Color.Red, true, false);
                    }
                    else
                    {
                        this.addlog("OK", Color.Green, true, false);
                    }
                    
                    Thread.Sleep(0xbb8);
                    this.Delay(0.5);
                    this.addlog("\r\n Writing Data 3", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);

                    if (!this.ATSend(SerialPort1, "AT+DEBUGLVC=0,5\r\n"))
                    {
                        this.addlog("FAIL", Color.Red, true, false);
                    }
                    else
                    {
                        this.addlog("OK", Color.Green, true, false);

                    }
                    
                    Thread.Sleep(0xbb8);
                    this.Delay(0.5);
                    this.addlog("\r\n Writing Data 4", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);

                    if (!this.ATSend(SerialPort1, "AT+SWATD=0\r\n"))
                    {
                        this.addlog("FAIL", Color.Red, true, false);
                    }
                    else
                    {
                        this.addlog("OK", Color.Green, true, false);
                    }
                    
                    Thread.Sleep(0xbb8);
                    this.Delay(0.5);
                    this.addlog("\r\n Writing Data 5", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);

                    if (!this.ATSend(SerialPort1, "AT+ACTIVATE=0,0,0\r\n"))
                    {
                        this.addlog("FAIL", Color.Red, true, false);
                    }
                    else
                    {
                        this.addlog("OK", Color.Green, true, false);

                    }
                    
                    Thread.Sleep(0xbb8);
                    this.Delay(0.5);
                    this.addlog("\r\n Writing Data 6", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);

                    if (!this.ATSend(SerialPort1, "AT+SWATD=1\r\n"))
                    {
                        this.addlog("FAIL", Color.Red, true, false);
                    }
                    else
                    {
                        this.addlog("OK", Color.Green, true, false);
                    }
                    
                    Thread.Sleep(0xbb8);
                    this.Delay(0.5);
                    this.addlog("\r\n Writing Data 7", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);

                    if (!this.ATSend(SerialPort1, "AT+DEBUGLVC=0,5\r\n"))
                    {
                        this.addlog("FAIL", Color.Red, true, false);
                    }
                    else
                    {
                        this.addlog("OK", Color.Green, true, false);
                    }
                    
                    this.SerialPort1.Close();
                    this.addlog(".", Color.Black, true, false);
                    this.addlog("\r\n process Enable ", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(" Done ", Color.Green, true, false);
                    this.Delay(0.3);
                    this.addlog("\r\n  ======================================  ", Color.Green, true, false);
                    
                }
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            this.frp();
        }
        private string ATSendEx(SerialPort ATPort, string command, int timeout = 0xbb8)
        {
            if (!ATPort.IsOpen)
            {
                return null;
            }
            ATPort.WriteLine(command);
            string str = "";
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            do
            {
                Thread.Sleep(50);
                if (!ATPort.IsOpen)
                {
                    break;
                }
                try
                {
                    str = ATPort.ReadExisting();
                }
                catch (Exception)
                {
                    break;
                }
            }
            while ((!str.Contains("\nOK") && !str.Contains("\nERROR")) && (!str.Contains("FAIL") && (stopwatch.ElapsedMilliseconds <= timeout)));
            Console.WriteLine(str);
            return str;
        }
        private void frp()
        {


            ManagementObjectCollection instances = new ManagementClass("Win32_SerialPort").GetInstances();
            using (ManagementObjectCollection.ManagementObjectEnumerator enumerator = instances.GetEnumerator())
            {
                while (true)
                {
                    if (!enumerator.MoveNext())
                    {
                        break;
                    }
                    ManagementBaseObject current = enumerator.Current;
                    ManagementObject obj3 = (ManagementObject)current;
                    if (obj3["name"].ToString().Contains("SAMSUNG Mobile USB Modem"))
                    {
                        this.SerialPort1 = new SerialPort();
                        this.SerialPort1.PortName = obj3["deviceid"].ToString();
                        this.SerialPort1.DataBits = 8;
                        this.SerialPort1.BaudRate = 0x1c200;
                        this.SerialPort1.Parity = Parity.None;
                        this.SerialPort1.Open();
                    }
                }
            }
            if (!this.SerialPort1.IsOpen)
            {
                this.log.Clear();
                this.Delay(0.5);
                this.addlog("Device not detect ", Color.Black, true, false);
            }
            else
            {
                
                this.log.Clear();
                this.Delay(0.5);
                this.addlog("Device ", Color.Black, true, false);
                this.addlog(this.Comport.Text, Color.BlueViolet, true, false);
                this.Delay(0.5);
                this.SerialPort1.WriteLine("AT+DEVCONINFO\r");
                Thread.Sleep(0x7d0);

               

                {

                    string Message = (" Go to emergency dialer enter *#0*#, click OK when done");
                    string title = (" << Introduction >>");
                    MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                    DialogResult result = MessageBox.Show(Message, title, buttons);
                    if (result == DialogResult.Yes)
                    {

                    }
                    else
                    {

                    }
                    
                    this.Delay(0.1);
                    this.addlog("\r\n Process Enable  ", Color.Black, true, false);
                    this.Delay(0.5);
                    this.addlog("\r\n Writing Data 1", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);

                    if (!this.ATSend(SerialPort1, "AT+KSTRINGB=0,3"))
                    {
                        this.addlog("FAIL ", Color.Red, false, true);
                    }
                    else
                    {
                        this.addlog("OK", Color.Green, true, false);
                    }
                    
                    Thread.Sleep(0xbb8);
                    this.Delay(0.5);
                    this.addlog("\r\n Writing Data 2", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);

                    if (!this.ATSend(SerialPort1, "AT+DUMPCTRL=1,0\r\n"))
                    {
                        this.addlog("FAIL", Color.Red, true, false);
                    }
                    else
                    {
                        this.addlog("OK", Color.Green, true, false);
                    }
                   
                    Thread.Sleep(0xbb8);
                    this.Delay(0.5);
                    this.addlog("\r\n Writing Data 3", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);

                    if (!this.ATSend(SerialPort1, "AT+DEBUGLVC=0,5\r\n"))
                    {
                        this.addlog("FAIL", Color.Red, true, false);
                    }
                    else
                    {
                        this.addlog("OK", Color.Green, true, false);

                    }
                    
                    Thread.Sleep(0xbb8);
                    this.Delay(0.5);
                    this.addlog("\r\n Writing Data 4", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);

                    if (!this.ATSend(SerialPort1, "AT+SWATD=0\r\n"))
                    {
                        this.addlog("FAIL", Color.Red, true, false);
                    }
                    else
                    {
                        this.addlog("OK", Color.Green, true, false);
                    }
                   
                    Thread.Sleep(0xbb8);
                    this.Delay(0.5);
                    this.addlog("\r\n Writing Data 5", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);

                    if (!this.ATSend(SerialPort1, "AT+ACTIVATE=0,0,0\r\n"))
                    {
                        this.addlog("FAIL", Color.Red, true, false);
                    }
                    else
                    {
                        this.addlog("OK", Color.Green, true, false);

                    }
                    
                    Thread.Sleep(0xbb8);
                    this.Delay(0.5);
                    this.addlog("\r\n Writing Data 6", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);

                    if (!this.ATSend(SerialPort1, "AT+SWATD=1\r\n"))
                    {
                        this.addlog("FAIL", Color.Red, true, false);
                    }
                    else
                    {
                        this.addlog("OK", Color.Green, true, false);
                    }
                    
                    Thread.Sleep(0xbb8);
                    this.Delay(0.5);
                    this.addlog("\r\n Writing Data 7", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);
                    this.Delay(0.3);
                    this.addlog(".", Color.Black, true, false);

                    if (!this.ATSend(SerialPort1, "AT+DEBUGLVC=0,5\r\n"))
                    {
                        this.addlog("FAIL", Color.Red, true, false);
                    }
                    else
                    {
                        this.addlog("OK", Color.Green, true, false);
                    }
                    
                   
                    this.addlog(".", Color.Black, true, false);
                    this.addlog("\r\n process Enable ", Color.Black, true, false);
                    
                    this.addlog(" Done ", Color.Green, true, false);

                    this.addlog("\r\n  =========================================  ", Color.Green, true, false);
                    this.Delay(0.5);
                    this.addlog("\r\n OPERATION DONE BY BITZEAR GSM TOOL ", Color.OrangeRed, true, false);
                }
            }

        }
        private static string Adb(string cmd)
        {
            Process process = new Process();
            ProcessStartInfo startInfo = process.StartInfo;
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = true;
            startInfo.FileName = @"Data\adb.exe";
            startInfo.Arguments = cmd;
            startInfo.RedirectStandardOutput = true;
            process.Start();
            return process.StandardOutput.ReadToEnd();
        }

        public static string AdbCommand(string cmd) =>
            Adb(cmd);

        public void addlog(string s, Color color, bool isBold, bool newline = false)
        {
            RichTextBox log = this.log;
            if (newline && (log.TextLength > 0))
            {
                log.AppendText("\r\n");
            }
            log.SelectionStart = log.Text.Length;
            Color selectionColor = log.SelectionColor;
            log.SelectionColor = color;
            if (isBold)
            {
                log.SelectionFont = new Font(log.Font, FontStyle.Bold);
            }
            log.AppendText(s);
            log.SelectionColor = selectionColor;
            log.SelectionFont = new Font(log.Font, FontStyle.Regular);
            log.ScrollToCaret();
        }
        public void Delay(double dblSecs)
        {
            DateAndTime.Now.AddSeconds(1.1574074074074073E-05);
            DateTime time = DateAndTime.Now.AddSeconds(1.1574074074074073E-05).AddSeconds(dblSecs);
            while (DateTime.Compare(DateAndTime.Now, time) <= 0)
            {
                Application.DoEvents();
            }
        }

        private void Comport_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

            if (InvokeRequired)
            {

                
            }

            AdbCommand("start-server");
            this.Comport.Text = "";
            this.Comport.Items.Clear();
            this.Delay(0.1);
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM WIN32_SerialPort"))
            {
                ManagementObjectCollection.ManagementObjectEnumerator enumerator;
                try
                {
                    enumerator = searcher.Get().GetEnumerator();
                    while (true)
                    {
                        if (!enumerator.MoveNext())
                        {
                            break;
                        }
                        ManagementBaseObject current = enumerator.Current;
                        if (current["Caption"].ToString().ToUpper().Contains("SAMSUNG MOBILE USB MODEM"))
                        {
                            this.Comport.Items.Add(current["Caption"].ToString() + " (" + current["DeviceID"].ToString() + ")");
                        }
                    }
                }
                finally
                {

                }
            }
            if (this.Comport.Items.Count <= 0)
            {
                this.log.Clear();
                this.Delay(0.1);
                this.addlog("\r\n   ", Color.Green, true, false);
                this.Delay(0.1);
                Thread.Sleep(0xbb8);
                this.addlog("\r\n    ", Color.Green, true, false);
                this.Delay(0.1);
                this.addlog("\r\n   ", Color.Green, true, false);
                this.Delay(0.1);
                this.addlog("\r\n   ", Color.Green, true, false);
                this.Delay(0.1);
                Thread.Sleep(0xbb8);
                this.addlog("\r\n   ", Color.Green, true, false);
                this.Delay(0.1);
                this.addlog("\r\n   ", Color.Green, true, false);
                Thread.Sleep(0xbb8);
                this.addlog("\r\n    ", Color.Green, true, false);
                this.Delay(0.2);
                this.addlog("\r\n  ", Color.OrangeRed, true, false); ;
            }
            else
            {
                this.Comport.Text = Conversions.ToString(this.Comport.Items[0]);
                this.log.Clear();
            }
        }

        private void refreshDevices()
        {
            throw new NotImplementedException();
        }

        private string executeAdb(string exe, string arg)
        {
            Console.WriteLine(arg);
            Process process = new Process();
            ProcessStartInfo info1 = new ProcessStartInfo
            {
                FileName = exe,
                WorkingDirectory = Directory.GetCurrentDirectory() + @"\data",
                Arguments = arg,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true
            };
            process.StartInfo = info1;
            process.Start();
            string str = "";
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            while (!process.HasExited)
            {
                if (stopwatch.ElapsedMilliseconds > 0x4e20L)
                {
                    process.Kill();
                    break;
                }
                Thread.Sleep(100);
                str = str + process.StandardOutput.ReadToEnd() + process.StandardError.ReadToEnd();
            }
            process.Dispose();
            Console.WriteLine(str);
            return str;
        }

        private void butFrp_Click(object sender, EventArgs e)
        {


            

            if (!this.ConnectDevice())
            {
                this.log.Clear();
                this.Delay(0.5);
                this.addlog("Device not detect ", Color.Black, true, false);
            }
            else
            {
                this.log.Clear();
                this.addlog("\r\n Redinfo...", Color.Black, true, false);
                this.Delay(0.3);
                this.addlog(" Ok ", Color.Green, true, false);
                this.addlog("\r\n Serial : ", Color.Black, true, false);
                this.addlog(AdbCommand(" get-serialno "), Color.BlueViolet, true, false);
                this.Delay(0.1);
                this.addlog("\r\n Brand ", Color.Black, true, false);
                this.Delay(0.1);
                this.addlog(AdbCommand("shell getprop ro.product.brand"), Color.Green, true, false);
                this.Delay(0.1);
                this.addlog(" Model ", Color.Black, true, false);
                this.Delay(0.1);
                this.addlog(AdbCommand("shell getprop ro.product.model"), Color.Green, true, false);
                this.Delay(0.1);
                this.addlog(" Android Version ", Color.Black, true, false);
                this.Delay(0.1);
                this.addlog(AdbCommand("shell getprop ro.build.version.release"), Color.Green, true, false);
                this.Delay(0.1);
                this.addlog(" Baseband ", Color.Black, true, false);
                this.Delay(0.1);
                this.addlog(AdbCommand("shell getprop gsm.version.baseband"), Color.Green, true, false);
                this.Delay(0.1);
                this.addlog(" Process Reset Frp ", Color.Black, true, false);
                this.addlog(".", Color.Black, true, false);
                this.Delay(0.3);
                this.addlog(".", Color.Black, true, false);
               
                AdbCommand("shell content insert --uri content://settings/secure --bind name:s:user_setup_complete --bind value:s:1");
                this.Delay(0.3);
                this.addlog(".", Color.Black, true, false);
                AdbCommand("shell am start -n com.google.android.gsf.login");
                this.Delay(0.3);
                this.addlog(".", Color.Black, true, false);
                AdbCommand("shell am start -n com.google.android.gsf.login.LoginActivity");
                this.Delay(0.3);
                this.addlog(".", Color.Black, true, false);
                AdbCommand("push frp.bin /data/local/tmp/temp");
                this.Delay(0.3);
                this.addlog(".", Color.Black, true, false);
                AdbCommand("shell chmod 777 /data/local/tmp/temp");
                this.Delay(0.3);
                this.addlog(".", Color.Black, true, false);
                AdbCommand("shell /data/local/tmp/temp");
                this.Delay(0.3);
                this.addlog(" Done ", Color.Green, true, false);
                this.Delay(0.3);
                this.addlog("\r\n Reboot ...", Color.Black, true, false);
                this.Delay(0.3);
                AdbCommand("reboot");
                this.Delay(0.3);
                this.addlog(" Done ", Color.Green, true, false);
                this.Delay(0.3);
               
                this.addlog("\r\n  =========================================  ", Color.Green, true, false);
                this.Delay(0.5);
                this.addlog("\r\n OPERATION DONE BY BITZEAR GSM TOOL ", Color.OrangeRed, true, false);
            }
        }

        private bool ConnectDevice()
        {
            using (StringReader reader = new StringReader(AdbCommand("devices")))
            {
                while (true)
                {
                    if (reader.Peek() == -1)
                    {
                        break;
                    }
                    string str = reader.ReadLine();
                    if (!str.StartsWith("List of devices attached") && (str != ""))
                    {
                        
                        return (str.Substring(str.IndexOf("\t")).Replace("\t", "") == "device");
                    }
                }
            }
            return false;
        }

        private void buttonR_Click(object sender, EventArgs e)
        {



          
            this.Comport.Text = "";
            this.Comport.Items.Clear();
            this.Delay(0.5);
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM WIN32_SerialPort"))
            {
                ManagementObjectCollection.ManagementObjectEnumerator enumerator;
                try
                {
                    enumerator = searcher.Get().GetEnumerator();
                    while (true)
                    {
                        if (!enumerator.MoveNext())
                        {
                            break;
                        }
                        ManagementBaseObject current = enumerator.Current;
                        if (current["Caption"].ToString().ToUpper().Contains("SAMSUNG MOBILE USB MODEM"))
                        {
                            this.Comport.Items.Add(current["Caption"].ToString() + " (" + current["DeviceID"].ToString() + ")");
                        }
                    }
                }
                finally
                {

                }
            }
            if (this.Comport.Items.Count <= 0)
            {
                this.log.Clear();
                this.Delay(0.5);
                this.addlog("Device not detect ", Color.Black, true, false);
            }
            else
            {
                this.Comport.Text = Conversions.ToString(this.Comport.Items[0]);
                this.log.Clear();
                this.addlog("\r\n  =========================================  ", Color.Green, true, false);
                this.Delay(0.5);
                this.addlog("\r\n Android samfrp ", Color.OrangeRed, true, false);

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {


            
            this.log.Clear();
            this.Delay(0.5);
            
            if (!this.ConnectDevice())
            {
                this.log.Clear();
                this.Delay(0.5);
                this.addlog("Device not detect ", Color.Black, true, false);
               
            }
            else
            {
                
                this.addlog("\r\n Reading Device Info ADB Mode ", Color.Black, true, false);
                this.Delay(0.3);
                this.addlog("\r\n   ", Color.Black, true, false);
                this.addlog("\r\n Serial : ", Color.Black, true, false);
                this.addlog(AdbCommand(" shell getprop ril.serialnumber "), Color.BlueViolet, true, false);
                
                this.Delay(0.1);
                this.addlog(" Brand : ", Color.Black, true, false);
                this.Delay(0.1);
                this.addlog(AdbCommand("shell getprop ro.product.brand"), Color.Green, true, false);
                this.Delay(0.1);
                
                this.addlog(" Model : ", Color.Black, true, false);
                this.Delay(0.1);
                this.addlog(AdbCommand("shell getprop ro.product.model"), Color.Green, true, false);
                this.Delay(0.1);
                this.addlog(" Product Name : ", Color.Black, true, false);
                this.Delay(0.1);
                this.addlog(AdbCommand("shell getprop ro.product.name"), Color.Green, true, false);
                this.Delay(0.1);
                this.addlog(" Security Level : ", Color.Black, true, false);
                this.Delay(0.3);
                this.addlog(AdbCommand("shell getprop ro.build.version.security_patch"), Color.Green, true, false);
                this.Delay(0.1);
                this.addlog(" Android Version : ", Color.Black, true, false);
                this.Delay(0.3);
                this.addlog(AdbCommand("shell getprop ro.build.version.release"), Color.Green, true, false);
                this.Delay(0.1);
                this.addlog(" Hardware : ", Color.Black, true, false);
                this.Delay(0.1);
                this.addlog(AdbCommand("shell getprop ro.board.platform"), Color.Green, true, false);
                
                this.Delay(0.1);
                this.addlog(" Build Number : ", Color.Black, true, false);
                this.Delay(0.1);
                this.addlog(AdbCommand("shell getprop ro.build.display.id "), Color.Green, true, false);
                this.Delay(0.1);
                this.addlog(" Build_date : ", Color.Black, true, false);
                this.Delay(0.1);
                this.addlog(AdbCommand("shell getprop ro.bootimage.build.date"), Color.Green, true, false);
                this.Delay(0.1);
                this.addlog(" PDA : ", Color.Black, true, false);
                this.Delay(0.1);
                this.addlog(AdbCommand("shell getprop ro.build.PDA"), Color.Green, true, false);
                this.Delay(0.1);
                this.addlog(" BOOT : ", Color.Black, true, false);
                this.Delay(0.1);
                this.addlog(AdbCommand("shell getprop ro.boot.bootloader"), Color.Green, true, false);
                this.Delay(0.1);
                this.addlog(" CP : ", Color.Black, true, false);
                this.Delay(0.1);
                this.addlog(AdbCommand("shell getprop gsm.version.baseband "), Color.Green, true, false);
                this.Delay(0.1);
                
                this.addlog(" CSC : ", Color.Black, true, false);
                this.Delay(0.1);
                this.addlog(AdbCommand("shell getprop ril.official_cscver"), Color.Green, true, false);
                this.Delay(0.1);
                this.addlog(" Cpu : ", Color.Black, true, false);
                this.Delay(0.1);
                this.addlog(AdbCommand("shell getprop ro.product.cpu.abi"), Color.Green, true, false);
                this.addlog(" Country : ", Color.Black, true, false);
                this.Delay(0.1);
                this.addlog(AdbCommand("shell getprop ro.csc.sales_code"), Color.Green, true, false);
                this.Delay(0.1);
                this.addlog(" Vendor_gsm : ", Color.Black, true, false);
                this.Delay(0.1);
                this.addlog(AdbCommand("shell getprop vendor.gsm.sim.state"), Color.Green, true, false);
                this.addlog(" Description : ", Color.Black, true, false);
                this.Delay(0.1);
                this.addlog(AdbCommand("shell getprop ro.build.description"), Color.Green, true, false);
                this.addlog(" Fingerprint : ", Color.Black, true, false);
                this.Delay(0.1);
                this.addlog(AdbCommand("shell getprop ro.system.build.fingerprint"), Color.Green, true, false);
                this.Delay(0.1);
                this.addlog("\r\n  =========================================  ", Color.Green, true, false);
                this.Delay(0.5);
                this.addlog("\r\n BITZEAR GSM TOOL HAS FINISHED ", Color.OrangeRed, true, false);
            }
        }
    }
}
