Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 1 | /* |
| 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 Liutikas | 85ef144 | 2018-03-02 14:16:21 -0800 | [diff] [blame] | 17 | package androidx.slice.render; |
Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 18 | |
| 19 | import static android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE; |
| 20 | |
Mady Mellor | 588bcc1 | 2018-03-02 13:56:51 -0800 | [diff] [blame] | 21 | import static androidx.slice.builders.ListBuilder.ICON_IMAGE; |
| 22 | import static androidx.slice.builders.ListBuilder.LARGE_IMAGE; |
| 23 | import static androidx.slice.builders.ListBuilder.SMALL_IMAGE; |
| 24 | |
Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 25 | import android.app.PendingIntent; |
| 26 | import android.content.ContentResolver; |
| 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
| 29 | import android.graphics.drawable.Icon; |
| 30 | import android.net.Uri; |
| 31 | import android.net.wifi.WifiManager; |
| 32 | import android.provider.Settings; |
| 33 | import android.text.SpannableString; |
| 34 | import android.text.format.DateUtils; |
| 35 | import android.text.style.ForegroundColorSpan; |
| 36 | |
Mady Mellor | bb51b59 | 2018-03-08 17:07:24 -0800 | [diff] [blame^] | 37 | import java.util.Arrays; |
| 38 | |
Aurimas Liutikas | 85ef144 | 2018-03-02 14:16:21 -0800 | [diff] [blame] | 39 | import androidx.slice.Slice; |
| 40 | import androidx.slice.builders.GridBuilder; |
| 41 | import androidx.slice.builders.ListBuilder; |
| 42 | import androidx.slice.builders.MessagingSliceBuilder; |
| 43 | import androidx.slice.builders.SliceAction; |
| 44 | import androidx.slice.view.test.R; |
Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Examples of using slice template builders. |
| 48 | */ |
| 49 | public 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 Mellor | 53380fe | 2018-02-14 16:31:38 -0800 | [diff] [blame] | 57 | public static final String[] URI_PATHS = {"message", "wifi", "wifi2", "note", "ride", "toggle", |
Mady Mellor | 55312b3 | 2018-02-22 19:02:15 -0800 | [diff] [blame] | 58 | "toggle2", "contact", "gallery", "subscription", "subscription2", "weather"}; |
Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 59 | |
| 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 Liutikas | 85ef144 | 2018-03-02 14:16:21 -0800 | [diff] [blame] | 76 | .authority("androidx.slice.view.test") |
Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 77 | .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 Mellor | 53380fe | 2018-02-14 16:31:38 -0800 | [diff] [blame] | 87 | return createWifiSlice(sliceUri, false /* customSeeMore */); |
| 88 | case "/wifi2": |
| 89 | return createWifiSlice(sliceUri, true /* customSeeMore */); |
Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 90 | 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 Mellor | 55312b3 | 2018-02-22 19:02:15 -0800 | [diff] [blame] | 102 | case "/subscription": |
| 103 | return createSubSlice(sliceUri, false /* customSeeMore */); |
| 104 | case "/subscription2": |
| 105 | return createSubSlice(sliceUri, true /* customSeeMore */); |
Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 106 | case "/weather": |
| 107 | return createWeather(sliceUri); |
| 108 | } |
| 109 | throw new IllegalArgumentException("Unknown uri " + sliceUri); |
| 110 | } |
| 111 | |
| 112 | private Slice createWeather(Uri sliceUri) { |
Mady Mellor | ea77b88 | 2018-02-12 11:49:19 -0800 | [diff] [blame] | 113 | SliceAction primaryAction = new SliceAction(getBroadcastIntent(ACTION_TOAST, |
| 114 | "open weather app"), Icon.createWithResource(getContext(), R.drawable.weather_1), |
| 115 | "Weather is happening!"); |
Mady Mellor | 9b0a49d | 2018-02-13 13:55:06 -0800 | [diff] [blame] | 116 | ListBuilder b = new ListBuilder(getContext(), sliceUri); |
| 117 | GridBuilder gb = new GridBuilder(b); |
| 118 | gb.setPrimaryAction(primaryAction); |
| 119 | gb.addCell(new GridBuilder.CellBuilder(gb) |
Mady Mellor | 5ca9e16 | 2018-02-22 15:19:50 -0800 | [diff] [blame] | 120 | .addImage(Icon.createWithResource(getContext(), R.drawable.weather_1), |
Mady Mellor | 588bcc1 | 2018-03-02 13:56:51 -0800 | [diff] [blame] | 121 | SMALL_IMAGE) |
Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 122 | .addText("MON") |
| 123 | .addTitleText("69\u00B0")) |
Mady Mellor | 9b0a49d | 2018-02-13 13:55:06 -0800 | [diff] [blame] | 124 | .addCell(new GridBuilder.CellBuilder(gb) |
Mady Mellor | 5ca9e16 | 2018-02-22 15:19:50 -0800 | [diff] [blame] | 125 | .addImage(Icon.createWithResource(getContext(), R.drawable.weather_2), |
Mady Mellor | 588bcc1 | 2018-03-02 13:56:51 -0800 | [diff] [blame] | 126 | SMALL_IMAGE) |
Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 127 | .addText("TUE") |
| 128 | .addTitleText("71\u00B0")) |
Mady Mellor | 9b0a49d | 2018-02-13 13:55:06 -0800 | [diff] [blame] | 129 | .addCell(new GridBuilder.CellBuilder(gb) |
Mady Mellor | 5ca9e16 | 2018-02-22 15:19:50 -0800 | [diff] [blame] | 130 | .addImage(Icon.createWithResource(getContext(), R.drawable.weather_3), |
Mady Mellor | 588bcc1 | 2018-03-02 13:56:51 -0800 | [diff] [blame] | 131 | SMALL_IMAGE) |
Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 132 | .addText("WED") |
| 133 | .addTitleText("76\u00B0")) |
Mady Mellor | 9b0a49d | 2018-02-13 13:55:06 -0800 | [diff] [blame] | 134 | .addCell(new GridBuilder.CellBuilder(gb) |
Mady Mellor | 5ca9e16 | 2018-02-22 15:19:50 -0800 | [diff] [blame] | 135 | .addImage(Icon.createWithResource(getContext(), R.drawable.weather_4), |
Mady Mellor | 588bcc1 | 2018-03-02 13:56:51 -0800 | [diff] [blame] | 136 | SMALL_IMAGE) |
Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 137 | .addText("THU") |
| 138 | .addTitleText("72\u00B0")) |
Mady Mellor | 9b0a49d | 2018-02-13 13:55:06 -0800 | [diff] [blame] | 139 | .addCell(new GridBuilder.CellBuilder(gb) |
Mady Mellor | 5ca9e16 | 2018-02-22 15:19:50 -0800 | [diff] [blame] | 140 | .addImage(Icon.createWithResource(getContext(), R.drawable.weather_1), |
Mady Mellor | 588bcc1 | 2018-03-02 13:56:51 -0800 | [diff] [blame] | 141 | SMALL_IMAGE) |
Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 142 | .addText("FRI") |
Mady Mellor | 9b0a49d | 2018-02-13 13:55:06 -0800 | [diff] [blame] | 143 | .addTitleText("68\u00B0")); |
| 144 | return b.addGrid(gb).build(); |
Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | private Slice createGallery(Uri sliceUri) { |
Mady Mellor | 9b0a49d | 2018-02-13 13:55:06 -0800 | [diff] [blame] | 148 | ListBuilder b = new ListBuilder(getContext(), sliceUri); |
| 149 | GridBuilder gb = new GridBuilder(b); |
Mady Mellor | 55312b3 | 2018-02-22 19:02:15 -0800 | [diff] [blame] | 150 | 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 Mellor | 9b0a49d | 2018-02-13 13:55:06 -0800 | [diff] [blame] | 155 | .addCell(new GridBuilder.CellBuilder(gb) |
Mady Mellor | 55312b3 | 2018-02-22 19:02:15 -0800 | [diff] [blame] | 156 | .addImage(Icon.createWithResource(getContext(), R.drawable.slices_2), |
| 157 | LARGE_IMAGE)) |
Mady Mellor | 9b0a49d | 2018-02-13 13:55:06 -0800 | [diff] [blame] | 158 | .addCell(new GridBuilder.CellBuilder(gb) |
Mady Mellor | 55312b3 | 2018-02-22 19:02:15 -0800 | [diff] [blame] | 159 | .addImage(Icon.createWithResource(getContext(), R.drawable.slices_3), |
| 160 | LARGE_IMAGE)) |
Mady Mellor | 9b0a49d | 2018-02-13 13:55:06 -0800 | [diff] [blame] | 161 | .addCell(new GridBuilder.CellBuilder(gb) |
Mady Mellor | 55312b3 | 2018-02-22 19:02:15 -0800 | [diff] [blame] | 162 | .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 Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 215 | } |
| 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 Mellor | 5ca9e16 | 2018-02-22 15:19:50 -0800 | [diff] [blame] | 228 | .addImage(Icon.createWithResource(getContext(), R.drawable.ic_call), |
Mady Mellor | 588bcc1 | 2018-03-02 13:56:51 -0800 | [diff] [blame] | 229 | ICON_IMAGE) |
Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 230 | .addText("Call") |
| 231 | .setContentIntent(getBroadcastIntent(ACTION_TOAST, "call"))) |
| 232 | .addCell(new GridBuilder.CellBuilder(gb) |
Mady Mellor | 5ca9e16 | 2018-02-22 15:19:50 -0800 | [diff] [blame] | 233 | .addImage(Icon.createWithResource(getContext(), R.drawable.ic_text), |
Mady Mellor | 588bcc1 | 2018-03-02 13:56:51 -0800 | [diff] [blame] | 234 | ICON_IMAGE) |
Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 235 | .addText("Text") |
| 236 | .setContentIntent(getBroadcastIntent(ACTION_TOAST, "text"))) |
| 237 | .addCell(new GridBuilder.CellBuilder(gb) |
Mady Mellor | 5ca9e16 | 2018-02-22 15:19:50 -0800 | [diff] [blame] | 238 | .addImage(Icon.createWithResource(getContext(), R.drawable.ic_video), |
Mady Mellor | 588bcc1 | 2018-03-02 13:56:51 -0800 | [diff] [blame] | 239 | ICON_IMAGE) |
Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 240 | .setContentIntent(getBroadcastIntent(ACTION_TOAST, "video")) |
| 241 | .addText("Video")) |
| 242 | .addCell(new GridBuilder.CellBuilder(gb) |
Mady Mellor | 5ca9e16 | 2018-02-22 15:19:50 -0800 | [diff] [blame] | 243 | .addImage(Icon.createWithResource(getContext(), R.drawable.ic_email), |
Mady Mellor | 588bcc1 | 2018-03-02 13:56:51 -0800 | [diff] [blame] | 244 | ICON_IMAGE) |
Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 245 | .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 Mellor | af76b3b | 2018-02-07 10:31:15 -0800 | [diff] [blame] | 277 | .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 Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 286 | .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 Mellor | af76b3b | 2018-02-07 10:31:15 -0800 | [diff] [blame] | 297 | 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 Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 319 | } |
| 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 Mellor | af76b3b | 2018-02-07 10:31:15 -0800 | [diff] [blame] | 327 | .addEndItem(new SliceAction(getBroadcastIntent(ACTION_TOAST, "star toggled"), |
| 328 | Icon.createWithResource(getContext(), R.drawable.toggle_star), |
| 329 | "Toggle star", true /* isChecked */))) |
Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 330 | .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 Mellor | af76b3b | 2018-02-07 10:31:15 -0800 | [diff] [blame] | 339 | .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 Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 347 | .build(); |
| 348 | } |
| 349 | |
Mady Mellor | 53380fe | 2018-02-14 16:31:38 -0800 | [diff] [blame] | 350 | private Slice createWifiSlice(Uri sliceUri, boolean customSeeMore) { |
Jason Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 351 | // 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 Mellor | 53380fe | 2018-02-14 16:31:38 -0800 | [diff] [blame] | 372 | ListBuilder lb = new ListBuilder(getContext(), sliceUri); |
Mady Mellor | af76b3b | 2018-02-07 10:31:15 -0800 | [diff] [blame] | 373 | SliceAction primaryAction = new SliceAction(getIntent(Settings.ACTION_WIFI_SETTINGS), |
| 374 | Icon.createWithResource(getContext(), R.drawable.ic_wifi), "Wi-fi Settings"); |
Mady Mellor | 53380fe | 2018-02-14 16:31:38 -0800 | [diff] [blame] | 375 | lb.setColor(0xff4285f4); |
| 376 | lb.addRow(new ListBuilder.RowBuilder(lb) |
| 377 | .setTitle("Wi-fi") |
Mady Mellor | 588bcc1 | 2018-03-02 13:56:51 -0800 | [diff] [blame] | 378 | .setTitleItem(Icon.createWithResource(getContext(), R.drawable.ic_wifi), ICON_IMAGE) |
Mady Mellor | 53380fe | 2018-02-14 16:31:38 -0800 | [diff] [blame] | 379 | .setSubtitle(state) |
| 380 | .addEndItem(new SliceAction(getBroadcastIntent(ACTION_WIFI_CHANGED, null), |
| 381 | "Toggle wifi", finalWifiEnabled)) |
| 382 | .setPrimaryAction(primaryAction)); |
| 383 | |
Mady Mellor | bb51b59 | 2018-03-08 17:07:24 -0800 | [diff] [blame^] | 384 | // Add keywords |
| 385 | String[] keywords = new String[] {"internet", "wifi", "data", "network"}; |
| 386 | lb.setKeywords(Arrays.asList(keywords)); |
| 387 | |
Mady Mellor | 53380fe | 2018-02-14 16:31:38 -0800 | [diff] [blame] | 388 | // 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 Mellor | 588bcc1 | 2018-03-02 13:56:51 -0800 | [diff] [blame] | 395 | ListBuilder.RowBuilder rb = new ListBuilder.RowBuilder(lb) |
| 396 | .setTitleItem(icon, ICON_IMAGE) |
Mady Mellor | 53380fe | 2018-02-14 16:31:38 -0800 | [diff] [blame] | 397 | .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 Monk | 8f8784c | 2018-01-22 21:29:42 -0500 | [diff] [blame] | 416 | } |
| 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 | } |