1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/camera/autozoom_nudge_controller.h"
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/notifier_catalogs.h"
#include "ash/public/cpp/system/anchored_nudge_data.h"
#include "ash/public/cpp/system/anchored_nudge_manager.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/camera/autozoom_controller_impl.h"
#include "base/json/values_util.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "ui/base/l10n/l10n_util.h"
namespace ash {
namespace {
// Keys for autozoom nudge sub-preferences for shown count and last time shown.
constexpr char kShownCount[] = "shown_count";
constexpr char kLastTimeShown[] = "last_time_shown";
constexpr char kHadEnabled[] = "had_enabled";
constexpr char kAutozoomNudgeId[] = "AutozoomNudge";
constexpr int kNotificationLimit = 3;
constexpr base::TimeDelta kMinInterval = base::Days(1);
} // namespace
AutozoomNudgeController::AutozoomNudgeController(
AutozoomControllerImpl* autozoom_controller)
: autozoom_controller_(autozoom_controller) {
autozoom_controller_->AddObserver(this);
if (base::FeatureList::IsEnabled(features::kAutozoomNudgeSessionReset))
Shell::Get()->session_controller()->AddObserver(this);
}
AutozoomNudgeController::~AutozoomNudgeController() {
autozoom_controller_->RemoveObserver(this);
if (base::FeatureList::IsEnabled(features::kAutozoomNudgeSessionReset))
Shell::Get()->session_controller()->RemoveObserver(this);
}
// static
void AutozoomNudgeController::RegisterProfilePrefs(
PrefRegistrySimple* registry) {
registry->RegisterDictionaryPref(prefs::kAutozoomNudges);
}
void AutozoomNudgeController::OnActiveUserPrefServiceChanged(
PrefService* prefs) {
// Reset the nudge prefs so that the nudge can be shown again. This observer
// callback is only registered and called when
// features::kAutozoomNudgeSessionReset is enabled.
ScopedDictPrefUpdate update(prefs, prefs::kAutozoomNudges);
update->Set(kShownCount, 0);
update->Set(kLastTimeShown, base::TimeToValue(base::Time()));
update->Set(kHadEnabled, false);
}
base::Time AutozoomNudgeController::GetTime() {
return base::Time::Now();
}
void AutozoomNudgeController::HandleNudgeShown() {
PrefService* prefs =
Shell::Get()->session_controller()->GetLastActiveUserPrefService();
if (!prefs)
return;
const int shown_count = GetShownCount(prefs);
ScopedDictPrefUpdate update(prefs, prefs::kAutozoomNudges);
update->Set(kShownCount, shown_count + 1);
update->Set(kLastTimeShown, base::TimeToValue(GetTime()));
}
bool AutozoomNudgeController::GetHadEnabled(PrefService* prefs) {
const base::Value::Dict& dictionary = prefs->GetDict(prefs::kAutozoomNudges);
return dictionary.FindBool(kHadEnabled).value_or(false);
}
int AutozoomNudgeController::GetShownCount(PrefService* prefs) {
const base::Value::Dict& dictionary = prefs->GetDict(prefs::kAutozoomNudges);
return dictionary.FindInt(kShownCount).value_or(0);
}
base::Time AutozoomNudgeController::GetLastShownTime(PrefService* prefs) {
const base::Value::Dict& dictionary = prefs->GetDict(prefs::kAutozoomNudges);
std::optional<base::Time> last_shown_time =
base::ValueToTime(dictionary.Find(kLastTimeShown));
return last_shown_time.value_or(base::Time());
}
bool AutozoomNudgeController::ShouldShowNudge(PrefService* prefs) {
if (!prefs)
return false;
bool had_enabled = GetHadEnabled(prefs);
// We should not show more nudge after user had enabled autozoom before.
if (had_enabled)
return false;
int nudge_shown_count = GetShownCount(prefs);
// We should not show more nudges after hitting the limit.
if (nudge_shown_count >= kNotificationLimit)
return false;
base::Time last_shown_time = GetLastShownTime(prefs);
// If the nudge has yet to be shown, we should return true.
if (last_shown_time.is_null())
return true;
// We should show the nudge if enough time has passed since the nudge was last
// shown.
return (base::Time::Now() - last_shown_time) > kMinInterval;
}
void AutozoomNudgeController::OnAutozoomControlEnabledChanged(bool enabled) {
if (!enabled)
return;
PrefService* prefs =
Shell::Get()->session_controller()->GetLastActiveUserPrefService();
if (!ShouldShowNudge(prefs)) {
return;
}
AnchoredNudgeData nudge_data(
kAutozoomNudgeId, NudgeCatalogName::kAutozoom,
l10n_util::GetStringUTF16(
IDS_ASH_STATUS_TRAY_AUTOZOOM_EDUCATIONAL_NUDGE_TEXT));
AnchoredNudgeManager::Get()->Show(nudge_data);
HandleNudgeShown();
}
void AutozoomNudgeController::OnAutozoomStateChanged(
cros::mojom::CameraAutoFramingState state) {
if (state != cros::mojom::CameraAutoFramingState::OFF) {
PrefService* prefs =
Shell::Get()->session_controller()->GetLastActiveUserPrefService();
if (!prefs)
return;
ScopedDictPrefUpdate update(prefs, prefs::kAutozoomNudges);
update->Set(kHadEnabled, true);
}
}
} // namespace ash
|