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

ash / public / cpp / ambient / ambient_ui_model.cc [blame]

// Copyright 2020 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/public/cpp/ambient/ambient_ui_model.h"

#include "base/check_op.h"

namespace ash {

namespace {

AmbientUiModel* g_ambient_ui_model = nullptr;

constexpr AmbientJitterConfig kSlideshowPeripheralUiJitterConfig{
    .step_size = 5,
    .x_min_translation = 0,
    .x_max_translation = 20,
    .y_min_translation = -20,
    .y_max_translation = 0};

constexpr AmbientJitterConfig kAnimationJitterConfig{.step_size = 2,
                                                     .x_min_translation = -10,
                                                     .x_max_translation = 10,
                                                     .y_min_translation = -10,
                                                     .y_max_translation = 10};

}  // namespace

// static
// TODO(b/158039112): return a const* instead.
AmbientUiModel* AmbientUiModel::Get() {
  return g_ambient_ui_model;
}

AmbientUiModel::AmbientUiModel() {
  DCHECK(!g_ambient_ui_model);
  g_ambient_ui_model = this;
}

AmbientUiModel::~AmbientUiModel() {
  DCHECK_EQ(g_ambient_ui_model, this);
  g_ambient_ui_model = nullptr;
}

void AmbientUiModel::AddObserver(AmbientUiModelObserver* observer) {
  observers_.AddObserver(observer);
}

void AmbientUiModel::RemoveObserver(AmbientUiModelObserver* observer) {
  observers_.RemoveObserver(observer);
}

void AmbientUiModel::SetUiVisibility(AmbientUiVisibility visibility) {
  if (ui_visibility_ == visibility)
    return;

  ui_visibility_ = visibility;
  NotifyAmbientUiVisibilityChanged();
}

void AmbientUiModel::SetLockScreenInactivityTimeout(base::TimeDelta timeout) {
  if (timeout == lock_screen_inactivity_timeout_)
    return;

  lock_screen_inactivity_timeout_ = timeout;
  NotifyLockScreenInactivityTimeoutChanged();
}

void AmbientUiModel::SetBackgroundLockScreenTimeout(base::TimeDelta timeout) {
  if (timeout == background_lock_screen_timeout_)
    return;

  background_lock_screen_timeout_ = timeout;
  NotifyBackgroundLockScreenTimeoutChanged();
}

void AmbientUiModel::SetPhotoRefreshInterval(base::TimeDelta interval) {
  if (interval == photo_refresh_interval_)
    return;

  photo_refresh_interval_ = interval;
}

AmbientJitterConfig AmbientUiModel::GetSlideshowPeripheralUiJitterConfig() {
  return jitter_config_for_testing_.value_or(
      kSlideshowPeripheralUiJitterConfig);
}

AmbientJitterConfig AmbientUiModel::GetAnimationJitterConfig() {
  return jitter_config_for_testing_.value_or(kAnimationJitterConfig);
}

void AmbientUiModel::NotifyAmbientUiVisibilityChanged() {
  for (auto& observer : observers_)
    observer.OnAmbientUiVisibilityChanged(ui_visibility_);
}

void AmbientUiModel::NotifyLockScreenInactivityTimeoutChanged() {
  for (auto& observer : observers_) {
    observer.OnLockScreenInactivityTimeoutChanged(
        lock_screen_inactivity_timeout_);
  }
}

void AmbientUiModel::NotifyBackgroundLockScreenTimeoutChanged() {
  for (auto& observer : observers_) {
    observer.OnBackgroundLockScreenTimeoutChanged(
        background_lock_screen_timeout_);
  }
}

std::ostream& operator<<(std::ostream& out, AmbientUiMode mode) {
  switch (mode) {
    case AmbientUiMode::kLockScreenUi:
      out << "kLockScreenUi";
      break;
    case AmbientUiMode::kInSessionUi:
      out << "kInSessionUi";
      break;
  }
  return out;
}

std::ostream& operator<<(std::ostream& out, AmbientUiVisibility visibility) {
  switch (visibility) {
    case AmbientUiVisibility::kShouldShow:
      out << "kShown";
      break;
    case AmbientUiVisibility::kPreview:
      out << "kPreview";
      break;
    case AmbientUiVisibility::kHidden:
      out << "kHidden";
      break;
    case AmbientUiVisibility::kClosed:
      out << "kClosed";
      break;
  }
  return out;
}

}  // namespace ash