• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.systemui.screenshot;
18 
19 import static java.util.Objects.requireNonNull;
20 
21 import android.app.PendingIntent;
22 import android.content.Context;
23 import android.graphics.drawable.Icon;
24 import android.util.AttributeSet;
25 import android.util.Log;
26 import android.widget.FrameLayout;
27 import android.widget.ImageView;
28 import android.widget.LinearLayout;
29 import android.widget.TextView;
30 
31 import com.android.systemui.R;
32 
33 
34 /**
35  * View for a chip with an icon and text.
36  */
37 public class OverlayActionChip extends FrameLayout {
38 
39     private static final String TAG = "ScreenshotActionChip";
40 
41     private ImageView mIconView;
42     private TextView mTextView;
43     private boolean mIsPending = false;
44 
OverlayActionChip(Context context)45     public OverlayActionChip(Context context) {
46         this(context, null);
47     }
48 
OverlayActionChip(Context context, AttributeSet attrs)49     public OverlayActionChip(Context context, AttributeSet attrs) {
50         this(context, attrs, 0);
51     }
52 
OverlayActionChip(Context context, AttributeSet attrs, int defStyleAttr)53     public OverlayActionChip(Context context, AttributeSet attrs, int defStyleAttr) {
54         this(context, attrs, defStyleAttr, 0);
55     }
56 
OverlayActionChip( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)57     public OverlayActionChip(
58             Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
59         super(context, attrs, defStyleAttr, defStyleRes);
60     }
61 
62     @Override
onFinishInflate()63     protected void onFinishInflate() {
64         mIconView = requireNonNull(findViewById(R.id.overlay_action_chip_icon));
65         mTextView = requireNonNull(findViewById(R.id.overlay_action_chip_text));
66         updatePadding(mTextView.getText().length() > 0);
67     }
68 
69     @Override
setPressed(boolean pressed)70     public void setPressed(boolean pressed) {
71         // override pressed state to true if there is an action pending
72         super.setPressed(mIsPending || pressed);
73     }
74 
75     /**
76      * Set chip icon and whether to tint with theme color
77      */
setIcon(Icon icon, boolean tint)78     public void setIcon(Icon icon, boolean tint) {
79         mIconView.setImageIcon(icon);
80         if (!tint) {
81             mIconView.setImageTintList(null);
82         }
83     }
84 
85     /**
86      * Set chip text
87      */
setText(CharSequence text)88     public void setText(CharSequence text) {
89         mTextView.setText(text);
90         updatePadding(text.length() > 0);
91     }
92 
93     /**
94      * Set PendingIntent to be sent and Runnable to be run, when chip is clicked
95      */
setPendingIntent(PendingIntent intent, Runnable finisher)96     public void setPendingIntent(PendingIntent intent, Runnable finisher) {
97         setOnClickListener(v -> {
98             try {
99                 intent.send();
100                 finisher.run();
101             } catch (PendingIntent.CanceledException e) {
102                 Log.e(TAG, "Intent cancelled", e);
103             }
104         });
105     }
106 
107     /**
108      * Set pressed state of chip (to be used when chip is clicked before underlying intent is ready)
109      */
setIsPending(boolean isPending)110     public void setIsPending(boolean isPending) {
111         mIsPending = isPending;
112         setPressed(mIsPending);
113     }
114 
updatePadding(boolean hasText)115     private void updatePadding(boolean hasText) {
116         LinearLayout.LayoutParams iconParams =
117                 (LinearLayout.LayoutParams) mIconView.getLayoutParams();
118         LinearLayout.LayoutParams textParams =
119                 (LinearLayout.LayoutParams) mTextView.getLayoutParams();
120         if (hasText) {
121             int paddingHorizontal = mContext.getResources().getDimensionPixelSize(
122                     R.dimen.overlay_action_chip_padding_horizontal);
123             int spacing = mContext.getResources().getDimensionPixelSize(
124                     R.dimen.overlay_action_chip_spacing);
125             iconParams.setMarginStart(paddingHorizontal);
126             iconParams.setMarginEnd(spacing);
127             textParams.setMarginEnd(paddingHorizontal);
128         } else {
129             int paddingHorizontal = mContext.getResources().getDimensionPixelSize(
130                     R.dimen.overlay_action_chip_icon_only_padding_horizontal);
131             iconParams.setMarginStart(paddingHorizontal);
132             iconParams.setMarginEnd(paddingHorizontal);
133         }
134         mIconView.setLayoutParams(iconParams);
135         mTextView.setLayoutParams(textParams);
136     }
137 }
138