Пример #1
0
 public static T CurrentOrDefault <T>(this BindingSource bs)
 {
     return((T)bs.CurrentOrDefault());
 }
Пример #2
0
        public AndroidLogcatUI(Settings settings)
        {
            InitializeComponent();
            m_tt             = new ToolTip();
            m_settings       = new AndroidLogcat(settings.AndroidLogcat);
            m_device_list    = new BindingList <string>();
            m_bs_device_list = new BindingSource {
                DataSource = m_device_list
            };
            m_filterspecs = new BindingList <AndroidLogcat.FilterSpec>(m_settings.FilterSpecs.ToList());

            var output_filepaths = settings.OutputFilepathHistory;

            Launch          = new LaunchApp();
            PreferredDevice = m_settings.SelectedDevice;

            const string prompt_text = "<Please set the path to adb.exe>";

            m_edit_adb_fullpath.ToolTip(m_tt, "The full path to the android debug bridge executable ('adb.exe')");
            m_edit_adb_fullpath.Text      = prompt_text;
            m_edit_adb_fullpath.ForeColor = Color.LightGray;
            m_edit_adb_fullpath.GotFocus += (s, a) =>
            {
                if (m_edit_adb_fullpath.ForeColor != Color.LightGray)
                {
                    return;
                }
                m_edit_adb_fullpath.Text      = string.Empty;
                m_edit_adb_fullpath.ForeColor = Color.Black;
            };
            m_edit_adb_fullpath.LostFocus += (s, a) =>
            {
                if (m_edit_adb_fullpath.Text.HasValue())
                {
                    return;
                }
                m_edit_adb_fullpath.ForeColor = Color.LightGray;
                m_edit_adb_fullpath.Text      = prompt_text;
            };
            m_edit_adb_fullpath.TextChanged += (s, a) =>
            {
                if (!((TextBox)s).Modified)
                {
                    return;
                }
                SetAdbPath(m_edit_adb_fullpath.Text);
            };

            // The version info printed by adb
            UpdateAdbVersionInfo(false);

            // Browse for the adb path
            m_btn_browse_adb.ToolTip(m_tt, "Browse the file system for the android debug bridge executable ('adb.exe')");
            m_btn_browse_adb.Click += (s, a) =>
            {
                var dlg = new OpenFileDialog {
                    Filter = "Programs (*.exe)|*.exe", FileName = "adb.exe"
                };
                if (dlg.ShowDialog(this) != DialogResult.OK)
                {
                    return;
                }
                SetAdbPath(dlg.FileName);
            };
            m_btn_browse_adb.Focus();

            // Devices list
            m_listbox_devices.ToolTip(m_tt, "The android devices current connected to adb. Hit 'Refresh' to repopulate this list");
            m_listbox_devices.DataSource     = m_bs_device_list;
            m_bs_device_list.CurrentChanged += (s, a) =>
            {
                UpdateAdbCommand();
                SelectPreferredOutputFile();
                m_btn_ok.Enabled = m_bs_device_list.CurrentOrDefault() != null;
            };

            // Connect button
            m_btn_connect.ToolTip(m_tt, "Add an android device not currently shown in the 'Connected Devices' list");
            m_btn_connect.Click  += (s, a) => ConnectDevice();
            m_btn_connect.Enabled = false;             // Until a valid adb path is set

            // Log buffers (main and system by default)
            m_listbox_log_buffers.ToolTip(m_tt, "The android log buffers to output");
            m_listbox_log_buffers.DataSource = Enum.GetNames(typeof(AndroidLogcat.ELogBuffer)).Select(x => x.ToLowerInvariant()).ToArray();
            foreach (var x in m_settings.LogBuffers)
            {
                m_listbox_log_buffers.SetSelected((int)x, true);
            }
            m_listbox_log_buffers.SelectedIndexChanged += (s, a) => UpdateAdbCommand();

            // Filter specs
            m_grid_filterspec.ToolTip(m_tt, "Configure filters to apply to the logcat output. Tag = '*' applies the filter to all log entries");
            m_grid_filterspec.AutoGenerateColumns = false;
            m_grid_filterspec.Columns.Add(new DataGridViewTextBoxColumn
            {
                DataPropertyName = nameof(AndroidLogcat.FilterSpec.Tag)
            });
            m_grid_filterspec.Columns.Add(new DataGridViewComboBoxColumn
            {
                DataPropertyName = nameof(AndroidLogcat.FilterSpec.Priority),
                DataSource       = Enum.GetValues(typeof(AndroidLogcat.EFilterPriority)),
                Sorted           = false
            });
            m_grid_filterspec.DataSource = m_filterspecs;
            m_filterspecs.ListChanged   += (s, a) => UpdateAdbCommand();

            // Log format
            m_combo_log_format.ToolTip(m_tt, "Select the format of the log output");
            m_combo_log_format.DataSource            = Enum.GetNames(typeof(AndroidLogcat.ELogFormat)).Select(x => x.ToLowerInvariant()).ToArray();
            m_combo_log_format.SelectedIndex         = (int)m_settings.LogFormat;
            m_combo_log_format.SelectedIndexChanged += (s, a) => UpdateAdbCommand();

            // Capture log output
            m_check_capture_to_log.ToolTip(m_tt, "Enable capturing the logcat output to a file");
            m_check_capture_to_log.Checked         = m_settings.CaptureOutputToFile;
            m_check_capture_to_log.CheckedChanged += (s, a) =>
            {
                m_combo_output_file.Enabled      = m_check_capture_to_log.Checked;
                m_btn_browse_output_file.Enabled = m_check_capture_to_log.Checked;
                m_check_append.Enabled           = m_check_capture_to_log.Checked;
            };

            // Log out capture file
            m_combo_output_file.ToolTip(m_tt, "The file path to save captured logcat output to");
            m_combo_output_file.Load(output_filepaths);
            m_combo_output_file.Enabled = m_check_capture_to_log.Checked;

            // Browse for capture output file
            m_btn_browse_output_file.ToolTip(m_tt, "Browse the file system for the file to write logcat output to");
            m_btn_browse_output_file.Enabled = m_check_capture_to_log.Checked;
            m_btn_browse_output_file.Click  += (s, a) =>
            {
                var dlg = new OpenFileDialog();
                if (dlg.ShowDialog(this) != DialogResult.OK)
                {
                    return;
                }
                m_combo_output_file.Items.Insert(0, dlg.FileName);
                m_combo_output_file.SelectedIndex = 0;
            };

            // Append to existing
            m_check_append.ToolTip(m_tt, "If checked, captured output is appended to the capture file.\r\nIf not, then the capture file is overwritten");
            m_check_append.Checked = Launch.AppendOutputFile;
            m_check_append.Enabled = m_check_capture_to_log.Checked;

            // Refresh button
            m_btn_refresh.ToolTip(m_tt, "Repopulate this dialog with data collected using adb.exe");
            m_btn_refresh.Click += (s, a) => PopulateUsingAdb();

            // Reset adb button
            m_btn_resetadb.ToolTip(m_tt, "Restart the adb server process.\r\nCan solve problems when connecting to devices");
            m_btn_resetadb.Enabled = false;             // unless there is an adb path
            m_btn_resetadb.Click  += (s, a) =>
            {
                var adbs = Process.GetProcessesByName("adb");
                foreach (var adb in adbs)
                {
                    adb.Kill();
                }
                Adb("start-server");
                PopulateUsingAdb();
            };

            // Ok button
            m_btn_ok.Enabled = m_listbox_devices.SelectedItem != null;

            Load += (s, a) =>
            {
                AutoDetectAdbPath();
            };

            // Save settings on close
            FormClosing += (s, a) =>
            {
                // If launch is selected, add the launch command line to the history
                if (DialogResult == DialogResult.OK)
                {
                    // Cancelled until we're sure we have a valid launch command
                    DialogResult = DialogResult.Cancel;

                    // Save settings - Only save here, so that cancel causes settings to be unchanged
                    m_settings.AdbFullPath         = m_edit_adb_fullpath.Text;
                    m_settings.CaptureOutputToFile = m_check_capture_to_log.Checked;
                    m_settings.AppendOutputFile    = m_check_append.Checked;
                    m_settings.LogBuffers          = m_listbox_log_buffers.SelectedIndices.Cast <AndroidLogcat.ELogBuffer>().ToArray();
                    m_settings.FilterSpecs         = m_filterspecs.ToArray();
                    m_settings.LogFormat           = (AndroidLogcat.ELogFormat)m_combo_log_format.SelectedIndex;
                    m_settings.SelectedDevice      = m_listbox_devices.SelectedItem.ToString();
                    settings.AndroidLogcat         = m_settings;
                    settings.RowDelimiter          = "<CR><CR><LF>";

                    // Use cancelled if no device was selected or no valid adb path found
                    if (ValidAdbPath && m_listbox_devices.SelectedItem != null)
                    {
                        string exe, args;
                        GenerateAdbCommand(out exe, out args);

                        // Update the launch options
                        Launch.Executable       = exe;
                        Launch.Arguments        = args;
                        Launch.WorkingDirectory = Path.GetDirectoryName(exe) ?? string.Empty;
                        Launch.OutputFilepath   = m_settings.CaptureOutputToFile ? m_combo_output_file.Text : string.Empty;
                        Launch.ShowWindow       = false;
                        Launch.AppendOutputFile = m_check_append.Checked;
                        Launch.Streams          = StandardStreams.Stdout | StandardStreams.Stderr;
                        DialogResult            = DialogResult.OK;
                    }
                }

                // If the adb full path has changed, update the settings even if DialogResult == cancel
                else if (ValidAdbPath && settings.AndroidLogcat.AdbFullPath != m_edit_adb_fullpath.Text)
                {
                    var save = new AndroidLogcat(settings.AndroidLogcat);
                    save.AdbFullPath       = m_edit_adb_fullpath.Text;
                    settings.AndroidLogcat = save;
                }
            };

            Disposed += (s, a) =>
            {
                m_tt.Dispose();
            };
        }