1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.support.v4.app;
18 
19 import static android.support.v4.app.NotificationCompat.DEFAULT_SOUND;
20 import static android.support.v4.app.NotificationCompat.DEFAULT_VIBRATE;
21 import static android.support.v4.app.NotificationCompat.FLAG_GROUP_SUMMARY;
22 import static android.support.v4.app.NotificationCompat.GROUP_ALERT_ALL;
23 import static android.support.v4.app.NotificationCompat.GROUP_ALERT_CHILDREN;
24 import static android.support.v4.app.NotificationCompat.GROUP_ALERT_SUMMARY;
25 
26 import android.app.Notification;
27 import android.app.PendingIntent;
28 import android.app.RemoteInput;
29 import android.content.Context;
30 import android.graphics.Bitmap;
31 import android.net.Uri;
32 import android.os.Bundle;
33 import android.support.annotation.RequiresApi;
34 import android.widget.RemoteViews;
35 
36 import java.util.ArrayList;
37 import java.util.List;
38 
39 @RequiresApi(24)
40 class NotificationCompatApi24 {
41 
42     public static class Builder implements NotificationBuilderWithBuilderAccessor,
43             NotificationBuilderWithActions {
44         private Notification.Builder b;
45         private int mGroupAlertBehavior;
46 
Builder(Context context, Notification n, CharSequence contentTitle, CharSequence contentText, CharSequence contentInfo, RemoteViews tickerView, int number, PendingIntent contentIntent, PendingIntent fullScreenIntent, Bitmap largeIcon, int progressMax, int progress, boolean progressIndeterminate, boolean showWhen, boolean useChronometer, int priority, CharSequence subText, boolean localOnly, String category, ArrayList<String> people, Bundle extras, int color, int visibility, Notification publicVersion, String groupKey, boolean groupSummary, String sortKey, CharSequence[] remoteInputHistory, RemoteViews contentView, RemoteViews bigContentView, RemoteViews headsUpContentView, int groupAlertBehavior)47         public Builder(Context context, Notification n,
48                 CharSequence contentTitle, CharSequence contentText, CharSequence contentInfo,
49                 RemoteViews tickerView, int number,
50                 PendingIntent contentIntent, PendingIntent fullScreenIntent, Bitmap largeIcon,
51                 int progressMax, int progress, boolean progressIndeterminate, boolean showWhen,
52                 boolean useChronometer, int priority, CharSequence subText, boolean localOnly,
53                 String category, ArrayList<String> people, Bundle extras, int color,
54                 int visibility, Notification publicVersion, String groupKey, boolean groupSummary,
55                 String sortKey, CharSequence[] remoteInputHistory, RemoteViews contentView,
56                 RemoteViews bigContentView, RemoteViews headsUpContentView,
57                 int groupAlertBehavior) {
58             b = new Notification.Builder(context)
59                     .setWhen(n.when)
60                     .setShowWhen(showWhen)
61                     .setSmallIcon(n.icon, n.iconLevel)
62                     .setContent(n.contentView)
63                     .setTicker(n.tickerText, tickerView)
64                     .setSound(n.sound, n.audioStreamType)
65                     .setVibrate(n.vibrate)
66                     .setLights(n.ledARGB, n.ledOnMS, n.ledOffMS)
67                     .setOngoing((n.flags & Notification.FLAG_ONGOING_EVENT) != 0)
68                     .setOnlyAlertOnce((n.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0)
69                     .setAutoCancel((n.flags & Notification.FLAG_AUTO_CANCEL) != 0)
70                     .setDefaults(n.defaults)
71                     .setContentTitle(contentTitle)
72                     .setContentText(contentText)
73                     .setSubText(subText)
74                     .setContentInfo(contentInfo)
75                     .setContentIntent(contentIntent)
76                     .setDeleteIntent(n.deleteIntent)
77                     .setFullScreenIntent(fullScreenIntent,
78                             (n.flags & Notification.FLAG_HIGH_PRIORITY) != 0)
79                     .setLargeIcon(largeIcon)
80                     .setNumber(number)
81                     .setUsesChronometer(useChronometer)
82                     .setPriority(priority)
83                     .setProgress(progressMax, progress, progressIndeterminate)
84                     .setLocalOnly(localOnly)
85                     .setExtras(extras)
86                     .setGroup(groupKey)
87                     .setGroupSummary(groupSummary)
88                     .setSortKey(sortKey)
89                     .setCategory(category)
90                     .setColor(color)
91                     .setVisibility(visibility)
92                     .setPublicVersion(publicVersion)
93                     .setRemoteInputHistory(remoteInputHistory);
94             if (contentView != null) {
95                 b.setCustomContentView(contentView);
96             }
97             if (bigContentView != null) {
98                 b.setCustomBigContentView(bigContentView);
99             }
100             if (headsUpContentView != null) {
101                 b.setCustomHeadsUpContentView(headsUpContentView);
102             }
103             for (String person: people) {
104                 b.addPerson(person);
105             }
106 
107             mGroupAlertBehavior = groupAlertBehavior;
108         }
109 
110         @Override
addAction(NotificationCompatBase.Action action)111         public void addAction(NotificationCompatBase.Action action) {
112             NotificationCompatApi24.addAction(b, action);
113         }
114 
115         @Override
getBuilder()116         public Notification.Builder getBuilder() {
117             return b;
118         }
119 
120         @Override
build()121         public Notification build() {
122             Notification notification =  b.build();
123 
124             if (mGroupAlertBehavior != GROUP_ALERT_ALL) {
125                 // if is summary and only children should alert
126                 if (notification.getGroup() != null
127                         && (notification.flags & FLAG_GROUP_SUMMARY) != 0
128                         && mGroupAlertBehavior == GROUP_ALERT_CHILDREN) {
129                     removeSoundAndVibration(notification);
130                 }
131                 // if is group child and only summary should alert
132                 if (notification.getGroup() != null
133                         && (notification.flags & FLAG_GROUP_SUMMARY) == 0
134                         && mGroupAlertBehavior == GROUP_ALERT_SUMMARY) {
135                     removeSoundAndVibration(notification);
136                 }
137             }
138 
139             return notification;
140         }
141 
removeSoundAndVibration(Notification notification)142         private void removeSoundAndVibration(Notification notification) {
143             notification.sound = null;
144             notification.vibrate = null;
145             notification.defaults &= ~DEFAULT_SOUND;
146             notification.defaults &= ~DEFAULT_VIBRATE;
147         }
148     }
149 
addMessagingStyle(NotificationBuilderWithBuilderAccessor b, CharSequence userDisplayName, CharSequence conversationTitle, List<CharSequence> texts, List<Long> timestamps, List<CharSequence> senders, List<String> dataMimeTypes, List<Uri> dataUris)150     public static void addMessagingStyle(NotificationBuilderWithBuilderAccessor b,
151             CharSequence userDisplayName, CharSequence conversationTitle, List<CharSequence> texts,
152             List<Long> timestamps, List<CharSequence> senders, List<String> dataMimeTypes,
153             List<Uri> dataUris) {
154         Notification.MessagingStyle style = new Notification.MessagingStyle(userDisplayName)
155                 .setConversationTitle(conversationTitle);
156         for (int i = 0; i < texts.size(); i++) {
157             Notification.MessagingStyle.Message message = new Notification.MessagingStyle.Message(
158                     texts.get(i), timestamps.get(i), senders.get(i));
159             if (dataMimeTypes.get(i) != null) {
160                 message.setData(dataMimeTypes.get(i), dataUris.get(i));
161             }
162             style.addMessage(message);
163         }
164         style.setBuilder(b.getBuilder());
165     }
166 
addAction(Notification.Builder b, NotificationCompatBase.Action action)167     public static void addAction(Notification.Builder b, NotificationCompatBase.Action action) {
168         Notification.Action.Builder actionBuilder = new Notification.Action.Builder(
169                 action.getIcon(), action.getTitle(), action.getActionIntent());
170         if (action.getRemoteInputs() != null) {
171             for (RemoteInput remoteInput : RemoteInputCompatApi20.fromCompat(
172                     action.getRemoteInputs())) {
173                 actionBuilder.addRemoteInput(remoteInput);
174             }
175         }
176         Bundle actionExtras;
177         if (action.getExtras() != null) {
178             actionExtras = new Bundle(action.getExtras());
179         } else {
180             actionExtras = new Bundle();
181         }
182         actionExtras.putBoolean(NotificationCompatJellybean.EXTRA_ALLOW_GENERATED_REPLIES,
183                 action.getAllowGeneratedReplies());
184         actionBuilder.addExtras(actionExtras);
185         actionBuilder.setAllowGeneratedReplies(action.getAllowGeneratedReplies());
186         b.addAction(actionBuilder.build());
187     }
188 }
189