Commit 1e988321 authored by Qiang Xu's avatar Qiang Xu Committed by Commit Bot

arc: sort app shortcut items in context menu

changes:
This CL (paired with Android side CL) gets some metadata from Android
to sort the app shortcut items in context menu based on two rules:
(1) Static (declared in manifest) shortcuts and then dynamic shortcuts;
(2) Within each shortcut type (static and dynamic), shortcuts are sorted
in order of increasing rank according to getRank().

This CL is a first step of crbug.com/848962.

Bug: 848962
Test: manual
Change-Id: I10a4e8e1a7d8cb80c69f5bb81f6e2c6abf564645
Reviewed-on: https://chromium-review.googlesource.com/1086118
Commit-Queue: Qiang Xu <warx@google.com>
Reviewed-by: default avatarLuis Hector Chavez <lhchavez@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarYury Khmel <khmel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#566030}
parent d8484b3b
...@@ -310,6 +310,7 @@ source_set("chromeos") { ...@@ -310,6 +310,7 @@ source_set("chromeos") {
"arc/accessibility/arc_accessibility_util.h", "arc/accessibility/arc_accessibility_util.h",
"arc/accessibility/ax_tree_source_arc.cc", "arc/accessibility/ax_tree_source_arc.cc",
"arc/accessibility/ax_tree_source_arc.h", "arc/accessibility/ax_tree_source_arc.h",
"arc/app_shortcuts/arc_app_shortcut_item.cc",
"arc/app_shortcuts/arc_app_shortcut_item.h", "arc/app_shortcuts/arc_app_shortcut_item.h",
"arc/app_shortcuts/arc_app_shortcuts_menu_builder.cc", "arc/app_shortcuts/arc_app_shortcuts_menu_builder.cc",
"arc/app_shortcuts/arc_app_shortcuts_menu_builder.h", "arc/app_shortcuts/arc_app_shortcuts_menu_builder.h",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/arc/app_shortcuts/arc_app_shortcut_item.h"
namespace arc {
ArcAppShortcutItem::ArcAppShortcutItem() = default;
ArcAppShortcutItem::ArcAppShortcutItem(const ArcAppShortcutItem& item) =
default;
ArcAppShortcutItem::~ArcAppShortcutItem() = default;
} // namespace arc
...@@ -9,12 +9,17 @@ ...@@ -9,12 +9,17 @@
#include <vector> #include <vector>
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "components/arc/common/app.mojom.h"
#include "ui/gfx/image/image_skia.h" #include "ui/gfx/image/image_skia.h"
namespace arc { namespace arc {
// Describes app shortcut that is published by Android's ShortcutManager. // Describes app shortcut that is published by Android's ShortcutManager.
struct ArcAppShortcutItem { struct ArcAppShortcutItem {
ArcAppShortcutItem();
ArcAppShortcutItem(const ArcAppShortcutItem& item);
~ArcAppShortcutItem();
// The ID of this shortcut. Unique within each publisher app and stable across // The ID of this shortcut. Unique within each publisher app and stable across
// devices. // devices.
std::string shortcut_id; std::string shortcut_id;
...@@ -24,6 +29,12 @@ struct ArcAppShortcutItem { ...@@ -24,6 +29,12 @@ struct ArcAppShortcutItem {
// The icon for this shortcut. // The icon for this shortcut.
gfx::ImageSkia icon; gfx::ImageSkia icon;
// The category type of this shortcut.
mojom::AppShortcutItemType type = mojom::AppShortcutItemType::kStatic;
// "Rank" of a shortcut, which is a non-negative, sequential value.
int rank = 0;
}; };
using ArcAppShortcutItems = std::vector<ArcAppShortcutItem>; using ArcAppShortcutItems = std::vector<ArcAppShortcutItem>;
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "chrome/browser/chromeos/arc/app_shortcuts/arc_app_shortcuts_menu_builder.h" #include "chrome/browser/chromeos/arc/app_shortcuts/arc_app_shortcuts_menu_builder.h"
#include <tuple>
#include <utility> #include <utility>
#include "base/bind.h" #include "base/bind.h"
...@@ -62,9 +63,20 @@ void ArcAppShortcutsMenuBuilder::OnGetAppShortcutItems( ...@@ -62,9 +63,20 @@ void ArcAppShortcutsMenuBuilder::OnGetAppShortcutItems(
std::unique_ptr<ArcAppShortcutItems> app_shortcut_items) { std::unique_ptr<ArcAppShortcutItems> app_shortcut_items) {
app_shortcut_items_ = std::move(app_shortcut_items); app_shortcut_items_ = std::move(app_shortcut_items);
if (app_shortcut_items_ && !app_shortcut_items_->empty()) { if (app_shortcut_items_ && !app_shortcut_items_->empty()) {
ArcAppShortcutItems& items = *app_shortcut_items_;
// Sort the shortcuts based on two rules: (1) Static (declared in manifest)
// shortcuts and then dynamic shortcuts; (2) Within each shortcut type
// (static and dynamic), shortcuts are sorted in order of increasing rank.
std::sort(
items.begin(), items.end(),
[](const ArcAppShortcutItem& item1, const ArcAppShortcutItem& item2) {
return std::tie(item1.type, item1.rank) <
std::tie(item2.type, item2.rank);
});
menu_model->AddSeparator(ui::DOUBLE_SEPARATOR); menu_model->AddSeparator(ui::DOUBLE_SEPARATOR);
int command_id = command_id_first_; int command_id = command_id_first_;
for (const auto& item : *app_shortcut_items_) { for (const auto& item : items) {
if (command_id != command_id_first_) if (command_id != command_id_first_)
menu_model->AddSeparator(ui::PADDED_SEPARATOR); menu_model->AddSeparator(ui::PADDED_SEPARATOR);
menu_model->AddItemWithIcon(command_id++, item.short_label, item.icon); menu_model->AddItemWithIcon(command_id++, item.short_label, item.icon);
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "chrome/browser/chromeos/arc/app_shortcuts/arc_app_shortcuts_request.h" #include "chrome/browser/chromeos/arc/app_shortcuts/arc_app_shortcuts_request.h"
#include <string>
#include <utility> #include <utility>
#include "base/barrier_closure.h" #include "base/barrier_closure.h"
...@@ -61,6 +62,8 @@ void ArcAppShortcutsRequest::OnGetAppShortcutItems( ...@@ -61,6 +62,8 @@ void ArcAppShortcutsRequest::OnGetAppShortcutItems(
ArcAppShortcutItem item; ArcAppShortcutItem item;
item.shortcut_id = shortcut_item_ptr->shortcut_id; item.shortcut_id = shortcut_item_ptr->shortcut_id;
item.short_label = base::UTF8ToUTF16(shortcut_item_ptr->short_label); item.short_label = base::UTF8ToUTF16(shortcut_item_ptr->short_label);
item.type = shortcut_item_ptr->type;
item.rank = shortcut_item_ptr->rank;
items_->emplace_back(std::move(item)); items_->emplace_back(std::move(item));
icon_decode_requests_.emplace_back(std::make_unique<IconDecodeRequest>( icon_decode_requests_.emplace_back(std::make_unique<IconDecodeRequest>(
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
// Next MinVersion: 32 // Next MinVersion: 33
module arc.mojom; module arc.mojom;
...@@ -156,6 +156,18 @@ enum AppDataRequestState { ...@@ -156,6 +156,18 @@ enum AppDataRequestState {
FAILED_TO_CALL_GLOBALQUERY = 8, FAILED_TO_CALL_GLOBALQUERY = 8,
}; };
// Describes the category type of app shortcut item.
[Extensible]
enum AppShortcutItemType {
// Static shortcut, which means it was published from AndroidManifest.xml.
kStatic = 0,
// Dynamic shortcut, which means it was published at runtime using the
// ShortcutManagerAPI. Dynamic shortcuts are links to specific,
// context-sensitive actions within the app.
kDynamic = 1,
};
// Describes app shortcut that is published by Android's ShortcutManager. // Describes app shortcut that is published by Android's ShortcutManager.
struct AppShortcutItem { struct AppShortcutItem {
// The ID of this shortcut. Unique within each publisher app and stable across // The ID of this shortcut. Unique within each publisher app and stable across
...@@ -170,6 +182,12 @@ struct AppShortcutItem { ...@@ -170,6 +182,12 @@ struct AppShortcutItem {
// The package name of the publisher app. // The package name of the publisher app.
[MinVersion=31] string? package_name; [MinVersion=31] string? package_name;
// The category type of this shortcut.
[MinVersion=32] AppShortcutItemType type;
// "Rank" of a shortcut, which is a non-negative, sequential value.
[MinVersion=32] int32 rank;
}; };
// Next method ID: 18 // Next method ID: 18
......
...@@ -404,7 +404,8 @@ void FakeAppInstance::GetAppShortcutItems( ...@@ -404,7 +404,8 @@ void FakeAppInstance::GetAppShortcutItems(
fake_app_shortcut_items.push_back(mojom::AppShortcutItem::New( fake_app_shortcut_items.push_back(mojom::AppShortcutItem::New(
base::StringPrintf("ShortcutId %d", i), base::StringPrintf("ShortcutId %d", i),
base::StringPrintf("ShortLabel %d", i), fake_icon_png_data, base::StringPrintf("ShortLabel %d", i), fake_icon_png_data,
package_name.empty() ? "FakeAppPackageName" : package_name)); package_name.empty() ? "FakeAppPackageName" : package_name,
mojom::AppShortcutItemType::kStatic, i));
} }
std::move(callback).Run(std::move(fake_app_shortcut_items)); std::move(callback).Run(std::move(fake_app_shortcut_items));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment