Mady Mellor | 4575aa5 | 2017-09-29 09:49:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | |
Mady Mellor | 800ba36 | 2017-10-07 11:46:49 -0700 | [diff] [blame] | 17 | package android.app.slice.widget; |
Mady Mellor | 4575aa5 | 2017-09-29 09:49:59 -0700 | [diff] [blame] | 18 | |
| 19 | import android.app.PendingIntent.CanceledException; |
Jason Monk | d18651f | 2017-10-05 14:18:49 -0400 | [diff] [blame] | 20 | import android.app.slice.Slice; |
| 21 | import android.app.slice.SliceItem; |
| 22 | import android.app.slice.SliceQuery; |
Mady Mellor | 800ba36 | 2017-10-07 11:46:49 -0700 | [diff] [blame] | 23 | import android.app.slice.widget.LargeSliceAdapter.SliceListView; |
| 24 | import android.app.slice.widget.SliceView.SliceModeView; |
Mady Mellor | 4575aa5 | 2017-09-29 09:49:59 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.os.AsyncTask; |
Mady Mellor | 4575aa5 | 2017-09-29 09:49:59 -0700 | [diff] [blame] | 27 | import android.view.View; |
| 28 | import android.widget.ImageView; |
| 29 | import android.widget.LinearLayout; |
| 30 | import android.widget.TextView; |
| 31 | |
| 32 | import com.android.internal.R; |
| 33 | |
| 34 | import java.text.Format; |
| 35 | import java.text.SimpleDateFormat; |
| 36 | import java.util.ArrayList; |
Mady Mellor | 4575aa5 | 2017-09-29 09:49:59 -0700 | [diff] [blame] | 37 | import java.util.Date; |
| 38 | import java.util.List; |
| 39 | |
| 40 | /** |
| 41 | * Small template is also used to construct list items for use with {@link LargeTemplateView}. |
| 42 | * |
| 43 | * @hide |
| 44 | */ |
| 45 | public class SmallTemplateView extends SliceModeView implements SliceListView { |
| 46 | |
| 47 | private static final String TAG = "SmallTemplateView"; |
| 48 | |
| 49 | private int mIconSize; |
| 50 | private int mPadding; |
| 51 | |
| 52 | private LinearLayout mStartContainer; |
| 53 | private TextView mTitleText; |
| 54 | private TextView mSecondaryText; |
| 55 | private LinearLayout mEndContainer; |
| 56 | |
| 57 | public SmallTemplateView(Context context) { |
| 58 | super(context); |
| 59 | inflate(context, R.layout.slice_small_template, this); |
| 60 | mIconSize = getContext().getResources().getDimensionPixelSize(R.dimen.slice_icon_size); |
| 61 | mPadding = getContext().getResources().getDimensionPixelSize(R.dimen.slice_padding); |
| 62 | |
| 63 | mStartContainer = (LinearLayout) findViewById(android.R.id.icon_frame); |
| 64 | mTitleText = (TextView) findViewById(android.R.id.title); |
| 65 | mSecondaryText = (TextView) findViewById(android.R.id.summary); |
| 66 | mEndContainer = (LinearLayout) findViewById(android.R.id.widget_frame); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public String getMode() { |
| 71 | return SliceView.MODE_SMALL; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void setSliceItem(SliceItem slice) { |
| 76 | resetViews(); |
| 77 | SliceItem colorItem = SliceQuery.find(slice, SliceItem.TYPE_COLOR); |
| 78 | int color = colorItem != null ? colorItem.getColor() : -1; |
| 79 | |
| 80 | // Look for any title elements |
| 81 | List<SliceItem> titleItems = SliceQuery.findAll(slice, -1, Slice.HINT_TITLE, |
| 82 | null); |
| 83 | boolean hasTitleText = false; |
| 84 | boolean hasTitleItem = false; |
| 85 | for (int i = 0; i < titleItems.size(); i++) { |
| 86 | SliceItem item = titleItems.get(i); |
| 87 | if (!hasTitleItem) { |
| 88 | // icon, action icon, or timestamp |
| 89 | if (item.getType() == SliceItem.TYPE_ACTION) { |
| 90 | hasTitleItem = addIcon(item, color, mStartContainer); |
| 91 | } else if (item.getType() == SliceItem.TYPE_IMAGE) { |
| 92 | addIcon(item, color, mStartContainer); |
| 93 | hasTitleItem = true; |
| 94 | } else if (item.getType() == SliceItem.TYPE_TIMESTAMP) { |
| 95 | TextView tv = new TextView(getContext()); |
| 96 | tv.setText(convertTimeToString(item.getTimestamp())); |
| 97 | hasTitleItem = true; |
| 98 | } |
| 99 | } |
| 100 | if (!hasTitleText && item.getType() == SliceItem.TYPE_TEXT) { |
| 101 | mTitleText.setText(item.getText()); |
| 102 | hasTitleText = true; |
| 103 | } |
| 104 | if (hasTitleText && hasTitleItem) { |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | mTitleText.setVisibility(hasTitleText ? View.VISIBLE : View.GONE); |
| 109 | mStartContainer.setVisibility(hasTitleItem ? View.VISIBLE : View.GONE); |
| 110 | |
| 111 | if (slice.getType() != SliceItem.TYPE_SLICE) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | // Deal with remaining items |
| 116 | int itemCount = 0; |
| 117 | boolean hasSummary = false; |
| 118 | ArrayList<SliceItem> sliceItems = new ArrayList<SliceItem>( |
Jason Monk | d18651f | 2017-10-05 14:18:49 -0400 | [diff] [blame] | 119 | slice.getSlice().getItems()); |
Mady Mellor | 4575aa5 | 2017-09-29 09:49:59 -0700 | [diff] [blame] | 120 | for (int i = 0; i < sliceItems.size(); i++) { |
| 121 | SliceItem item = sliceItems.get(i); |
| 122 | if (!hasSummary && item.getType() == SliceItem.TYPE_TEXT |
| 123 | && !item.hasHint(Slice.HINT_TITLE)) { |
| 124 | // TODO -- Should combine all text items? |
| 125 | mSecondaryText.setText(item.getText()); |
| 126 | hasSummary = true; |
| 127 | } |
| 128 | if (itemCount <= 3) { |
| 129 | if (item.getType() == SliceItem.TYPE_ACTION) { |
| 130 | if (addIcon(item, color, mEndContainer)) { |
| 131 | itemCount++; |
| 132 | } |
| 133 | } else if (item.getType() == SliceItem.TYPE_IMAGE) { |
| 134 | addIcon(item, color, mEndContainer); |
| 135 | itemCount++; |
| 136 | } else if (item.getType() == SliceItem.TYPE_TIMESTAMP) { |
| 137 | TextView tv = new TextView(getContext()); |
| 138 | tv.setText(convertTimeToString(item.getTimestamp())); |
| 139 | mEndContainer.addView(tv); |
| 140 | itemCount++; |
| 141 | } else if (item.getType() == SliceItem.TYPE_SLICE) { |
Jason Monk | d18651f | 2017-10-05 14:18:49 -0400 | [diff] [blame] | 142 | List<SliceItem> subItems = item.getSlice().getItems(); |
| 143 | for (int j = 0; j < subItems.size(); j++) { |
| 144 | sliceItems.add(subItems.get(j)); |
Mady Mellor | 4575aa5 | 2017-09-29 09:49:59 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void setSlice(Slice slice) { |
Jason Monk | d18651f | 2017-10-05 14:18:49 -0400 | [diff] [blame] | 153 | setSliceItem(new SliceItem(slice, SliceItem.TYPE_SLICE, |
| 154 | slice.getHints().toArray(new String[slice.getHints().size()]))); |
Mady Mellor | 4575aa5 | 2017-09-29 09:49:59 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @return Whether an icon was added. |
| 159 | */ |
| 160 | private boolean addIcon(SliceItem sliceItem, int color, LinearLayout container) { |
| 161 | SliceItem image = null; |
| 162 | SliceItem action = null; |
| 163 | if (sliceItem.getType() == SliceItem.TYPE_ACTION) { |
| 164 | image = SliceQuery.find(sliceItem.getSlice(), SliceItem.TYPE_IMAGE); |
| 165 | action = sliceItem; |
| 166 | } else if (sliceItem.getType() == SliceItem.TYPE_IMAGE) { |
| 167 | image = sliceItem; |
| 168 | } |
| 169 | if (image != null) { |
| 170 | ImageView iv = new ImageView(getContext()); |
| 171 | iv.setImageIcon(image.getIcon()); |
| 172 | if (action != null) { |
| 173 | final SliceItem sliceAction = action; |
| 174 | iv.setOnClickListener(v -> AsyncTask.execute( |
| 175 | () -> { |
| 176 | try { |
| 177 | sliceAction.getAction().send(); |
| 178 | } catch (CanceledException e) { |
| 179 | e.printStackTrace(); |
| 180 | } |
| 181 | })); |
| 182 | iv.setBackground(SliceViewUtil.getDrawable(getContext(), |
| 183 | android.R.attr.selectableItemBackground)); |
| 184 | } |
| 185 | if (color != -1 && !sliceItem.hasHint(Slice.HINT_NO_TINT)) { |
| 186 | iv.setColorFilter(color); |
| 187 | } |
| 188 | container.addView(iv); |
| 189 | LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) iv.getLayoutParams(); |
| 190 | lp.width = mIconSize; |
| 191 | lp.height = mIconSize; |
| 192 | lp.setMarginStart(mPadding); |
| 193 | return true; |
| 194 | } |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | private String convertTimeToString(long time) { |
| 199 | // TODO -- figure out what format(s) we support |
| 200 | Date date = new Date(time); |
| 201 | Format format = new SimpleDateFormat("MM dd yyyy HH:mm:ss"); |
| 202 | return format.format(date); |
| 203 | } |
| 204 | |
| 205 | private void resetViews() { |
| 206 | mStartContainer.removeAllViews(); |
| 207 | mEndContainer.removeAllViews(); |
| 208 | mTitleText.setText(null); |
| 209 | mSecondaryText.setText(null); |
| 210 | } |
| 211 | } |