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"
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"
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
,
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
,
58 void AddToHomescreenDialogHelper::OnDataAvailable(const ShortcutInfo
& info
,
59 const SkBitmap
& icon
) {
60 JNIEnv
* env
= base::android::AttachCurrentThread();
61 ScopedJavaLocalRef
<jobject
> java_bitmap
;
63 java_bitmap
= gfx::ConvertToJavaBitmap(&icon
);
65 Java_AddToHomescreenDialogHelper_onIconAvailable(env
,
69 if (add_shortcut_pending_
)
70 AddShortcut(info
, icon
);
73 void AddToHomescreenDialogHelper::Destroy(JNIEnv
* env
, jobject obj
) {
77 SkBitmap
AddToHomescreenDialogHelper::FinalizeLauncherIcon(
78 const SkBitmap
& bitmap
,
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
;
95 java_bitmap
= gfx::ConvertToJavaBitmap(&bitmap
);
97 base::android::ScopedJavaLocalRef
<jobject
> ref
=
98 Java_AddToHomescreenDialogHelper_finalizeLauncherIcon(env
,
104 return gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(ref
.obj()));
107 void AddToHomescreenDialogHelper::AddShortcut(JNIEnv
* env
,
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_
)
129 add_shortcut_pending_
= false;
131 RecordAddToHomescreen();
133 content::BrowserThread::PostTask(
134 content::BrowserThread::IO
,
136 base::Bind(&ShortcutHelper::AddShortcutInBackgroundWithSkBitmap
,
141 bool AddToHomescreenDialogHelper::RegisterAddToHomescreenDialogHelper(
143 return RegisterNativesImpl(env
);
146 void AddToHomescreenDialogHelper::RecordAddToHomescreen() {
147 // Record that the shortcut has been added, so no banners will be shown
149 content::WebContents
* web_contents
= data_fetcher_
->web_contents();
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
,