summaryrefslogtreecommitdiff
path: root/TwoPanelSettingsLib/src/com/android/tv/twopanelsettings/slices/SlicePreferencesUtil.java
blob: b45f89132f154c60069c9b722cbc81c0f08ac43b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.twopanelsettings.slices;

import static android.app.slice.Slice.HINT_PARTIAL;
import static android.app.slice.Slice.HINT_SUMMARY;
import static android.app.slice.Slice.HINT_TITLE;
import static android.app.slice.Slice.SUBTYPE_CONTENT_DESCRIPTION;
import static android.app.slice.SliceItem.FORMAT_ACTION;
import static android.app.slice.SliceItem.FORMAT_IMAGE;
import static android.app.slice.SliceItem.FORMAT_INT;
import static android.app.slice.SliceItem.FORMAT_LONG;
import static android.app.slice.SliceItem.FORMAT_SLICE;
import static android.app.slice.SliceItem.FORMAT_TEXT;

import static com.android.tv.twopanelsettings.slices.HasCustomContentDescription.CONTENT_DESCRIPTION_SEPARATOR;
import static com.android.tv.twopanelsettings.slices.SlicesConstants.CHECKMARK;
import static com.android.tv.twopanelsettings.slices.SlicesConstants.EXTRA_ACTION_ID;
import static com.android.tv.twopanelsettings.slices.SlicesConstants.EXTRA_ADD_INFO_STATUS;
import static com.android.tv.twopanelsettings.slices.SlicesConstants.EXTRA_PAGE_ID;
import static com.android.tv.twopanelsettings.slices.SlicesConstants.EXTRA_PREFERENCE_INFO_IMAGE;
import static com.android.tv.twopanelsettings.slices.SlicesConstants.EXTRA_PREFERENCE_INFO_STATUS;
import static com.android.tv.twopanelsettings.slices.SlicesConstants.EXTRA_PREFERENCE_INFO_SUMMARY;
import static com.android.tv.twopanelsettings.slices.SlicesConstants.EXTRA_PREFERENCE_INFO_TEXT;
import static com.android.tv.twopanelsettings.slices.SlicesConstants.EXTRA_PREFERENCE_INFO_TITLE_ICON;
import static com.android.tv.twopanelsettings.slices.SlicesConstants.RADIO;
import static com.android.tv.twopanelsettings.slices.SlicesConstants.SEEKBAR;
import static com.android.tv.twopanelsettings.slices.SlicesConstants.SWITCH;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Pair;
import android.view.ContextThemeWrapper;

import androidx.core.graphics.drawable.IconCompat;
import androidx.preference.Preference;
import androidx.slice.Slice;
import androidx.slice.SliceItem;
import androidx.slice.core.SliceActionImpl;
import androidx.slice.core.SliceQuery;
import androidx.slice.widget.SliceContent;

import com.android.tv.twopanelsettings.IconUtil;
import com.android.tv.twopanelsettings.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Generate corresponding preference based upon the slice data.
 */
public final class SlicePreferencesUtil {

