1 /*
2  * Copyright (C) 2020 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 android.service.controls.templates;
18 
19 import android.annotation.NonNull;
20 import android.content.Context;
21 import android.graphics.drawable.Icon;
22 import android.os.Bundle;
23 import android.service.controls.Control;
24 
25 import com.android.internal.R;
26 import com.android.internal.util.Preconditions;
27 
28 /**
29  * A template for a {@link Control} that displays an image.
30  */
31 public final class ThumbnailTemplate extends ControlTemplate {
32 
33     private static final @TemplateType int TYPE = TYPE_THUMBNAIL;
34     private static final String KEY_ICON = "key_icon";
35     private static final String KEY_ACTIVE = "key_active";
36     private static final String KEY_CONTENT_DESCRIPTION = "key_content_description";
37 
38     private final boolean mActive;
39     private final @NonNull Icon mThumbnail;
40     private final @NonNull CharSequence mContentDescription;
41 
42     /**
43      * @param templateId the identifier for this template object
44      * @param active whether the image corresponds to an active (live) stream.
45      * @param thumbnail an image to display on the {@link Control}
46      * @param contentDescription a description of the image for accessibility.
47      */
ThumbnailTemplate( @onNull String templateId, boolean active, @NonNull Icon thumbnail, @NonNull CharSequence contentDescription)48     public ThumbnailTemplate(
49             @NonNull String templateId,
50             boolean active,
51             @NonNull Icon thumbnail,
52             @NonNull CharSequence contentDescription) {
53         super(templateId);
54         Preconditions.checkNotNull(thumbnail);
55         Preconditions.checkNotNull(contentDescription);
56         mActive = active;
57         mThumbnail = thumbnail;
58         mContentDescription = contentDescription;
59     }
60 
61     /**
62      * @param b
63      * @hide
64      */
ThumbnailTemplate(Bundle b)65     ThumbnailTemplate(Bundle b) {
66         super(b);
67         mActive = b.getBoolean(KEY_ACTIVE);
68         mThumbnail = b.getParcelable(KEY_ICON, android.graphics.drawable.Icon.class);
69         mContentDescription = b.getCharSequence(KEY_CONTENT_DESCRIPTION, "");
70     }
71 
72     /*
73      * @return {@code true} if the thumbnail corresponds to an active (live) stream.
74      */
isActive()75     public boolean isActive() {
76         return mActive;
77     }
78 
79     /**
80      * The {@link Icon} (image) displayed by this template.
81      */
82     @NonNull
getThumbnail()83     public Icon getThumbnail() {
84         return mThumbnail;
85     }
86 
87     /**
88      * The description of the image returned by {@link ThumbnailTemplate#getThumbnail()}
89      */
90     @NonNull
getContentDescription()91     public CharSequence getContentDescription() {
92         return mContentDescription;
93     }
94 
95     /**
96      * @return {@link ControlTemplate#TYPE_THUMBNAIL}
97      */
98     @Override
getTemplateType()99     public int getTemplateType() {
100         return TYPE;
101     }
102 
103     /**
104      * Rescales the image down if necessary (in the case of a Bitmap).
105      *
106      * @hide
107      */
108     @Override
prepareTemplateForBinder(@onNull Context context)109     public void prepareTemplateForBinder(@NonNull Context context) {
110         int width = context.getResources()
111                 .getDimensionPixelSize(R.dimen.controls_thumbnail_image_max_width);
112         int height = context.getResources()
113                 .getDimensionPixelSize(R.dimen.controls_thumbnail_image_max_height);
114         rescaleThumbnail(width, height);
115     }
116 
rescaleThumbnail(int width, int height)117     private void rescaleThumbnail(int width, int height) {
118         mThumbnail.scaleDownIfNecessary(width, height);
119     }
120 
121     /**
122      * @return
123      * @hide
124      */
125     @Override
126     @NonNull
getDataBundle()127     Bundle getDataBundle() {
128         Bundle b = super.getDataBundle();
129         b.putBoolean(KEY_ACTIVE, mActive);
130         b.putObject(KEY_ICON, mThumbnail);
131         b.putObject(KEY_CONTENT_DESCRIPTION, mContentDescription);
132         return b;
133     }
134 }
135