[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 1 | // Copyright 2013 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. |
| 4 | |
| 5 | #include "chrome/browser/android/shortcut_helper.h" |
| 6 | |
| 7 | #include <jni.h> |
| 8 | |
| 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" |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 14 | #include "base/strings/utf_string_conversions.h" |
benwells | 840ae90 | 2015-02-17 21:13:28 | [diff] [blame] | 15 | #include "chrome/browser/banners/app_banner_settings_helper.h" |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 16 | #include "content/public/browser/browser_thread.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 17 | #include "content/public/browser/web_contents.h" |
mlamouri | b225ae56 | 2014-09-17 11:51:37 | [diff] [blame] | 18 | #include "content/public/common/manifest.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 19 | #include "jni/ShortcutHelper_jni.h" |
| 20 | #include "ui/gfx/android/java_bitmap.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 21 | #include "ui/gfx/color_analysis.h" |
| 22 | #include "url/gurl.h" |
| 23 | |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 24 | using content::Manifest; |
| 25 | |
yfriedman | 4846e39 | 2015-02-19 19:12:10 | [diff] [blame] | 26 | jlong Initialize(JNIEnv* env, jobject obj, jobject java_web_contents) { |
| 27 | content::WebContents* web_contents = |
| 28 | content::WebContents::FromJavaWebContents(java_web_contents); |
| 29 | ShortcutHelper* shortcut_helper = new ShortcutHelper(env, obj, web_contents); |
mlamouri | ecb9b408 | 2014-09-12 13:04:15 | [diff] [blame] | 30 | return reinterpret_cast<intptr_t>(shortcut_helper); |
| 31 | } |
| 32 | |
| 33 | ShortcutHelper::ShortcutHelper(JNIEnv* env, |
| 34 | jobject obj, |
| 35 | content::WebContents* web_contents) |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 36 | : add_shortcut_pending_(false), |
| 37 | data_fetcher_(new ShortcutDataFetcher(web_contents, this)) { |
| 38 | java_ref_.Reset(env, obj); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 39 | } |
| 40 | |
mlamouri | ecb9b408 | 2014-09-12 13:04:15 | [diff] [blame] | 41 | ShortcutHelper::~ShortcutHelper() { |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 42 | data_fetcher_->set_weak_observer(nullptr); |
| 43 | data_fetcher_ = nullptr; |
mlamouri | ecb9b408 | 2014-09-12 13:04:15 | [diff] [blame] | 44 | } |
| 45 | |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 46 | void ShortcutHelper::OnTitleAvailable(const base::string16& title) { |
mlamouri | ecb9b408 | 2014-09-12 13:04:15 | [diff] [blame] | 47 | JNIEnv* env = base::android::AttachCurrentThread(); |
mlamouri | ecb9b408 | 2014-09-12 13:04:15 | [diff] [blame] | 48 | ScopedJavaLocalRef<jstring> j_title = |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 49 | base::android::ConvertUTF16ToJavaString(env, title); |
| 50 | Java_ShortcutHelper_onTitleAvailable(env, |
| 51 | java_ref_.obj(), |
| 52 | j_title.obj()); |
mlamouri | ecb9b408 | 2014-09-12 13:04:15 | [diff] [blame] | 53 | } |
| 54 | |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 55 | void ShortcutHelper::OnDataAvailable(const ShortcutInfo& info, |
| 56 | const SkBitmap& icon) { |
| 57 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 58 | ScopedJavaLocalRef<jobject> java_bitmap; |
| 59 | if (icon.getSize()) |
| 60 | java_bitmap = gfx::ConvertToJavaBitmap(&icon); |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 61 | |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 62 | Java_ShortcutHelper_onIconAvailable(env, |
| 63 | java_ref_.obj(), |
| 64 | java_bitmap.obj()); |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 65 | |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 66 | if (add_shortcut_pending_) |
| 67 | AddShortcut(info, icon); |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 68 | } |
| 69 | |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 70 | void ShortcutHelper::Destroy(JNIEnv* env, jobject obj) { |
mlamouri | ecb9b408 | 2014-09-12 13:04:15 | [diff] [blame] | 71 | delete this; |
| 72 | } |
| 73 | |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 74 | SkBitmap ShortcutHelper::FinalizeLauncherIcon(const SkBitmap& bitmap, |
| 75 | const GURL& url) { |
| 76 | DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 77 | |
| 78 | // Determine a single color to use for the favicon if the favicon that is |
| 79 | // returned it is too low quality. |
| 80 | SkColor color = color_utils::CalculateKMeanColorOfBitmap(bitmap); |
| 81 | int dominant_red = SkColorGetR(color); |
| 82 | int dominant_green = SkColorGetG(color); |
| 83 | int dominant_blue = SkColorGetB(color); |
| 84 | |
| 85 | // Make the icon acceptable for the Android launcher. |
| 86 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 87 | ScopedJavaLocalRef<jstring> java_url = |
| 88 | base::android::ConvertUTF8ToJavaString(env, url.spec()); |
| 89 | ScopedJavaLocalRef<jobject> java_bitmap; |
| 90 | if (bitmap.getSize()) |
| 91 | java_bitmap = gfx::ConvertToJavaBitmap(&bitmap); |
| 92 | |
| 93 | base::android::ScopedJavaLocalRef<jobject> ref = |
| 94 | Java_ShortcutHelper_finalizeLauncherIcon(env, |
| 95 | java_url.obj(), |
| 96 | java_bitmap.obj(), |
| 97 | dominant_red, |
| 98 | dominant_green, |
| 99 | dominant_blue); |
| 100 | return gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(ref.obj())); |
| 101 | } |
| 102 | |
| 103 | void ShortcutHelper::AddShortcut(JNIEnv* env, jobject obj, jstring jtitle) { |
| 104 | add_shortcut_pending_ = true; |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 105 | |
mlamouri | ecb9b408 | 2014-09-12 13:04:15 | [diff] [blame] | 106 | base::string16 title = base::android::ConvertJavaStringToUTF16(env, jtitle); |
| 107 | if (!title.empty()) |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 108 | data_fetcher_->shortcut_info().title = title; |
mlamouri | ecb9b408 | 2014-09-12 13:04:15 | [diff] [blame] | 109 | |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 110 | if (data_fetcher_->is_ready()) { |
| 111 | // If the fetcher isn't ready yet, the shortcut will be added when it is |
| 112 | // via OnDataAvailable(); |
| 113 | AddShortcut(data_fetcher_->shortcut_info(), data_fetcher_->shortcut_icon()); |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 117 | void ShortcutHelper::AddShortcut(const ShortcutInfo& info, |
| 118 | const SkBitmap& icon) { |
| 119 | DCHECK(add_shortcut_pending_); |
| 120 | if (!add_shortcut_pending_) |
| 121 | return; |
| 122 | add_shortcut_pending_ = false; |
| 123 | |
dfalcantara | 30728015 | 2015-02-19 18:27:48 | [diff] [blame] | 124 | RecordAddToHomescreen(); |
| 125 | |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 126 | content::BrowserThread::PostTask( |
| 127 | content::BrowserThread::IO, |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 128 | FROM_HERE, |
| 129 | base::Bind(&ShortcutHelper::AddShortcutInBackgroundWithSkBitmap, |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 130 | info, |
| 131 | icon)); |
[email protected] | 5820d32c | 2013-08-28 10:03:50 | [diff] [blame] | 132 | } |
| 133 | |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 134 | bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { |
| 135 | return RegisterNativesImpl(env); |
| 136 | } |
| 137 | |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 138 | // static |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 139 | void ShortcutHelper::AddShortcutInBackgroundWithSkBitmap( |
dfalcantara | 16e84de | 2015-02-03 22:07:40 | [diff] [blame] | 140 | const ShortcutInfo& info, |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 141 | const SkBitmap& icon_bitmap) { |
| 142 | DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 143 | |
| 144 | // Send the data to the Java side to create the shortcut. |
| 145 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 146 | ScopedJavaLocalRef<jstring> java_url = |
dfalcantara | 16e84de | 2015-02-03 22:07:40 | [diff] [blame] | 147 | base::android::ConvertUTF8ToJavaString(env, info.url.spec()); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 148 | ScopedJavaLocalRef<jstring> java_title = |
dfalcantara | 16e84de | 2015-02-03 22:07:40 | [diff] [blame] | 149 | base::android::ConvertUTF16ToJavaString(env, info.title); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 150 | ScopedJavaLocalRef<jobject> java_bitmap; |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 151 | if (icon_bitmap.getSize()) |
| 152 | java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 153 | |
mlamouri | 89ccc63 | 2014-09-16 19:29:58 | [diff] [blame] | 154 | Java_ShortcutHelper_addShortcut( |
| 155 | env, |
| 156 | base::android::GetApplicationContext(), |
| 157 | java_url.obj(), |
| 158 | java_title.obj(), |
| 159 | java_bitmap.obj(), |
dfalcantara | 16e84de | 2015-02-03 22:07:40 | [diff] [blame] | 160 | info.display == content::Manifest::DISPLAY_MODE_STANDALONE, |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 161 | info.orientation); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 162 | } |
benwells | 840ae90 | 2015-02-17 21:13:28 | [diff] [blame] | 163 | |
| 164 | void ShortcutHelper::RecordAddToHomescreen() { |
| 165 | // Record that the shortcut has been added, so no banners will be shown |
| 166 | // for this app. |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 167 | content::WebContents* web_contents = data_fetcher_->web_contents(); |
| 168 | if (!web_contents) |
| 169 | return; |
| 170 | |
benwells | 840ae90 | 2015-02-17 21:13:28 | [diff] [blame] | 171 | AppBannerSettingsHelper::RecordBannerEvent( |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame^] | 172 | web_contents, web_contents->GetURL(), |
| 173 | data_fetcher_->shortcut_info().url.spec(), |
benwells | 840ae90 | 2015-02-17 21:13:28 | [diff] [blame] | 174 | AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, |
| 175 | base::Time::Now()); |
| 176 | } |