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 
17 package com.android.car.carlauncher.displayarea;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.graphics.Rect;
22 import android.view.SurfaceControl;
23 
24 import androidx.annotation.NonNull;
25 
26 import com.android.car.carlauncher.R;
27 
28 import java.io.PrintWriter;
29 
30 /**
31  * Abstracts the common operations on {@link SurfaceControl.Transaction} for DA's
32  * transition.
33  */
34 public class CarLauncherDisplayAreaTransactionHelper {
35     private static final String TAG = "CarLauncherDisplayAreaTransactionHelper";
36 
37     private final boolean mEnableCornerRadius;
38     private final float mCornerRadius;
39     private final float mCornerRadiusAdjustment;
40 
CarLauncherDisplayAreaTransactionHelper(Context context)41     public CarLauncherDisplayAreaTransactionHelper(Context context) {
42         Resources res = context.getResources();
43         mCornerRadiusAdjustment = res.getDimension(
44                 com.android.internal.R.dimen.rounded_corner_radius_adjustment);
45         mCornerRadius = res.getDimension(com.android.internal.R.dimen.rounded_corner_radius)
46                 - mCornerRadiusAdjustment;
47         mEnableCornerRadius = res.getBoolean(
48                 R.bool.config_enableRoundedCornerForForegroundDisplayArea);
49     }
50 
51     /**
52      * Operates the translation (setPosition) on a given transaction and leash
53      *
54      * @return same {@link CarLauncherDisplayAreaTransactionHelper}
55      * instance for method chaining
56      */
translate( SurfaceControl.Transaction tx, SurfaceControl leash, float offset)57     CarLauncherDisplayAreaTransactionHelper translate(
58             SurfaceControl.Transaction tx, SurfaceControl leash,
59             float offset) {
60         tx.setPosition(leash, 0, offset);
61         return this;
62     }
63 
64     /**
65      * Operates the crop (setMatrix) on a given transaction and leash
66      *
67      * @return same {@link CarLauncherDisplayAreaTransactionHelper}
68      * instance for method chaining
69      */
crop( SurfaceControl.Transaction tx, SurfaceControl leash, Rect destinationBounds)70     CarLauncherDisplayAreaTransactionHelper crop(
71             SurfaceControl.Transaction tx, SurfaceControl leash,
72             Rect destinationBounds) {
73         tx.setWindowCrop(leash, destinationBounds.width(), destinationBounds.height());
74         return this;
75     }
76 
77     /**
78      * Operates the round corner radius on a given transaction and leash
79      *
80      * @return same {@link CarLauncherDisplayAreaTransactionHelper}
81      * instance for method chaining
82      */
round( SurfaceControl.Transaction tx, SurfaceControl leash)83     CarLauncherDisplayAreaTransactionHelper round(
84             SurfaceControl.Transaction tx, SurfaceControl leash) {
85         if (mEnableCornerRadius) {
86             tx.setCornerRadius(leash, mCornerRadius);
87         }
88         return this;
89     }
90 
91     interface SurfaceControlTransactionFactory {
getTransaction()92         SurfaceControl.Transaction getTransaction();
93     }
94 
dump(@onNull PrintWriter pw)95     void dump(@NonNull PrintWriter pw) {
96         final String innerPrefix = "  ";
97         pw.println(TAG + "states: ");
98         pw.print(innerPrefix + "mEnableCornerRadius=");
99         pw.println(mEnableCornerRadius);
100         pw.print(innerPrefix + "mCornerRadiusAdjustment=");
101         pw.println(mCornerRadiusAdjustment);
102         pw.print(innerPrefix + "mCornerRadius=");
103         pw.println(mCornerRadius);
104     }
105 }
106 
107