NotificationCompatApi24.java revision 32d191750bbfbad479cc84a8cdc2f42244bfee1f
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) {
61            b = new Notification.Builder(context)
62                    .setWhen(n.when)
63                    .setShowWhen(showWhen)
64                    .setSmallIcon(n.icon, n.iconLevel)
65                    .setContent(n.contentView)
66                    .setTicker(n.tickerText, tickerView)
67                    .setSound(n.sound, n.audioStreamType)
68                    .setVibrate(n.vibrate)
69                    .setLights(n.ledARGB, n.ledOnMS, n.ledOffMS)
70                    .setOngoing((n.flags & Notification.FLAG_ONGOING_EVENT) != 0)
71                    .setOnlyAlertOnce((n.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0)
72                    .setAutoCancel((n.flags & Notification.FLAG_AUTO_CANCEL) != 0)
73                    .setDefaults(n.defaults)
74                    .setContentTitle(contentTitle)
75                    .setContentText(contentText)
76                    .setSubText(subText)
77                    .setContentInfo(contentInfo)
78                    .setContentIntent(contentIntent)
79                    .setDeleteIntent(n.deleteIntent)
80                    .setFullScreenIntent(fullScreenIntent,
81                            (n.flags & Notification.FLAG_HIGH_PRIORITY) != 0)
82                    .setLargeIcon(largeIcon)
83                    .setNumber(number)
84                    .setUsesChronometer(useChronometer)
85                    .setPriority(priority)
86                    .setProgress(progressMax, progress, progressIndeterminate)
87                    .setLocalOnly(localOnly)
88                    .setExtras(extras)
89                    .setGroup(groupKey)
90                    .setGroupSummary(groupSummary)
91                    .setSortKey(sortKey)
92                    .setCategory(category)
93                    .setColor(color)
94                    .setVisibility(visibility)
95                    .setPublicVersion(publicVersion)
96                    .setRemoteInputHistory(remoteInputHistory);
97            for (String person: people) {
98                b.addPerson(person);
99            }
100        }
101
102        @Override
103        public void addAction(NotificationCompatBase.Action action) {
104            Notification.Action.Builder actionBuilder = new Notification.Action.Builder(
105                    action.getIcon(), action.getTitle(), action.getActionIntent());
106            if (action.getRemoteInputs() != null) {
107                for (RemoteInput remoteInput : RemoteInputCompatApi20.fromCompat(
108                        action.getRemoteInputs())) {
109                    actionBuilder.addRemoteInput(remoteInput);
110                }
111            }
112            if (action.getExtras() != null) {
113                actionBuilder.addExtras(action.getExtras());
114            }
115            actionBuilder.setAllowGeneratedReplies(action.getAllowGeneratedReplies());
116            b.addAction(actionBuilder.build());
117        }
118
119        @Override
120        public Notification.Builder getBuilder() {
121            return b;
122        }
123
124        @Override
125        public Notification build() {
126            return b.build();
127        }
128    }
129
130    public static void addMessagingStyle(NotificationBuilderWithBuilderAccessor b,
131            CharSequence userDisplayName, CharSequence conversationTitle, List<CharSequence> texts,
132            List<Long> timestamps, List<CharSequence> senders, List<String> dataMimeTypes,
133            List<Uri> dataUris) {
134        Notification.MessagingStyle style = new Notification.MessagingStyle(userDisplayName)
135                .setConversationTitle(conversationTitle);
136        for (int i = 0; i < texts.size(); i++) {
137            Notification.MessagingStyle.Message message = new Notification.MessagingStyle.Message(
138                    texts.get(i), timestamps.get(i), senders.get(i));
139            if (dataMimeTypes.get(i) != null) {
140                message.setData(dataMimeTypes.get(i), dataUris.get(i));
141            }
142            style.addMessage(message);
143        }
144        style.setBuilder(b.getBuilder());
145    }
146}
147