1 /* 2 * Copyright 2017 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 androidx.slice.builders; 18 19 import static androidx.annotation.RestrictTo.Scope.LIBRARY; 20 import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP; 21 22 import android.content.Context; 23 import android.graphics.drawable.Icon; 24 import android.net.Uri; 25 import android.os.Build; 26 27 import androidx.annotation.NonNull; 28 import androidx.annotation.RequiresApi; 29 import androidx.annotation.RestrictTo; 30 import androidx.core.graphics.drawable.IconCompat; 31 import androidx.core.util.Consumer; 32 import androidx.slice.SliceSpecs; 33 import androidx.slice.builders.impl.MessagingBasicImpl; 34 import androidx.slice.builders.impl.MessagingBuilder; 35 import androidx.slice.builders.impl.MessagingListV1Impl; 36 import androidx.slice.builders.impl.MessagingV1Impl; 37 import androidx.slice.builders.impl.TemplateBuilderImpl; 38 39 /** 40 * Builder to construct slice content in a messaging format. 41 * @hide 42 */ 43 @RestrictTo(LIBRARY_GROUP) 44 public class MessagingSliceBuilder extends TemplateSliceBuilder { 45 46 /** 47 * The maximum number of messages that will be retained in the Slice itself (the 48 * number displayed is up to the platform). 49 */ 50 public static final int MAXIMUM_RETAINED_MESSAGES = 50; 51 52 private MessagingBuilder mBuilder; 53 54 /** 55 * Create a MessagingSliceBuilder with the specified uri. 56 */ 57 public MessagingSliceBuilder(@NonNull Context context, @NonNull Uri uri) { 58 super(context, uri); 59 } 60 61 /** 62 * Add a subslice to this builder. 63 */ 64 public MessagingSliceBuilder add(MessageBuilder builder) { 65 mBuilder.add((TemplateBuilderImpl) builder.mImpl); 66 return this; 67 } 68 69 /** 70 * Add a subslice to this builder. 71 */ 72 public MessagingSliceBuilder add(Consumer<MessageBuilder> c) { 73 MessageBuilder b = new MessageBuilder(this); 74 c.accept(b); 75 return add(b); 76 } 77 78 @Override 79 void setImpl(TemplateBuilderImpl impl) { 80 mBuilder = (MessagingBuilder) impl; 81 } 82 83 /** 84 * @hide 85 */ 86 @RestrictTo(LIBRARY) 87 @Override 88 protected TemplateBuilderImpl selectImpl() { 89 if (checkCompatible(SliceSpecs.MESSAGING)) { 90 return new MessagingV1Impl(getBuilder(), SliceSpecs.MESSAGING); 91 } else if (checkCompatible(SliceSpecs.LIST)) { 92 return new MessagingListV1Impl(getBuilder(), SliceSpecs.LIST); 93 } else if (checkCompatible(SliceSpecs.BASIC)) { 94 return new MessagingBasicImpl(getBuilder(), SliceSpecs.BASIC); 95 } 96 return null; 97 } 98 99 /** 100 * Builder for adding a message to {@link MessagingSliceBuilder}. 101 */ 102 public static final class MessageBuilder extends TemplateSliceBuilder { 103 104 private MessagingBuilder.MessageBuilder mImpl; 105 106 /** 107 * Creates a MessageBuilder with the specified parent. 108 */ 109 public MessageBuilder(MessagingSliceBuilder parent) { 110 super(parent.mBuilder.createMessageBuilder()); 111 } 112 113 /** 114 * Add the icon used to display contact in the messaging experience 115 */ 116 @RequiresApi(23) 117 public MessageBuilder addSource(Icon source) { 118 mImpl.addSource(source); 119 return this; 120 } 121 122 /** 123 * Add the icon used to display contact in the messaging experience 124 */ 125 public MessageBuilder addSource(IconCompat source) { 126 if (Build.VERSION.SDK_INT >= 23) { 127 mImpl.addSource(source.toIcon()); 128 } 129 return this; 130 } 131 132 /** 133 * Add the text to be used for this message. 134 */ 135 public MessageBuilder addText(CharSequence text) { 136 mImpl.addText(text); 137 return this; 138 } 139 140 /** 141 * Add the time at which this message arrived in ms since Unix epoch 142 */ 143 public MessageBuilder addTimestamp(long timestamp) { 144 mImpl.addTimestamp(timestamp); 145 return this; 146 } 147 148 @Override 149 void setImpl(TemplateBuilderImpl impl) { 150 mImpl = (MessagingBuilder.MessageBuilder) impl; 151 } 152 } 153 } 154