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(awdf) We should probably use a Compat builder. |
144 ChromeNotificationBuilder builder = | |
145 ((ChromeApplication) mContext) | |
146 .getChromeNotificationBuilder(false, NotificationConstan ts.TYPE_ID_GENERAL, | |
Peter Beverloo
2017/02/20 01:10:20
nit: false /* preferCompat */
Peter Beverloo
2017/02/20 01:10:20
These are not generic notifications. Do you plan t
awdf
2017/02/24 17:37:30
Done.
awdf
2017/02/28 18:04:11
Done.
| |
147 NotificationConstants.TYPE_NAME_GENERAL); | |
143 builder.setTicker(mTickerText); | 148 builder.setTicker(mTickerText); |
144 builder.setContentIntent(mContentIntent); | 149 builder.setContentIntent(mContentIntent); |
145 builder.setDeleteIntent(mDeleteIntent); | 150 builder.setDeleteIntent(mDeleteIntent); |
146 builder.setDefaults(mDefaults); | 151 builder.setDefaults(mDefaults); |
147 builder.setVibrate(mVibratePattern); | 152 builder.setVibrate(mVibratePattern); |
148 builder.setWhen(mTimestamp); | 153 builder.setWhen(mTimestamp); |
149 builder.setOnlyAlertOnce(!mRenotify); | 154 builder.setOnlyAlertOnce(!mRenotify); |
150 ApiCompatibilityUtils.setContentViewForNotificationBuilder(builder, comp actView); | 155 builder.setContent(compactView); |
151 | 156 |
152 // Some things are duplicated in the builder to ensure the notification shows correctly on | 157 // Some things are duplicated in the builder to ensure the notification shows correctly on |
153 // Wear devices and custom lock screens. | 158 // Wear devices and custom lock screens. |
154 builder.setContentTitle(mTitle); | 159 builder.setContentTitle(mTitle); |
155 builder.setContentText(mBody); | 160 builder.setContentText(mBody); |
156 builder.setSubText(mOrigin); | 161 builder.setSubText(mOrigin); |
157 builder.setLargeIcon(getNormalizedLargeIcon()); | 162 builder.setLargeIcon(getNormalizedLargeIcon()); |
158 setSmallIconOnBuilder(builder, mSmallIconId, mSmallIconBitmap); | 163 setSmallIconOnBuilder(builder, mSmallIconId, mSmallIconBitmap); |
159 for (Action action : mActions) { | 164 for (Action action : mActions) { |
160 addActionToBuilder(builder, action); | 165 addActionToBuilder(builder, action); |
161 } | 166 } |
162 if (mSettingsAction != null) { | 167 if (mSettingsAction != null) { |
163 addActionToBuilder(builder, mSettingsAction); | 168 addActionToBuilder(builder, mSettingsAction); |
164 } | 169 } |
165 setGroupOnBuilder(builder, mOrigin); | 170 setGroupOnBuilder(builder, mOrigin); |
166 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | 171 builder.setPublicVersion(createPublicNotification(mContext)); |
167 // Notification.Builder.setPublicVersion was added in Android L. | |
168 builder.setPublicVersion(createPublicNotification(mContext)); | |
169 } | |
170 | 172 |
171 return ApiCompatibilityUtils.notificationWithBigContentView(builder, big View); | 173 return builder.buildWithBigContentView(bigView); |
172 } | 174 } |
173 | 175 |
174 /** | 176 /** |
175 * If there are actions, shows the button related views, and adds a button f or each action. | 177 * If there are actions, shows the button related views, and adds a button f or each action. |
176 */ | 178 */ |
177 private void addActionButtons(RemoteViews bigView) { | 179 private void addActionButtons(RemoteViews bigView) { |
178 // Remove the existing buttons in case an existing notification is being updated. | 180 // Remove the existing buttons in case an existing notification is being updated. |
179 bigView.removeAllViews(R.id.buttons); | 181 bigView.removeAllViews(R.id.buttons); |
180 | 182 |
181 // Always set the visibility of the views associated with the action but tons. The current | 183 // 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 } | 316 } |
315 | 317 |
316 /** | 318 /** |
317 * Whether to use the Material look and feel or fall back to Holo. | 319 * Whether to use the Material look and feel or fall back to Holo. |
318 */ | 320 */ |
319 @VisibleForTesting | 321 @VisibleForTesting |
320 static boolean useMaterial() { | 322 static boolean useMaterial() { |
321 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; | 323 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; |
322 } | 324 } |
323 } | 325 } |
OLD | NEW |