blob: ef96a275ab72dd68aa6389ca105af70d904f718b [file] [log] [blame]
Jason Monk8f8784c2018-01-22 21:29:42 -05001/*
2 * Copyright 2018 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
Aurimas Liutikas85ef1442018-03-02 14:16:21 -080017package androidx.slice.render;
Jason Monk8f8784c2018-01-22 21:29:42 -050018
19import static android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
20
Mady Mellor588bcc12018-03-02 13:56:51 -080021import static androidx.slice.builders.ListBuilder.ICON_IMAGE;
22import static androidx.slice.builders.ListBuilder.LARGE_IMAGE;
23import static androidx.slice.builders.ListBuilder.SMALL_IMAGE;
24
Jason Monk8f8784c2018-01-22 21:29:42 -050025import android.app.PendingIntent;
26import android.content.ContentResolver;
27import android.content.Context;
28import android.content.Intent;
29import android.graphics.drawable.Icon;
30import android.net.Uri;
31import android.net.wifi.WifiManager;
32import android.provider.Settings;
33import android.text.SpannableString;
34import android.text.format.DateUtils;
35import android.text.style.ForegroundColorSpan;
36
Mady Mellorbb51b592018-03-08 17:07:24 -080037import java.util.Arrays;
38
Aurimas Liutikas85ef1442018-03-02 14:16:21 -080039import androidx.slice.Slice;
40import androidx.slice.builders.GridBuilder;
41import androidx.slice.builders.ListBuilder;
42import androidx.slice.builders.MessagingSliceBuilder;
43import androidx.slice.builders.SliceAction;
44import androidx.slice.view.test.R;
Jason Monk8f8784c2018-01-22 21:29:42 -050045
46/**
47 * Examples of using slice template builders.
48 */
49public class SliceCreator {
50
51 public static final String ACTION_WIFI_CHANGED =
52 "com.example.androidx.slice.action.WIFI_CHANGED";
53 public static final String ACTION_TOAST =
54 "com.example.androidx.slice.action.TOAST";
55 public static final String EXTRA_TOAST_MESSAGE = "com.example.androidx.extra.TOAST_MESSAGE";
56
Mady Mellor53380fe2018-02-14 16:31:38 -080057 public static final String[] URI_PATHS = {"message", "wifi", "wifi2", "note", "ride", "toggle",
Mady Mellor55312b32018-02-22 19:02:15 -080058 "toggle2", "contact", "gallery", "subscription", "subscription2", "weather"};
Jason Monk8f8784c2018-01-22 21:29:42 -050059
60 private final Context mContext;
61
62 public SliceCreator(Context context) {
63 mContext = context;
64 }
65
66 private Context getContext() {
67 return mContext;
68 }
69
70 /**
71 * @return Uri with the provided path
72 */
73 public static Uri getUri(String path, Context context) {
74 return new Uri.Builder()
75 .scheme(ContentResolver.SCHEME_CONTENT)
Aurimas Liutikas85ef1442018-03-02 14:16:21 -080076 .authority("androidx.slice.view.test")
Jason Monk8f8784c2018-01-22 21:29:42 -050077 .appendPath(path)
78 .build();
79 }
80
81 public Slice onBindSlice(Uri sliceUri) {
82 String path = sliceUri.getPath();
83 switch (path) {
84 case "/message":
85 return createMessagingSlice(sliceUri);
86 case "/wifi":
Mady Mellor53380fe2018-02-14 16:31:38 -080087 return createWifiSlice(sliceUri, false /* customSeeMore */);
88 case "/wifi2":
89 return createWifiSlice(sliceUri, true /* customSeeMore */);
Jason Monk8f8784c2018-01-22 21:29:42 -050090 case "/note":
91 return createNoteSlice(sliceUri);
92 case "/ride":
93 return createRideSlice(sliceUri);
94 case "/toggle":
95 return createCustomToggleSlice(sliceUri);
96 case "/toggle2":
97 return createTwoCustomToggleSlices(sliceUri);
98 case "/contact":
99 return createContact(sliceUri);
100 case "/gallery":
101 return createGallery(sliceUri);
Mady Mellor55312b32018-02-22 19:02:15 -0800102 case "/subscription":
103 return createSubSlice(sliceUri, false /* customSeeMore */);
104 case "/subscription2":
105 return createSubSlice(sliceUri, true /* customSeeMore */);
Jason Monk8f8784c2018-01-22 21:29:42 -0500106 case "/weather":
107 return createWeather(sliceUri);
108 }
109 throw new IllegalArgumentException("Unknown uri " + sliceUri);
110 }
111
112 private Slice createWeather(Uri sliceUri) {
Mady Mellorea77b882018-02-12 11:49:19 -0800113 SliceAction primaryAction = new SliceAction(getBroadcastIntent(ACTION_TOAST,
114 "open weather app"), Icon.createWithResource(getContext(), R.drawable.weather_1),
115 "Weather is happening!");
Mady Mellor9b0a49d2018-02-13 13:55:06 -0800116 ListBuilder b = new ListBuilder(getContext(), sliceUri);
117 GridBuilder gb = new GridBuilder(b);
118 gb.setPrimaryAction(primaryAction);
119 gb.addCell(new GridBuilder.CellBuilder(gb)
Mady Mellor5ca9e162018-02-22 15:19:50 -0800120 .addImage(Icon.createWithResource(getContext(), R.drawable.weather_1),
Mady Mellor588bcc12018-03-02 13:56:51 -0800121 SMALL_IMAGE)
Jason Monk8f8784c2018-01-22 21:29:42 -0500122 .addText("MON")
123 .addTitleText("69\u00B0"))
Mady Mellor9b0a49d2018-02-13 13:55:06 -0800124 .addCell(new GridBuilder.CellBuilder(gb)
Mady Mellor5ca9e162018-02-22 15:19:50 -0800125 .addImage(Icon.createWithResource(getContext(), R.drawable.weather_2),
Mady Mellor588bcc12018-03-02 13:56:51 -0800126 SMALL_IMAGE)
Jason Monk8f8784c2018-01-22 21:29:42 -0500127 .addText("TUE")
128 .addTitleText("71\u00B0"))
Mady Mellor9b0a49d2018-02-13 13:55:06 -0800129 .addCell(new GridBuilder.CellBuilder(gb)
Mady Mellor5ca9e162018-02-22 15:19:50 -0800130 .addImage(Icon.createWithResource(getContext(), R.drawable.weather_3),
Mady Mellor588bcc12018-03-02 13:56:51 -0800131 SMALL_IMAGE)
Jason Monk8f8784c2018-01-22 21:29:42 -0500132 .addText("WED")
133 .addTitleText("76\u00B0"))
Mady Mellor9b0a49d2018-02-13 13:55:06 -0800134 .addCell(new GridBuilder.CellBuilder(gb)
Mady Mellor5ca9e162018-02-22 15:19:50 -0800135 .addImage(Icon.createWithResource(getContext(), R.drawable.weather_4),
Mady Mellor588bcc12018-03-02 13:56:51 -0800136 SMALL_IMAGE)
Jason Monk8f8784c2018-01-22 21:29:42 -0500137 .addText("THU")
138 .addTitleText("72\u00B0"))
Mady Mellor9b0a49d2018-02-13 13:55:06 -0800139 .addCell(new GridBuilder.CellBuilder(gb)
Mady Mellor5ca9e162018-02-22 15:19:50 -0800140 .addImage(Icon.createWithResource(getContext(), R.drawable.weather_1),
Mady Mellor588bcc12018-03-02 13:56:51 -0800141 SMALL_IMAGE)
Jason Monk8f8784c2018-01-22 21:29:42 -0500142 .addText("FRI")
Mady Mellor9b0a49d2018-02-13 13:55:06 -0800143 .addTitleText("68\u00B0"));
144 return b.addGrid(gb).build();
Jason Monk8f8784c2018-01-22 21:29:42 -0500145 }
146
147 private Slice createGallery(Uri sliceUri) {
Mady Mellor9b0a49d2018-02-13 13:55:06 -0800148 ListBuilder b = new ListBuilder(getContext(), sliceUri);
149 GridBuilder gb = new GridBuilder(b);
Mady Mellor55312b32018-02-22 19:02:15 -0800150 PendingIntent pi = getBroadcastIntent(ACTION_TOAST, "see more of your gallery");
151 gb.addSeeMoreAction(pi);
152 gb.addCell(new GridBuilder.CellBuilder(gb)
153 .addImage(Icon.createWithResource(getContext(), R.drawable.slices_1),
154 LARGE_IMAGE))
Mady Mellor9b0a49d2018-02-13 13:55:06 -0800155 .addCell(new GridBuilder.CellBuilder(gb)
Mady Mellor55312b32018-02-22 19:02:15 -0800156 .addImage(Icon.createWithResource(getContext(), R.drawable.slices_2),
157 LARGE_IMAGE))
Mady Mellor9b0a49d2018-02-13 13:55:06 -0800158 .addCell(new GridBuilder.CellBuilder(gb)
Mady Mellor55312b32018-02-22 19:02:15 -0800159 .addImage(Icon.createWithResource(getContext(), R.drawable.slices_3),
160 LARGE_IMAGE))
Mady Mellor9b0a49d2018-02-13 13:55:06 -0800161 .addCell(new GridBuilder.CellBuilder(gb)
Mady Mellor55312b32018-02-22 19:02:15 -0800162 .addImage(Icon.createWithResource(getContext(), R.drawable.slices_4),
163 LARGE_IMAGE))
164 .addCell(new GridBuilder.CellBuilder(gb)
165 .addImage(Icon.createWithResource(getContext(), R.drawable.slices_2),
166 LARGE_IMAGE))
167 .addCell(new GridBuilder.CellBuilder(gb)
168 .addImage(Icon.createWithResource(getContext(), R.drawable.slices_3),
169 LARGE_IMAGE))
170 .addCell(new GridBuilder.CellBuilder(gb)
171 .addImage(Icon.createWithResource(getContext(), R.drawable.slices_4),
172 LARGE_IMAGE));
173 return b.addGrid(gb).build();
174 }
175
176 private Slice createSubSlice(Uri sliceUri, boolean customSeeMore) {
177 ListBuilder b = new ListBuilder(getContext(), sliceUri);
178 GridBuilder gb = new GridBuilder(b);
179 GridBuilder.CellBuilder cb = new GridBuilder.CellBuilder(gb);
180 PendingIntent pi = getBroadcastIntent(ACTION_TOAST, "See cats you follow");
181 if (customSeeMore) {
182 cb.addImage(Icon.createWithResource(getContext(), R.drawable.ic_right_caret),
183 ICON_IMAGE);
184 cb.setContentIntent(pi);
185 cb.addText("All cats");
186 gb.addSeeMoreCell(cb);
187 } else {
188 gb.addSeeMoreAction(pi);
189 }
190 gb.addCell(new GridBuilder.CellBuilder(gb)
191 .addImage(Icon.createWithResource(getContext(), R.drawable.cat_1),
192 SMALL_IMAGE)
193 .addTitleText("Oreo"))
194 .addCell(new GridBuilder.CellBuilder(gb)
195 .addImage(Icon.createWithResource(getContext(), R.drawable.cat_2),
196 SMALL_IMAGE)
197 .addTitleText("Silver"))
198 .addCell(new GridBuilder.CellBuilder(gb)
199 .addImage(Icon.createWithResource(getContext(), R.drawable.cat_3),
200 SMALL_IMAGE)
201 .addTitleText("Drake"))
202 .addCell(new GridBuilder.CellBuilder(gb)
203 .addImage(Icon.createWithResource(getContext(), R.drawable.cat_5),
204 SMALL_IMAGE)
205 .addTitleText("Olive"))
206 .addCell(new GridBuilder.CellBuilder(gb)
207 .addImage(Icon.createWithResource(getContext(), R.drawable.cat_4),
208 SMALL_IMAGE)
209 .addTitleText("Lady Marmalade"))
210 .addCell(new GridBuilder.CellBuilder(gb)
211 .addImage(Icon.createWithResource(getContext(), R.drawable.cat_6),
212 SMALL_IMAGE)
213 .addTitleText("Grapefruit"));
214 return b.addGrid(gb).build();
Jason Monk8f8784c2018-01-22 21:29:42 -0500215 }
216
217 private Slice createContact(Uri sliceUri) {
218 ListBuilder b = new ListBuilder(getContext(), sliceUri);
219 ListBuilder.RowBuilder rb = new ListBuilder.RowBuilder(b);
220 GridBuilder gb = new GridBuilder(b);
221 return b.setColor(0xff3949ab)
222 .addRow(rb
223 .setTitle("Mady Pitza")
224 .setSubtitle("Frequently contacted contact")
225 .addEndItem(Icon.createWithResource(getContext(), R.drawable.mady)))
226 .addGrid(gb
227 .addCell(new GridBuilder.CellBuilder(gb)
Mady Mellor5ca9e162018-02-22 15:19:50 -0800228 .addImage(Icon.createWithResource(getContext(), R.drawable.ic_call),
Mady Mellor588bcc12018-03-02 13:56:51 -0800229 ICON_IMAGE)
Jason Monk8f8784c2018-01-22 21:29:42 -0500230 .addText("Call")
231 .setContentIntent(getBroadcastIntent(ACTION_TOAST, "call")))
232 .addCell(new GridBuilder.CellBuilder(gb)
Mady Mellor5ca9e162018-02-22 15:19:50 -0800233 .addImage(Icon.createWithResource(getContext(), R.drawable.ic_text),
Mady Mellor588bcc12018-03-02 13:56:51 -0800234 ICON_IMAGE)
Jason Monk8f8784c2018-01-22 21:29:42 -0500235 .addText("Text")
236 .setContentIntent(getBroadcastIntent(ACTION_TOAST, "text")))
237 .addCell(new GridBuilder.CellBuilder(gb)
Mady Mellor5ca9e162018-02-22 15:19:50 -0800238 .addImage(Icon.createWithResource(getContext(), R.drawable.ic_video),
Mady Mellor588bcc12018-03-02 13:56:51 -0800239 ICON_IMAGE)
Jason Monk8f8784c2018-01-22 21:29:42 -0500240 .setContentIntent(getBroadcastIntent(ACTION_TOAST, "video"))
241 .addText("Video"))
242 .addCell(new GridBuilder.CellBuilder(gb)
Mady Mellor5ca9e162018-02-22 15:19:50 -0800243 .addImage(Icon.createWithResource(getContext(), R.drawable.ic_email),
Mady Mellor588bcc12018-03-02 13:56:51 -0800244 ICON_IMAGE)
Jason Monk8f8784c2018-01-22 21:29:42 -0500245 .addText("Email")
246 .setContentIntent(getBroadcastIntent(ACTION_TOAST, "email"))))
247 .build();
248 }
249
250 private Slice createMessagingSlice(Uri sliceUri) {
251 // TODO: Remote input.
252 MessagingSliceBuilder mb = new MessagingSliceBuilder(getContext(), sliceUri);
253 return mb
254 .add(new MessagingSliceBuilder.MessageBuilder(mb)
255 .addText("yo home \uD83C\uDF55, I emailed you the info")
256 .addTimestamp(System.currentTimeMillis() - 20 * DateUtils.MINUTE_IN_MILLIS)
257 .addSource(Icon.createWithResource(getContext(), R.drawable.mady)))
258 .add(new MessagingSliceBuilder.MessageBuilder(mb)
259 .addText("just bought my tickets")
260 .addTimestamp(System.currentTimeMillis() - 10 * DateUtils.MINUTE_IN_MILLIS))
261 .add(new MessagingSliceBuilder.MessageBuilder(mb)
262 .addText("yay! can't wait for getContext() weekend!\n"
263 + "\uD83D\uDE00")
264 .addTimestamp(System.currentTimeMillis() - 5 * DateUtils.MINUTE_IN_MILLIS)
265 .addSource(Icon.createWithResource(getContext(), R.drawable.mady)))
266 .build();
267
268 }
269
270 private Slice createNoteSlice(Uri sliceUri) {
271 // TODO: Remote input.
272 ListBuilder lb = new ListBuilder(getContext(), sliceUri);
273 return lb.setColor(0xfff4b400)
274 .addRow(new ListBuilder.RowBuilder(lb)
275 .setTitle("Create new note")
276 .setSubtitle("with this note taking app")
Mady Melloraf76b3b2018-02-07 10:31:15 -0800277 .addEndItem(new SliceAction(getBroadcastIntent(ACTION_TOAST, "create note"),
278 Icon.createWithResource(getContext(), R.drawable.ic_create),
279 "Create note"))
280 .addEndItem(new SliceAction(getBroadcastIntent(ACTION_TOAST, "voice note"),
281 Icon.createWithResource(getContext(), R.drawable.ic_voice),
282 "Voice note"))
283 .addEndItem(new SliceAction(getIntent("android.media.action.IMAGE_CAPTURE"),
284 Icon.createWithResource(getContext(), R.drawable.ic_camera),
285 "Photo note")))
Jason Monk8f8784c2018-01-22 21:29:42 -0500286 .build();
287 }
288
289 private Slice createRideSlice(Uri sliceUri) {
290 final ForegroundColorSpan colorSpan = new ForegroundColorSpan(0xff0F9D58);
291 SpannableString headerSubtitle = new SpannableString("Ride in 4 min");
292 headerSubtitle.setSpan(colorSpan, 8, headerSubtitle.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
293 SpannableString homeSubtitle = new SpannableString("12 miles | 12 min | $9.00");
294 homeSubtitle.setSpan(colorSpan, 20, homeSubtitle.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
295 SpannableString workSubtitle = new SpannableString("44 miles | 1 hour 45 min | $31.41");
296 workSubtitle.setSpan(colorSpan, 27, workSubtitle.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
Mady Melloraf76b3b2018-02-07 10:31:15 -0800297 SliceAction primaryAction = new SliceAction(getBroadcastIntent(ACTION_TOAST, "get ride"),
298 Icon.createWithResource(getContext(), R.drawable.ic_car), "Get Ride");
299 ListBuilder lb = new ListBuilder(getContext(), sliceUri);
300 return lb.setColor(0xff0F9D58)
301 .setHeader(new ListBuilder.HeaderBuilder(lb)
302 .setTitle("Get ride")
303 .setSubtitle(headerSubtitle)
304 .setSummarySubtitle("Ride to work in 12 min | Ride home in 1 hour 45 min")
305 .setPrimaryAction(primaryAction))
306 .addRow(new ListBuilder.RowBuilder(lb)
307 .setTitle("Work")
308 .setSubtitle(workSubtitle)
309 .addEndItem(new SliceAction(getBroadcastIntent(ACTION_TOAST, "work"),
310 Icon.createWithResource(getContext(), R.drawable.ic_work),
311 "Get ride work")))
312 .addRow(new ListBuilder.RowBuilder(lb)
313 .setTitle("Home")
314 .setSubtitle(homeSubtitle)
315 .addEndItem(new SliceAction(getBroadcastIntent(ACTION_TOAST, "home"),
316 Icon.createWithResource(getContext(), R.drawable.ic_home),
317 "Get ride home")))
318 .build();
Jason Monk8f8784c2018-01-22 21:29:42 -0500319 }
320
321 private Slice createCustomToggleSlice(Uri sliceUri) {
322 ListBuilder b = new ListBuilder(getContext(), sliceUri);
323 return b.setColor(0xffff4081)
324 .addRow(new ListBuilder.RowBuilder(b)
325 .setTitle("Custom toggle")
326 .setSubtitle("It can support two states")
Mady Melloraf76b3b2018-02-07 10:31:15 -0800327 .addEndItem(new SliceAction(getBroadcastIntent(ACTION_TOAST, "star toggled"),
328 Icon.createWithResource(getContext(), R.drawable.toggle_star),
329 "Toggle star", true /* isChecked */)))
Jason Monk8f8784c2018-01-22 21:29:42 -0500330 .build();
331 }
332
333 private Slice createTwoCustomToggleSlices(Uri sliceUri) {
334 ListBuilder lb = new ListBuilder(getContext(), sliceUri);
335 return lb.setColor(0xffff4081)
336 .addRow(new ListBuilder.RowBuilder(lb)
337 .setTitle("2 toggles")
338 .setSubtitle("each supports two states")
Mady Melloraf76b3b2018-02-07 10:31:15 -0800339 .addEndItem(new SliceAction(
340 getBroadcastIntent(ACTION_TOAST, "first star toggled"),
341 Icon.createWithResource(getContext(), R.drawable.toggle_star),
342 "Toggle star", true /* isChecked */))
343 .addEndItem(new SliceAction(
344 getBroadcastIntent(ACTION_TOAST, "second star toggled"),
345 Icon.createWithResource(getContext(), R.drawable.toggle_star),
346 "Toggle star", false /* isChecked */)))
Jason Monk8f8784c2018-01-22 21:29:42 -0500347 .build();
348 }
349
Mady Mellor53380fe2018-02-14 16:31:38 -0800350 private Slice createWifiSlice(Uri sliceUri, boolean customSeeMore) {
Jason Monk8f8784c2018-01-22 21:29:42 -0500351 // Get wifi state
352 WifiManager wifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
353 int wifiState = wifiManager.getWifiState();
354 boolean wifiEnabled = false;
355 String state;
356 switch (wifiState) {
357 case WifiManager.WIFI_STATE_DISABLED:
358 case WifiManager.WIFI_STATE_DISABLING:
359 state = "disconnected";
360 break;
361 case WifiManager.WIFI_STATE_ENABLED:
362 case WifiManager.WIFI_STATE_ENABLING:
363 state = wifiManager.getConnectionInfo().getSSID();
364 wifiEnabled = true;
365 break;
366 case WifiManager.WIFI_STATE_UNKNOWN:
367 default:
368 state = ""; // just don't show anything?
369 break;
370 }
371 boolean finalWifiEnabled = wifiEnabled;
Mady Mellor53380fe2018-02-14 16:31:38 -0800372 ListBuilder lb = new ListBuilder(getContext(), sliceUri);
Mady Melloraf76b3b2018-02-07 10:31:15 -0800373 SliceAction primaryAction = new SliceAction(getIntent(Settings.ACTION_WIFI_SETTINGS),
374 Icon.createWithResource(getContext(), R.drawable.ic_wifi), "Wi-fi Settings");
Mady Mellor53380fe2018-02-14 16:31:38 -0800375 lb.setColor(0xff4285f4);
376 lb.addRow(new ListBuilder.RowBuilder(lb)
377 .setTitle("Wi-fi")
Mady Mellor588bcc12018-03-02 13:56:51 -0800378 .setTitleItem(Icon.createWithResource(getContext(), R.drawable.ic_wifi), ICON_IMAGE)
Mady Mellor53380fe2018-02-14 16:31:38 -0800379 .setSubtitle(state)
380 .addEndItem(new SliceAction(getBroadcastIntent(ACTION_WIFI_CHANGED, null),
381 "Toggle wifi", finalWifiEnabled))
382 .setPrimaryAction(primaryAction));
383
Mady Mellorbb51b592018-03-08 17:07:24 -0800384 // Add keywords
385 String[] keywords = new String[] {"internet", "wifi", "data", "network"};
386 lb.setKeywords(Arrays.asList(keywords));
387
Mady Mellor53380fe2018-02-14 16:31:38 -0800388 // Add fake wifi networks
389 int[] wifiIcons = new int[] {R.drawable.ic_wifi_full, R.drawable.ic_wifi_low,
390 R.drawable.ic_wifi_fair};
391 for (int i = 0; i < 10; i++) {
392 final int iconId = wifiIcons[i % wifiIcons.length];
393 Icon icon = Icon.createWithResource(getContext(), iconId);
394 final String networkName = "Network" + i;
Mady Mellor588bcc12018-03-02 13:56:51 -0800395 ListBuilder.RowBuilder rb = new ListBuilder.RowBuilder(lb)
396 .setTitleItem(icon, ICON_IMAGE)
Mady Mellor53380fe2018-02-14 16:31:38 -0800397 .setTitle("Network" + networkName);
398 boolean locked = i % 3 == 0;
399 if (locked) {
400 rb.addEndItem(Icon.createWithResource(getContext(), R.drawable.ic_lock));
401 }
402 String message = locked ? "Open wifi password dialog" : "Connect to " + networkName;
403 rb.setPrimaryAction(new SliceAction(getBroadcastIntent(ACTION_TOAST, message), icon,
404 message));
405 lb.addRow(rb);
406 }
407 if (customSeeMore) {
408 lb.addSeeMoreRow(new ListBuilder.RowBuilder(lb)
409 .setTitle("See all available networks")
410 .addEndItem(Icon.createWithResource(getContext(), R.drawable.ic_right_caret))
411 .setPrimaryAction(primaryAction));
412 } else {
413 lb.addSeeMoreAction(primaryAction.getAction());
414 }
415 return lb.build();
Jason Monk8f8784c2018-01-22 21:29:42 -0500416 }
417
418 private PendingIntent getIntent(String action) {
419 Intent intent = new Intent(action);
420 intent.setClassName(getContext().getPackageName(), SliceRenderActivity.class.getName());
421 PendingIntent pi = PendingIntent.getActivity(getContext(), 0, intent, 0);
422 return pi;
423 }
424
425 private PendingIntent getBroadcastIntent(String action, String message) {
426 Intent intent = new Intent(action);
427 intent.setClassName(getContext().getPackageName(), SliceRenderActivity.class.getName());
428 // Ensure a new PendingIntent is created for each message.
429 int requestCode = 0;
430 if (message != null) {
431 intent.putExtra(EXTRA_TOAST_MESSAGE, message);
432 requestCode = message.hashCode();
433 }
434 return PendingIntent.getBroadcast(getContext(), requestCode, intent,
435 PendingIntent.FLAG_UPDATE_CURRENT);
436 }
437}