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