SampleSliceProvider.java revision 8a452e96e2308fe9515aa91b8e5b369eeefc25e7
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
17package com.example.androidx.slice.demos;
18
19import android.app.slice.Slice;
20import android.app.slice.SliceProvider;
21import android.graphics.drawable.Icon;
22import android.net.Uri;
23import android.support.annotation.RequiresApi;
24import android.text.format.DateUtils;
25
26import androidx.app.slice.builders.MessagingSliceBuilder;
27
28/**
29 * Examples of using slice template builders.
30 */
31@RequiresApi(api = 28)
32public class SampleSliceProvider extends SliceProvider {
33    public static final Uri MESSAGE =
34            Uri.parse("content://com.example.androidx.slice.demos/message");
35
36    @Override
37    public boolean onCreate() {
38        return true;
39    }
40
41    @Override
42    public Slice onBindSlice(Uri sliceUri) {
43        String path = sliceUri.getPath();
44        switch (path) {
45            case "/message":
46                return createMessagingSlice(sliceUri);
47        }
48        throw new IllegalArgumentException("Unknown uri " + sliceUri);
49    }
50
51    private Slice createMessagingSlice(Uri sliceUri) {
52        // TODO: Remote input.
53        return new MessagingSliceBuilder(sliceUri)
54                .startMessage()
55                        .addText("yo home \uD83C\uDF55, I emailed you the info")
56                        .addTimestamp(System.currentTimeMillis() - 20 * DateUtils.MINUTE_IN_MILLIS)
57                        .addSource(Icon.createWithResource(getContext(), R.drawable.mady))
58                        .endMessage()
59                .startMessage()
60                        .addText("just bought my tickets")
61                        .addTimestamp(System.currentTimeMillis() - 10 * DateUtils.MINUTE_IN_MILLIS)
62                        .endMessage()
63                .startMessage()
64                        .addText("yay! can't wait for getContext() weekend!\n"
65                                + "\uD83D\uDE00")
66                        .addTimestamp(System.currentTimeMillis() - 5 * DateUtils.MINUTE_IN_MILLIS)
67                        .addSource(Icon.createWithResource(getContext(), R.drawable.mady))
68                        .endMessage()
69                .build();
70
71    }
72}
73