    static Preference getPreference(SliceItem item, ContextThemeWrapper contextThemeWrapper,
            String className, boolean isTwoPanel) {
        Preference preference = null;
        if (item == null) {
            return null;
        }
        Data data = extract(item);
        if (item.getSubType() != null) {
            String subType = item.getSubType();
            if (subType.equals(SlicesConstants.TYPE_PREFERENCE)
                    || subType.equals(SlicesConstants.TYPE_PREFERENCE_EMBEDDED)
                    || subType.equals(SlicesConstants.TYPE_PREFERENCE_EMBEDDED_PLACEHOLDER)) {
                // TODO: Figure out all the possible cases and reorganize the logic
                if (data.mInfoItems.size() > 0) {
                    preference = new InfoPreference(
                                contextThemeWrapper, getInfoList(data.mInfoItems));
                } else if (data.mIntentItem != null) {
                    SliceActionImpl action = new SliceActionImpl(data.mIntentItem);
                    if (action != null) {
                        // Currently if we don't set icon for the SliceAction, slice lib will
                        // automatically treat it as a toggle. To distinguish preference action and
                        // toggle action, we need to add a subtype if this is a preference action.
                        preference = new SlicePreference(contextThemeWrapper);
                        ((SlicePreference) preference).setSliceAction(action);
                        ((SlicePreference) preference).setActionId(getActionId(item));
                        if (data.mFollowupIntentItem != null) {
                            SliceActionImpl followUpAction =
                                    new SliceActionImpl(data.mFollowupIntentItem);
                            ((SlicePreference) preference).setFollowupSliceAction(followUpAction);
                        }
                    }
                } else if (data.mEndItems.size() > 0 && data.mEndItems.get(0) != null) {
                    SliceActionImpl action = new SliceActionImpl(data.mEndItems.get(0));
                    if (action != null) {
                        int buttonStyle = SlicePreferencesUtil.getButtonStyle(item);
                        switch (buttonStyle) {
                            case CHECKMARK :
                                preference = new SliceCheckboxPreference(
                                        contextThemeWrapper, action);
                                break;
                            case SWITCH :
                                preference = new SliceSwitchPreference(contextThemeWrapper, action);
                                break;
                            case RADIO:
                                preference = new SliceRadioPreference(contextThemeWrapper, action);
                                preference.setLayoutResource(R.layout.preference_reversed_widget);
                                if (getRadioGroup(item) != null) {
                                    ((SliceRadioPreference) preference).setRadioGroup(
                                            getRadioGroup(item).toString());
                                }
                                break;
                            case SEEKBAR :
                                int min = SlicePreferencesUtil.getSeekbarMin(item);
                                int max = SlicePreferencesUtil.getSeekbarMax(item);
                                int value = SlicePreferencesUtil.getSeekbarValue(item);
                                preference = new SliceSeekbarPreference(
                                        contextThemeWrapper, action, min, max, value);
                                break;
                        }
                        if (preference instanceof HasSliceAction) {
                            ((HasSliceAction) preference).setActionId(getActionId(item));
                        }
                        if (data.mFollowupIntentItem != null) {
                            SliceActionImpl followUpAction =
                                    new SliceActionImpl(data.mFollowupIntentItem);
                            ((HasSliceAction) preference).setFollowupSliceAction(followUpAction);

                        }
                    }
                }

                CharSequence uri = getText(data.mTargetSliceItem);
                if (uri == null || TextUtils.isEmpty(uri)) {
                    if (preference == null) {
                        preference = new CustomContentDescriptionPreference(contextThemeWrapper);
                    }
                } else {
                    if (preference == null) {
                        if (subType.equals(SlicesConstants.TYPE_PREFERENCE_EMBEDDED_PLACEHOLDER)) {
                            preference = new EmbeddedSlicePreference(contextThemeWrapper,
                                    String.valueOf(uri));
                        } else {
                            preference = new SlicePreference(contextThemeWrapper);
                        }
                        if (hasEndIcon(data.mHasEndIconItem)) {
                            preference.setLayoutResource(R.layout.preference_reversed_icon);
                        }
                    }
                    ((HasSliceUri) preference).setUri(uri.toString());
                    if (preference instanceof HasSliceAction) {
                        ((HasSliceAction) preference).setActionId(getActionId(item));
                    }
                    preference.setFragment(className);
                }
            } else if (item.getSubType().equals(SlicesConstants.TYPE_PREFERENCE_CATEGORY)) {
                preference = new CustomContentDescriptionPreferenceCategory(contextThemeWrapper);
            }
        }

        if (preference != null) {
            boolean isEnabled = enabled(item);
            // Set whether preference is enabled.
            if (preference instanceof InfoPreference || !isEnabled) {
                preference.setEnabled(false);
            }
            // Set whether preference is selectable
            if (!selectable(item) || !isEnabled) {
                preference.setSelectable(false);
            }
            // Set the key for the preference
            CharSequence key = getKey(item);
            if (key != null) {
                preference.setKey(key.toString());
            }

            Icon icon = getIcon(data.mStartItem);
            if (icon != null) {
                boolean isIconNeedToBeProcessed =
                        SlicePreferencesUtil.isIconNeedsToBeProcessed(item);
                Drawable iconDrawable = icon.loadDrawable(contextThemeWrapper);
                if (isIconNeedToBeProcessed && isTwoPanel) {
                    preference.setIcon(IconUtil.getCompoundIcon(contextThemeWrapper, iconDrawable));
                } else {
                    preference.setIcon(iconDrawable);
                }
            }

            if (data.mTitleItem != null) {
                preference.setTitle(getText(data.mTitleItem));
            }

            //Set summary
            CharSequence subtitle =
                    data.mSubtitleItem != null ? data.mSubtitleItem.getText() : null;
            boolean subtitleExists = !TextUtils.isEmpty(subtitle)
                    || (data.mSubtitleItem != null && data.mSubtitleItem.hasHint(HINT_PARTIAL));
            if (subtitleExists) {
                preference.setSummary(subtitle);
            } else {
                if (data.mSummaryItem != null) {
                    preference.setSummary(getText(data.mSummaryItem));
                }
            }

            // Set preview info image and text
            CharSequence infoText = getInfoText(item);
            CharSequence infoSummary = getInfoSummary(item);
            boolean addInfoStatus = addInfoStatus(item);
            IconCompat infoImage = getInfoImage(item);
            IconCompat infoTitleIcon = getInfoTitleIcon(item);
            Bundle b = preference.getExtras();
            String fallbackInfoContentDescription = "";
            if (preference.getTitle() != null) {
                fallbackInfoContentDescription += preference.getTitle().toString();
            }
            if (infoImage != null) {
                b.putParcelable(EXTRA_PREFERENCE_INFO_IMAGE, infoImage.toIcon());
            }
            if (infoTitleIcon != null) {
                b.putParcelable(EXTRA_PREFERENCE_INFO_TITLE_ICON, infoTitleIcon.toIcon());
            }
            if (infoText != null) {
                if (preference instanceof SliceSwitchPreference && addInfoStatus) {
                    b.putBoolean(InfoFragment.EXTRA_INFO_HAS_STATUS, true);
                    b.putBoolean(EXTRA_PREFERENCE_INFO_STATUS,
                            ((SliceSwitchPreference) preference).isChecked());
                } else {
                    b.putBoolean(InfoFragment.EXTRA_INFO_HAS_STATUS, false);
                }
                b.putCharSequence(EXTRA_PREFERENCE_INFO_TEXT, infoText);
                if (preference.getTitle() != null
                        && !preference.getTitle().equals(infoText.toString())) {
                    fallbackInfoContentDescription +=
                            CONTENT_DESCRIPTION_SEPARATOR + infoText.toString();
                }

            }
            if (infoSummary != null) {
                b.putCharSequence(EXTRA_PREFERENCE_INFO_SUMMARY, infoSummary);
                fallbackInfoContentDescription +=
                        CONTENT_DESCRIPTION_SEPARATOR + infoSummary;
            }
            String contentDescription = getInfoContentDescription(item);
            // Respect the content description values provided by slice.
            // If not provided, for SlicePreference, SliceSwitchPreference,
            // CustomContentDescriptionPreference, use the fallback value.
            // Otherwise, do not set the contentDescription for preference. Rely on the talkback
            // framework to generate the value itself.
            if (!TextUtils.isEmpty(contentDescription)) {
                if (preference instanceof HasCustomContentDescription) {
                    ((HasCustomContentDescription) preference).setContentDescription(
                            contentDescription);
                }
            } else {
                if ((preference instanceof SlicePreference)
                        || (preference instanceof SliceSwitchPreference)
                        || (preference instanceof CustomContentDescriptionPreference)) {
                    ((HasCustomContentDescription) preference).setContentDescription(
                            fallbackInfoContentDescription);
                }
            }
            if (infoImage != null || infoText != null || infoSummary != null) {
                preference.setFragment(InfoFragment.class.getCanonicalName());
            }
        }

        return preference;
    }

