1/* 2 * Copyright (C) 2014 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.content.Context; 22import android.graphics.Bitmap; 23import android.os.Bundle; 24import android.util.SparseArray; 25import android.widget.RemoteViews; 26 27import java.util.ArrayList; 28import java.util.List; 29 30class NotificationCompatKitKat { 31 public static class Builder implements NotificationBuilderWithBuilderAccessor, 32 NotificationBuilderWithActions { 33 private Notification.Builder b; 34 private Bundle mExtras; 35 private List<Bundle> mActionExtrasList = new ArrayList<Bundle>(); 36 37 public Builder(Context context, Notification n, 38 CharSequence contentTitle, CharSequence contentText, CharSequence contentInfo, 39 RemoteViews tickerView, int number, 40 PendingIntent contentIntent, PendingIntent fullScreenIntent, Bitmap largeIcon, 41 int progressMax, int progress, boolean progressIndeterminate, boolean showWhen, 42 boolean useChronometer, int priority, CharSequence subText, boolean localOnly, 43 ArrayList<String> people, Bundle extras, String groupKey, boolean groupSummary, 44 String sortKey) { 45 b = new Notification.Builder(context) 46 .setWhen(n.when) 47 .setShowWhen(showWhen) 48 .setSmallIcon(n.icon, n.iconLevel) 49 .setContent(n.contentView) 50 .setTicker(n.tickerText, tickerView) 51 .setSound(n.sound, n.audioStreamType) 52 .setVibrate(n.vibrate) 53 .setLights(n.ledARGB, n.ledOnMS, n.ledOffMS) 54 .setOngoing((n.flags & Notification.FLAG_ONGOING_EVENT) != 0) 55 .setOnlyAlertOnce((n.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0) 56 .setAutoCancel((n.flags & Notification.FLAG_AUTO_CANCEL) != 0) 57 .setDefaults(n.defaults) 58 .setContentTitle(contentTitle) 59 .setContentText(contentText) 60 .setSubText(subText) 61 .setContentInfo(contentInfo) 62 .setContentIntent(contentIntent) 63 .setDeleteIntent(n.deleteIntent) 64 .setFullScreenIntent(fullScreenIntent, 65 (n.flags & Notification.FLAG_HIGH_PRIORITY) != 0) 66 .setLargeIcon(largeIcon) 67 .setNumber(number) 68 .setUsesChronometer(useChronometer) 69 .setPriority(priority) 70 .setProgress(progressMax, progress, progressIndeterminate); 71 mExtras = new Bundle(); 72 if (extras != null) { 73 mExtras.putAll(extras); 74 } 75 if (people != null && !people.isEmpty()) { 76 mExtras.putStringArray(Notification.EXTRA_PEOPLE, 77 people.toArray(new String[people.size()])); 78 } 79 if (localOnly) { 80 mExtras.putBoolean(NotificationCompatJellybean.EXTRA_LOCAL_ONLY, true); 81 } 82 if (groupKey != null) { 83 mExtras.putString(NotificationCompatJellybean.EXTRA_GROUP_KEY, groupKey); 84 if (groupSummary) { 85 mExtras.putBoolean(NotificationCompatJellybean.EXTRA_GROUP_SUMMARY, true); 86 } else { 87 mExtras.putBoolean(NotificationCompatJellybean.EXTRA_USE_SIDE_CHANNEL, true); 88 } 89 } 90 if (sortKey != null) { 91 mExtras.putString(NotificationCompatJellybean.EXTRA_SORT_KEY, sortKey); 92 } 93 } 94 95 @Override 96 public void addAction(NotificationCompatBase.Action action) { 97 mActionExtrasList.add(NotificationCompatJellybean.writeActionAndGetExtras(b, action)); 98 } 99 100 @Override 101 public Notification.Builder getBuilder() { 102 return b; 103 } 104 105 public Notification build() { 106 SparseArray<Bundle> actionExtrasMap = NotificationCompatJellybean.buildActionExtrasMap( 107 mActionExtrasList); 108 if (actionExtrasMap != null) { 109 // Add the action extras sparse array if any action was added with extras. 110 mExtras.putSparseParcelableArray( 111 NotificationCompatJellybean.EXTRA_ACTION_EXTRAS, actionExtrasMap); 112 } 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