Home | History | Annotate | Download | only in widget
      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.slice.widget;
     18 
     19 import static android.app.slice.Slice.SUBTYPE_SOURCE;
     20 import static android.app.slice.SliceItem.FORMAT_IMAGE;
     21 import static android.app.slice.SliceItem.FORMAT_TEXT;
     22 
     23 import android.content.Context;
     24 import android.graphics.Bitmap;
     25 import android.graphics.Canvas;
     26 import android.graphics.drawable.Drawable;
     27 import android.text.SpannableStringBuilder;
     28 import android.util.TypedValue;
     29 import android.widget.ImageView;
     30 import android.widget.TextView;
     31 
     32 import androidx.annotation.RestrictTo;
     33 import androidx.slice.SliceItem;
     34 import androidx.slice.core.SliceQuery;
     35 
     36 import java.util.List;
     37 
     38 /**
     39  * @hide
     40  */
     41 @RestrictTo(RestrictTo.Scope.LIBRARY)
     42 public class MessageView extends SliceChildView {
     43 
     44     private TextView mDetails;
     45     private ImageView mIcon;
     46 
     47     private int mRowIndex;
     48 
     49     public MessageView(Context context) {
     50         super(context);
     51     }
     52 
     53     @Override
     54     public int getMode() {
     55         return SliceView.MODE_LARGE;
     56     }
     57 
     58     @Override
     59     public void resetView() {
     60         // TODO
     61     }
     62 
     63     @Override
     64     protected void onFinishInflate() {
     65         super.onFinishInflate();
     66         mDetails = findViewById(android.R.id.summary);
     67         mIcon = findViewById(android.R.id.icon);
     68     }
     69 
     70     @Override
     71     public void setSliceItem(SliceItem slice, boolean isHeader, int index,
     72             int rowCount, SliceView.OnSliceActionListener observer) {
     73         setSliceActionListener(observer);
     74         mRowIndex = index;
     75         SliceItem source = SliceQuery.findSubtype(slice, FORMAT_IMAGE, SUBTYPE_SOURCE);
     76         if (source != null) {
     77             final int iconSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
     78                     24, getContext().getResources().getDisplayMetrics());
     79             // TODO: try and turn this into a drawable
     80             Bitmap iconBm = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888);
     81             Canvas iconCanvas = new Canvas(iconBm);
     82             Drawable d = source.getIcon().loadDrawable(getContext());
     83             d.setBounds(0, 0, iconSize, iconSize);
     84             d.draw(iconCanvas);
     85             mIcon.setImageBitmap(SliceViewUtil.getCircularBitmap(iconBm));
     86         }
     87         final SpannableStringBuilder builder = new SpannableStringBuilder();
     88         List<SliceItem> items = SliceQuery.findAll(slice, FORMAT_TEXT);
     89         for (SliceItem text : items) {
     90             if (builder.length() != 0) {
     91                 builder.append('\n');
     92             }
     93             builder.append(text.getText());
     94         }
     95         mDetails.setText(builder.toString());
     96     }
     97 }
     98