    static class Data {
        SliceItem mStartItem;
        SliceItem mTitleItem;
        SliceItem mSubtitleItem;
        SliceItem mSummaryItem;
        SliceItem mTargetSliceItem;
        SliceItem mRadioGroupItem;
        SliceItem mIntentItem;
        SliceItem mFollowupIntentItem;
        SliceItem mHasEndIconItem;
        List<SliceItem> mEndItems = new ArrayList<>();
        List<SliceItem> mInfoItems = new ArrayList<>();
    }

    static Data extract(SliceItem sliceItem) {
        Data data = new Data();
        List<SliceItem> possibleStartItems =
                SliceQuery.findAll(sliceItem, null, HINT_TITLE, null);
        if (possibleStartItems.size() > 0) {
            // The start item will be at position 0 if it exists
            String format = possibleStartItems.get(0).getFormat();
            if ((FORMAT_ACTION.equals(format)
                    && SliceQuery.find(possibleStartItems.get(0), FORMAT_IMAGE) != null)
                    || FORMAT_SLICE.equals(format)
                    || FORMAT_LONG.equals(format)
                    || FORMAT_IMAGE.equals(format)) {
                data.mStartItem = possibleStartItems.get(0);
            }
        }

        List<SliceItem> items = sliceItem.getSlice().getItems();
        for (int i = 0; i < items.size(); i++) {
            final SliceItem item = items.get(i);
            String subType = item.getSubType();
            if (subType != null) {
                switch (subType) {
                    case SlicesConstants.SUBTYPE_INFO_PREFERENCE :
                        data.mInfoItems.add(item);
                        break;
                    case SlicesConstants.SUBTYPE_INTENT :
                        data.mIntentItem = item;
                        break;
                    case SlicesConstants.SUBTYPE_FOLLOWUP_INTENT :
                        data.mFollowupIntentItem = item;
                        break;
                    case SlicesConstants.TAG_TARGET_URI :
                        data.mTargetSliceItem = item;
                        break;
                    case SlicesConstants.EXTRA_HAS_END_ICON:
                        data.mHasEndIconItem = item;
                        break;
                }
            } else if (FORMAT_TEXT.equals(item.getFormat()) && (item.getSubType() == null)) {
                if ((data.mTitleItem == null || !data.mTitleItem.hasHint(HINT_TITLE))
                        && item.hasHint(HINT_TITLE) && !item.hasHint(HINT_SUMMARY)) {
                    data.mTitleItem = item;
                } else if (data.mSubtitleItem == null && !item.hasHint(HINT_SUMMARY)) {
                    data.mSubtitleItem = item;
                } else if (data.mSummaryItem == null && item.hasHint(HINT_SUMMARY)) {
                    data.mSummaryItem = item;
                }
            } else {
                data.mEndItems.add(item);
            }
        }
        data.mEndItems.remove(data.mStartItem);
        return data;
    }

