• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.car.media;
18 
19 import android.view.View;
20 
21 import androidx.annotation.NonNull;
22 import androidx.constraintlayout.widget.ConstraintLayout;
23 
24 import com.android.car.ui.baselayout.Insets;
25 import com.android.car.ui.baselayout.InsetsChangedListener;
26 
27 import java.util.HashSet;
28 import java.util.Set;
29 
30 /**
31  * Applies the insets computed by the car-ui-lib to the spacer views in ui_guides.xml. This allows
32  * the Media app to have different instances of the application bar.
33  */
34 class GuidelinesUpdater implements InsetsChangedListener {
35 
36     private final View mGuidedView;
37     private final Set<InsetsChangedListener> mListeners = new HashSet<>();
38 
GuidelinesUpdater(View guidedView)39     public GuidelinesUpdater(View guidedView) {
40         mGuidedView = guidedView;
41     }
42 
addListener(InsetsChangedListener listener)43     public void addListener(InsetsChangedListener listener) {
44         mListeners.add(listener);
45     }
46 
47     // Read the results of the base layout measurements and adjust the guidelines to match
onCarUiInsetsChanged(@onNull Insets insets)48     public void onCarUiInsetsChanged(@NonNull Insets insets) {
49         View startPad = mGuidedView.findViewById(R.id.ui_content_start_guideline);
50         ConstraintLayout.LayoutParams start =
51                 (ConstraintLayout.LayoutParams)startPad.getLayoutParams();
52         start.setMargins(insets.getLeft(), 0,0, 0);
53         startPad.setLayoutParams(start);
54 
55         View endPad = mGuidedView.findViewById(R.id.ui_content_end_guideline);
56         ConstraintLayout.LayoutParams end = (ConstraintLayout.LayoutParams)endPad.getLayoutParams();
57         end.setMargins(0, 0, insets.getRight(), 0);
58         endPad.setLayoutParams(end);
59 
60         View topPad = mGuidedView.findViewById(R.id.ui_content_top_guideline);
61         ConstraintLayout.LayoutParams top = (ConstraintLayout.LayoutParams)topPad.getLayoutParams();
62         top.setMargins(0, insets.getTop(), 0, 0);
63         topPad.setLayoutParams(top);
64 
65         View bottomPad = mGuidedView.findViewById(R.id.ui_content_bottom_guideline);
66         ConstraintLayout.LayoutParams bottom =
67                 (ConstraintLayout.LayoutParams)bottomPad.getLayoutParams();
68         bottom.setMargins(0, 0, 0, insets.getBottom());
69         bottomPad.setLayoutParams(bottom);
70 
71         for (InsetsChangedListener listener : mListeners) {
72             listener.onCarUiInsetsChanged(insets);
73         }
74     }
75 }
76