15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (C) 2014 The Android Open Source Project
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Licensed under the Apache License, Version 2.0 (the "License");
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * you may not use this file except in compliance with the License.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * You may obtain a copy of the License at
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *      http://www.apache.org/licenses/LICENSE-2.0
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Unless required by applicable law or agreed to in writing, software
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * distributed under the License is distributed on an "AS IS" BASIS,
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * See the License for the specific language governing permissions and
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * limitations under the License.
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)package android.support.v4.app;
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import android.app.Notification;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import android.app.PendingIntent;
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import android.content.Context;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import android.graphics.Bitmap;
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import android.os.Bundle;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import android.util.SparseArray;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import android.widget.RemoteViews;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import java.util.ArrayList;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import java.util.List;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NotificationCompatKitKat {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    public static class Builder implements NotificationBuilderWithBuilderAccessor,
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            NotificationBuilderWithActions {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        private Notification.Builder b;
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        private Bundle mExtras;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        private List<Bundle> mActionExtrasList = new ArrayList<Bundle>();
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        public Builder(Context context, Notification n,
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                CharSequence contentTitle, CharSequence contentText, CharSequence contentInfo,
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                RemoteViews tickerView, int number,
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                PendingIntent contentIntent, PendingIntent fullScreenIntent, Bitmap largeIcon,
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                int progressMax, int progress, boolean progressIndeterminate, boolean showWhen,
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                boolean useChronometer, int priority, CharSequence subText, boolean localOnly,
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                ArrayList<String> people, Bundle extras, String groupKey, boolean groupSummary,
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                String sortKey) {
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            b = new Notification.Builder(context)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setWhen(n.when)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setShowWhen(showWhen)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setSmallIcon(n.icon, n.iconLevel)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setContent(n.contentView)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setTicker(n.tickerText, tickerView)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setSound(n.sound, n.audioStreamType)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setVibrate(n.vibrate)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setLights(n.ledARGB, n.ledOnMS, n.ledOffMS)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setOngoing((n.flags & Notification.FLAG_ONGOING_EVENT) != 0)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setOnlyAlertOnce((n.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setAutoCancel((n.flags & Notification.FLAG_AUTO_CANCEL) != 0)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setDefaults(n.defaults)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setContentTitle(contentTitle)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setContentText(contentText)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setSubText(subText)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setContentInfo(contentInfo)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setContentIntent(contentIntent)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setDeleteIntent(n.deleteIntent)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setFullScreenIntent(fullScreenIntent,
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        (n.flags & Notification.FLAG_HIGH_PRIORITY) != 0)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setLargeIcon(largeIcon)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setNumber(number)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setUsesChronometer(useChronometer)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setPriority(priority)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                .setProgress(progressMax, progress, progressIndeterminate);
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            mExtras = new Bundle();
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            if (extras != null) {
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                mExtras.putAll(extras);
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            }
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            if (people != null && !people.isEmpty()) {
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                mExtras.putStringArray(Notification.EXTRA_PEOPLE,
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        people.toArray(new String[people.size()]));
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            }
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            if (localOnly) {
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                mExtras.putBoolean(NotificationCompatJellybean.EXTRA_LOCAL_ONLY, true);
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            }
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            if (groupKey != null) {
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                mExtras.putString(NotificationCompatJellybean.EXTRA_GROUP_KEY, groupKey);
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                if (groupSummary) {
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    mExtras.putBoolean(NotificationCompatJellybean.EXTRA_GROUP_SUMMARY, true);
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                } else {
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    mExtras.putBoolean(NotificationCompatJellybean.EXTRA_USE_SIDE_CHANNEL, true);
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                }
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            }
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            if (sortKey != null) {
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                mExtras.putString(NotificationCompatJellybean.EXTRA_SORT_KEY, sortKey);
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            }
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        @Override
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        public void addAction(NotificationCompatBase.Action action) {
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            mActionExtrasList.add(NotificationCompatJellybean.writeActionAndGetExtras(b, action));
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        @Override
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        public Notification.Builder getBuilder() {
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            return b;
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        public Notification build() {
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            SparseArray<Bundle> actionExtrasMap = NotificationCompatJellybean.buildActionExtrasMap(
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    mActionExtrasList);
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            if (actionExtrasMap != null) {
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                // Add the action extras sparse array if any action was added with extras.
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                mExtras.putSparseParcelableArray(
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        NotificationCompatJellybean.EXTRA_ACTION_EXTRAS, actionExtrasMap);
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            }
113            b.setExtras(mExtras);
114            return b.build();
115        }
116    }
117
118    public static Bundle getExtras(Notification notif) {
119        return notif.extras;
120    }
121
122    public static int getActionCount(Notification notif) {
123        return notif.actions != null ? notif.actions.length : 0;
124    }
125
126    public static NotificationCompatBase.Action getAction(Notification notif,
127            int actionIndex, NotificationCompatBase.Action.Factory factory,
128            RemoteInputCompatBase.RemoteInput.Factory remoteInputFactory) {
129        Notification.Action action = notif.actions[actionIndex];
130        Bundle actionExtras = null;
131        SparseArray<Bundle> actionExtrasMap = notif.extras.getSparseParcelableArray(
132                NotificationCompatJellybean.EXTRA_ACTION_EXTRAS);
133        if (actionExtrasMap != null) {
134            actionExtras = actionExtrasMap.get(actionIndex);
135        }
136        return NotificationCompatJellybean.readAction(factory, remoteInputFactory,
137                action.icon, action.title, action.actionIntent, actionExtras);
138    }
139
140    public static boolean getLocalOnly(Notification notif) {
141        return notif.extras.getBoolean(NotificationCompatJellybean.EXTRA_LOCAL_ONLY);
142    }
143
144    public static String getGroup(Notification notif) {
145        return notif.extras.getString(NotificationCompatJellybean.EXTRA_GROUP_KEY);
146    }
147
148    public static boolean isGroupSummary(Notification notif) {
149        return notif.extras.getBoolean(NotificationCompatJellybean.EXTRA_GROUP_SUMMARY);
150    }
151
152    public static String getSortKey(Notification notif) {
153        return notif.extras.getString(NotificationCompatJellybean.EXTRA_SORT_KEY);
154    }
155}
156