blob: c519b69e5fd531fdaabd85f3daa8a7d4d6df9e92 [file] [log] [blame]
Jason Monk8a452e92017-10-31 19:21:47 -04001/*
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
17package androidx.app.slice.widget;
18
Jason Monkdcb5e2f2017-11-15 20:19:43 -050019import static android.app.slice.Slice.SUBTYPE_SOURCE;
Jason Monk0c76d302017-11-21 14:02:27 -050020import static android.app.slice.SliceItem.FORMAT_IMAGE;
21import static android.app.slice.SliceItem.FORMAT_TEXT;
22
Jason Monk2a7d0fc2017-11-15 10:10:24 -050023import android.annotation.TargetApi;
Jason Monk8a452e92017-10-31 19:21:47 -040024import android.content.Context;
25import android.graphics.Bitmap;
26import android.graphics.Canvas;
27import android.graphics.drawable.Drawable;
28import android.support.annotation.RestrictTo;
29import android.text.SpannableStringBuilder;
Jason Monk8a452e92017-10-31 19:21:47 -040030import android.util.TypedValue;
31import android.widget.ImageView;
Jason Monk8a452e92017-10-31 19:21:47 -040032import android.widget.TextView;
33
Jason Monk2a7d0fc2017-11-15 10:10:24 -050034import java.util.function.Consumer;
35
Mady Mellordc052042018-01-04 17:11:07 -080036import androidx.app.slice.Slice;
Jason Monkdcb5e2f2017-11-15 20:19:43 -050037import androidx.app.slice.SliceItem;
Jason Monk8a452e92017-10-31 19:21:47 -040038import androidx.app.slice.core.SliceQuery;
39
40/**
41 * @hide
42 */
43@RestrictTo(RestrictTo.Scope.LIBRARY)
Jason Monk2a7d0fc2017-11-15 10:10:24 -050044@TargetApi(24)
Mady Mellordc052042018-01-04 17:11:07 -080045public class MessageView extends SliceChildView {
Jason Monk8a452e92017-10-31 19:21:47 -040046
47 private TextView mDetails;
48 private ImageView mIcon;
Mady Mellordc052042018-01-04 17:11:07 -080049
Mady Mellor238b9b62018-01-09 16:15:40 -080050 private int mRowIndex;
51 private SliceView.SliceObserver mSliceObserver;
Jason Monk8a452e92017-10-31 19:21:47 -040052
Mady Mellordc052042018-01-04 17:11:07 -080053 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 Monk8a452e92017-10-31 19:21:47 -040070 }
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 Mellor238b9b62018-01-09 16:15:40 -080080 public void setSliceItem(SliceItem slice, boolean isHeader, int index,
81 SliceView.SliceObserver observer) {
82 mSliceObserver = observer;
83 mRowIndex = index;
Jason Monkdcb5e2f2017-11-15 20:19:43 -050084 SliceItem source = SliceQuery.findSubtype(slice, FORMAT_IMAGE, SUBTYPE_SOURCE);
Jason Monk8a452e92017-10-31 19:21:47 -040085 if (source != null) {
86 final int iconSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
87 24, getContext().getResources().getDisplayMetrics());
Mady Mellordc052042018-01-04 17:11:07 -080088 // TODO: try and turn this into a drawable
Jason Monk8a452e92017-10-31 19:21:47 -040089 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 Monk2a7d0fc2017-11-15 10:10:24 -050096 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 Monk8a452e92017-10-31 19:21:47 -0400104 }
Jason Monk8a452e92017-10-31 19:21:47 -0400105 });
106 mDetails.setText(builder.toString());
107 }
Jason Monk8a452e92017-10-31 19:21:47 -0400108}