Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 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 | |
| 17 | package androidx.app.slice.widget; |
| 18 | |
| 19 | import static android.app.slice.Slice.HINT_TITLE; |
Amin Shaikh | f2cdc3a | 2018-01-17 09:35:23 -0500 | [diff] [blame] | 20 | import static android.app.slice.Slice.SUBTYPE_SLIDER; |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 21 | import static android.app.slice.SliceItem.FORMAT_ACTION; |
| 22 | import static android.app.slice.SliceItem.FORMAT_IMAGE; |
Amin Shaikh | f2cdc3a | 2018-01-17 09:35:23 -0500 | [diff] [blame] | 23 | import static android.app.slice.SliceItem.FORMAT_INT; |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 24 | import static android.app.slice.SliceItem.FORMAT_REMOTE_INPUT; |
| 25 | import static android.app.slice.SliceItem.FORMAT_SLICE; |
| 26 | import static android.app.slice.SliceItem.FORMAT_TEXT; |
| 27 | import static android.app.slice.SliceItem.FORMAT_TIMESTAMP; |
| 28 | |
| 29 | import android.support.annotation.Nullable; |
| 30 | import android.support.annotation.RestrictTo; |
| 31 | import android.util.Log; |
| 32 | |
| 33 | import java.util.ArrayList; |
| 34 | import java.util.List; |
| 35 | |
| 36 | import androidx.app.slice.SliceItem; |
| 37 | import androidx.app.slice.core.SliceHints; |
| 38 | import androidx.app.slice.core.SliceQuery; |
| 39 | |
| 40 | /** |
| 41 | * Extracts information required to present content in a row format from a slice. |
| 42 | * @hide |
| 43 | */ |
| 44 | @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) |
| 45 | public class RowContent { |
| 46 | private static final String TAG = "RowContent"; |
| 47 | |
| 48 | private SliceItem mContentIntent; |
| 49 | private SliceItem mStartItem; |
| 50 | private SliceItem mTitleItem; |
| 51 | private SliceItem mSubtitleItem; |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 52 | private ArrayList<SliceItem> mEndItems = new ArrayList<>(); |
Amin Shaikh | bfeddba | 2018-01-10 14:51:13 -0500 | [diff] [blame] | 53 | private boolean mEndItemsContainAction; |
Amin Shaikh | f2cdc3a | 2018-01-17 09:35:23 -0500 | [diff] [blame] | 54 | private SliceItem mSlider; |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 55 | |
| 56 | public RowContent(SliceItem rowSlice, boolean showStartItem) { |
| 57 | populate(rowSlice, showStartItem); |
| 58 | } |
| 59 | |
| 60 | /** |
Mady Mellor | 3220613 | 2017-12-21 22:22:26 -0800 | [diff] [blame] | 61 | * Resets the content. |
| 62 | */ |
| 63 | public void reset() { |
| 64 | mContentIntent = null; |
| 65 | mStartItem = null; |
| 66 | mTitleItem = null; |
| 67 | mSubtitleItem = null; |
Mady Mellor | 3220613 | 2017-12-21 22:22:26 -0800 | [diff] [blame] | 68 | mEndItems.clear(); |
| 69 | } |
| 70 | |
| 71 | /** |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 72 | * @return whether this row has content that is valid to display. |
| 73 | */ |
| 74 | public boolean populate(SliceItem rowSlice, boolean showStartItem) { |
Mady Mellor | 3220613 | 2017-12-21 22:22:26 -0800 | [diff] [blame] | 75 | reset(); |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 76 | if (!isValidRow(rowSlice)) { |
| 77 | Log.w(TAG, "Provided SliceItem is invalid for RowContent"); |
| 78 | return false; |
| 79 | } |
| 80 | // Filter anything not viable for displaying in a row |
Amin Shaikh | f2cdc3a | 2018-01-17 09:35:23 -0500 | [diff] [blame] | 81 | ArrayList<SliceItem> rowItems = filterInvalidItems(rowSlice); |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 82 | // If we've only got one item that's a slice / action use those items instead |
| 83 | if (rowItems.size() == 1 && (FORMAT_ACTION.equals(rowItems.get(0).getFormat()) |
| 84 | || FORMAT_SLICE.equals(rowItems.get(0).getFormat()))) { |
| 85 | if (isValidRow(rowItems.get(0))) { |
| 86 | rowSlice = rowItems.get(0); |
Amin Shaikh | f2cdc3a | 2018-01-17 09:35:23 -0500 | [diff] [blame] | 87 | rowItems = filterInvalidItems(rowSlice); |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | // Content intent |
| 91 | if (FORMAT_ACTION.equals(rowSlice.getFormat())) { |
| 92 | mContentIntent = rowSlice; |
| 93 | } |
Amin Shaikh | f2cdc3a | 2018-01-17 09:35:23 -0500 | [diff] [blame] | 94 | if (SUBTYPE_SLIDER.equals(rowSlice.getSubType())) { |
| 95 | mSlider = rowSlice; |
| 96 | } |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 97 | if (rowItems.size() > 0) { |
| 98 | // Start item |
Mady Mellor | 238b9b6 | 2018-01-09 16:15:40 -0800 | [diff] [blame] | 99 | if (isStartType(rowItems.get(0))) { |
| 100 | if (showStartItem) { |
| 101 | mStartItem = rowItems.get(0); |
| 102 | } |
| 103 | rowItems.remove(0); |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 104 | } |
| 105 | // Text + end items |
Mady Mellor | 238b9b6 | 2018-01-09 16:15:40 -0800 | [diff] [blame] | 106 | ArrayList<SliceItem> endItems = new ArrayList<>(); |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 107 | for (int i = 0; i < rowItems.size(); i++) { |
| 108 | final SliceItem item = rowItems.get(i); |
| 109 | if (FORMAT_TEXT.equals(item.getFormat())) { |
| 110 | if ((mTitleItem == null || !mTitleItem.hasHint(HINT_TITLE)) |
| 111 | && item.hasHint(HINT_TITLE)) { |
| 112 | mTitleItem = item; |
| 113 | } else if (mSubtitleItem == null) { |
| 114 | mSubtitleItem = item; |
| 115 | } |
| 116 | } else { |
Mady Mellor | 238b9b6 | 2018-01-09 16:15:40 -0800 | [diff] [blame] | 117 | endItems.add(item); |
| 118 | } |
| 119 | } |
| 120 | // Special rules for end items: only one timestamp, can't be mixture of icons / actions |
| 121 | boolean hasTimestamp = mStartItem != null |
| 122 | && FORMAT_TIMESTAMP.equals(mStartItem.getFormat()); |
| 123 | String desiredFormat = null; |
| 124 | for (int i = 0; i < endItems.size(); i++) { |
| 125 | final SliceItem item = endItems.get(i); |
| 126 | if (FORMAT_TIMESTAMP.equals(item.getFormat())) { |
| 127 | if (!hasTimestamp) { |
| 128 | hasTimestamp = true; |
| 129 | mEndItems.add(item); |
| 130 | } |
| 131 | } else if (desiredFormat == null) { |
| 132 | desiredFormat = item.getFormat(); |
| 133 | mEndItems.add(item); |
| 134 | } else if (desiredFormat.equals(item.getFormat())) { |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 135 | mEndItems.add(item); |
Amin Shaikh | bfeddba | 2018-01-10 14:51:13 -0500 | [diff] [blame] | 136 | mEndItemsContainAction |= FORMAT_ACTION.equals(item.getFormat()); |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | } |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 140 | return isValid(); |
| 141 | } |
| 142 | |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 143 | /** |
Amin Shaikh | f2cdc3a | 2018-01-17 09:35:23 -0500 | [diff] [blame] | 144 | * @return the {@link SliceItem} representing the slider in this row; can be null |
| 145 | */ |
| 146 | @Nullable |
| 147 | public SliceItem getSlider() { |
| 148 | return mSlider; |
| 149 | } |
| 150 | |
| 151 | /** |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 152 | * @return whether this row has content that is valid to display. |
| 153 | */ |
| 154 | public boolean isValid() { |
| 155 | return mStartItem != null |
| 156 | || mTitleItem != null |
| 157 | || mSubtitleItem != null |
| 158 | || mEndItems.size() > 0; |
| 159 | } |
| 160 | |
| 161 | @Nullable |
| 162 | public SliceItem getContentIntent() { |
| 163 | return mContentIntent; |
| 164 | } |
| 165 | |
| 166 | @Nullable |
| 167 | public SliceItem getStartItem() { |
| 168 | return mStartItem; |
| 169 | } |
| 170 | |
| 171 | @Nullable |
| 172 | public SliceItem getTitleItem() { |
| 173 | return mTitleItem; |
| 174 | } |
| 175 | |
| 176 | @Nullable |
| 177 | public SliceItem getSubtitleItem() { |
| 178 | return mSubtitleItem; |
| 179 | } |
| 180 | |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 181 | public ArrayList<SliceItem> getEndItems() { |
| 182 | return mEndItems; |
| 183 | } |
| 184 | |
| 185 | /** |
Amin Shaikh | bfeddba | 2018-01-10 14:51:13 -0500 | [diff] [blame] | 186 | * @return whether {@link #getEndItems()} contains a SliceItem with FORMAT_ACTION |
| 187 | */ |
| 188 | public boolean endItemsContainAction() { |
| 189 | return mEndItemsContainAction; |
| 190 | } |
| 191 | |
| 192 | /** |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 193 | * @return whether this is a valid item to use to populate a row of content. |
| 194 | */ |
Amin Shaikh | f2cdc3a | 2018-01-17 09:35:23 -0500 | [diff] [blame] | 195 | private static boolean isValidRow(SliceItem rowSlice) { |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 196 | // Must be slice or action |
Amin Shaikh | f2cdc3a | 2018-01-17 09:35:23 -0500 | [diff] [blame] | 197 | if (FORMAT_SLICE.equals(rowSlice.getFormat()) |
| 198 | || FORMAT_ACTION.equals(rowSlice.getFormat())) { |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 199 | // Must have at least one legitimate child |
Amin Shaikh | f2cdc3a | 2018-01-17 09:35:23 -0500 | [diff] [blame] | 200 | List<SliceItem> rowItems = rowSlice.getSlice().getItems(); |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 201 | for (int i = 0; i < rowItems.size(); i++) { |
Amin Shaikh | f2cdc3a | 2018-01-17 09:35:23 -0500 | [diff] [blame] | 202 | if (isValidRowContent(rowSlice, rowItems.get(i))) { |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 203 | return true; |
| 204 | } |
| 205 | } |
| 206 | } |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 207 | return false; |
| 208 | } |
| 209 | |
Amin Shaikh | f2cdc3a | 2018-01-17 09:35:23 -0500 | [diff] [blame] | 210 | private static ArrayList<SliceItem> filterInvalidItems(SliceItem rowSlice) { |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 211 | ArrayList<SliceItem> filteredList = new ArrayList<>(); |
Amin Shaikh | f2cdc3a | 2018-01-17 09:35:23 -0500 | [diff] [blame] | 212 | for (SliceItem i : rowSlice.getSlice().getItems()) { |
| 213 | if (isValidRowContent(rowSlice, i)) { |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 214 | filteredList.add(i); |
| 215 | } |
| 216 | } |
| 217 | return filteredList; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * @return whether this item has valid content to display in a row. |
| 222 | */ |
Amin Shaikh | f2cdc3a | 2018-01-17 09:35:23 -0500 | [diff] [blame] | 223 | private static boolean isValidRowContent(SliceItem slice, SliceItem item) { |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 224 | // TODO -- filter for shortcut once that's in |
| 225 | final String itemFormat = item.getFormat(); |
| 226 | // Must be a format that is presentable |
| 227 | return FORMAT_TEXT.equals(itemFormat) |
| 228 | || FORMAT_IMAGE.equals(itemFormat) |
| 229 | || FORMAT_TIMESTAMP.equals(itemFormat) |
| 230 | || FORMAT_REMOTE_INPUT.equals(itemFormat) |
Amin Shaikh | f2cdc3a | 2018-01-17 09:35:23 -0500 | [diff] [blame] | 231 | || FORMAT_ACTION.equals(itemFormat) |
| 232 | || (FORMAT_INT.equals(itemFormat) && SUBTYPE_SLIDER.equals(slice.getSubType())); |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /** |
| 236 | * @return Whether this item is appropriate to be considered a "start" item, i.e. go in the |
| 237 | * front slot of a row. |
| 238 | */ |
| 239 | private static boolean isStartType(SliceItem item) { |
| 240 | final String type = item.getFormat(); |
| 241 | return (!item.hasHint(SliceHints.SUBTYPE_TOGGLE) |
Mady Mellor | 6b5cd61 | 2017-12-14 11:36:59 -0800 | [diff] [blame] | 242 | && (FORMAT_ACTION.equals(type) && (SliceQuery.find(item, FORMAT_IMAGE) != null))) |
| 243 | || FORMAT_IMAGE.equals(type) |
| 244 | || FORMAT_TIMESTAMP.equals(type); |
| 245 | } |
| 246 | } |