1 /* 2 * Copyright (C) 2021 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 package com.android.launcher3.widget.picker; 17 18 import static com.android.launcher3.widget.picker.WidgetsListDrawableState.FIRST; 19 import static com.android.launcher3.widget.picker.WidgetsListDrawableState.FIRST_EXPANDED; 20 import static com.android.launcher3.widget.picker.WidgetsListDrawableState.LAST; 21 import static com.android.launcher3.widget.picker.WidgetsListDrawableState.MIDDLE; 22 import static com.android.launcher3.widget.picker.WidgetsListDrawableState.MIDDLE_EXPANDED; 23 import static com.android.launcher3.widget.picker.WidgetsListDrawableState.SINGLE; 24 25 import android.content.Context; 26 import android.content.res.ColorStateList; 27 import android.content.res.Resources; 28 import android.graphics.drawable.Drawable; 29 import android.graphics.drawable.GradientDrawable; 30 import android.graphics.drawable.RippleDrawable; 31 import android.graphics.drawable.StateListDrawable; 32 33 import com.android.launcher3.R; 34 import com.android.launcher3.util.Themes; 35 36 /** Factory for creating drawables to use as background for list elements. */ 37 final class WidgetsListDrawableFactory { 38 39 private final float mTopBottomCornerRadius; 40 private final float mMiddleCornerRadius; 41 private final ColorStateList mSurfaceColor; 42 private final ColorStateList mRippleColor; 43 WidgetsListDrawableFactory(Context context)44 WidgetsListDrawableFactory(Context context) { 45 Resources res = context.getResources(); 46 mTopBottomCornerRadius = res.getDimension(R.dimen.widget_list_top_bottom_corner_radius); 47 mMiddleCornerRadius = res.getDimension(R.dimen.widget_list_content_corner_radius); 48 mSurfaceColor = context.getColorStateList(R.color.surface); 49 mRippleColor = ColorStateList.valueOf( 50 Themes.getAttrColor(context, android.R.attr.colorControlHighlight)); 51 } 52 53 /** 54 * Creates a drawable for widget header list items. This drawable supports all positions 55 * in {@link WidgetsListDrawableState}. 56 */ createHeaderBackgroundDrawable()57 Drawable createHeaderBackgroundDrawable() { 58 StateListDrawable stateList = new StateListDrawable(); 59 stateList.addState( 60 SINGLE.mStateSet, 61 createRoundedRectDrawable(mTopBottomCornerRadius, mTopBottomCornerRadius)); 62 stateList.addState( 63 FIRST_EXPANDED.mStateSet, 64 createRoundedRectDrawable(mTopBottomCornerRadius, 0)); 65 stateList.addState( 66 FIRST.mStateSet, 67 createRoundedRectDrawable(mTopBottomCornerRadius, mMiddleCornerRadius)); 68 stateList.addState( 69 MIDDLE_EXPANDED.mStateSet, 70 createRoundedRectDrawable(mMiddleCornerRadius, 0)); 71 stateList.addState( 72 MIDDLE.mStateSet, 73 createRoundedRectDrawable(mMiddleCornerRadius, mMiddleCornerRadius)); 74 stateList.addState( 75 LAST.mStateSet, 76 createRoundedRectDrawable(mMiddleCornerRadius, mTopBottomCornerRadius)); 77 return new RippleDrawable(mRippleColor, /* content= */ stateList, /* mask= */ stateList); 78 } 79 80 /** 81 * Creates a drawable for widget content list items. This state list supports the middle and 82 * last states. 83 */ createContentBackgroundDrawable()84 Drawable createContentBackgroundDrawable() { 85 StateListDrawable stateList = new StateListDrawable(); 86 stateList.addState( 87 MIDDLE.mStateSet, 88 createRoundedRectDrawable(0, mMiddleCornerRadius)); 89 stateList.addState( 90 LAST.mStateSet, 91 createRoundedRectDrawable(0, mTopBottomCornerRadius)); 92 return new RippleDrawable(mRippleColor, /* content= */ stateList, /* mask= */ stateList); 93 } 94 95 /** Creates a rounded-rect drawable with the specified radii. */ createRoundedRectDrawable(float topRadius, float bottomRadius)96 private Drawable createRoundedRectDrawable(float topRadius, float bottomRadius) { 97 GradientDrawable backgroundMask = new GradientDrawable(); 98 backgroundMask.setColor(mSurfaceColor); 99 backgroundMask.setShape(GradientDrawable.RECTANGLE); 100 backgroundMask.setCornerRadii( 101 new float[]{ 102 topRadius, 103 topRadius, 104 topRadius, 105 topRadius, 106 bottomRadius, 107 bottomRadius, 108 bottomRadius, 109 bottomRadius 110 }); 111 return backgroundMask; 112 } 113 } 114