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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
// 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 "components/webapps/browser/android/webapps_icon_utils.h"
#include "base/android/build_info.h"
#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "components/webapps/browser/android/webapps_jni_headers/WebappsIconUtils_jni.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/android/java_bitmap.h"
#include "ui/gfx/color_analysis.h"
#include "url/android/gurl_android.h"
#include "url/gurl.h"
using base::android::JavaParamRef;
using base::android::ScopedJavaLocalRef;
namespace webapps {
namespace {
int g_ideal_homescreen_icon_size = -1;
int g_minimum_homescreen_icon_size = -1;
int g_ideal_splash_image_size = -1;
int g_minimum_splash_image_size = -1;
int g_ideal_monochrome_icon_size = -1;
int g_ideal_adaptive_launcher_icon_size = -1;
int g_ideal_shortcut_icon_size = -1;
// Retrieves and caches the ideal and minimum sizes of the Home screen icon,
// the splash screen image, and the shortcut icons.
void GetIconSizes() {
JNIEnv* env = base::android::AttachCurrentThread();
ScopedJavaLocalRef<jintArray> java_size_array =
Java_WebappsIconUtils_getIconSizes(env);
std::vector<int> sizes;
base::android::JavaIntArrayToIntVector(env, java_size_array, &sizes);
// Check that the size returned is what is expected.
DCHECK_EQ(7u, sizes.size());
// This ordering must be kept up to date with the Java WebappsIconUtils.
g_ideal_homescreen_icon_size = sizes[0];
g_minimum_homescreen_icon_size = sizes[1];
g_ideal_splash_image_size = sizes[2];
g_minimum_splash_image_size = sizes[3];
g_ideal_monochrome_icon_size = sizes[4];
g_ideal_adaptive_launcher_icon_size = sizes[5];
g_ideal_shortcut_icon_size = sizes[6];
// Try to ensure that the data returned is sensible.
DCHECK_LE(g_minimum_homescreen_icon_size, g_ideal_homescreen_icon_size);
DCHECK_LE(g_minimum_splash_image_size, g_ideal_splash_image_size);
}
} // anonymous namespace
int WebappsIconUtils::GetIdealHomescreenIconSizeInPx() {
if (g_ideal_homescreen_icon_size == -1)
GetIconSizes();
return g_ideal_homescreen_icon_size;
}
int WebappsIconUtils::GetMinimumHomescreenIconSizeInPx() {
if (g_minimum_homescreen_icon_size == -1)
GetIconSizes();
return g_minimum_homescreen_icon_size;
}
int WebappsIconUtils::GetIdealSplashImageSizeInPx() {
if (g_ideal_splash_image_size == -1)
GetIconSizes();
return g_ideal_splash_image_size;
}
int WebappsIconUtils::GetMinimumSplashImageSizeInPx() {
if (g_minimum_splash_image_size == -1)
GetIconSizes();
return g_minimum_splash_image_size;
}
int WebappsIconUtils::GetIdealAdaptiveLauncherIconSizeInPx() {
if (g_ideal_adaptive_launcher_icon_size == -1)
GetIconSizes();
return g_ideal_adaptive_launcher_icon_size;
}
int WebappsIconUtils::GetIdealShortcutIconSizeInPx() {
if (g_ideal_shortcut_icon_size == -1)
GetIconSizes();
return g_ideal_shortcut_icon_size;
}
int WebappsIconUtils::GetIdealIconSizeForIconType(
webapk::Image::Usage usage,
webapk::Image::Purpose purpose) {
switch (usage) {
case webapk::Image::PRIMARY_ICON:
if (purpose == webapk::Image::MASKABLE) {
return GetIdealAdaptiveLauncherIconSizeInPx();
} else {
return GetIdealHomescreenIconSizeInPx();
}
case webapk::Image::SPLASH_ICON:
return GetIdealSplashImageSizeInPx();
case webapk::Image::SHORTCUT_ICON:
return GetIdealShortcutIconSizeInPx();
default:
return 0;
}
}
bool WebappsIconUtils::DoesAndroidSupportMaskableIcons() {
return base::android::BuildInfo::GetInstance()->sdk_int() >=
base::android::SDK_VERSION_OREO;
}
SkBitmap WebappsIconUtils::FinalizeLauncherIconInBackground(
const SkBitmap& bitmap,
const GURL& url,
bool* is_generated) {
base::AssertLongCPUWorkAllowed();
JNIEnv* env = base::android::AttachCurrentThread();
*is_generated = false;
if (!bitmap.isNull()) {
if (Java_WebappsIconUtils_isIconLargeEnoughForLauncher(env, bitmap.width(),
bitmap.height())) {
return bitmap;
}
}
ScopedJavaLocalRef<jobject> java_url =
url::GURLAndroid::FromNativeGURL(env, url);
SkColor mean_color = SkColorSetRGB(0x91, 0x91, 0x91);
if (!bitmap.isNull()) {
mean_color = color_utils::CalculateKMeanColorOfBitmap(bitmap);
}
*is_generated = true;
ScopedJavaLocalRef<jobject> result =
Java_WebappsIconUtils_generateHomeScreenIcon(
env, java_url, SkColorGetR(mean_color), SkColorGetG(mean_color),
SkColorGetB(mean_color));
return result.obj()
? gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(result))
: SkBitmap();
}
SkBitmap WebappsIconUtils::GenerateAdaptiveIconBitmap(const SkBitmap& bitmap) {
JNIEnv* env = base::android::AttachCurrentThread();
ScopedJavaLocalRef<jobject> result;
if (!bitmap.isNull()) {
ScopedJavaLocalRef<jobject> java_bitmap = gfx::ConvertToJavaBitmap(bitmap);
result = Java_WebappsIconUtils_generateAdaptiveIconBitmap(env, java_bitmap);
}
return result.obj()
? gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(result))
: SkBitmap();
}
int WebappsIconUtils::GetIdealIconCornerRadiusPxForPromptUI() {
return Java_WebappsIconUtils_getIdealIconCornerRadiusPxForPromptUI(
base::android::AttachCurrentThread());
}
void WebappsIconUtils::SetIdealShortcutSizeForTesting(int size) {
g_ideal_shortcut_icon_size = size;
}
void WebappsIconUtils::SetIconSizesForTesting(std::vector<int> sizes) {
// This ordering must be kept up to date with the |GetIconSizes()| above.
g_ideal_homescreen_icon_size = sizes[0];
g_minimum_homescreen_icon_size = sizes[1];
g_ideal_splash_image_size = sizes[2];
g_minimum_splash_image_size = sizes[3];
g_ideal_monochrome_icon_size = sizes[4];
g_ideal_adaptive_launcher_icon_size = sizes[5];
g_ideal_shortcut_icon_size = sizes[6];
}
} // namespace webapps
|