SampleSliceProvider.java revision bfeddbaf2e0d5707b101a91fa1287c93757ff4a9
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.GridBuilder;
33import androidx.app.slice.builders.ListBuilder;
34import androidx.app.slice.builders.MessagingSliceBuilder;
35
36/**
37 * Examples of using slice template builders.
38 */
39public class SampleSliceProvider extends SliceProvider {
40
41    public static final String ACTION_WIFI_CHANGED =
42            "com.example.androidx.slice.action.WIFI_CHANGED";
43    public static final String ACTION_TOAST =
44            "com.example.androidx.slice.action.TOAST";
45    public static final String EXTRA_TOAST_MESSAGE = "com.example.androidx.extra.TOAST_MESSAGE";
46
47    public static final String[] URI_PATHS = {"message", "wifi", "note", "ride", "toggle",
48            "toggle2", "contact", "gallery", "weather"};
49
50    /**
51     * @return Uri with the provided path
52     */
53    public static Uri getUri(String path, Context context) {
54        return new Uri.Builder()
55                .scheme(ContentResolver.SCHEME_CONTENT)
56                .authority(context.getPackageName())
57                .appendPath(path)
58                .build();
59    }
60
61    @Override
62    public boolean onCreateSliceProvider() {
63        return true;
64    }
65
66    @NonNull
67    @Override
68    public Uri onMapIntentToUri(Intent intent) {
69        return getUri("wifi", getContext());
70    }
71
72    @Override
73    public Slice onBindSlice(Uri sliceUri) {
74        String path = sliceUri.getPath();
75        switch (path) {
76            case "/message":
77                return createMessagingSlice(sliceUri);
78            case "/wifi":
79                return createWifiSlice(sliceUri);
80            case "/note":
81                return createNoteSlice(sliceUri);
82            case "/ride":
83                return createRideSlice(sliceUri);
84            case "/toggle":
85                return createCustomToggleSlice(sliceUri);
86            case "/toggle2":
87                return createTwoCustomToggleSlices(sliceUri);
88            case "/contact":
89                return createContact(sliceUri);
90            case "/gallery":
91                return createGallery(sliceUri);
92            case "/weather":
93                return createWeather(sliceUri);
94        }
95        throw new IllegalArgumentException("Unknown uri " + sliceUri);
96    }
97
98    private Slice createWeather(Uri sliceUri) {
99        return new GridBuilder(getContext(), sliceUri)
100                .addCell(cb -> cb
101                        .addLargeImage(Icon.createWithResource(getContext(), R.drawable.weather_1))
102                        .addText("MON")
103                        .addTitleText("69\u00B0"))
104                .addCell(cb -> cb
105                        .addLargeImage(Icon.createWithResource(getContext(), R.drawable.weather_2))
106                        .addText("TUE")
107                        .addTitleText("71\u00B0"))
108                .addCell(cb -> cb
109                        .addLargeImage(Icon.createWithResource(getContext(), R.drawable.weather_3))
110                        .addText("WED")
111                        .addTitleText("76\u00B0"))
112                .addCell(cb -> cb
113                        .addLargeImage(Icon.createWithResource(getContext(), R.drawable.weather_4))
114                        .addText("THU")
115                        .addTitleText("72\u00B0"))
116                .addCell(cb -> cb
117                        .addLargeImage(Icon.createWithResource(getContext(), R.drawable.weather_1))
118                        .addText("FRI")
119                        .addTitleText("68\u00B0"))
120                .build();
121    }
122
123    private Slice createGallery(Uri sliceUri) {
124        return new GridBuilder(getContext(), sliceUri)
125                .addCell(cb -> cb
126                    .addLargeImage(Icon.createWithResource(getContext(), R.drawable.slices_1)))
127                .addCell(cb -> cb
128                    .addLargeImage(Icon.createWithResource(getContext(), R.drawable.slices_2)))
129                .addCell(cb -> cb
130                    .addLargeImage(Icon.createWithResource(getContext(), R.drawable.slices_3)))
131                .addCell(cb -> cb
132                    .addLargeImage(Icon.createWithResource(getContext(), R.drawable.slices_4)))
133                .build();
134    }
135
136    private Slice createContact(Uri sliceUri) {
137        return new ListBuilder(getContext(), sliceUri)
138                .setColor(0xff3949ab)
139                .addRow(b -> b
140                        .setTitle("Mady Pitza")
141                        .setSubtitle("Frequently contacted contact")
142                        .setIsHeader(true)
143                        .addEndItem(Icon.createWithResource(getContext(), R.drawable.mady)))
144                .addGrid(b -> b
145                        .addCell(cb -> cb
146                            .addImage(Icon.createWithResource(getContext(), R.drawable.ic_call))
147                            .addText("Call")
148                            .setContentIntent(getBroadcastIntent(ACTION_TOAST, "call")))
149                        .addCell(cb -> cb
150                            .addImage(Icon.createWithResource(getContext(), R.drawable.ic_text))
151                            .addText("Text")
152                            .setContentIntent(getBroadcastIntent(ACTION_TOAST, "text")))
153                        .addCell(cb ->cb
154                            .addImage(Icon.createWithResource(getContext(), R.drawable.ic_video))
155                            .setContentIntent(getBroadcastIntent(ACTION_TOAST, "video"))
156                            .addText("Video"))
157                        .addCell(cb -> cb
158                            .addImage(Icon.createWithResource(getContext(), R.drawable.ic_email))
159                            .addText("Email")
160                            .setContentIntent(getBroadcastIntent(ACTION_TOAST, "email"))))
161                .build();
162    }
163
164    private Slice createMessagingSlice(Uri sliceUri) {
165        // TODO: Remote input.
166        return new MessagingSliceBuilder(getContext(), sliceUri)
167                .add(b -> b
168                        .addText("yo home \uD83C\uDF55, I emailed you the info")
169                        .addTimestamp(System.currentTimeMillis() - 20 * DateUtils.MINUTE_IN_MILLIS)
170                        .addSource(Icon.createWithResource(getContext(), R.drawable.mady)))
171                .add(b -> b
172                        .addText("just bought my tickets")
173                        .addTimestamp(System.currentTimeMillis() - 10 * DateUtils.MINUTE_IN_MILLIS))
174                .add(b -> b
175                        .addText("yay! can't wait for getContext() weekend!\n"
176                                + "\uD83D\uDE00")
177                        .addTimestamp(System.currentTimeMillis() - 5 * DateUtils.MINUTE_IN_MILLIS)
178                        .addSource(Icon.createWithResource(getContext(), R.drawable.mady)))
179                .build();
180
181    }
182
183    private Slice createNoteSlice(Uri sliceUri) {
184        // TODO: Remote input.
185        return new ListBuilder(getContext(), sliceUri)
186                .setColor(0xfff4b400)
187                .addRow(b -> b
188                    .setTitle("Create new note")
189                    .setSubtitle("with this note taking app")
190                    .addEndItem(Icon.createWithResource(getContext(), R.drawable.ic_create),
191                            getBroadcastIntent(ACTION_TOAST, "create note"))
192                    .addEndItem(Icon.createWithResource(getContext(), R.drawable.ic_voice),
193                            getBroadcastIntent(ACTION_TOAST, "voice note"))
194                    .addEndItem(Icon.createWithResource(getContext(), R.drawable.ic_camera),
195                            getIntent("android.media.action.IMAGE_CAPTURE")))
196                .build();
197    }
198
199    private Slice createRideSlice(Uri sliceUri) {
200        return new ListBuilder(getContext(), sliceUri)
201                .setColor(0xff1b5e20)
202                .addSummaryRow(b -> b
203                    .setTitle("Get ride")
204                    .setSubtitle("Multiple cars 4 minutes away")
205                    .addEndItem(Icon.createWithResource(getContext(), R.drawable.ic_home),
206                            getBroadcastIntent(ACTION_TOAST, "home"))
207                    .addEndItem(Icon.createWithResource(getContext(), R.drawable.ic_work),
208                            getBroadcastIntent(ACTION_TOAST, "work")))
209                .addRow(b -> b
210                    .setContentIntent(getBroadcastIntent(ACTION_TOAST, "work"))
211                    .setTitle("Work")
212                    .setSubtitle("2 min")
213                    .addEndItem(Icon.createWithResource(getContext(), R.drawable.ic_work)))
214                .addRow(b -> b
215                    .setContentIntent(getBroadcastIntent(ACTION_TOAST, "home"))
216                    .setTitle("Home")
217                    .setSubtitle("2 hours 33 min via 101")
218                    .addEndItem(Icon.createWithResource(getContext(), R.drawable.ic_home)))
219                .addRow(b -> b
220                    .setContentIntent(getBroadcastIntent(ACTION_TOAST, "book ride"))
221                    .setTitle("Book ride")
222                    .addEndItem(Icon.createWithResource(getContext(), R.drawable.ic_car)))
223                .build();
224    }
225
226    private Slice createCustomToggleSlice(Uri sliceUri) {
227        return new ListBuilder(getContext(), sliceUri)
228                .setColor(0xffff4081)
229                .addRow(b -> b
230                    .setTitle("Custom toggle")
231                    .setSubtitle("It can support two states")
232                    .addToggle(getBroadcastIntent(ACTION_TOAST, "star toggled"),
233                            true /* isChecked */,
234                            Icon.createWithResource(getContext(), R.drawable.toggle_star)))
235                .build();
236    }
237
238    private Slice createTwoCustomToggleSlices(Uri sliceUri) {
239        return new ListBuilder(getContext(), sliceUri)
240                .setColor(0xffff4081)
241                .addRow(b -> b
242                        .setTitle("2 toggles")
243                        .setSubtitle("each supports two states")
244                        .addToggle(getBroadcastIntent(ACTION_TOAST, "first star toggled"),
245                                true /* isChecked */,
246                                Icon.createWithResource(getContext(), R.drawable.toggle_star))
247                        .addToggle(getBroadcastIntent(ACTION_TOAST, "second star toggled"),
248                                false /* isChecked */,
249                                Icon.createWithResource(getContext(), R.drawable.toggle_star)))
250                .build();
251    }
252
253    private Slice createWifiSlice(Uri sliceUri) {
254        // Get wifi state
255        WifiManager wifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
256        int wifiState = wifiManager.getWifiState();
257        boolean wifiEnabled = false;
258        String state;
259        switch (wifiState) {
260            case WifiManager.WIFI_STATE_DISABLED:
261            case WifiManager.WIFI_STATE_DISABLING:
262                state = "disconnected";
263                break;
264            case WifiManager.WIFI_STATE_ENABLED:
265            case WifiManager.WIFI_STATE_ENABLING:
266                state = wifiManager.getConnectionInfo().getSSID();
267                wifiEnabled = true;
268                break;
269            case WifiManager.WIFI_STATE_UNKNOWN:
270            default:
271                state = ""; // just don't show anything?
272                break;
273        }
274        boolean finalWifiEnabled = wifiEnabled;
275        return new ListBuilder(getContext(), sliceUri)
276                .setColor(0xff4285f4)
277                .addRow(b -> b
278                    .setTitle("Wi-fi")
279                    .setTitleItem(Icon.createWithResource(getContext(), R.drawable.ic_wifi))
280                    .setSubtitle(state)
281                    .addToggle(getBroadcastIntent(ACTION_WIFI_CHANGED, null), finalWifiEnabled)
282                    .setContentIntent(getIntent(Settings.ACTION_WIFI_SETTINGS)))
283            .build();
284    }
285
286    private PendingIntent getIntent(String action) {
287        Intent intent = new Intent(action);
288        PendingIntent pi = PendingIntent.getActivity(getContext(), 0, intent, 0);
289        return pi;
290    }
291
292    private PendingIntent getBroadcastIntent(String action, String message) {
293        Intent intent = new Intent(action);
294        intent.setClass(getContext(), SliceBroadcastReceiver.class);
295        // Ensure a new PendingIntent is created for each message.
296        int requestCode = 0;
297        if (message != null) {
298            intent.putExtra(EXTRA_TOAST_MESSAGE, message);
299            requestCode = message.hashCode();
300        }
301        return PendingIntent.getBroadcast(getContext(), requestCode, intent,
302                PendingIntent.FLAG_UPDATE_CURRENT);
303    }
304}
305