Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / android / webapps / add_to_homescreen_dialog_helper.cc
blob03f310df77862c70d77693472cc0a6d7b0d92444
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/android/webapps/add_to_homescreen_dialog_helper.h"
7 #include <jni.h>
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/basictypes.h"
12 #include "base/location.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/browser/android/shortcut_helper.h"
16 #include "chrome/browser/banners/app_banner_settings_helper.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/common/manifest.h"
20 #include "jni/AddToHomescreenDialogHelper_jni.h"
21 #include "ui/gfx/android/java_bitmap.h"
22 #include "ui/gfx/color_analysis.h"
23 #include "url/gurl.h"
25 using content::Manifest;
27 jlong Initialize(JNIEnv* env, jobject obj, jobject java_web_contents) {
28 content::WebContents* web_contents =
29 content::WebContents::FromJavaWebContents(java_web_contents);
30 AddToHomescreenDialogHelper* add_to_homescreen_helper =
31 new AddToHomescreenDialogHelper(env, obj, web_contents);
32 return reinterpret_cast<intptr_t>(add_to_homescreen_helper);
35 AddToHomescreenDialogHelper::AddToHomescreenDialogHelper(JNIEnv* env,
36 jobject obj,
37 content::WebContents* web_contents)
38 : add_shortcut_pending_(false),
39 data_fetcher_(new AddToHomescreenDataFetcher(web_contents, this)) {
40 java_ref_.Reset(env, obj);
43 AddToHomescreenDialogHelper::~AddToHomescreenDialogHelper() {
44 data_fetcher_->set_weak_observer(nullptr);
45 data_fetcher_ = nullptr;
48 void AddToHomescreenDialogHelper::OnUserTitleAvailable(
49 const base::string16& user_title) {
50 JNIEnv* env = base::android::AttachCurrentThread();
51 ScopedJavaLocalRef<jstring> j_user_title =
52 base::android::ConvertUTF16ToJavaString(env, user_title);
53 Java_AddToHomescreenDialogHelper_onUserTitleAvailable(env,
54 java_ref_.obj(),
55 j_user_title.obj());
58 void AddToHomescreenDialogHelper::OnDataAvailable(const ShortcutInfo& info,
59 const SkBitmap& icon) {
60 JNIEnv* env = base::android::AttachCurrentThread();
61 ScopedJavaLocalRef<jobject> java_bitmap;
62 if (icon.getSize())
63 java_bitmap = gfx::ConvertToJavaBitmap(&icon);
65 Java_AddToHomescreenDialogHelper_onIconAvailable(env,
66 java_ref_.obj(),
67 java_bitmap.obj());
69 if (add_shortcut_pending_)
70 AddShortcut(info, icon);
73 void AddToHomescreenDialogHelper::Destroy(JNIEnv* env, jobject obj) {
74 delete this;
77 SkBitmap AddToHomescreenDialogHelper::FinalizeLauncherIcon(
78 const SkBitmap& bitmap,
79 const GURL& url) {
80 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
82 // Determine a single color to use for the favicon if the favicon that is
83 // returned it is too low quality.
84 SkColor color = color_utils::CalculateKMeanColorOfBitmap(bitmap);
85 int dominant_red = SkColorGetR(color);
86 int dominant_green = SkColorGetG(color);
87 int dominant_blue = SkColorGetB(color);
89 // Make the icon acceptable for the Android launcher.
90 JNIEnv* env = base::android::AttachCurrentThread();
91 ScopedJavaLocalRef<jstring> java_url =
92 base::android::ConvertUTF8ToJavaString(env, url.spec());
93 ScopedJavaLocalRef<jobject> java_bitmap;
94 if (bitmap.getSize())
95 java_bitmap = gfx::ConvertToJavaBitmap(&bitmap);
97 base::android::ScopedJavaLocalRef<jobject> ref =
98 Java_AddToHomescreenDialogHelper_finalizeLauncherIcon(env,
99 java_url.obj(),
100 java_bitmap.obj(),
101 dominant_red,
102 dominant_green,
103 dominant_blue);
104 return gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(ref.obj()));
107 void AddToHomescreenDialogHelper::AddShortcut(JNIEnv* env,
108 jobject obj,
109 jstring j_user_title) {
110 add_shortcut_pending_ = true;
112 base::string16 user_title =
113 base::android::ConvertJavaStringToUTF16(env, j_user_title);
114 if (!user_title.empty())
115 data_fetcher_->shortcut_info().user_title = user_title;
117 if (data_fetcher_->is_ready()) {
118 // If the fetcher isn't ready yet, the shortcut will be added when it is
119 // via OnDataAvailable();
120 AddShortcut(data_fetcher_->shortcut_info(), data_fetcher_->shortcut_icon());
124 void AddToHomescreenDialogHelper::AddShortcut(const ShortcutInfo& info,
125 const SkBitmap& icon) {
126 DCHECK(add_shortcut_pending_);
127 if (!add_shortcut_pending_)
128 return;
129 add_shortcut_pending_ = false;
131 RecordAddToHomescreen();
133 content::BrowserThread::PostTask(
134 content::BrowserThread::IO,
135 FROM_HERE,
136 base::Bind(&ShortcutHelper::AddShortcutInBackgroundWithSkBitmap,
137 info,
138 icon));
141 bool AddToHomescreenDialogHelper::RegisterAddToHomescreenDialogHelper(
142 JNIEnv* env) {
143 return RegisterNativesImpl(env);
146 void AddToHomescreenDialogHelper::RecordAddToHomescreen() {
147 // Record that the shortcut has been added, so no banners will be shown
148 // for this app.
149 content::WebContents* web_contents = data_fetcher_->web_contents();
150 if (!web_contents)
151 return;
153 AppBannerSettingsHelper::RecordBannerEvent(
154 web_contents, web_contents->GetURL(),
155 data_fetcher_->shortcut_info().url.spec(),
156 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN,
157 base::Time::Now());