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
|
// 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 "chrome/browser/ash/app_list/search/files/justifications.h"
#include "ash/constants/ash_features.h"
#include "ash/strings/grit/ash_strings.h"
#include "base/i18n/time_formatting.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/browser/ash/file_suggest/file_suggest_util.h"
#include "ui/base/l10n/l10n_util.h"
namespace app_list {
namespace {
// Time limits for how last accessed or modified time maps to each justification
// string.
constexpr base::TimeDelta kJustNow = base::Minutes(15);
constexpr base::TimeDelta kToday = base::Days(1);
constexpr base::TimeDelta kYesterday = base::Days(2);
constexpr base::TimeDelta kPastWeek = base::Days(7);
constexpr base::TimeDelta kPastMonth = base::Days(31);
std::u16string GetTimeString(const base::Time& timestamp) {
const base::Time now = base::Time::Now();
const base::Time midnight = now.LocalMidnight();
if ((now - timestamp).magnitude() <= kJustNow) {
return l10n_util::GetStringUTF16(
IDS_FILE_SUGGESTION_JUSTIFICATION_TIME_NOW);
}
if (timestamp >= midnight && timestamp < midnight + base::Days(1)) {
return base::TimeFormatTimeOfDay(timestamp);
}
return base::LocalizedTimeFormatWithPattern(timestamp, "MMMd");
}
std::optional<std::u16string> GetEditStringFromTime(const base::Time& time) {
const auto& delta = base::Time::Now() - time;
if (delta <= kJustNow) {
return l10n_util::GetStringUTF16(IDS_APP_LIST_CONTINUE_EDITED_JUST_NOW);
} else if (delta <= kToday) {
return l10n_util::GetStringUTF16(IDS_APP_LIST_CONTINUE_EDITED_TODAY);
} else if (delta <= kYesterday) {
return l10n_util::GetStringUTF16(IDS_APP_LIST_CONTINUE_EDITED_YESTERDAY);
} else if (delta <= kPastWeek) {
return l10n_util::GetStringUTF16(IDS_APP_LIST_CONTINUE_EDITED_PAST_WEEK);
} else if (delta <= kPastMonth) {
return l10n_util::GetStringUTF16(IDS_APP_LIST_CONTINUE_EDITED_PAST_MONTH);
} else {
return std::nullopt;
}
}
std::optional<std::u16string> GetOpenStringFromTime(const base::Time& time) {
const auto& delta = base::Time::Now() - time;
if (delta <= kJustNow) {
return l10n_util::GetStringUTF16(IDS_APP_LIST_CONTINUE_OPENED_JUST_NOW);
} else if (delta <= kToday) {
return l10n_util::GetStringUTF16(IDS_APP_LIST_CONTINUE_OPENED_TODAY);
} else if (delta <= kYesterday) {
return l10n_util::GetStringUTF16(IDS_APP_LIST_CONTINUE_OPENED_YESTERDAY);
} else if (delta <= kPastWeek) {
return l10n_util::GetStringUTF16(IDS_APP_LIST_CONTINUE_OPENED_PAST_WEEK);
} else if (delta <= kPastMonth) {
return l10n_util::GetStringUTF16(IDS_APP_LIST_CONTINUE_OPENED_PAST_MONTH);
} else {
return std::nullopt;
}
}
std::u16string GetActionString(ash::FileSuggestionJustificationType type,
const std::string& user_name) {
switch (type) {
case ash::FileSuggestionJustificationType::kViewed: {
return l10n_util::GetStringUTF16(
IDS_FILE_SUGGESTION_JUSTIFICATION_YOU_VIEWED_ACTION);
}
case ash::FileSuggestionJustificationType::kModified: {
if (user_name.empty()) {
return l10n_util::GetStringUTF16(
IDS_FILE_SUGGESTION_JUSTIFICATION_GENERIC_MODIFIED_ACTION);
}
return l10n_util::GetStringFUTF16(
IDS_FILE_SUGGESTION_JUSTIFICATION_USER_MODIFIED_ACTION,
base::UTF8ToUTF16(user_name));
}
case ash::FileSuggestionJustificationType::kModifiedByCurrentUser: {
return l10n_util::GetStringUTF16(
IDS_FILE_SUGGESTION_JUSTIFICATION_YOU_MODIFIED_ACTION);
}
case ash::FileSuggestionJustificationType::kShared: {
if (user_name.empty()) {
return l10n_util::GetStringUTF16(
IDS_FILE_SUGGESTION_JUSTIFICATION_GENERIC_SHARED_ACTION);
}
return l10n_util::GetStringFUTF16(
IDS_FILE_SUGGESTION_JUSTIFICATION_USER_SHARED_ACTION,
base::UTF8ToUTF16(user_name));
}
case ash::FileSuggestionJustificationType::kUnknown: {
return u"";
}
}
}
} // namespace
std::optional<std::u16string> GetJustificationString(
ash::FileSuggestionJustificationType type,
const base::Time& timestamp,
const std::string& user_name) {
if (ash::features::IsLauncherContinueSectionWithRecentsEnabled()) {
return l10n_util::GetStringFUTF16(IDS_FILE_SUGGESTION_JUSTIFICATION,
GetActionString(type, user_name),
GetTimeString(timestamp));
}
switch (type) {
case ash::FileSuggestionJustificationType::kViewed:
return GetOpenStringFromTime(timestamp);
case ash::FileSuggestionJustificationType::kModified:
case ash::FileSuggestionJustificationType::kModifiedByCurrentUser:
return GetEditStringFromTime(timestamp);
case ash::FileSuggestionJustificationType::kShared:
case ash::FileSuggestionJustificationType::kUnknown:
return std::nullopt;
}
}
} // namespace app_list
|