Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds App filter #135

Merged
merged 10 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions capture_service/device_mgr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ absl::StatusOr<std::vector<std::string>> AndroidDevice::ListPackage(PackageListO
{
std::vector<std::string> package_list;
std::string cmd = "shell pm list packages";
if (!option.with_system_package)
if (option != PackageListOptions::kAll && option != PackageListOptions::kNonDebuggableOnly)
{
cmd += " -3";
}
Expand All @@ -96,29 +96,43 @@ absl::StatusOr<std::vector<std::string>> AndroidDevice::ListPackage(PackageListO
return result.status();
}
output = *result;

std::vector<std::string> lines = absl::StrSplit(output, '\n');
for (const auto &line : lines)
{
std::vector<std::string> fields = absl::StrSplit(line, ':');
if (fields.size() == 2 && fields[0] == "package")
{
std::string package(absl::StripAsciiWhitespace(fields[1]));
if (option.debuggable_only)
switch (option)
{
case PackageListOptions::kAll: package_list.push_back(package); break;
case PackageListOptions::kDebuggableOnly:
result = Adb().RunAndGetResult("shell dumpsys package " + package);
if (!result.ok())
{
return result.status();
}
output = *result;
// TODO: find out more reliable way to find if app is debuggable.
if (absl::StrContains(output, "DEBUGGABLE"))
{
package_list.push_back(package);
}
GrantComm marked this conversation as resolved.
Show resolved Hide resolved
break;
case PackageListOptions::kNonDebuggableOnly:
result = Adb().RunAndGetResult("shell dumpsys package " + package);
if (!result.ok())
{
return result.status();
}
output = *result;
if (!absl::StrContains(output, "DEBUGGABLE"))
{
continue;
package_list.push_back(package);
}
break;
GrantComm marked this conversation as resolved.
Show resolved Hide resolved
default: assert("Error, unknown package list option.");
GrantComm marked this conversation as resolved.
Show resolved Hide resolved
}
package_list.push_back(package);
}
}
std::sort(package_list.begin(), package_list.end());
Expand Down
25 changes: 13 additions & 12 deletions capture_service/device_mgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,22 @@ class AndroidDevice
absl::Status SetupDevice();
absl::Status CleanupDevice();

struct PackageListOptions
enum class PackageListOptions
{
bool with_system_package;
bool debuggable_only;
kAll,
kDebuggableOnly,
kNonDebuggableOnly,
};

absl::StatusOr<std::vector<std::string>> ListPackage(PackageListOptions option = { 0,
1 }) const;
std::string GetDeviceDisplayName() const;
absl::Status SetupApp(const std::string &package,
const ApplicationType type,
const std::string &command_args);
absl::Status SetupApp(const std::string &binary,
const std::string &args,
const ApplicationType type);
absl::StatusOr<std::vector<std::string>> ListPackage(
PackageListOptions option = PackageListOptions::kAll) const;
std::string GetDeviceDisplayName() const;
absl::Status SetupApp(const std::string &package,
const ApplicationType type,
const std::string &command_args);
absl::Status SetupApp(const std::string &binary,
const std::string &args,
const ApplicationType type);

absl::Status CleanupAPP();
absl::Status StartApp();
Expand Down
Binary file added ui/images/filter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
149 changes: 149 additions & 0 deletions ui/package_filter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "package_filter.h"
#include <qevent.h>
#include <qnamespace.h>
#include <QLabel>
#include <iostream>
#include <string>
#include "QCheckBox"
#include "QHBoxLayout"
#include "QPushButton"
#include "QVBoxLayout"

//--------------------------------------------------------------------------------------------------
PackageFilter::PackageFilter(QWidget* parent) :
QWidget(parent)

{
m_all_filter = new QCheckBox("All", this);
m_all_filter->setCheckState(Qt::Checked);
m_active_filters.insert(m_all_filter);
m_debuggable_filter = new QCheckBox("Debuggable", this);
m_non_debuggable_filter = new QCheckBox("Non-Debuggable", this);
m_apply = new QPushButton("Apply Filters", this);
m_apply->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

connect(this, SIGNAL(rejected()), this, SLOT(onReject()));
connect(m_all_filter, SIGNAL(stateChanged(int)), this, SLOT(selectAllEventsFilter(int)));
connect(m_debuggable_filter, SIGNAL(stateChanged(int)), this, SLOT(selectFilter(int)));
connect(m_non_debuggable_filter, SIGNAL(stateChanged(int)), this, SLOT(selectFilter(int)));
connect(m_apply, SIGNAL(clicked()), this, SLOT(applyFilters()));

QHBoxLayout* filter_options_layout = new QHBoxLayout;
filter_options_layout->addWidget(m_all_filter);
filter_options_layout->addWidget(m_debuggable_filter);
filter_options_layout->addWidget(m_non_debuggable_filter);
filter_options_layout->addWidget(m_apply);

setLayout(filter_options_layout);
}

//--------------------------------------------------------------------------------------------------
void PackageFilter::selectAllEventsFilter(int state)
{
if (state == Qt::Checked)
{
m_non_debuggable_filter->setCheckState(Qt::Unchecked);
m_debuggable_filter->setCheckState(Qt::Unchecked);
m_filters.clear();
}
}

//--------------------------------------------------------------------------------------------------
void PackageFilter::selectFilter(int state)
{
if (state == Qt::Checked)
{
m_all_filter->setCheckState(Qt::Unchecked);
auto iterator = std::find_if(m_filters.begin(),
m_filters.end(),
[](const QCheckBox* checkBox) {
return checkBox->text() == "All";
});
if (iterator != m_filters.end())
{
m_filters.erase(iterator);
}
m_filters.insert(qobject_cast<QCheckBox*>(sender()));

if (m_filters.size() == (kTotalFilterCount - 1))
{
m_all_filter->setCheckState(Qt::Checked);
}
}
else
{
m_filters.erase(qobject_cast<QCheckBox*>(sender()));
}
}

//--------------------------------------------------------------------------------------------------
void PackageFilter::applyFilters()
{
m_active_filters.clear();

if (m_filters.empty())
{
m_all_filter->setCheckState(Qt::Checked);
m_active_filters.insert(m_all_filter);
emit filtersApplied({ m_all_filter->text() });
}
else
{
QSet<QString> applied_filter_texts;
for (QCheckBox* selectedCheckBox : m_filters)
{
if (selectedCheckBox->text() == "All")
{
m_active_filters.clear();
applied_filter_texts.clear();
m_active_filters.insert(selectedCheckBox);
applied_filter_texts.insert(selectedCheckBox->text());
emit filtersApplied(applied_filter_texts);
break;
}
m_active_filters.insert(selectedCheckBox);
applied_filter_texts.insert(selectedCheckBox->text());
}
emit filtersApplied(applied_filter_texts);
m_filters.clear();
}
}

//--------------------------------------------------------------------------------------------------
void PackageFilter::onReject()
{
close();
}

//--------------------------------------------------------------------------------------------------
void PackageFilter::closeEvent(QCloseEvent* event)
{
// Iterate over the copy of filters
auto unappliedFilters = m_filters;
for (QCheckBox* checkBox : unappliedFilters)
{
checkBox->setCheckState(m_active_filters.count(checkBox) ? Qt::Checked : Qt::Unchecked);
}

// Ensure all checkboxes in m_active_filters are checked
for (QCheckBox* checkBox : m_active_filters)
{
if (!checkBox->isChecked())
{
checkBox->setCheckState(Qt::Checked);
}
}
}
54 changes: 54 additions & 0 deletions ui/package_filter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <qevent.h>
#include <QDialog>
#include <unordered_set>

#pragma once

// Forward declarations
class QCheckBox;
class QLabel;
class QPushButton;

class PackageFilter : public QWidget
{
Q_OBJECT

public:
PackageFilter(QWidget *parent = nullptr);

public slots:
void selectAllEventsFilter(int state);
void selectFilter(int state);
void applyFilters();
void onReject();

protected:
void closeEvent(QCloseEvent *event) override;

signals:
void filtersApplied(QSet<QString> filters);

private:
const std::size_t kTotalFilterCount = 3;
QCheckBox *m_all_filter = nullptr;
QCheckBox *m_non_debuggable_filter = nullptr;
QCheckBox *m_debuggable_filter = nullptr;
QPushButton *m_apply = nullptr;

std::unordered_set<QCheckBox *> m_active_filters;
std::unordered_set<QCheckBox *> m_filters;
bool m_applied;
};
1 change: 1 addition & 0 deletions ui/resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
<file>images/dive.png</file>
<file>images/dive.ico</file>
<file>stylesheet.qss</file>
<file>images/filter.png</file>
</qresource>
</RCC>
Loading
Loading