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 17 package android.support.v4.app; 18 19 import android.app.Notification; 20 import android.app.PendingIntent; 21 import android.content.Context; 22 import android.graphics.Bitmap; 23 import android.os.Bundle; 24 import android.util.SparseArray; 25 import android.widget.RemoteViews; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 30 class 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 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, ArrayList<String> people, Bundle extras, String groupKey, boolean groupSummary, String sortKey)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 addAction(NotificationCompatBase.Action action)96 public void addAction(NotificationCompatBase.Action action) { 97 mActionExtrasList.add(NotificationCompatJellybean.writeActionAndGetExtras(b, action)); 98 } 99 100 @Override getBuilder()101 public Notification.Builder getBuilder() { 102 return b; 103 } 104 105 @Override build()106 public Notification build() { 107 SparseArray<Bundle> actionExtrasMap = NotificationCompatJellybean.buildActionExtrasMap( 108 mActionExtrasList); 109 if (actionExtrasMap != null) { 110 // Add the action extras sparse array if any action was added with extras. 111 mExtras.putSparseParcelableArray( 112 NotificationCompatJellybean.EXTRA_ACTION_EXTRAS, actionExtrasMap); 113 } 114 b.setExtras(mExtras); 115 return b.build(); 116 } 117 } 118 getExtras(Notification notif)119 public static Bundle getExtras(Notification notif) { 120 return notif.extras; 121 } 122 getActionCount(Notification notif)123 public static int getActionCount(Notification notif) { 124 return notif.actions != null ? notif.actions.length : 0; 125 } 126 getAction(Notification notif, int actionIndex, NotificationCompatBase.Action.Factory factory, RemoteInputCompatBase.RemoteInput.Factory remoteInputFactory)127 public static NotificationCompatBase.Action getAction(Notification notif, 128 int actionIndex, NotificationCompatBase.Action.Factory factory, 129 RemoteInputCompatBase.RemoteInput.Factory remoteInputFactory) { 130 Notification.Action action = notif.actions[actionIndex]; 131 Bundle actionExtras = null; 132 SparseArray<Bundle> actionExtrasMap = notif.extras.getSparseParcelableArray( 133 NotificationCompatJellybean.EXTRA_ACTION_EXTRAS); 134 if (actionExtrasMap != null) { 135 actionExtras = actionExtrasMap.get(actionIndex); 136 } 137 return NotificationCompatJellybean.readAction(factory, remoteInputFactory, 138 action.icon, action.title, action.actionIntent, actionExtras); 139 } 140 getLocalOnly(Notification notif)141 public static boolean getLocalOnly(Notification notif) { 142 return notif.extras.getBoolean(NotificationCompatJellybean.EXTRA_LOCAL_ONLY); 143 } 144 getGroup(Notification notif)145 public static String getGroup(Notification notif) { 146 return notif.extras.getString(NotificationCompatJellybean.EXTRA_GROUP_KEY); 147 } 148 isGroupSummary(Notification notif)149 public static boolean isGroupSummary(Notification notif) { 150 return notif.extras.getBoolean(NotificationCompatJellybean.EXTRA_GROUP_SUMMARY); 151 } 152 getSortKey(Notification notif)153 public static String getSortKey(Notification notif) { 154 return notif.extras.getString(NotificationCompatJellybean.EXTRA_SORT_KEY); 155 } 156 } 157