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.wallpaper.picker;
17 
18 import android.app.Activity;
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.view.ViewGroup;
22 
23 import androidx.cardview.widget.CardView;
24 
25 import com.android.wallpaper.R;
26 import com.android.wallpaper.util.ScreenSizeCalculator;
27 import com.android.wallpaper.util.SizeCalculator;
28 
29 /** The wallpaper section view in the customization picker fragment. */
30 public final class WallpaperSectionView extends SectionView {
31 
32     private CardView mHomePreviewCard;
33     private CardView mLockscreenPreviewCard;
34 
WallpaperSectionView(Context context, AttributeSet attrs)35     public WallpaperSectionView(Context context, AttributeSet attrs) {
36         super(context, attrs);
37     }
38 
39     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)40     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
41         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
42         matchDeviceShape(mHomePreviewCard);
43         matchDeviceShape(mLockscreenPreviewCard);
44     }
45 
46     @Override
onFinishInflate()47     protected void onFinishInflate() {
48         super.onFinishInflate();
49         mHomePreviewCard = findViewById(R.id.home_preview);
50         mLockscreenPreviewCard = findViewById(R.id.lock_preview);
51 
52         // Disable the shadows of these card views.
53         mHomePreviewCard.setCardElevation(0);
54         mLockscreenPreviewCard.setCardElevation(0);
55     }
56 
matchDeviceShape(CardView cardView)57     private void matchDeviceShape(CardView cardView) {
58         // Match device aspect ratio
59         float screenAspectRatio =
60                 ScreenSizeCalculator.getInstance().getScreenAspectRatio(getContext());
61         int cardWidth = cardView.getMeasuredWidth();
62         int cardHeight = (int) (cardWidth * screenAspectRatio);
63         ViewGroup.LayoutParams layoutParams = cardView.getLayoutParams();
64         layoutParams.height = cardHeight;
65 
66         // Match device corner
67         cardView.setRadius(
68                 SizeCalculator.getPreviewCornerRadius((Activity) getContext(), cardWidth));
69     }
70 }
71