SampleSliceProvider.java revision f49ab625fc7f05baebfbef00beebf9a9f78dcabc
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 static android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE; 20 21import static androidx.slice.builders.ListBuilder.ICON_IMAGE; 22import static androidx.slice.builders.ListBuilder.INFINITY; 23import static androidx.slice.builders.ListBuilder.LARGE_IMAGE; 24import static androidx.slice.builders.ListBuilder.SMALL_IMAGE; 25 26import android.app.PendingIntent; 27import android.content.ContentResolver; 28import android.content.Context; 29import android.content.Intent; 30import android.net.Uri; 31import android.net.wifi.WifiManager; 32import android.os.Handler; 33import android.provider.Settings; 34import android.text.SpannableString; 35import android.text.TextUtils; 36import android.text.format.DateUtils; 37import android.text.style.ForegroundColorSpan; 38import android.util.SparseArray; 39 40import androidx.annotation.NonNull; 41import androidx.core.graphics.drawable.IconCompat; 42import androidx.slice.Slice; 43import androidx.slice.SliceProvider; 44import androidx.slice.builders.GridRowBuilder; 45import androidx.slice.builders.ListBuilder; 46import androidx.slice.builders.MessagingSliceBuilder; 47import androidx.slice.builders.SliceAction; 48 49import java.util.Arrays; 50import java.util.Calendar; 51import java.util.concurrent.TimeUnit; 52 53/** 54 * Examples of using slice template builders. 55 */ 56public class SampleSliceProvider extends SliceProvider { 57 58 private static final boolean TEST_CUSTOM_SEE_MORE = false; 59 60 public static final String ACTION_WIFI_CHANGED = 61 "com.example.androidx.slice.action.WIFI_CHANGED"; 62 public static final String ACTION_TOAST = 63 "com.example.androidx.slice.action.TOAST"; 64 public static final String EXTRA_TOAST_MESSAGE = "com.example.androidx.extra.TOAST_MESSAGE"; 65 public static final String ACTION_TOAST_RANGE_VALUE = 66 "com.example.androidx.slice.action.TOAST_RANGE_VALUE"; 67 68 public static final String[] URI_PATHS = {"message", "wifi", "note", "ride", "toggle", 69 "toggle2", "contact", "gallery", "weather", "reservation", "loadlist", "loadgrid", 70 "inputrange", "range", "contact2", "subscription"}; 71 72 /** 73 * @return Uri with the provided path 74 */ 75 public static Uri getUri(String path, Context context) { 76 return new Uri.Builder() 77 .scheme(ContentResolver.SCHEME_CONTENT) 78 .authority(context.getPackageName()) 79 .appendPath(path) 80 .build(); 81 } 82 83 @Override 84 public boolean onCreateSliceProvider() { 85 return true; 86 } 87 88 @NonNull 89 @Override 90 public Uri onMapIntentToUri(Intent intent) { 91 return getUri("wifi", getContext()); 92 } 93 94 @Override 95 public Slice onBindSlice(Uri sliceUri) { 96 String path = sliceUri.getPath(); 97 if (!path.equals("/loadlist")) { 98 mListSummaries.clear(); 99 mListLastUpdate = 0; 100 } 101 if (!path.equals("/loadgrid")) { 102 mGridSummaries.clear(); 103 mGridLastUpdate = 0; 104 } 105 switch (path) { 106 // TODO: add list / grid slices with 'see more' options 107 case "/message": 108 return createMessagingSlice(sliceUri); 109 case "/wifi": 110 return createWifiSlice(sliceUri); 111 case "/note": 112 return createNoteSlice(sliceUri); 113 case "/ride": 114 return createRideSlice(sliceUri); 115 case "/toggle": 116 return createCustomToggleSlice(sliceUri); 117 case "/toggle2": 118 return createTwoCustomToggleSlices(sliceUri); 119 case "/contact": 120 return createContact(sliceUri); 121 case "/contact2": 122 return createContact2(sliceUri); 123 case "/gallery": 124 return createGallery(sliceUri); 125 case "/weather": 126 return createWeather(sliceUri); 127 case "/reservation": 128 return createReservationSlice(sliceUri); 129 case "/loadlist": 130 return createLoadingListSlice(sliceUri); 131 case "/loadgrid": 132 return createLoadingGridSlice(sliceUri); 133 case "/inputrange": 134 return createStarRatingInputRange(sliceUri); 135 case "/range": 136 return createDownloadProgressRange(sliceUri); 137 case "/subscription": 138 return createCatSlice(sliceUri, false /* customSeeMore */); 139 } 140 throw new IllegalArgumentException("Unknown uri " + sliceUri); 141 } 142 143 private Slice createWeather(Uri sliceUri) { 144 SliceAction primaryAction = new SliceAction(getBroadcastIntent(ACTION_TOAST, 145 "open weather app"), 146 IconCompat.createWithResource(getContext(), R.drawable.weather_1), 147 "Weather is happening!"); 148 return new ListBuilder(getContext(), sliceUri, INFINITY) 149 .addGrid(gb -> gb 150 .setPrimaryAction(primaryAction) 151 .addCell(cb -> cb 152 .addImage(IconCompat.createWithResource(getContext(), 153 R.drawable.weather_1), 154 SMALL_IMAGE) 155 .addText("MON") 156 .addTitleText("69\u00B0")) 157 .addCell(cb -> cb 158 .addImage(IconCompat.createWithResource(getContext(), 159 R.drawable.weather_2), 160 SMALL_IMAGE) 161 .addText("TUE") 162 .addTitleText("71\u00B0")) 163 .addCell(cb -> cb 164 .addImage(IconCompat.createWithResource(getContext(), 165 R.drawable.weather_3), 166 SMALL_IMAGE) 167 .addText("WED") 168 .addTitleText("76\u00B0")) 169 .addCell(cb -> cb 170 .addImage(IconCompat.createWithResource(getContext(), 171 R.drawable.weather_4), 172 SMALL_IMAGE) 173 .addText("THU") 174 .addTitleText("72\u00B0")) 175 .addCell(cb -> cb 176 .addImage(IconCompat.createWithResource(getContext(), 177 R.drawable.weather_1), 178 SMALL_IMAGE) 179 .addText("FRI") 180 .addTitleText("68\u00B0"))) 181 .build(); 182 } 183 184 private Slice createGallery(Uri sliceUri) { 185 return new ListBuilder(getContext(), sliceUri, INFINITY) 186 .setColor(0xff4285F4) 187 .addRow(b -> b 188 .setTitle("Family trip to Hawaii") 189 .setSubtitle("Sep 30, 2017 - Oct 2, 2017")) 190 .addAction(new SliceAction( 191 getBroadcastIntent(ACTION_TOAST, "cast photo album"), 192 IconCompat.createWithResource(getContext(), R.drawable.ic_cast), 193 "Cast photo album")) 194 .addAction(new SliceAction( 195 getBroadcastIntent(ACTION_TOAST, "share photo album"), 196 IconCompat.createWithResource(getContext(), R.drawable.ic_share), 197 "Share photo album")) 198 .addGridRow(b -> b 199 .addCell(cb -> cb 200 .addImage(IconCompat.createWithResource(getContext(), 201 R.drawable.slices_1), 202 LARGE_IMAGE)) 203 .addCell(cb -> cb 204 .addImage(IconCompat.createWithResource(getContext(), 205 R.drawable.slices_2), 206 LARGE_IMAGE)) 207 .addCell(cb -> cb 208 .addImage(IconCompat.createWithResource(getContext(), 209 R.drawable.slices_3), 210 LARGE_IMAGE)) 211 .addCell(cb -> cb 212 .addImage(IconCompat.createWithResource(getContext(), 213 R.drawable.slices_4), 214 LARGE_IMAGE)) 215 .addCell(cb -> cb 216 .addImage(IconCompat.createWithResource(getContext(), 217 R.drawable.slices_2), 218 LARGE_IMAGE)) 219 .addCell(cb -> cb 220 .addImage(IconCompat.createWithResource(getContext(), 221 R.drawable.slices_3), 222 LARGE_IMAGE)) 223 .addCell(cb -> cb 224 .addImage(IconCompat.createWithResource(getContext(), 225 R.drawable.slices_4), 226 LARGE_IMAGE)) 227 .setSeeMoreAction(getBroadcastIntent(ACTION_TOAST, "see your gallery")) 228 .setContentDescription("Images from your trip to Hawaii")) 229 .build(); 230 } 231 232 private Slice createCatSlice(Uri sliceUri, boolean customSeeMore) { 233 ListBuilder b = new ListBuilder(getContext(), sliceUri, INFINITY); 234 GridRowBuilder gb = new GridRowBuilder(b); 235 PendingIntent pi = getBroadcastIntent(ACTION_TOAST, "See cats you follow"); 236 if (customSeeMore) { 237 GridRowBuilder.CellBuilder cb = new GridRowBuilder.CellBuilder(gb); 238 cb.addImage(IconCompat.createWithResource(getContext(), R.drawable.ic_right_caret), 239 ICON_IMAGE); 240 cb.setContentIntent(pi); 241 cb.addTitleText("All cats"); 242 gb.setSeeMoreCell(cb); 243 } else { 244 gb.setSeeMoreAction(pi); 245 } 246 gb.addCell(new GridRowBuilder.CellBuilder(gb) 247 .addImage(IconCompat.createWithResource(getContext(), R.drawable.cat_1), 248 SMALL_IMAGE) 249 .addTitleText("Oreo")) 250 .addCell(new GridRowBuilder.CellBuilder(gb) 251 .addImage(IconCompat.createWithResource(getContext(), R.drawable.cat_2), 252 SMALL_IMAGE) 253 .addTitleText("Silver")) 254 .addCell(new GridRowBuilder.CellBuilder(gb) 255 .addImage(IconCompat.createWithResource(getContext(), R.drawable.cat_3), 256 SMALL_IMAGE) 257 .addTitleText("Drake")) 258 .addCell(new GridRowBuilder.CellBuilder(gb) 259 .addImage(IconCompat.createWithResource(getContext(), R.drawable.cat_5), 260 SMALL_IMAGE) 261 .addTitleText("Olive")) 262 .addCell(new GridRowBuilder.CellBuilder(gb) 263 .addImage(IconCompat.createWithResource(getContext(), R.drawable.cat_4), 264 SMALL_IMAGE) 265 .addTitleText("Lady Marmalade")) 266 .addCell(new GridRowBuilder.CellBuilder(gb) 267 .addImage(IconCompat.createWithResource(getContext(), R.drawable.cat_6), 268 SMALL_IMAGE) 269 .addTitleText("Grapefruit")); 270 return b.addGridRow(gb).build(); 271 } 272 273 private Slice createContact2(Uri sliceUri) { 274 ListBuilder b = new ListBuilder(getContext(), sliceUri, INFINITY); 275 ListBuilder.RowBuilder rb = new ListBuilder.RowBuilder(b); 276 GridRowBuilder gb = new GridRowBuilder(b); 277 return b.setColor(0xff3949ab) 278 .addRow(rb 279 .setTitle("Mady Pitza") 280 .setSubtitle("Frequently contacted contact") 281 .addEndItem(IconCompat.createWithResource(getContext(), R.drawable.mady), 282 SMALL_IMAGE)) 283 .addGridRow(gb 284 .addCell(new GridRowBuilder.CellBuilder(gb) 285 .addImage(IconCompat.createWithResource(getContext(), 286 R.drawable.ic_call), 287 ICON_IMAGE) 288 .addText("Call") 289 .setContentIntent(getBroadcastIntent(ACTION_TOAST, "call"))) 290 .addCell(new GridRowBuilder.CellBuilder(gb) 291 .addImage(IconCompat.createWithResource(getContext(), 292 R.drawable.ic_text), 293 ICON_IMAGE) 294 .addText("Text") 295 .setContentIntent(getBroadcastIntent(ACTION_TOAST, "text"))) 296 .addCell(new GridRowBuilder.CellBuilder(gb) 297 .addImage(IconCompat.createWithResource(getContext(), 298 R.drawable.ic_video), ICON_IMAGE) 299 .setContentIntent(getBroadcastIntent(ACTION_TOAST, "video")) 300 .addText("Video")) 301 .addCell(new GridRowBuilder.CellBuilder(gb) 302 .addImage(IconCompat.createWithResource(getContext(), 303 R.drawable.ic_email), ICON_IMAGE) 304 .addText("Email") 305 .setContentIntent(getBroadcastIntent(ACTION_TOAST, "email")))) 306 .build(); 307 } 308 309 private Slice createContact(Uri sliceUri) { 310 final long lastCalled = System.currentTimeMillis() - 20 * DateUtils.MINUTE_IN_MILLIS; 311 CharSequence lastCalledString = DateUtils.getRelativeTimeSpanString(lastCalled, 312 Calendar.getInstance().getTimeInMillis(), 313 DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE); 314 SliceAction primaryAction = new SliceAction(getBroadcastIntent(ACTION_TOAST, 315 "See contact info"), IconCompat.createWithResource(getContext(), 316 R.drawable.mady), SMALL_IMAGE, "Mady"); 317 318 return new ListBuilder(getContext(), sliceUri, INFINITY) 319 .setColor(0xff3949ab) 320 .setHeader(b -> b 321 .setTitle("Mady Pitza") 322 .setSummarySubtitle("Called " + lastCalledString) 323 .setPrimaryAction(primaryAction)) 324 .addRow(b -> b 325 .setTitleItem( 326 IconCompat.createWithResource(getContext(), R.drawable.ic_call), 327 ICON_IMAGE) 328 .setTitle("314-259-2653") 329 .setSubtitle("Call lasted 1 hr 17 min") 330 .addEndItem(lastCalled)) 331 .addRow(b -> b 332 .setTitleItem( 333 IconCompat.createWithResource(getContext(), R.drawable.ic_text), 334 ICON_IMAGE) 335 .setTitle("You: Coooooool see you then") 336 .addEndItem(System.currentTimeMillis() - 40 * DateUtils.MINUTE_IN_MILLIS)) 337 .addAction(new SliceAction(getBroadcastIntent(ACTION_TOAST, "call"), 338 IconCompat.createWithResource(getContext(), R.drawable.ic_call), 339 "Call mady")) 340 .addAction(new SliceAction(getBroadcastIntent(ACTION_TOAST, "text"), 341 IconCompat.createWithResource(getContext(), R.drawable.ic_text), 342 "Text mady")) 343 .addAction(new SliceAction(getBroadcastIntent(ACTION_TOAST, "video"), 344 IconCompat.createWithResource(getContext(), R.drawable.ic_video), 345 "Video call mady")) 346 .addAction(new SliceAction(getBroadcastIntent(ACTION_TOAST, "email"), 347 IconCompat.createWithResource(getContext(), R.drawable.ic_email), 348 "Email mady")) 349 .build(); 350 } 351 352 private Slice createMessagingSlice(Uri sliceUri) { 353 // TODO: Remote input. 354 return new MessagingSliceBuilder(getContext(), sliceUri) 355 .add(b -> b 356 .addText("yo home \uD83C\uDF55, I emailed you the info") 357 .addTimestamp(System.currentTimeMillis() - 20 * DateUtils.MINUTE_IN_MILLIS) 358 .addSource(IconCompat.createWithResource(getContext(), R.drawable.mady))) 359 .add(b -> b 360 .addText("just bought my tickets") 361 .addTimestamp(System.currentTimeMillis() - 10 * DateUtils.MINUTE_IN_MILLIS)) 362 .add(b -> b 363 .addText("yay! can't wait for getContext() weekend!\n" 364 + "\uD83D\uDE00") 365 .addTimestamp(System.currentTimeMillis() - 5 * DateUtils.MINUTE_IN_MILLIS) 366 .addSource(IconCompat.createWithResource(getContext(), R.drawable.mady))) 367 .build(); 368 369 } 370 371 private Slice createNoteSlice(Uri sliceUri) { 372 // TODO: Remote input. 373 return new ListBuilder(getContext(), sliceUri, INFINITY) 374 .setColor(0xfff4b400) 375 .addRow(b -> b.setTitle("Create new note")) 376 .addAction(new SliceAction(getBroadcastIntent(ACTION_TOAST, "create note"), 377 IconCompat.createWithResource(getContext(), R.drawable.ic_create), 378 "Create note")) 379 .addAction(new SliceAction(getBroadcastIntent(ACTION_TOAST, "voice note"), 380 IconCompat.createWithResource(getContext(), R.drawable.ic_voice), 381 "Voice note")) 382 .addAction(new SliceAction(getIntent("android.media.action.IMAGE_CAPTURE"), 383 IconCompat.createWithResource(getContext(), R.drawable.ic_camera), 384 "Photo note")) 385 .build(); 386 } 387 388 private Slice createReservationSlice(Uri sliceUri) { 389 return new ListBuilder(getContext(), sliceUri, INFINITY) 390 .setColor(0xffFF5252) 391 .setHeader(b -> b 392 .setTitle("Upcoming trip to Seattle") 393 .setSubtitle("Feb 1 - 19 | 2 guests")) 394 .addAction(new SliceAction( 395 getBroadcastIntent(ACTION_TOAST, "show location on map"), 396 IconCompat.createWithResource(getContext(), R.drawable.ic_location), 397 "Show reservation location")) 398 .addAction(new SliceAction(getBroadcastIntent(ACTION_TOAST, "contact host"), 399 IconCompat.createWithResource(getContext(), R.drawable.ic_text), 400 "Contact host")) 401 .addGrid(b -> b 402 .addCell(cb -> cb 403 .addImage(IconCompat.createWithResource(getContext(), 404 R.drawable.reservation), 405 LARGE_IMAGE) 406 .setContentDescription("Image of your reservation in Seattle"))) 407 .addGrid(b -> b 408 .addCell(cb -> cb 409 .addTitleText("Check In") 410 .addText("12:00 PM, Feb 1")) 411 .addCell(cb -> cb 412 .addTitleText("Check Out") 413 .addText("11:00 AM, Feb 19"))) 414 .build(); 415 } 416 417 private Slice createRideSlice(Uri sliceUri) { 418 final ForegroundColorSpan colorSpan = new ForegroundColorSpan(0xff0F9D58); 419 SpannableString headerSubtitle = new SpannableString("Ride in 4 min"); 420 headerSubtitle.setSpan(colorSpan, 8, headerSubtitle.length(), SPAN_EXCLUSIVE_EXCLUSIVE); 421 SpannableString homeSubtitle = new SpannableString("12 miles | 12 min | $9.00"); 422 homeSubtitle.setSpan(colorSpan, 20, homeSubtitle.length(), SPAN_EXCLUSIVE_EXCLUSIVE); 423 SpannableString workSubtitle = new SpannableString("44 miles | 1 hour 45 min | $31.41"); 424 workSubtitle.setSpan(colorSpan, 27, workSubtitle.length(), SPAN_EXCLUSIVE_EXCLUSIVE); 425 426 SliceAction primaryAction = new SliceAction(getBroadcastIntent(ACTION_TOAST, "get ride"), 427 IconCompat.createWithResource(getContext(), R.drawable.ic_car), "Get Ride"); 428 return new ListBuilder(getContext(), sliceUri, -TimeUnit.MINUTES.toMillis(2)) 429 .setColor(0xff0F9D58) 430 .setHeader(b -> b 431 .setTitle("Get ride") 432 .setSubtitle(headerSubtitle) 433 .setSummarySubtitle("Ride to work in 12 min | Ride home in 1 hour 45 min") 434 .setPrimaryAction(primaryAction)) 435 .addRow(b -> b 436 .setTitle("Work") 437 .setSubtitle(workSubtitle) 438 .addEndItem(new SliceAction(getBroadcastIntent(ACTION_TOAST, "work"), 439 IconCompat.createWithResource(getContext(), R.drawable.ic_work), 440 "Get ride to work"))) 441 .addRow(b -> b 442 .setTitle("Home") 443 .setSubtitle(homeSubtitle) 444 .addEndItem(new SliceAction(getBroadcastIntent(ACTION_TOAST, "home"), 445 IconCompat.createWithResource(getContext(), R.drawable.ic_home), 446 "Get ride home"))) 447 .build(); 448 } 449 450 private Slice createCustomToggleSlice(Uri sliceUri) { 451 return new ListBuilder(getContext(), sliceUri, INFINITY) 452 .setColor(0xffff4081) 453 .addRow(b -> b 454 .setTitle("Custom toggle") 455 .setSubtitle("It can support two states") 456 .setPrimaryAction(new SliceAction(getBroadcastIntent(ACTION_TOAST, 457 "star toggled"), 458 IconCompat.createWithResource(getContext(), R.drawable.toggle_star), 459 "Toggle star", true /* isChecked */))) 460 .build(); 461 } 462 463 private Slice createTwoCustomToggleSlices(Uri sliceUri) { 464 return new ListBuilder(getContext(), sliceUri, INFINITY) 465 .setColor(0xffff4081) 466 .addRow(b -> b 467 .setTitle("2 toggles") 468 .setSubtitle("each supports two states") 469 .addEndItem(new SliceAction( 470 getBroadcastIntent(ACTION_TOAST, "first star toggled"), 471 IconCompat.createWithResource(getContext(), R.drawable.toggle_star), 472 "Toggle star", true /* isChecked */)) 473 .addEndItem(new SliceAction( 474 getBroadcastIntent(ACTION_TOAST, "second star toggled"), 475 IconCompat.createWithResource(getContext(), R.drawable.toggle_star), 476 "Toggle star", false /* isChecked */))) 477 .build(); 478 } 479 480 private Slice createWifiSlice(Uri sliceUri) { 481 // Get wifi state 482 WifiManager wifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE); 483 int wifiState = wifiManager.getWifiState(); 484 boolean wifiEnabled = false; 485 String state; 486 switch (wifiState) { 487 case WifiManager.WIFI_STATE_DISABLED: 488 case WifiManager.WIFI_STATE_DISABLING: 489 state = "disconnected"; 490 break; 491 case WifiManager.WIFI_STATE_ENABLED: 492 case WifiManager.WIFI_STATE_ENABLING: 493 state = wifiManager.getConnectionInfo().getSSID(); 494 wifiEnabled = true; 495 break; 496 case WifiManager.WIFI_STATE_UNKNOWN: 497 default: 498 state = ""; // just don't show anything? 499 break; 500 } 501 502 // Set the first row as a toggle 503 boolean finalWifiEnabled = wifiEnabled; 504 SliceAction primaryAction = new SliceAction(getIntent(Settings.ACTION_WIFI_SETTINGS), 505 IconCompat.createWithResource(getContext(), R.drawable.ic_wifi), "Wi-fi Settings"); 506 String toggleCDString = wifiEnabled ? "Turn wifi off" : "Turn wifi on"; 507 String sliceCDString = wifiEnabled ? "Wifi connected to " + state 508 : "Wifi disconnected, 10 networks available"; 509 ListBuilder lb = new ListBuilder(getContext(), sliceUri, INFINITY) 510 .setColor(0xff4285f4) 511 .setHeader(b -> b 512 .setTitle("Wi-fi") 513 .setSubtitle(state) 514 .setContentDescription(sliceCDString) 515 .setPrimaryAction(primaryAction)) 516 .addAction((new SliceAction(getBroadcastIntent(ACTION_WIFI_CHANGED, null), 517 toggleCDString, finalWifiEnabled))); 518 519 // Add fake wifi networks 520 int[] wifiIcons = new int[]{R.drawable.ic_wifi_full, R.drawable.ic_wifi_low, 521 R.drawable.ic_wifi_fair}; 522 for (int i = 0; i < 10; i++) { 523 final int iconId = wifiIcons[i % wifiIcons.length]; 524 IconCompat icon = IconCompat.createWithResource(getContext(), iconId); 525 final String networkName = "Network" + i; 526 ListBuilder.RowBuilder rb = new ListBuilder.RowBuilder(lb); 527 rb.setTitleItem(icon, ICON_IMAGE).setTitle(networkName); 528 boolean locked = i % 3 == 0; 529 if (locked) { 530 rb.addEndItem(IconCompat.createWithResource(getContext(), R.drawable.ic_lock), 531 ICON_IMAGE); 532 rb.setContentDescription("Connect to " + networkName + ", password needed"); 533 } else { 534 rb.setContentDescription("Connect to " + networkName); 535 } 536 String message = locked ? "Open wifi password dialog" : "Connect to " + networkName; 537 rb.setPrimaryAction(new SliceAction(getBroadcastIntent(ACTION_TOAST, message), icon, 538 message)); 539 lb.addRow(rb); 540 } 541 542 // Add keywords 543 String[] keywords = new String[]{"internet", "wifi", "data", "network"}; 544 lb.setKeywords(Arrays.asList(keywords)); 545 546 // Add see more intent 547 if (TEST_CUSTOM_SEE_MORE) { 548 lb.setSeeMoreRow(rb -> rb 549 .setTitle("See all available networks") 550 .addEndItem( 551 IconCompat.createWithResource(getContext(), R.drawable.ic_right_caret), 552 SMALL_IMAGE) 553 .setPrimaryAction(primaryAction)); 554 } else { 555 lb.setSeeMoreAction(primaryAction.getAction()); 556 } 557 return lb.build(); 558 } 559 560 private Slice createStarRatingInputRange(Uri sliceUri) { 561 IconCompat icon = IconCompat.createWithResource(getContext(), R.drawable.ic_star_on); 562 SliceAction primaryAction = 563 new SliceAction(getBroadcastIntent(ACTION_TOAST, "open star rating"), icon, "Rate"); 564 return new ListBuilder(getContext(), sliceUri, INFINITY) 565 .setColor(0xffff4081) 566 .addInputRange(c -> c 567 .setTitle("Star rating") 568 .setSubtitle("Pick a rating from 0 to 5") 569 .setThumb(icon) 570 .setInputAction(getBroadcastIntent(ACTION_TOAST_RANGE_VALUE, null)) 571 .setMax(5) 572 .setValue(3) 573 .setPrimaryAction(primaryAction) 574 .setContentDescription("Slider for star ratings")) 575 .build(); 576 } 577 578 private Slice createDownloadProgressRange(Uri sliceUri) { 579 IconCompat icon = IconCompat.createWithResource(getContext(), R.drawable.ic_star_on); 580 SliceAction primaryAction = 581 new SliceAction( 582 getBroadcastIntent(ACTION_TOAST, "open download"), icon, "Download"); 583 return new ListBuilder(getContext(), sliceUri, INFINITY) 584 .setColor(0xffff4081) 585 .addRange(c -> c 586 .setTitle("Download progress") 587 .setSubtitle("Download is happening") 588 .setMax(100) 589 .setValue(75) 590 .setPrimaryAction(primaryAction)) 591 .build(); 592 } 593 594 private Handler mHandler = new Handler(); 595 private SparseArray<String> mListSummaries = new SparseArray<>(); 596 private long mListLastUpdate; 597 private SparseArray<String> mGridSummaries = new SparseArray<>(); 598 private long mGridLastUpdate; 599 600 private void update(long delay, SparseArray<String> summaries, int id, String s, Uri uri, 601 Runnable r) { 602 mHandler.postDelayed(() -> { 603 summaries.put(id, s); 604 getContext().getContentResolver().notifyChange(uri, null); 605 r.run(); 606 }, delay); 607 } 608 609 private Slice createLoadingListSlice(Uri sliceUri) { 610 boolean updating = mListLastUpdate == 0 611 || mListLastUpdate < (System.currentTimeMillis() - 10 * System.currentTimeMillis()); 612 if (updating) { 613 Runnable r = () -> mListLastUpdate = System.currentTimeMillis(); 614 update(1000, mListSummaries, 0, "44 miles | 1 hour 45 min | $31.41", sliceUri, r); 615 update(1500, mListSummaries, 1, "12 miles | 12 min | $9.00", sliceUri, r); 616 update(1700, mListSummaries, 2, "5 miles | 10 min | $8.00", sliceUri, r); 617 } 618 CharSequence work = mListSummaries.get(0, ""); 619 CharSequence home = mListSummaries.get(1, ""); 620 CharSequence school = mListSummaries.get(2, ""); 621 Slice s = new ListBuilder(getContext(), sliceUri, -TimeUnit.MINUTES.toMillis(50)) 622 .addRow(b -> b 623 .setTitle("Work") 624 .setSubtitle(work, 625 updating || TextUtils.isEmpty(work)) 626 .addEndItem(IconCompat.createWithResource(getContext(), R.drawable.ic_work), 627 ICON_IMAGE)) 628 .addRow(b -> b 629 .setTitle("Home") 630 .setSubtitle(mListSummaries.get(1, ""), 631 updating || TextUtils.isEmpty(home)) 632 .addEndItem( 633 IconCompat.createWithResource(getContext(), R.drawable.ic_home), 634 ICON_IMAGE)) 635 .addRow(b -> b 636 .setTitle("School") 637 .setSubtitle(mListSummaries.get(2, ""), 638 updating || TextUtils.isEmpty(school)) 639 .addEndItem( 640 IconCompat.createWithResource(getContext(), R.drawable.ic_school), 641 ICON_IMAGE)) 642 .build(); 643 return s; 644 } 645 646 // TODO: Should test large image grids 647 private Slice createLoadingGridSlice(Uri sliceUri) { 648 boolean updating = mGridLastUpdate == 0 649 || mGridLastUpdate < (System.currentTimeMillis() - 10 * System.currentTimeMillis()); 650 if (updating) { 651 Runnable r = () -> mGridLastUpdate = System.currentTimeMillis(); 652 update(2000, mGridSummaries, 0, "Heavy traffic in your area", sliceUri, r); 653 update(3500, mGridSummaries, 1, "Typical conditions with delays up to 28 min", 654 sliceUri, r); 655 update(3000, mGridSummaries, 2, "41 min", sliceUri, r); 656 update(1500, mGridSummaries, 3, "33 min", sliceUri, r); 657 update(1000, mGridSummaries, 4, "12 min", sliceUri, r); 658 } 659 CharSequence title = mGridSummaries.get(0, ""); 660 CharSequence subtitle = mGridSummaries.get(1, ""); 661 CharSequence home = mGridSummaries.get(2, ""); 662 CharSequence work = mGridSummaries.get(3, ""); 663 CharSequence school = mGridSummaries.get(4, ""); 664 Slice s = new ListBuilder(getContext(), sliceUri, INFINITY) 665 .setHeader(hb -> hb 666 .setTitle(title, 667 updating || TextUtils.isEmpty(title)) 668 .setSubtitle(subtitle, 669 updating || TextUtils.isEmpty(subtitle))) 670 .addGrid(gb -> gb 671 .addCell(cb -> cb 672 .addImage(IconCompat.createWithResource(getContext(), 673 R.drawable.ic_home), 674 ICON_IMAGE) 675 .addTitleText("Home") 676 .addText(home, 677 updating || TextUtils.isEmpty(home))) 678 .addCell(cb -> cb 679 .addImage(IconCompat.createWithResource(getContext(), 680 R.drawable.ic_work), 681 ICON_IMAGE) 682 .addTitleText("Work") 683 .addText(work, 684 updating || TextUtils.isEmpty(work))) 685 .addCell(cb -> cb 686 .addImage(IconCompat.createWithResource(getContext(), 687 R.drawable.ic_school), 688 ICON_IMAGE) 689 .addTitleText("School") 690 .addText(school, 691 updating || TextUtils.isEmpty(school)))) 692 .build(); 693 return s; 694 } 695 696 private PendingIntent getIntent(String action) { 697 Intent intent = new Intent(action); 698 PendingIntent pi = PendingIntent.getActivity(getContext(), 0, intent, 0); 699 return pi; 700 } 701 702 private PendingIntent getBroadcastIntent(String action, String message) { 703 Intent intent = new Intent(action); 704 intent.setClass(getContext(), SliceBroadcastReceiver.class); 705 // Ensure a new PendingIntent is created for each message. 706 int requestCode = 0; 707 if (message != null) { 708 intent.putExtra(EXTRA_TOAST_MESSAGE, message); 709 requestCode = message.hashCode(); 710 } 711 return PendingIntent.getBroadcast(getContext(), requestCode, intent, 712 PendingIntent.FLAG_UPDATE_CURRENT); 713 } 714} 715