Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [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 | |
Jason Monk | dcb5e2f | 2017-11-15 20:19:43 -0500 | [diff] [blame] | 19 | import static android.app.slice.Slice.SUBTYPE_SOURCE; |
Jason Monk | 0c76d30 | 2017-11-21 14:02:27 -0500 | [diff] [blame] | 20 | import static android.app.slice.SliceItem.FORMAT_IMAGE; |
| 21 | import static android.app.slice.SliceItem.FORMAT_TEXT; |
| 22 | |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 23 | import android.annotation.TargetApi; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.graphics.Bitmap; |
| 26 | import android.graphics.Canvas; |
| 27 | import android.graphics.drawable.Drawable; |
| 28 | import android.support.annotation.RestrictTo; |
| 29 | import android.text.SpannableStringBuilder; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 30 | import android.util.TypedValue; |
| 31 | import android.widget.ImageView; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 32 | import android.widget.TextView; |
| 33 | |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 34 | import java.util.function.Consumer; |
| 35 | |
Mady Mellor | dc05204 | 2018-01-04 17:11:07 -0800 | [diff] [blame] | 36 | import androidx.app.slice.Slice; |
Jason Monk | dcb5e2f | 2017-11-15 20:19:43 -0500 | [diff] [blame] | 37 | import androidx.app.slice.SliceItem; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 38 | import androidx.app.slice.core.SliceQuery; |
| 39 | |
| 40 | /** |
| 41 | * @hide |
| 42 | */ |
| 43 | @RestrictTo(RestrictTo.Scope.LIBRARY) |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 44 | @TargetApi(24) |
Mady Mellor | dc05204 | 2018-01-04 17:11:07 -0800 | [diff] [blame] | 45 | public class MessageView extends SliceChildView { |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 46 | |
| 47 | private TextView mDetails; |
| 48 | private ImageView mIcon; |
Mady Mellor | dc05204 | 2018-01-04 17:11:07 -0800 | [diff] [blame] | 49 | |
Mady Mellor | 238b9b6 | 2018-01-09 16:15:40 -0800 | [diff] [blame] | 50 | private int mRowIndex; |
| 51 | private SliceView.SliceObserver mSliceObserver; |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 52 | |
Mady Mellor | dc05204 | 2018-01-04 17:11:07 -0800 | [diff] [blame] | 53 | public MessageView(Context context) { |
| 54 | super(context); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public int getMode() { |
| 59 | return SliceView.MODE_LARGE; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void setSlice(Slice slice) { |
| 64 | // Do nothing it's always a list item |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void resetView() { |
| 69 | // TODO |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
| 73 | protected void onFinishInflate() { |
| 74 | super.onFinishInflate(); |
| 75 | mDetails = findViewById(android.R.id.summary); |
| 76 | mIcon = findViewById(android.R.id.icon); |
| 77 | } |
| 78 | |
| 79 | @Override |
Mady Mellor | 238b9b6 | 2018-01-09 16:15:40 -0800 | [diff] [blame] | 80 | public void setSliceItem(SliceItem slice, boolean isHeader, int index, |
| 81 | SliceView.SliceObserver observer) { |
| 82 | mSliceObserver = observer; |
| 83 | mRowIndex = index; |
Jason Monk | dcb5e2f | 2017-11-15 20:19:43 -0500 | [diff] [blame] | 84 | SliceItem source = SliceQuery.findSubtype(slice, FORMAT_IMAGE, SUBTYPE_SOURCE); |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 85 | if (source != null) { |
| 86 | final int iconSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, |
| 87 | 24, getContext().getResources().getDisplayMetrics()); |
Mady Mellor | dc05204 | 2018-01-04 17:11:07 -0800 | [diff] [blame] | 88 | // TODO: try and turn this into a drawable |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 89 | Bitmap iconBm = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888); |
| 90 | Canvas iconCanvas = new Canvas(iconBm); |
| 91 | Drawable d = source.getIcon().loadDrawable(getContext()); |
| 92 | d.setBounds(0, 0, iconSize, iconSize); |
| 93 | d.draw(iconCanvas); |
| 94 | mIcon.setImageBitmap(SliceViewUtil.getCircularBitmap(iconBm)); |
| 95 | } |
Jason Monk | 2a7d0fc | 2017-11-15 10:10:24 -0500 | [diff] [blame] | 96 | final SpannableStringBuilder builder = new SpannableStringBuilder(); |
| 97 | SliceQuery.findAll(slice, FORMAT_TEXT).forEach(new Consumer<SliceItem>() { |
| 98 | @Override |
| 99 | public void accept(SliceItem text) { |
| 100 | if (builder.length() != 0) { |
| 101 | builder.append('\n'); |
| 102 | } |
| 103 | builder.append(text.getText()); |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 104 | } |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 105 | }); |
| 106 | mDetails.setText(builder.toString()); |
| 107 | } |
Jason Monk | 8a452e9 | 2017-10-31 19:21:47 -0400 | [diff] [blame] | 108 | } |