    private static List<Pair<CharSequence, CharSequence>> getInfoList(List<SliceItem> sliceItems) {
        List<Pair<CharSequence, CharSequence>> infoList = new ArrayList<>();
        for (SliceItem item : sliceItems) {
            Slice itemSlice = item.getSlice();
            if (itemSlice != null) {
                CharSequence title = null;
                CharSequence summary = null;
                for (SliceItem element : itemSlice.getItems()) {
                    if (element.getHints().contains(HINT_TITLE)) {
                        title = element.getText();
                    } else if (element.getHints().contains(HINT_SUMMARY)) {
                        summary = element.getText();
                    }
                }
                infoList.add(new Pair<CharSequence, CharSequence>(title, summary));
            }
        }
        return infoList;
    }

    private static CharSequence getKey(SliceItem item) {
        SliceItem target = SliceQuery.findSubtype(item, FORMAT_TEXT, SlicesConstants.TAG_KEY);
        return target != null ? target.getText() : null;
    }

    private static CharSequence getRadioGroup(SliceItem item) {
        SliceItem target = SliceQuery.findSubtype(
                item, FORMAT_TEXT, SlicesConstants.TAG_RADIO_GROUP);
        return target != null ? target.getText() : null;
    }

    /**
     * Get the screen title item for the slice.
     * @param sliceItems list of SliceItem extracted from slice data.
     * @return screen title item.
     */
    static SliceItem getScreenTitleItem(List<SliceContent> sliceItems) {
        for (SliceContent contentItem : sliceItems)  {
            SliceItem item = contentItem.getSliceItem();
            if (item.getSubType() != null
                    && item.getSubType().equals(SlicesConstants.TYPE_PREFERENCE_SCREEN_TITLE)) {
                return item;
            }
        }
        return null;
    }

