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
17package android.support.v4.app;
18
19import android.app.Notification;
20import android.app.PendingIntent;
21import android.app.RemoteInput;
22import android.content.Context;
23import android.graphics.Bitmap;
24import android.net.Uri;
25import android.os.Bundle;
26import android.widget.RemoteViews;
27
28import java.util.ArrayList;
29import java.util.List;
30
31class NotificationCompatApi24 {
32
33    public static final String CATEGORY_CALL = Notification.CATEGORY_CALL;
34    public static final String CATEGORY_MESSAGE = Notification.CATEGORY_MESSAGE;
35    public static final String CATEGORY_EMAIL = Notification.CATEGORY_EMAIL;
36    public static final String CATEGORY_EVENT = Notification.CATEGORY_EVENT;
37    public static final String CATEGORY_PROMO = Notification.CATEGORY_PROMO;
38    public static final String CATEGORY_ALARM = Notification.CATEGORY_ALARM;
39    public static final String CATEGORY_PROGRESS = Notification.CATEGORY_PROGRESS;
40    public static final String CATEGORY_SOCIAL = Notification.CATEGORY_SOCIAL;
41    public static final String CATEGORY_ERROR = Notification.CATEGORY_ERROR;
42    public static final String CATEGORY_TRANSPORT = Notification.CATEGORY_TRANSPORT;
43    public static final String CATEGORY_SYSTEM = Notification.CATEGORY_SYSTEM;
44    public static final String CATEGORY_SERVICE = Notification.CATEGORY_SERVICE;
45    public static final String CATEGORY_RECOMMENDATION = Notification.CATEGORY_RECOMMENDATION;
46    public static final String CATEGORY_STATUS = Notification.CATEGORY_STATUS;
47
48    public static class Builder implements NotificationBuilderWithBuilderAccessor,
49            NotificationBuilderWithActions {
50        private Notification.Builder b;
51
52        public Builder(Context context, Notification n,
53                CharSequence contentTitle, CharSequence contentText, CharSequence contentInfo,
54                RemoteViews tickerView, int number,
55                PendingIntent contentIntent, PendingIntent fullScreenIntent, Bitmap largeIcon,
56                int progressMax, int progress, boolean progressIndeterminate, boolean showWhen,
57                boolean useChronometer, int priority, CharSequence subText, boolean localOnly,
58                String category, ArrayList<String> people, Bundle extras, int color,
59                int visibility, Notification publicVersion, String groupKey, boolean groupSummary,
60                String sortKey, CharSequence[] remoteInputHistory, RemoteViews contentView,
61                RemoteViews bigContentView, RemoteViews headsUpContentView) {
62            b = new Notification.Builder(context)
63                    .setWhen(n.when)
64                    .setShowWhen(showWhen)
65                    .setSmallIcon(n.icon, n.iconLevel)
66                    .setContent(n.contentView)
67                    .setTicker(n.tickerText, tickerView)
68                    .setSound(n.sound, n.audioStreamType)
69                    .setVibrate(n.vibrate)
70                    .setLights(n.ledARGB, n.ledOnMS, n.ledOffMS)
71                    .setOngoing((n.flags & Notification.FLAG_ONGOING_EVENT) != 0)
72                    .setOnlyAlertOnce((n.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0)
73                    .setAutoCancel((n.flags & Notification.FLAG_AUTO_CANCEL) != 0)
74                    .setDefaults(n.defaults)
75                    .setContentTitle(contentTitle)
76                    .setContentText(contentText)
77                    .setSubText(subText)
78                    .setContentInfo(contentInfo)
79                    .setContentIntent(contentIntent)
80                    .setDeleteIntent(n.deleteIntent)
81                    .setFullScreenIntent(fullScreenIntent,
82                            (n.flags & Notification.FLAG_HIGH_PRIORITY) != 0)
83                    .setLargeIcon(largeIcon)
84                    .setNumber(number)
85                    .setUsesChronometer(useChronometer)
86                    .setPriority(priority)
87                    .setProgress(progressMax, progress, progressIndeterminate)
88                    .setLocalOnly(localOnly)
89                    .setExtras(extras)
90                    .setGroup(groupKey)
91                    .setGroupSummary(groupSummary)
92                    .setSortKey(sortKey)
93                    .setCategory(category)
94                    .setColor(color)
95                    .setVisibility(visibility)
96                    .setPublicVersion(publicVersion)
97                    .setRemoteInputHistory(remoteInputHistory);
98            if (contentView != null) {
99                b.setCustomContentView(contentView);
100            }
101            if (bigContentView != null) {
102                b.setCustomBigContentView(bigContentView);
103            }
104            if (headsUpContentView != null) {
105                b.setCustomHeadsUpContentView(headsUpContentView);
106            }
107            for (String person: people) {
108                b.addPerson(person);
109            }
110        }
111
112        @Override
113        public void addAction(NotificationCompatBase.Action action) {
114            Notification.Action.Builder actionBuilder = new Notification.Action.Builder(
115                    action.getIcon(), action.getTitle(), action.getActionIntent());
116            if (action.getRemoteInputs() != null) {
117                for (RemoteInput remoteInput : RemoteInputCompatApi20.fromCompat(
118                        action.getRemoteInputs())) {
119                    actionBuilder.addRemoteInput(remoteInput);
120                }
121            }
122            Bundle actionExtras;
123            if (action.getExtras() != null) {
124                actionExtras = new Bundle(action.getExtras());
125            } else {
126                actionExtras = new Bundle();
127            }
128            actionExtras.putBoolean(NotificationCompatJellybean.EXTRA_ALLOW_GENERATED_REPLIES,
129                    action.getAllowGeneratedReplies());
130            actionBuilder.addExtras(actionExtras);
131            actionBuilder.setAllowGeneratedReplies(action.getAllowGeneratedReplies());
132            b.addAction(actionBuilder.build());
133        }
134
135        @Override
136        public Notification.Builder getBuilder() {
137            return b;
138        }
139
140        @Override
141        public Notification build() {
142            return b.build();
143        }
144    }
145
146    public static void addMessagingStyle(NotificationBuilderWithBuilderAccessor b,
147            CharSequence userDisplayName, CharSequence conversationTitle, List<CharSequence> texts,
148            List<Long> timestamps, List<CharSequence> senders, List<String> dataMimeTypes,
149            List<Uri> dataUris) {
150        Notification.MessagingStyle style = new Notification.MessagingStyle(userDisplayName)
151                .setConversationTitle(conversationTitle);
152        for (int i = 0; i < texts.size(); i++) {
153            Notification.MessagingStyle.Message message = new Notification.MessagingStyle.Message(
154                    texts.get(i), timestamps.get(i), senders.get(i));
155            if (dataMimeTypes.get(i) != null) {
156                message.setData(dataMimeTypes.get(i), dataUris.get(i));
157            }
158            style.addMessage(message);
159        }
160        style.setBuilder(b.getBuilder());
161    }
162}
163