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.support.annotation.RequiresApi; 25 import android.util.SparseArray; 26 import android.widget.RemoteViews; 27 28 import java.util.ArrayList; 29 import java.util.List; 30 31 @RequiresApi(19) 32 class NotificationCompatKitKat { 33 public static class Builder implements NotificationBuilderWithBuilderAccessor, 34 NotificationBuilderWithActions { 35 private Notification.Builder b; 36 private Bundle mExtras; 37 private List<Bundle> mActionExtrasList = new ArrayList<Bundle>(); 38 private RemoteViews mContentView; 39 private RemoteViews mBigContentView; 40 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, RemoteViews contentView, RemoteViews bigContentView)41 public Builder(Context context, Notification n, 42 CharSequence contentTitle, CharSequence contentText, CharSequence contentInfo, 43 RemoteViews tickerView, int number, 44 PendingIntent contentIntent, PendingIntent fullScreenIntent, Bitmap largeIcon, 45 int progressMax, int progress, boolean progressIndeterminate, boolean showWhen, 46 boolean useChronometer, int priority, CharSequence subText, boolean localOnly, 47 ArrayList<String> people, Bundle extras, String groupKey, boolean groupSummary, 48 String sortKey, RemoteViews contentView, RemoteViews bigContentView) { 49 b = new Notification.Builder(context) 50 .setWhen(n.when) 51 .setShowWhen(showWhen) 52 .setSmallIcon(n.icon, n.iconLevel) 53 .setContent(n.contentView) 54 .setTicker(n.tickerText, tickerView) 55 .setSound(n.sound, n.audioStreamType) 56 .setVibrate(n.vibrate) 57 .setLights(n.ledARGB, n.ledOnMS, n.ledOffMS) 58 .setOngoing((n.flags & Notification.FLAG_ONGOING_EVENT) != 0) 59 .setOnlyAlertOnce((n.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0) 60 .setAutoCancel((n.flags & Notification.FLAG_AUTO_CANCEL) != 0) 61 .setDefaults(n.defaults) 62 .setContentTitle(contentTitle) 63 .setContentText(contentText) 64 .setSubText(subText) 65 .setContentInfo(contentInfo) 66 .setContentIntent(contentIntent) 67 .setDeleteIntent(n.deleteIntent) 68 .setFullScreenIntent(fullScreenIntent, 69 (n.flags & Notification.FLAG_HIGH_PRIORITY) != 0) 70 .setLargeIcon(largeIcon) 71 .setNumber(number) 72 .setUsesChronometer(useChronometer) 73 .setPriority(priority) 74 .setProgress(progressMax, progress, progressIndeterminate); 75 mExtras = new Bundle(); 76 if (extras != null) { 77 mExtras.putAll(extras); 78 } 79 if (people != null && !people.isEmpty()) { 80 mExtras.putStringArray(Notification.EXTRA_PEOPLE, 81 people.toArray(new String[people.size()])); 82 } 83 if (localOnly) { 84 mExtras.putBoolean(NotificationCompatJellybean.EXTRA_LOCAL_ONLY, true); 85 } 86 if (groupKey != null) { 87 mExtras.putString(NotificationCompatJellybean.EXTRA_GROUP_KEY, groupKey); 88 if (groupSummary) { 89 mExtras.putBoolean(NotificationCompatJellybean.EXTRA_GROUP_SUMMARY, true); 90 } else { 91 mExtras.putBoolean(NotificationCompatJellybean.EXTRA_USE_SIDE_CHANNEL, true); 92 } 93 } 94 if (sortKey != null) { 95 mExtras.putString(NotificationCompatJellybean.EXTRA_SORT_KEY, sortKey); 96 } 97 mContentView = contentView; 98 mBigContentView = bigContentView; 99 } 100 101 @Override addAction(NotificationCompatBase.Action action)102 public void addAction(NotificationCompatBase.Action action) { 103 mActionExtrasList.add(NotificationCompatJellybean.writeActionAndGetExtras(b, action)); 104 } 105 106 @Override getBuilder()107 public Notification.Builder getBuilder() { 108 return b; 109 } 110 111 @Override build()112 public Notification build() { 113 SparseArray<Bundle> actionExtrasMap = NotificationCompatJellybean.buildActionExtrasMap( 114 mActionExtrasList); 115 if (actionExtrasMap != null) { 116 // Add the action extras sparse array if any action was added with extras. 117 mExtras.putSparseParcelableArray( 118 NotificationCompatJellybean.EXTRA_ACTION_EXTRAS, actionExtrasMap); 119 } 120 b.setExtras(mExtras); 121 Notification notification = b.build(); 122 if (mContentView != null) { 123 notification.contentView = mContentView; 124 } 125 if (mBigContentView != null) { 126 notification.bigContentView = mBigContentView; 127 } 128 return notification; 129 } 130 } 131 getAction(Notification notif, int actionIndex, NotificationCompatBase.Action.Factory factory, RemoteInputCompatBase.RemoteInput.Factory remoteInputFactory)132 public static NotificationCompatBase.Action getAction(Notification notif, 133 int actionIndex, NotificationCompatBase.Action.Factory factory, 134 RemoteInputCompatBase.RemoteInput.Factory remoteInputFactory) { 135 Notification.Action action = notif.actions[actionIndex]; 136 Bundle actionExtras = null; 137 SparseArray<Bundle> actionExtrasMap = notif.extras.getSparseParcelableArray( 138 NotificationCompatJellybean.EXTRA_ACTION_EXTRAS); 139 if (actionExtrasMap != null) { 140 actionExtras = actionExtrasMap.get(actionIndex); 141 } 142 return NotificationCompatJellybean.readAction(factory, remoteInputFactory, 143 action.icon, action.title, action.actionIntent, actionExtras); 144 } 145 } 146