Commit d5743f21 authored by Kyle Horimoto's avatar Kyle Horimoto Committed by Commit Bot

[CrOS PhoneHub] Add Notification class

This class includes metadata associated with a notification which
appears on the user's phone and has been synced to the Chrome OS device.

Bug: 1106937
Change-Id: I2591ca19b8752346390c649d85f2c31978f1f37d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2378881Reviewed-by: default avatarRegan Hsu <hsuregan@chromium.org>
Commit-Queue: Kyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#802069}
parent 3f68381f
...@@ -18,6 +18,8 @@ static_library("phonehub") { ...@@ -18,6 +18,8 @@ static_library("phonehub") {
"feature_status_provider_impl.h", "feature_status_provider_impl.h",
"mutable_phone_model.cc", "mutable_phone_model.cc",
"mutable_phone_model.h", "mutable_phone_model.h",
"notification.cc",
"notification.h",
"notification_access_manager.cc", "notification_access_manager.cc",
"notification_access_manager.h", "notification_access_manager.h",
"notification_access_manager_impl.cc", "notification_access_manager_impl.cc",
......
// Copyright 2020 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 "chromeos/components/phonehub/notification.h"
#include <tuple>
namespace chromeos {
namespace phonehub {
Notification::AppMetadata::AppMetadata(const base::string16& visible_app_name,
const std::string& package_name,
const gfx::Image& icon)
: visible_app_name(visible_app_name),
package_name(package_name),
icon(icon) {}
Notification::AppMetadata::AppMetadata(const AppMetadata& other) = default;
bool Notification::AppMetadata::operator==(const AppMetadata& other) const {
return visible_app_name == other.visible_app_name &&
package_name == other.package_name && icon == other.icon;
}
bool Notification::AppMetadata::operator!=(const AppMetadata& other) const {
return !(*this == other);
}
Notification::Notification(int64_t id,
const AppMetadata& app_metadata,
const base::Time& timestamp,
Importance importance,
int64_t inline_reply_id,
const base::Optional<base::string16>& title,
const base::Optional<base::string16>& text_content,
const base::Optional<gfx::Image>& shared_image,
const base::Optional<gfx::Image>& contact_image)
: id_(id),
app_metadata_(app_metadata),
timestamp_(timestamp),
importance_(importance),
inline_reply_id_(inline_reply_id),
title_(title),
text_content_(text_content),
shared_image_(shared_image),
contact_image_(contact_image) {}
Notification::Notification(const Notification& other) = default;
Notification::~Notification() = default;
bool Notification::operator<(const Notification& other) const {
return std::tie(timestamp_, id_) < std::tie(other.timestamp_, other.id_);
}
bool Notification::operator==(const Notification& other) const {
return id_ == other.id_ && app_metadata_ == other.app_metadata_ &&
timestamp_ == other.timestamp_ && importance_ == other.importance_ &&
inline_reply_id_ == other.inline_reply_id_ && title_ == other.title_ &&
text_content_ == other.text_content_ &&
shared_image_ == other.shared_image_ &&
contact_image_ == other.contact_image_;
}
bool Notification::operator!=(const Notification& other) const {
return !(*this == other);
}
std::ostream& operator<<(std::ostream& stream,
const Notification::AppMetadata& app_metadata) {
stream << "{VisibleAppName: \"" << app_metadata.visible_app_name << "\", "
<< "PackageName: \"" << app_metadata.package_name << "\"}";
return stream;
}
std::ostream& operator<<(std::ostream& stream,
Notification::Importance importance) {
switch (importance) {
case Notification::Importance::kUnspecified:
stream << "[Unspecified]";
break;
case Notification::Importance::kNone:
stream << "[None]";
break;
case Notification::Importance::kMin:
stream << "[Min]";
break;
case Notification::Importance::kLow:
stream << "[Low]";
break;
case Notification::Importance::kDefault:
stream << "[Default]";
break;
case Notification::Importance::kHigh:
stream << "[High]";
break;
}
return stream;
}
std::ostream& operator<<(std::ostream& stream,
const Notification& notification) {
stream << "{Id: " << notification.id() << ", "
<< "App: " << notification.app_metadata() << ", "
<< "Timestamp: " << notification.timestamp() << ", "
<< "Importance: " << notification.importance() << "}";
return stream;
}
} // namespace phonehub
} // namespace chromeos
// Copyright 2020 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.
#ifndef CHROMEOS_COMPONENTS_PHONEHUB_NOTIFICATION_H_
#define CHROMEOS_COMPONENTS_PHONEHUB_NOTIFICATION_H_
#include <stdint.h>
#include <ostream>
#include <string>
#include "base/optional.h"
#include "base/strings/string16.h"
#include "base/time/time.h"
#include "ui/gfx/image/image.h"
namespace chromeos {
namespace phonehub {
// A notification generated on the phone, whose contents are transferred to
// Chrome OS via a Phone Hub connection. Notifications in Phone Hub support
// inline reply and images.
class Notification {
public:
// Describes the app which generates a notification.
struct AppMetadata {
AppMetadata(const base::string16& visible_app_name,
const std::string& package_name,
const gfx::Image& icon);
AppMetadata(const AppMetadata& other);
bool operator==(const AppMetadata& other) const;
bool operator!=(const AppMetadata& other) const;
base::string16 visible_app_name;
std::string package_name;
gfx::Image icon;
};
// Notification importance; for more details, see
// https://developer.android.com/reference/android/app/NotificationManager.
enum class Importance {
// Older versions of Android do not specify an importance level.
kUnspecified,
// Does not show in the Android notification shade.
kNone,
// Shows in the Android notification shade, below the fold.
kMin,
// Shows in the Android notification shade and potentially status bar, but
// is not audibly intrusive.
kLow,
// Shows in the Android notification shade and status bar and makes noise,
// but does not visually intrude.
kDefault,
// Shows in the Android notification shade and status bar, makes noise, and
// "peeks" down onto the screen when received.
kHigh
};
// Note: A notification should include at least one of |title|,
// |text_content|, and |shared_image| so that it can be rendered in the UI.
Notification(
int64_t id,
const AppMetadata& app_metadata,
const base::Time& timestamp,
Importance importance,
int64_t inline_reply_id,
const base::Optional<base::string16>& title = base::nullopt,
const base::Optional<base::string16>& text_content = base::nullopt,
const base::Optional<gfx::Image>& shared_image = base::nullopt,
const base::Optional<gfx::Image>& contact_image = base::nullopt);
Notification(const Notification& other);
~Notification();
bool operator<(const Notification& other) const;
bool operator==(const Notification& other) const;
bool operator!=(const Notification& other) const;
int64_t id() const { return id_; }
const AppMetadata& app_metadata() const { return app_metadata_; }
base::Time timestamp() const { return timestamp_; }
Importance importance() const { return importance_; }
int64_t inline_reply_id() const { return inline_reply_id_; }
const base::Optional<base::string16>& title() const { return title_; }
const base::Optional<base::string16>& text_content() const {
return text_content_;
}
const base::Optional<gfx::Image>& shared_image() const {
return shared_image_;
}
const base::Optional<gfx::Image>& contact_image() const {
return contact_image_;
}
private:
int64_t id_;
AppMetadata app_metadata_;
base::Time timestamp_;
Importance importance_;
int64_t inline_reply_id_;
base::Optional<base::string16> title_;
base::Optional<base::string16> text_content_;
base::Optional<gfx::Image> shared_image_;
base::Optional<gfx::Image> contact_image_;
};
std::ostream& operator<<(std::ostream& stream,
const Notification::AppMetadata& app_metadata);
std::ostream& operator<<(std::ostream& stream,
Notification::Importance importance);
std::ostream& operator<<(std::ostream& stream,
const Notification& notification);
} // namespace phonehub
} // namespace chromeos
#endif // CHROMEOS_COMPONENTS_PHONEHUB_NOTIFICATION_H_
...@@ -12,12 +12,6 @@ namespace phonehub { ...@@ -12,12 +12,6 @@ namespace phonehub {
const char kFakeMobileProviderName[] = "Fake Mobile Provider"; const char kFakeMobileProviderName[] = "Fake Mobile Provider";
const char kFakeBrowserTabUrl1[] = "https://www.example.com/tab1";
const char kFakeBrowserTabName1[] = "Tab 1";
const char kFakeBrowserTabUrl2[] = "https://www.example.com/tab2";
const char kFakeBrowserTabName2[] = "Tab 2";
const PhoneStatusModel::MobileConnectionMetadata& const PhoneStatusModel::MobileConnectionMetadata&
CreateFakeMobileConnectionMetadata() { CreateFakeMobileConnectionMetadata() {
static const base::NoDestructor<PhoneStatusModel::MobileConnectionMetadata> static const base::NoDestructor<PhoneStatusModel::MobileConnectionMetadata>
...@@ -37,6 +31,11 @@ const PhoneStatusModel& CreateFakePhoneStatusModel() { ...@@ -37,6 +31,11 @@ const PhoneStatusModel& CreateFakePhoneStatusModel() {
return *fake_phone_status_model; return *fake_phone_status_model;
} }
const char kFakeBrowserTabUrl1[] = "https://www.example.com/tab1";
const char kFakeBrowserTabName1[] = "Tab 1";
const char kFakeBrowserTabUrl2[] = "https://www.example.com/tab2";
const char kFakeBrowserTabName2[] = "Tab 2";
const BrowserTabsModel::BrowserTabMetadata& CreateFakeBrowserTabMetadata() { const BrowserTabsModel::BrowserTabMetadata& CreateFakeBrowserTabMetadata() {
static const base::NoDestructor<BrowserTabsModel::BrowserTabMetadata> static const base::NoDestructor<BrowserTabsModel::BrowserTabMetadata>
fake_browser_tab_metadata{GURL(kFakeBrowserTabUrl1), fake_browser_tab_metadata{GURL(kFakeBrowserTabUrl1),
...@@ -58,5 +57,31 @@ const BrowserTabsModel& CreateFakeBrowserTabsModel() { ...@@ -58,5 +57,31 @@ const BrowserTabsModel& CreateFakeBrowserTabsModel() {
return *fake_browser_tabs_model; return *fake_browser_tabs_model;
} }
const char kFakeAppVisibleName[] = "Fake App";
const char kFakeAppPackageName[] = "com.fakeapp";
const int64_t kFakeAppId = 1234567890;
const int64_t kFakeInlineReplyId = 1337;
const char kFakeNotificationTitle[] = "Fake Title";
const char kFakeNotificationText[] = "Fake Text";
const Notification::AppMetadata& CreateFakeAppMetadata() {
static const base::NoDestructor<Notification::AppMetadata> fake_app_metadata{
base::UTF8ToUTF16(kFakeAppVisibleName), kFakeAppPackageName,
gfx::Image()};
return *fake_app_metadata;
}
const Notification& CreateFakeNotification() {
static const base::NoDestructor<Notification> fake_notification{
kFakeAppId,
CreateFakeAppMetadata(),
base::Time(),
Notification::Importance::kDefault,
kFakeInlineReplyId,
base::UTF8ToUTF16(kFakeNotificationTitle),
base::UTF8ToUTF16(kFakeNotificationText)};
return *fake_notification;
}
} // namespace phonehub } // namespace phonehub
} // namespace chromeos } // namespace chromeos
...@@ -5,29 +5,45 @@ ...@@ -5,29 +5,45 @@
#ifndef CHROMEOS_COMPONENTS_PHONEHUB_PHONE_MODEL_TEST_UTIL_H_ #ifndef CHROMEOS_COMPONENTS_PHONEHUB_PHONE_MODEL_TEST_UTIL_H_
#define CHROMEOS_COMPONENTS_PHONEHUB_PHONE_MODEL_TEST_UTIL_H_ #define CHROMEOS_COMPONENTS_PHONEHUB_PHONE_MODEL_TEST_UTIL_H_
#include <stdint.h>
#include "chromeos/components/phonehub/browser_tabs_model.h" #include "chromeos/components/phonehub/browser_tabs_model.h"
#include "chromeos/components/phonehub/notification.h"
#include "chromeos/components/phonehub/phone_status_model.h" #include "chromeos/components/phonehub/phone_status_model.h"
namespace chromeos { namespace chromeos {
namespace phonehub { namespace phonehub {
// Fake data for phone status.
extern const char kFakeMobileProviderName[]; extern const char kFakeMobileProviderName[];
extern const char kFakeBrowserTabUrl1[];
extern const char kFakeBrowserTabName1[];
extern const char kFakeBrowserTabUrl2[];
extern const char kFakeBrowserTabName2[];
// Creates fake phone status data for use in tests. // Creates fake phone status data for use in tests.
const PhoneStatusModel::MobileConnectionMetadata& const PhoneStatusModel::MobileConnectionMetadata&
CreateFakeMobileConnectionMetadata(); CreateFakeMobileConnectionMetadata();
const PhoneStatusModel& CreateFakePhoneStatusModel(); const PhoneStatusModel& CreateFakePhoneStatusModel();
// Fake data for browser tabs.
extern const char kFakeBrowserTabUrl1[];
extern const char kFakeBrowserTabName1[];
extern const char kFakeBrowserTabUrl2[];
extern const char kFakeBrowserTabName2[];
// Creates fake browser tab data for use in tests. // Creates fake browser tab data for use in tests.
const BrowserTabsModel::BrowserTabMetadata& CreateFakeBrowserTabMetadata(); const BrowserTabsModel::BrowserTabMetadata& CreateFakeBrowserTabMetadata();
const BrowserTabsModel& CreateFakeBrowserTabsModel(); const BrowserTabsModel& CreateFakeBrowserTabsModel();
// Fake data for notifications.
extern const char kFakeAppVisibleName[];
extern const char kFakeAppPackageName[];
extern const int64_t kFakeAppId;
extern const int64_t kFakeInlineReplyId;
extern const char kFakeNotificationTitle[];
extern const char kFakeNotificationText[];
// Creates fake notification data for use in tests.
const Notification::AppMetadata& CreateFakeAppMetadata();
const Notification& CreateFakeNotification();
} // namespace phonehub } // namespace phonehub
} // namespace chromeos } // namespace chromeos
......
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