    static SliceItem getRedirectSlice(List<SliceContent> sliceItems) {
        for (SliceContent contentItem : sliceItems)  {
            SliceItem item = contentItem.getSliceItem();
            if (item.getSubType() != null
                    && item.getSubType().equals(SlicesConstants.TYPE_REDIRECTED_SLICE_URI)) {
                return item;
            }
        }
        return null;
    }

    static SliceItem getFocusedPreferenceItem(List<SliceContent> sliceItems) {
        for (SliceContent contentItem : sliceItems)  {
            SliceItem item = contentItem.getSliceItem();
            if (item.getSubType() != null
                    && item.getSubType().equals(SlicesConstants.TYPE_FOCUSED_PREFERENCE)) {
                return item;
            }
        }
        return null;
    }

    static SliceItem getEmbeddedItem(List<SliceContent> sliceItems) {
        for (SliceContent contentItem : sliceItems)  {
            SliceItem item = contentItem.getSliceItem();
            if (item.getSubType() != null
                    && item.getSubType().equals(SlicesConstants.TYPE_PREFERENCE_EMBEDDED)) {
                return item;
            }
        }
        return null;
    }

    private static boolean isIconNeedsToBeProcessed(SliceItem sliceItem) {
        List<SliceItem> items = sliceItem.getSlice().getItems();
        for (SliceItem item : items)  {
            if (item.getSubType() != null && item.getSubType().equals(
                    SlicesConstants.SUBTYPE_ICON_NEED_TO_BE_PROCESSED)) {
                return item.getInt() == 1;
            }
        }
        return false;
    }

    private static int getButtonStyle(SliceItem sliceItem) {
        List<SliceItem> items = sliceItem.getSlice().getItems();
        for (SliceItem item : items)  {
            if (item.getSubType() != null
                    && item.getSubType().equals(SlicesConstants.SUBTYPE_BUTTON_STYLE)) {
                return item.getInt();
            }
        }
        return -1;
    }

    private static int getSeekbarMin(SliceItem sliceItem) {
        List<SliceItem> items = sliceItem.getSlice().getItems();
        for (SliceItem item : items)  {
            if (item.getSubType() != null
                    && item.getSubType().equals(SlicesConstants.SUBTYPE_SEEKBAR_MIN)) {
                return item.getInt();
            }
        }
        return -1;
    }

    private static int getSeekbarMax(SliceItem sliceItem) {
        List<SliceItem> items = sliceItem.getSlice().getItems();
        for (SliceItem item : items)  {
            if (item.getSubType() != null
                    && item.getSubType().equals(SlicesConstants.SUBTYPE_SEEKBAR_MAX)) {
                return item.getInt();
            }
        }
        return -1;
    }

    private static int getSeekbarValue(SliceItem sliceItem) {
        List<SliceItem> items = sliceItem.getSlice().getItems();
        for (SliceItem item : items)  {
            if (item.getSubType() != null
                    && item.getSubType().equals(SlicesConstants.SUBTYPE_SEEKBAR_VALUE)) {
                return item.getInt();
            }
        }
        return -1;
    }

    private static boolean enabled(SliceItem sliceItem) {
        List<SliceItem> items = sliceItem.getSlice().getItems();
        for (SliceItem item : items)  {
            if (item.getSubType() != null
                    && item.getSubType().equals(SlicesConstants.SUBTYPE_IS_ENABLED)) {
                return item.getInt() == 1;
            }
        }
        return true;
    }

    private static boolean selectable(SliceItem sliceItem) {
        List<SliceItem> items = sliceItem.getSlice().getItems();
        for (SliceItem item : items)  {
            if (item.getSubType() != null
                    && item.getSubType().equals(SlicesConstants.SUBTYPE_IS_SELECTABLE)) {
                return item.getInt() == 1;
            }
        }
        return true;
    }

