blob: 11d9acb237097513c1d6dc474dffe6b786ea5495 [file] [log] [blame]
Jason Monk8a452e92017-10-31 19:21:47 -04001/*
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
Mady Mellordb8aa8c2017-11-08 14:26:17 -080019import android.app.PendingIntent;
Mady Mellorc1334182017-11-10 15:50:35 -080020import android.content.ContentResolver;
Mady Mellordb8aa8c2017-11-08 14:26:17 -080021import android.content.Context;
22import android.content.Intent;
Jason Monk8a452e92017-10-31 19:21:47 -040023import android.graphics.drawable.Icon;
24import android.net.Uri;
Mady Mellordb8aa8c2017-11-08 14:26:17 -080025import android.net.wifi.WifiManager;
26import android.provider.Settings;
Jason Monked974952017-11-27 13:48:04 -050027import android.support.annotation.NonNull;
Jason Monk8a452e92017-10-31 19:21:47 -040028import android.text.format.DateUtils;
29
Jason Monkdcb5e2f2017-11-15 20:19:43 -050030import androidx.app.slice.Slice;
31import androidx.app.slice.SliceProvider;
Mady Mellordb971ff2017-11-20 11:03:43 -080032import androidx.app.slice.builders.GridBuilder;
Mady Mellorc1334182017-11-10 15:50:35 -080033import androidx.app.slice.builders.ListBuilder;
Jason Monk8a452e92017-10-31 19:21:47 -040034import androidx.app.slice.builders.MessagingSliceBuilder;
35
36/**
37 * Examples of using slice template builders.
38 */
Jason Monk8a452e92017-10-31 19:21:47 -040039public class SampleSliceProvider extends SliceProvider {
Mady Mellordb8aa8c2017-11-08 14:26:17 -080040
41 public static final String ACTION_WIFI_CHANGED =
Mady Mellorc1334182017-11-10 15:50:35 -080042 "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
Mady Mellordb971ff2017-11-20 11:03:43 -080047 public static final String[] URI_PATHS = {"message", "wifi", "note", "ride", "toggle",
Amin Shaikhbfeddba2018-01-10 14:51:13 -050048 "toggle2", "contact", "gallery", "weather"};
Mady Mellorc1334182017-11-10 15:50:35 -080049
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 }
Jason Monk8a452e92017-10-31 19:21:47 -040060
61 @Override
Jason Monkdcb5e2f2017-11-15 20:19:43 -050062 public boolean onCreateSliceProvider() {
Jason Monk8a452e92017-10-31 19:21:47 -040063 return true;
64 }
65
Jason Monked974952017-11-27 13:48:04 -050066 @NonNull
67 @Override
68 public Uri onMapIntentToUri(Intent intent) {
Mady Mellorc1334182017-11-10 15:50:35 -080069 return getUri("wifi", getContext());
Jason Monked974952017-11-27 13:48:04 -050070 }
71
Jason Monk8a452e92017-10-31 19:21:47 -040072 @Override
73 public Slice onBindSlice(Uri sliceUri) {
74 String path = sliceUri.getPath();
75 switch (path) {
76 case "/message":
77 return createMessagingSlice(sliceUri);
Mady Mellordb8aa8c2017-11-08 14:26:17 -080078 case "/wifi":
Mady Mellorc1334182017-11-10 15:50:35 -080079 return createWifiSlice(sliceUri);
80 case "/note":
81 return createNoteSlice(sliceUri);
82 case "/ride":
83 return createRideSlice(sliceUri);
84 case "/toggle":
85 return createCustomToggleSlice(sliceUri);
Amin Shaikhbfeddba2018-01-10 14:51:13 -050086 case "/toggle2":
87 return createTwoCustomToggleSlices(sliceUri);
Mady Mellordb971ff2017-11-20 11:03:43 -080088 case "/contact":
89 return createContact(sliceUri);
90 case "/gallery":
91 return createGallery(sliceUri);
92 case "/weather":
93 return createWeather(sliceUri);
Jason Monk8a452e92017-10-31 19:21:47 -040094 }
95 throw new IllegalArgumentException("Unknown uri " + sliceUri);
96 }
97
Mady Mellordb971ff2017-11-20 11:03:43 -080098 private Slice createWeather(Uri sliceUri) {
Jason Monka09cb672018-01-08 13:17:36 -050099 return new GridBuilder(getContext(), sliceUri)
Mady Mellordb971ff2017-11-20 11:03:43 -0800100 .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) {
Jason Monka09cb672018-01-08 13:17:36 -0500124 return new GridBuilder(getContext(), sliceUri)
Mady Mellordb971ff2017-11-20 11:03:43 -0800125 .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) {
Jason Monka09cb672018-01-08 13:17:36 -0500137 return new ListBuilder(getContext(), sliceUri)
Mady Mellordb971ff2017-11-20 11:03:43 -0800138 .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
Jason Monk8a452e92017-10-31 19:21:47 -0400164 private Slice createMessagingSlice(Uri sliceUri) {
165 // TODO: Remote input.
Jason Monka09cb672018-01-08 13:17:36 -0500166 return new MessagingSliceBuilder(getContext(), sliceUri)
Mady Mellorc1334182017-11-10 15:50:35 -0800167 .add(b -> b
Jason Monk8a452e92017-10-31 19:21:47 -0400168 .addText("yo home \uD83C\uDF55, I emailed you the info")
169 .addTimestamp(System.currentTimeMillis() - 20 * DateUtils.MINUTE_IN_MILLIS)
Mady Mellorc1334182017-11-10 15:50:35 -0800170 .addSource(Icon.createWithResource(getContext(), R.drawable.mady)))
171 .add(b -> b
Jason Monk8a452e92017-10-31 19:21:47 -0400172 .addText("just bought my tickets")
Mady Mellorc1334182017-11-10 15:50:35 -0800173 .addTimestamp(System.currentTimeMillis() - 10 * DateUtils.MINUTE_IN_MILLIS))
174 .add(b -> b
Jason Monk8a452e92017-10-31 19:21:47 -0400175 .addText("yay! can't wait for getContext() weekend!\n"
176 + "\uD83D\uDE00")
177 .addTimestamp(System.currentTimeMillis() - 5 * DateUtils.MINUTE_IN_MILLIS)
Mady Mellorc1334182017-11-10 15:50:35 -0800178 .addSource(Icon.createWithResource(getContext(), R.drawable.mady)))
Jason Monk8a452e92017-10-31 19:21:47 -0400179 .build();
180
181 }
Mady Mellordb8aa8c2017-11-08 14:26:17 -0800182
Mady Mellorc1334182017-11-10 15:50:35 -0800183 private Slice createNoteSlice(Uri sliceUri) {
Mady Mellordb971ff2017-11-20 11:03:43 -0800184 // TODO: Remote input.
Jason Monka09cb672018-01-08 13:17:36 -0500185 return new ListBuilder(getContext(), sliceUri)
Mady Mellorc1334182017-11-10 15:50:35 -0800186 .setColor(0xfff4b400)
Mady Mellordb971ff2017-11-20 11:03:43 -0800187 .addRow(b -> b
Mady Mellorc1334182017-11-10 15:50:35 -0800188 .setTitle("Create new note")
189 .setSubtitle("with this note taking app")
Mady Mellordb971ff2017-11-20 11:03:43 -0800190 .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")))
Mady Mellorc1334182017-11-10 15:50:35 -0800196 .build();
197 }
198
199 private Slice createRideSlice(Uri sliceUri) {
Jason Monka09cb672018-01-08 13:17:36 -0500200 return new ListBuilder(getContext(), sliceUri)
Mady Mellorc1334182017-11-10 15:50:35 -0800201 .setColor(0xff1b5e20)
Mady Mellor49a1b3e2017-12-11 14:15:14 -0800202 .addSummaryRow(b -> b
203 .setTitle("Get ride")
204 .setSubtitle("Multiple cars 4 minutes away")
Mady Mellordb971ff2017-11-20 11:03:43 -0800205 .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
Mady Mellorc1334182017-11-10 15:50:35 -0800210 .setContentIntent(getBroadcastIntent(ACTION_TOAST, "work"))
211 .setTitle("Work")
212 .setSubtitle("2 min")
213 .addEndItem(Icon.createWithResource(getContext(), R.drawable.ic_work)))
Mady Mellordb971ff2017-11-20 11:03:43 -0800214 .addRow(b -> b
Mady Mellorc1334182017-11-10 15:50:35 -0800215 .setContentIntent(getBroadcastIntent(ACTION_TOAST, "home"))
216 .setTitle("Home")
217 .setSubtitle("2 hours 33 min via 101")
Mady Mellor49a1b3e2017-12-11 14:15:14 -0800218 .addEndItem(Icon.createWithResource(getContext(), R.drawable.ic_home)))
Mady Mellordb971ff2017-11-20 11:03:43 -0800219 .addRow(b -> b
Mady Mellorc1334182017-11-10 15:50:35 -0800220 .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) {
Jason Monka09cb672018-01-08 13:17:36 -0500227 return new ListBuilder(getContext(), sliceUri)
Mady Mellorc1334182017-11-10 15:50:35 -0800228 .setColor(0xffff4081)
Mady Mellordb971ff2017-11-20 11:03:43 -0800229 .addRow(b -> b
Mady Mellorc1334182017-11-10 15:50:35 -0800230 .setTitle("Custom toggle")
231 .setSubtitle("It can support two states")
Amin Shaikhd264a862018-01-08 15:26:17 -0500232 .addToggle(getBroadcastIntent(ACTION_TOAST, "star toggled"),
Mady Mellordb971ff2017-11-20 11:03:43 -0800233 true /* isChecked */,
234 Icon.createWithResource(getContext(), R.drawable.toggle_star)))
Mady Mellorc1334182017-11-10 15:50:35 -0800235 .build();
236 }
237
Amin Shaikhbfeddba2018-01-10 14:51:13 -0500238 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
Mady Mellorc1334182017-11-10 15:50:35 -0800253 private Slice createWifiSlice(Uri sliceUri) {
Mady Mellordb8aa8c2017-11-08 14:26:17 -0800254 // Get wifi state
Mady Mellordb8aa8c2017-11-08 14:26:17 -0800255 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 }
Mady Mellorc1334182017-11-10 15:50:35 -0800274 boolean finalWifiEnabled = wifiEnabled;
Jason Monka09cb672018-01-08 13:17:36 -0500275 return new ListBuilder(getContext(), sliceUri)
Mady Mellorc1334182017-11-10 15:50:35 -0800276 .setColor(0xff4285f4)
Mady Mellordb971ff2017-11-20 11:03:43 -0800277 .addRow(b -> b
Mady Mellorc1334182017-11-10 15:50:35 -0800278 .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();
Mady Mellordb8aa8c2017-11-08 14:26:17 -0800284 }
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
Mady Mellorc1334182017-11-10 15:50:35 -0800292 private PendingIntent getBroadcastIntent(String action, String message) {
Mady Mellordb8aa8c2017-11-08 14:26:17 -0800293 Intent intent = new Intent(action);
Amin Shaikh3831ee62018-01-09 13:02:21 -0500294 intent.setClass(getContext(), SliceBroadcastReceiver.class);
295 // Ensure a new PendingIntent is created for each message.
296 int requestCode = 0;
Mady Mellorc1334182017-11-10 15:50:35 -0800297 if (message != null) {
298 intent.putExtra(EXTRA_TOAST_MESSAGE, message);
Amin Shaikh3831ee62018-01-09 13:02:21 -0500299 requestCode = message.hashCode();
Mady Mellorc1334182017-11-10 15:50:35 -0800300 }
Amin Shaikh3831ee62018-01-09 13:02:21 -0500301 return PendingIntent.getBroadcast(getContext(), requestCode, intent,
302 PendingIntent.FLAG_UPDATE_CURRENT);
Mady Mellordb8aa8c2017-11-08 14:26:17 -0800303 }
Jason Monk8a452e92017-10-31 19:21:47 -0400304}