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