| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.notifications; | 5 package org.chromium.chrome.browser.notifications; |
| 6 | 6 |
| 7 import android.app.Notification; | 7 import android.app.Notification; |
| 8 import android.content.Context; | 8 import android.content.Context; |
| 9 import android.content.res.Resources; | 9 import android.content.res.Resources; |
| 10 import android.graphics.Bitmap; | 10 import android.graphics.Bitmap; |
| 11 import android.graphics.BitmapFactory; | 11 import android.graphics.BitmapFactory; |
| 12 import android.graphics.drawable.BitmapDrawable; | 12 import android.graphics.drawable.BitmapDrawable; |
| 13 import android.graphics.drawable.Drawable; | 13 import android.graphics.drawable.Drawable; |
| 14 import android.os.Build; | 14 import android.os.Build; |
| 15 import android.os.StrictMode; | 15 import android.os.StrictMode; |
| 16 import android.os.SystemClock; | 16 import android.os.SystemClock; |
| 17 import android.text.format.DateFormat; | 17 import android.text.format.DateFormat; |
| 18 import android.util.DisplayMetrics; | 18 import android.util.DisplayMetrics; |
| 19 import android.util.TypedValue; | 19 import android.util.TypedValue; |
| 20 import android.view.View; | 20 import android.view.View; |
| 21 import android.widget.RemoteViews; | 21 import android.widget.RemoteViews; |
| 22 | 22 |
| 23 import org.chromium.base.ApiCompatibilityUtils; | 23 import org.chromium.base.ApiCompatibilityUtils; |
| 24 import org.chromium.base.VisibleForTesting; | 24 import org.chromium.base.VisibleForTesting; |
| 25 import org.chromium.base.metrics.RecordHistogram; | 25 import org.chromium.base.metrics.RecordHistogram; |
| 26 import org.chromium.chrome.R; | 26 import org.chromium.chrome.R; |
| 27 import org.chromium.chrome.browser.ChromeApplication; |
| 27 import org.chromium.ui.base.LocalizationUtils; | 28 import org.chromium.ui.base.LocalizationUtils; |
| 28 | 29 |
| 29 import java.util.Date; | 30 import java.util.Date; |
| 30 import java.util.concurrent.TimeUnit; | 31 import java.util.concurrent.TimeUnit; |
| 31 | 32 |
| 32 /** | 33 /** |
| 33 * Builds a notification using the given inputs. Uses RemoteViews to provide a c
ustom layout. | 34 * Builds a notification using the given inputs. Uses RemoteViews to provide a c
ustom layout. |
| 34 */ | 35 */ |
| 35 public class CustomNotificationBuilder extends NotificationBuilderBase { | 36 public class CustomNotificationBuilder extends NotificationBuilderBase { |
| 36 /** | 37 /** |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 view.setViewVisibility(smallIconId, View.VISIBLE); | 131 view.setViewVisibility(smallIconId, View.VISIBLE); |
| 131 if (mSmallIconBitmap != null) { | 132 if (mSmallIconBitmap != null) { |
| 132 view.setImageViewBitmap(smallIconId, mSmallIconBitmap); | 133 view.setImageViewBitmap(smallIconId, mSmallIconBitmap); |
| 133 } else { | 134 } else { |
| 134 view.setImageViewResource(smallIconId, mSmallIconId); | 135 view.setImageViewResource(smallIconId, mSmallIconId); |
| 135 } | 136 } |
| 136 } | 137 } |
| 137 addActionButtons(bigView); | 138 addActionButtons(bigView); |
| 138 configureSettingsButton(bigView); | 139 configureSettingsButton(bigView); |
| 139 | 140 |
| 140 // Note: this is not a NotificationCompat builder so be mindful of the | 141 // Note: under the hood this is not a NotificationCompat builder so be m
indful of the |
| 141 // API level of methods you call on the builder. | 142 // API level of methods you call on the builder. |
| 142 Notification.Builder builder = new Notification.Builder(mContext); | 143 // TODO(crbug.com/697104) We should probably use a Compat builder. |
| 144 ChromeNotificationBuilder builder = |
| 145 ((ChromeApplication) mContext.getApplicationContext()) |
| 146 .createChromeNotificationBuilder(false /* preferCompat *
/, |
| 147 NotificationConstants.CATEGORY_ID_SITES, |
| 148 mContext.getString( |
| 149 org.chromium.chrome.R.string.notificatio
n_category_sites), |
| 150 NotificationConstants.CATEGORY_GROUP_ID_GENERAL, |
| 151 mContext.getString(org.chromium.chrome.R.string |
| 152 .notification_categor
y_group_general)); |
| 143 builder.setTicker(mTickerText); | 153 builder.setTicker(mTickerText); |
| 144 builder.setContentIntent(mContentIntent); | 154 builder.setContentIntent(mContentIntent); |
| 145 builder.setDeleteIntent(mDeleteIntent); | 155 builder.setDeleteIntent(mDeleteIntent); |
| 146 builder.setDefaults(mDefaults); | 156 builder.setDefaults(mDefaults); |
| 147 builder.setVibrate(mVibratePattern); | 157 builder.setVibrate(mVibratePattern); |
| 148 builder.setWhen(mTimestamp); | 158 builder.setWhen(mTimestamp); |
| 149 builder.setOnlyAlertOnce(!mRenotify); | 159 builder.setOnlyAlertOnce(!mRenotify); |
| 150 ApiCompatibilityUtils.setContentViewForNotificationBuilder(builder, comp
actView); | 160 builder.setContent(compactView); |
| 151 | 161 |
| 152 // Some things are duplicated in the builder to ensure the notification
shows correctly on | 162 // Some things are duplicated in the builder to ensure the notification
shows correctly on |
| 153 // Wear devices and custom lock screens. | 163 // Wear devices and custom lock screens. |
| 154 builder.setContentTitle(mTitle); | 164 builder.setContentTitle(mTitle); |
| 155 builder.setContentText(mBody); | 165 builder.setContentText(mBody); |
| 156 builder.setSubText(mOrigin); | 166 builder.setSubText(mOrigin); |
| 157 builder.setLargeIcon(getNormalizedLargeIcon()); | 167 builder.setLargeIcon(getNormalizedLargeIcon()); |
| 158 setSmallIconOnBuilder(builder, mSmallIconId, mSmallIconBitmap); | 168 setSmallIconOnBuilder(builder, mSmallIconId, mSmallIconBitmap); |
| 159 for (Action action : mActions) { | 169 for (Action action : mActions) { |
| 160 addActionToBuilder(builder, action); | 170 addActionToBuilder(builder, action); |
| 161 } | 171 } |
| 162 if (mSettingsAction != null) { | 172 if (mSettingsAction != null) { |
| 163 addActionToBuilder(builder, mSettingsAction); | 173 addActionToBuilder(builder, mSettingsAction); |
| 164 } | 174 } |
| 165 setGroupOnBuilder(builder, mOrigin); | 175 setGroupOnBuilder(builder, mOrigin); |
| 166 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | 176 builder.setPublicVersion(createPublicNotification(mContext)); |
| 167 // Notification.Builder.setPublicVersion was added in Android L. | |
| 168 builder.setPublicVersion(createPublicNotification(mContext)); | |
| 169 } | |
| 170 | 177 |
| 171 return ApiCompatibilityUtils.notificationWithBigContentView(builder, big
View); | 178 return builder.buildWithBigContentView(bigView); |
| 172 } | 179 } |
| 173 | 180 |
| 174 /** | 181 /** |
| 175 * If there are actions, shows the button related views, and adds a button f
or each action. | 182 * If there are actions, shows the button related views, and adds a button f
or each action. |
| 176 */ | 183 */ |
| 177 private void addActionButtons(RemoteViews bigView) { | 184 private void addActionButtons(RemoteViews bigView) { |
| 178 // Remove the existing buttons in case an existing notification is being
updated. | 185 // Remove the existing buttons in case an existing notification is being
updated. |
| 179 bigView.removeAllViews(R.id.buttons); | 186 bigView.removeAllViews(R.id.buttons); |
| 180 | 187 |
| 181 // Always set the visibility of the views associated with the action but
tons. The current | 188 // Always set the visibility of the views associated with the action but
tons. The current |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 } | 321 } |
| 315 | 322 |
| 316 /** | 323 /** |
| 317 * Whether to use the Material look and feel or fall back to Holo. | 324 * Whether to use the Material look and feel or fall back to Holo. |
| 318 */ | 325 */ |
| 319 @VisibleForTesting | 326 @VisibleForTesting |
| 320 static boolean useMaterial() { | 327 static boolean useMaterial() { |
| 321 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; | 328 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; |
| 322 } | 329 } |
| 323 } | 330 } |
| OLD | NEW |