    private static boolean addInfoStatus(SliceItem sliceItem) {
        List<SliceItem> items = sliceItem.getSlice().getItems();
        for (SliceItem item : items)  {
            if (item.getSubType() != null
                    && item.getSubType().equals(EXTRA_ADD_INFO_STATUS)) {
                return item.getInt() == 1;
            }
        }
        return true;
    }

    private static boolean hasEndIcon(SliceItem item) {
        return item != null && item.getInt() > 0;
    }

    /**
     * Checks if custom content description should be forced to be used if provided. This function
     * can be extended with more cases if needed.
     *
     * @param item The {@link SliceItem} containing the necessary information.
     * @return <code>true</code> if custom content description should be used.
     */
    private static boolean shouldForceContentDescription(SliceItem sliceItem) {
        List<SliceItem> items = sliceItem.getSlice().getItems();
        for (SliceItem item : items)  {
            // Checks if an end icon has been set.
            if (item.getSubType() != null
                    && item.getSubType().equals(SlicesConstants.EXTRA_HAS_END_ICON)) {
                return hasEndIcon(item);
            }
        }
        return false;
    }

    /**
     * Get the text from the SliceItem.
     */
    static CharSequence getText(SliceItem item) {
        if (item == null) {
            return null;
        }
        return item.getText();
    }

    /** Get the icon from the SliceItem if available */
    static Icon getIcon(SliceItem startItem) {
        if (startItem != null && startItem.getSlice() != null
                && startItem.getSlice().getItems() != null
                && startItem.getSlice().getItems().size() > 0) {
            SliceItem iconItem = startItem.getSlice().getItems().get(0);
            if (FORMAT_IMAGE.equals(iconItem.getFormat())) {
                IconCompat icon = iconItem.getIcon();
                return icon.toIcon();
            }
        }
        return null;
    }

    static Uri getStatusPath(String uriString) {
        Uri statusUri = Uri.parse(uriString)
                .buildUpon().path("/" + SlicesConstants.PATH_STATUS).build();
        return statusUri;
    }

    static int getPageId(SliceItem item) {
        SliceItem target = SliceQuery.findSubtype(item, FORMAT_INT, EXTRA_PAGE_ID);
        return target != null ? target.getInt() : 0;
    }

    private static int getActionId(SliceItem item) {
        SliceItem target = SliceQuery.findSubtype(item, FORMAT_INT, EXTRA_ACTION_ID);
        return target != null ? target.getInt() : 0;
    }


    private static CharSequence getInfoText(SliceItem item) {
        SliceItem target = SliceQuery.findSubtype(item, FORMAT_TEXT, EXTRA_PREFERENCE_INFO_TEXT);
        return target != null ? target.getText() : null;
    }

    private static CharSequence getInfoSummary(SliceItem item) {
        SliceItem target = SliceQuery.findSubtype(item, FORMAT_TEXT, EXTRA_PREFERENCE_INFO_SUMMARY);
        return target != null ? target.getText() : null;
    }

    private static IconCompat getInfoImage(SliceItem item) {
        SliceItem target = SliceQuery.findSubtype(item, FORMAT_IMAGE, EXTRA_PREFERENCE_INFO_IMAGE);
        return target != null ? target.getIcon() : null;
    }

    private static IconCompat getInfoTitleIcon(SliceItem item) {
        SliceItem target = SliceQuery.findSubtype(
                item, FORMAT_IMAGE, EXTRA_PREFERENCE_INFO_TITLE_ICON);
        return target != null ? target.getIcon() : null;
    }

    /**
     * Get the content description from SliceItem if available
     */
    private static String getInfoContentDescription(
            SliceItem sliceItem) {
        List<SliceItem> items = sliceItem.getSlice().getItems();
        for (SliceItem item : items)  {
            if (item.getSubType() != null
                    && item.getSubType().equals(SUBTYPE_CONTENT_DESCRIPTION)) {
                return item.getText().toString();
            }
        }
        return null;
    }
}