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

ash / wallpaper / wallpaper_utils / wallpaper_online_variant_utils.cc [blame]

// Copyright 2023 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/wallpaper/wallpaper_utils/wallpaper_online_variant_utils.h"

#include "ash/public/cpp/wallpaper/online_wallpaper_variant.h"
#include "ash/wallpaper/wallpaper_constants.h"
#include "base/ranges/algorithm.h"

namespace ash {

namespace {

// Not all wallpapers have variants that map 1:1 to the checkpoints. D/L
// wallpapers are an example. In order to gracefully support these wallpapers,
// this method accepts a boolean |match_subtype| so that if true, variants with
// |backdrop::Image_ImageType_IMAGE_TYPE_LIGHT_MODE| is also considered valid
// for |ScheduleCheckpoint::kMorning| and |ScheduleCheckpoint::kLateAfternoon|.
bool IsSuitableOnlineWallpaperVariantInternal(
    const OnlineWallpaperVariant& variant,
    ScheduleCheckpoint checkpoint,
    bool match_subtype) {
  if (variant.type == backdrop::Image_ImageType_IMAGE_TYPE_UNKNOWN) {
    return true;
  }
  switch (checkpoint) {
    case ScheduleCheckpoint::kSunrise:
      return variant.type == backdrop::Image_ImageType_IMAGE_TYPE_LIGHT_MODE;
    // `kDisabled` is equivalent to Light mode.
    case ScheduleCheckpoint::kDisabled:
    case ScheduleCheckpoint::kMorning:
      return variant.type ==
                 backdrop::Image_ImageType_IMAGE_TYPE_MORNING_MODE ||
             (match_subtype &&
              variant.type == backdrop::Image_ImageType_IMAGE_TYPE_LIGHT_MODE);
    case ScheduleCheckpoint::kLateAfternoon:
      return variant.type ==
                 backdrop::Image_ImageType_IMAGE_TYPE_LATE_AFTERNOON_MODE ||
             (match_subtype &&
              variant.type == backdrop::Image_ImageType_IMAGE_TYPE_LIGHT_MODE);
    case ScheduleCheckpoint::kSunset:
    // `kEnabled` is equivalent to Dark mode.
    case ScheduleCheckpoint::kEnabled:
      return variant.type == backdrop::Image_ImageType_IMAGE_TYPE_DARK_MODE;
  }
}

}  // namespace

bool IsSuitableOnlineWallpaperVariant(const OnlineWallpaperVariant& variant,
                                      ScheduleCheckpoint checkpoint) {
  return IsSuitableOnlineWallpaperVariantInternal(variant, checkpoint,
                                                  /*match_subtype=*/true);
}

const OnlineWallpaperVariant* FirstValidVariant(
    const std::vector<OnlineWallpaperVariant>& variants,
    ScheduleCheckpoint checkpoint) {
  // Attempt to find the exact 1:1 match for |variant| and |checkpoint|.
  auto iter =
      base::ranges::find_if(variants, [checkpoint](const auto& variant) {
        return IsSuitableOnlineWallpaperVariantInternal(
            variant, checkpoint,
            /*match_subtype=*/false);
      });
  if (iter != variants.end()) {
    return &(*iter);
  }
  // Attempt to find a subtype |variant| for |checkpoint|.
  iter = base::ranges::find_if(variants, [checkpoint](const auto& variant) {
    return IsSuitableOnlineWallpaperVariantInternal(variant, checkpoint,
                                                    /*match_subtype=*/true);
  });
  if (iter != variants.end()) {
    return &(*iter);
  }
  return nullptr;
}

bool IsTimeOfDayWallpaper(const std::string& collection_id) {
  return collection_id == wallpaper_constants::kTimeOfDayWallpaperCollectionId;
}

}  // namespace ash