SampleSliceProvider.java revision 49a1b3e1a2c2553e95c2d0267dda5cd132a31fd5
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.PendingIntent;
20import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.graphics.drawable.Icon;
24import android.net.Uri;
25import android.net.wifi.WifiManager;
26import android.provider.Settings;
27import android.support.annotation.NonNull;
28import android.text.format.DateUtils;
29
30import androidx.app.slice.Slice;
31import androidx.app.slice.SliceProvider;
32import androidx.app.slice.builders.ListBuilder;
33import androidx.app.slice.builders.MessagingSliceBuilder;
34
35/**
36 * Examples of using slice template builders.
37 */
38public class SampleSliceProvider extends SliceProvider {
39
40    public static final String ACTION_WIFI_CHANGED =
41            "com.example.androidx.slice.action.WIFI_CHANGED";
42    public static final String ACTION_TOAST =
43            "com.example.androidx.slice.action.TOAST";
44    public static final String EXTRA_TOAST_MESSAGE = "com.example.androidx.extra.TOAST_MESSAGE";
45
46    public static final String[] URI_PATHS = { "message", "wifi", "note", "ride", "toggle"};
47
48    /**
49     * @return Uri with the provided path
50     */
51    public static Uri getUri(String path, Context context) {
52        return new Uri.Builder()
53                .scheme(ContentResolver.SCHEME_CONTENT)
54                .authority(context.getPackageName())
55                .appendPath(path)
56                .build();
57    }
58
59    @Override
60    public boolean onCreateSliceProvider() {
61        return true;
62    }
63
64    @NonNull
65    @Override
66    public Uri onMapIntentToUri(Intent intent) {
67        return getUri("wifi", getContext());
68    }
69
70    @Override
71    public Slice onBindSlice(Uri sliceUri) {
72        String path = sliceUri.getPath();
73        switch (path) {
74            case "/message":
75                return createMessagingSlice(sliceUri);
76            case "/wifi":
77                return createWifiSlice(sliceUri);
78            case "/note":
79                return createNoteSlice(sliceUri);
80            case "/ride":
81                return createRideSlice(sliceUri);
82            case "/toggle":
83                return createCustomToggleSlice(sliceUri);
84        }
85        throw new IllegalArgumentException("Unknown uri " + sliceUri);
86    }
87
88    private Slice createMessagingSlice(Uri sliceUri) {
89        // TODO: Remote input.
90        return new MessagingSliceBuilder(sliceUri)
91                .add(b -> b
92                        .addText("yo home \uD83C\uDF55, I emailed you the info")
93                        .addTimestamp(System.currentTimeMillis() - 20 * DateUtils.MINUTE_IN_MILLIS)
94                        .addSource(Icon.createWithResource(getContext(), R.drawable.mady)))
95                .add(b -> b
96                        .addText("just bought my tickets")
97                        .addTimestamp(System.currentTimeMillis() - 10 * DateUtils.MINUTE_IN_MILLIS))
98                .add(b -> b
99                        .addText("yay! can't wait for getContext() weekend!\n"
100                                + "\uD83D\uDE00")
101                        .addTimestamp(System.currentTimeMillis() - 5 * DateUtils.MINUTE_IN_MILLIS)
102                        .addSource(Icon.createWithResource(getContext(), R.drawable.mady)))
103                .build();
104
105    }
106
107    private Slice createNoteSlice(Uri sliceUri) {
108        return new ListBuilder(sliceUri)
109                .setColor(0xfff4b400)
110                .add(b -> b
111                    .setTitle("Create new note")
112                    .setSubtitle("with this note taking app")
113                    .addEndItem(getBroadcastIntent(ACTION_TOAST, "create note"),
114                            Icon.createWithResource(getContext(), R.drawable.ic_create))
115                    .addEndItem(getBroadcastIntent(ACTION_TOAST, "voice note"),
116                            Icon.createWithResource(getContext(), R.drawable.ic_voice))
117                    .addEndItem(getIntent("android.media.action.IMAGE_CAPTURE"),
118                            Icon.createWithResource(getContext(), R.drawable.ic_camera)))
119                .build();
120    }
121
122    private Slice createRideSlice(Uri sliceUri) {
123        return new ListBuilder(sliceUri)
124                .setColor(0xff1b5e20)
125                .addSummaryRow(b -> b
126                    .setTitle("Get ride")
127                    .setSubtitle("Multiple cars 4 minutes away")
128                    .addEndItem(getBroadcastIntent(ACTION_TOAST, "home"),
129                            Icon.createWithResource(getContext(), R.drawable.ic_home))
130                    .addEndItem(getBroadcastIntent(ACTION_TOAST, "work"),
131                            Icon.createWithResource(getContext(), R.drawable.ic_work)))
132                .add(b -> b
133                    .setContentIntent(getBroadcastIntent(ACTION_TOAST, "work"))
134                    .setTitle("Work")
135                    .setSubtitle("2 min")
136                    .addEndItem(Icon.createWithResource(getContext(), R.drawable.ic_work)))
137                .add(b -> b
138                    .setContentIntent(getBroadcastIntent(ACTION_TOAST, "home"))
139                    .setTitle("Home")
140                    .setSubtitle("2 hours 33 min via 101")
141                    .addEndItem(Icon.createWithResource(getContext(), R.drawable.ic_home)))
142                .add(b -> b
143                    .setContentIntent(getBroadcastIntent(ACTION_TOAST, "book ride"))
144                    .setTitle("Book ride")
145                    .addEndItem(Icon.createWithResource(getContext(), R.drawable.ic_car)))
146                .build();
147    }
148
149    private Slice createCustomToggleSlice(Uri sliceUri) {
150        return new ListBuilder(sliceUri)
151                .setColor(0xffff4081)
152                .add(b -> b
153                    .setTitle("Custom toggle")
154                    .setSubtitle("It can support two states")
155                    .addToggle(getBroadcastIntent(ACTION_TOAST, "start toggled"),
156                            Icon.createWithResource(getContext(), R.drawable.toggle_star),
157                            true /* isChecked */))
158                .build();
159    }
160
161    private Slice createWifiSlice(Uri sliceUri) {
162        // Get wifi state
163        WifiManager wifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
164        int wifiState = wifiManager.getWifiState();
165        boolean wifiEnabled = false;
166        String state;
167        switch (wifiState) {
168            case WifiManager.WIFI_STATE_DISABLED:
169            case WifiManager.WIFI_STATE_DISABLING:
170                state = "disconnected";
171                break;
172            case WifiManager.WIFI_STATE_ENABLED:
173            case WifiManager.WIFI_STATE_ENABLING:
174                state = wifiManager.getConnectionInfo().getSSID();
175                wifiEnabled = true;
176                break;
177            case WifiManager.WIFI_STATE_UNKNOWN:
178            default:
179                state = ""; // just don't show anything?
180                break;
181        }
182        boolean finalWifiEnabled = wifiEnabled;
183        return new ListBuilder(sliceUri)
184                .setColor(0xff4285f4)
185                .add(b -> b
186                    .setTitle("Wi-fi")
187                    .setTitleItem(Icon.createWithResource(getContext(), R.drawable.ic_wifi))
188                    .setSubtitle(state)
189                    .addToggle(getBroadcastIntent(ACTION_WIFI_CHANGED, null), finalWifiEnabled)
190                    .setContentIntent(getIntent(Settings.ACTION_WIFI_SETTINGS)))
191            .build();
192    }
193
194    private PendingIntent getIntent(String action) {
195        Intent intent = new Intent(action);
196        PendingIntent pi = PendingIntent.getActivity(getContext(), 0, intent, 0);
197        return pi;
198    }
199
200    private PendingIntent getBroadcastIntent(String action, String message) {
201        Intent intent = new Intent(action);
202        if (message != null) {
203            intent.putExtra(EXTRA_TOAST_MESSAGE, message);
204        }
205        intent.setClass(getContext(), SliceBroadcastReceiver.class);
206        return PendingIntent.getBroadcast(getContext(), 0, intent,
207                PendingIntent.FLAG_CANCEL_CURRENT);
208    }
209}
210