• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.pagination;
18 
19 import android.view.View;
20 import android.view.ViewTreeObserver.OnGlobalLayoutListener;
21 
22 import com.android.car.carlauncher.pagination.PageMeasurementHelper.GridDimensions;
23 import com.android.car.carlauncher.pagination.PageMeasurementHelper.PageDimensions;
24 
25 import java.util.HashSet;
26 import java.util.Set;
27 
28 /**
29  * Controller class that handling all pagination related logic.
30  */
31 public class PaginationController {
32     private final PageMeasurementHelper mPageMeasurementHelper;
33     private final DimensionUpdateCallback mCallback;
34 
PaginationController(View windowBackground, DimensionUpdateCallback callback)35     public PaginationController(View windowBackground, DimensionUpdateCallback callback) {
36         mCallback = callback;
37         mPageMeasurementHelper = new PageMeasurementHelper(windowBackground);
38         windowBackground.getViewTreeObserver().addOnGlobalLayoutListener(
39                 new OnGlobalLayoutListener() {
40                     @Override
41                     public void onGlobalLayout() {
42                         maybeHandleWindowResize(windowBackground.getMeasuredWidth(),
43                                 windowBackground.getMeasuredHeight());
44                     }
45                 });
46     }
47 
maybeHandleWindowResize(int windowWidth, int windowHeight)48     private void maybeHandleWindowResize(int windowWidth, int windowHeight) {
49         boolean consumed = mPageMeasurementHelper.handleWindowSizeChange(windowWidth, windowHeight);
50         if (consumed) {
51             mCallback.notifyDimensionsUpdated(mPageMeasurementHelper.getPageDimensions(),
52                     mPageMeasurementHelper.getGridDimensions());
53         }
54     }
55 
56     /**
57      * Callback contract between this controller and its {@link DimensionUpdateListener} classes.
58      *
59      * When {@link PageMeasurementHelper#handleWindowSizeChange} returns {@code true}, the callback
60      * will notify its listeners that the window size has changed.
61      */
62     public static class DimensionUpdateCallback {
63         Set<DimensionUpdateListener> mListeners = new HashSet<>();
64 
65         /**
66          * Adds an {@link DimensionUpdateListener} to
67          */
addListener(DimensionUpdateListener listener)68         public void addListener(DimensionUpdateListener listener) {
69             mListeners.add(listener);
70         }
71 
72         /**
73          * Updates all listeners with the new measured dimensions.
74          */
notifyDimensionsUpdated(PageDimensions pageDimens, GridDimensions gridDimens)75         public void notifyDimensionsUpdated(PageDimensions pageDimens, GridDimensions gridDimens) {
76             for (DimensionUpdateListener listener : mListeners) {
77                 listener.onDimensionsUpdated(pageDimens, gridDimens);
78             }
79         }
80     }
81 
82     /**
83      * Listener interface for {@link DimensionUpdateCallback}.
84      *
85      * Classes that implement the listener should use the measured dimensions from
86      * {@link DimensionUpdateListener#onDimensionsUpdated} to update relevant layout params.
87      */
88     public interface DimensionUpdateListener {
89         /**
90          * Updates layout params from the updated dimensions measurements in {@link PageDimensions}
91          * and {@link GridDimensions}*/
onDimensionsUpdated(PageDimensions pageDimens, GridDimensions gridDimens)92         void onDimensionsUpdated(PageDimensions pageDimens, GridDimensions gridDimens);
93     }
94 }
95