1 /* 2 * Copyright (C) 2018 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.internal.widget; 18 19 import android.content.Context; 20 import android.content.res.ColorStateList; 21 import android.graphics.drawable.DrawableWrapper; 22 import android.graphics.drawable.GradientDrawable; 23 import android.graphics.drawable.InsetDrawable; 24 import android.graphics.drawable.RippleDrawable; 25 import android.util.AttributeSet; 26 import android.view.RemotableViewMethod; 27 import android.widget.Button; 28 import android.widget.RemoteViews; 29 30 /** 31 * A button implementation for the emphasized notification style. 32 * 33 * @hide 34 */ 35 @RemoteViews.RemoteView 36 public class EmphasizedNotificationButton extends Button { 37 private final RippleDrawable mRipple; 38 private final int mStrokeWidth; 39 private final int mStrokeColor; 40 41 public EmphasizedNotificationButton(Context context) { 42 this(context, null); 43 } 44 45 public EmphasizedNotificationButton(Context context, AttributeSet attrs) { 46 this(context, attrs, 0); 47 } 48 49 public EmphasizedNotificationButton(Context context, AttributeSet attrs, int defStyleAttr) { 50 this(context, attrs, defStyleAttr, 0); 51 } 52 53 public EmphasizedNotificationButton(Context context, AttributeSet attrs, int defStyleAttr, 54 int defStyleRes) { 55 super(context, attrs, defStyleAttr, defStyleRes); 56 DrawableWrapper background = (DrawableWrapper) getBackground().mutate(); 57 mRipple = (RippleDrawable) background.getDrawable(); 58 mStrokeWidth = getResources().getDimensionPixelSize( 59 com.android.internal.R.dimen.emphasized_button_stroke_width); 60 mStrokeColor = getContext().getColor(com.android.internal.R.color.material_grey_300); 61 mRipple.mutate(); 62 } 63 64 @RemotableViewMethod 65 public void setRippleColor(ColorStateList color) { 66 mRipple.setColor(color); 67 invalidate(); 68 } 69 70 @RemotableViewMethod 71 public void setButtonBackground(ColorStateList color) { 72 GradientDrawable inner = (GradientDrawable) mRipple.getDrawable(0); 73 inner.setColor(color); 74 invalidate(); 75 } 76 77 @RemotableViewMethod 78 public void setHasStroke(boolean hasStroke) { 79 GradientDrawable inner = (GradientDrawable) mRipple.getDrawable(0); 80 inner.setStroke(hasStroke ? mStrokeWidth : 0, mStrokeColor); 81 invalidate(); 82 } 83 } 84