summaryrefslogtreecommitdiff
path: root/androidx/app/slice/builders/MessagingSliceBuilder.java
blob: 434ab750ddcd983b682c7915e024096a8d1640b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.app.slice.builders;

import static android.support.annotation.RestrictTo.Scope.LIBRARY;
import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;

import android.content.Context;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.annotation.RestrictTo;

import java.util.function.Consumer;

import androidx.app.slice.Slice;
import androidx.app.slice.SliceSpecs;
import androidx.app.slice.builders.impl.MessagingBasicImpl;
import androidx.app.slice.builders.impl.MessagingBuilder;
import androidx.app.slice.builders.impl.MessagingListV1Impl;
import androidx.app.slice.builders.impl.MessagingV1Impl;
import androidx.app.slice.builders.impl.TemplateBuilderImpl;

/**
 * Builder to construct slice content in a messaging format.
 * @hide
 */
@RestrictTo(LIBRARY_GROUP)
public class MessagingSliceBuilder extends TemplateSliceBuilder {

    /**
     * The maximum number of messages that will be retained in the Slice itself (the
     * number displayed is up to the platform).
     */
    public static final int MAXIMUM_RETAINED_MESSAGES = 50;

    private MessagingBuilder mBuilder;

    /**
     */
    public MessagingSliceBuilder(@NonNull Uri uri) {
        super(uri);
        throw new RuntimeException("Stub, to be removed");
    }

    /**
     * @hide
     */
    @RestrictTo(LIBRARY_GROUP)
    public MessagingSliceBuilder(@NonNull Context context, @NonNull Uri uri) {
        super(context, uri);
    }

    /**
     * Add a subslice to this builder.
     */
    public MessagingSliceBuilder add(MessageBuilder builder) {
        mBuilder.add((TemplateBuilderImpl) builder.mImpl);
        return this;
    }

    /**
     * Add a subslice to this builder.
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    public MessagingSliceBuilder add(Consumer<MessageBuilder> c) {
        MessageBuilder b = new MessageBuilder(this);
        c.accept(b);
        return add(b);
    }

    @Override
    void setImpl(TemplateBuilderImpl impl) {
        mBuilder = (MessagingBuilder) impl;
    }

    /**
     * @hide
     */
    @RestrictTo(LIBRARY)
    @Override
    protected TemplateBuilderImpl selectImpl() {
        if (checkCompatible(SliceSpecs.MESSAGING)) {
            return new MessagingV1Impl(getBuilder(), SliceSpecs.MESSAGING);
        } else if (checkCompatible(SliceSpecs.LIST)) {
            return new MessagingListV1Impl(getBuilder(), SliceSpecs.LIST);
        } else if (checkCompatible(SliceSpecs.BASIC)) {
            return new MessagingBasicImpl(getBuilder(), SliceSpecs.BASIC);
        }
        return null;
    }

    /**
     * Builder for adding a message to {@link MessagingSliceBuilder}.
     */
    public static final class MessageBuilder extends TemplateSliceBuilder {

        private MessagingBuilder.MessageBuilder mImpl;

        /**
         * Creates a MessageBuilder with the specified parent.
         * @hide
         */
        @RestrictTo(LIBRARY_GROUP)
        public MessageBuilder(MessagingSliceBuilder parent) {
            super(parent.mBuilder.createMessageBuilder());
        }

        /**
         * Add the icon used to display contact in the messaging experience
         */
        public MessageBuilder addSource(Icon source) {
            mImpl.addSource(source);
            return this;
        }

        /**
         * Add the text to be used for this message.
         */
        public MessageBuilder addText(CharSequence text) {
            mImpl.addText(text);
            return this;
        }

        /**
         * Add the time at which this message arrived in ms since Unix epoch
         */
        public MessageBuilder addTimestamp(long timestamp) {
            mImpl.addTimestamp(timestamp);
            return this;
        }

        @Override
        void setImpl(TemplateBuilderImpl impl) {
            mImpl = (MessagingBuilder.MessageBuilder) impl;
        }

        /**
         */
        public void apply(Slice.Builder builder) {
            throw new RuntimeException("Stub, to be removed");
        }
    }
}